Skip to content

[eunhwa99] Week 9 #981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions find-minimum-in-rotated-sorted-array/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
// TC: O(log N)
// SC: O(1)
public int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < nums[right]) {
right = mid;
} else {
left = mid + 1;
}
}
return nums[left];
}
}
36 changes: 36 additions & 0 deletions linked-list-cycle/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

class ListNode {

int val;
ListNode next;

ListNode(int x) {
val = x;
next = null;
}
}

public class Solution {

// Floyd's Tortoise and Hare Algorithm
// TC: O(N)
// SC: O(1)
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}

ListNode slow = head;
ListNode fast = head;

while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;

if (slow == fast) {
return true;
}
}
return false;
}
}
26 changes: 26 additions & 0 deletions maximum-product-subarray/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {

// TP: O(N)
// SP: O(1)
// 음수 원소를 처리하기 위해 곱의 Min 값도 생각해야 했던 문제!
public int maxProduct(int[] nums) {

int minProd = nums[0];
int maxProd = nums[0];
int result = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < 0) {
int temp = minProd;
minProd = maxProd;
maxProd = temp;
}

maxProd = Math.max(nums[i], maxProd * nums[i]);
minProd = Math.min(nums[i], minProd * nums[i]);
result = Math.max(result, maxProd);
}

return result;
}
}

63 changes: 63 additions & 0 deletions pacific-atlantic-water-flow/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.util.ArrayList;
import java.util.List;

class Solution {

// TP: O(N*N)
// SP: O(N*N)
// pacific에 접하는 지점으로부터 dfs를 시작해 pacific에 도달할 수 있는 지점을 체크하고,
// atlantic에 접하는 지점으로부터 dfs를 시작해 atlantic에 도달할 수 있는 지점을 체크한다.
// 마지막으로 두 지점 모두에 도달할 수 있는 지점을 찾아 반환한다.
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

public List<List<Integer>> pacificAtlantic(int[][] heights) {
int rowSize = heights.length;
int colSize = heights[0].length;

boolean[][] pacific = new boolean[rowSize][colSize];
boolean[][] atlantic = new boolean[rowSize][colSize];

for (int i = 0; i < rowSize; i++) {
dfs(i, 0, pacific, heights);
dfs(i, colSize - 1, atlantic, heights);
}

for (int i = 0; i < colSize; i++) {
dfs(0, i, pacific, heights);
dfs(rowSize - 1, i, atlantic, heights);
}

List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
if (pacific[i][j] && atlantic[i][j]) {
result.add(List.of(i, j));
}
}
}
return result;
}

private void dfs(int row, int col, boolean[][] visited, int[][] heights) {
visited[row][col] = true;

for (int[] direction : directions) {
int newRow = row + direction[0];
int newCol = col + direction[1];

if (newRow < 0 || newRow >= heights.length || newCol < 0 || newCol >= heights[0].length) {
continue;
}

if (visited[newRow][newCol]) {
continue;
}

if (heights[newRow][newCol] < heights[row][col]) {
continue;
}

dfs(newRow, newCol, visited, heights);
}
}
}