Skip to content

Commit e09d1ae

Browse files
authored
Merge pull request #981 from eunhwa99/main
[eunhwa99] Week 9
2 parents a911094 + 80ad9ab commit e09d1ae

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
// TC: O(log N)
3+
// SC: O(1)
4+
public int findMin(int[] nums) {
5+
int left = 0;
6+
int right = nums.length - 1;
7+
while (left < right) {
8+
int mid = left + (right - left) / 2;
9+
if (nums[mid] < nums[right]) {
10+
right = mid;
11+
} else {
12+
left = mid + 1;
13+
}
14+
}
15+
return nums[left];
16+
}
17+
}

linked-list-cycle/eunhwa99.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
class ListNode {
3+
4+
int val;
5+
ListNode next;
6+
7+
ListNode(int x) {
8+
val = x;
9+
next = null;
10+
}
11+
}
12+
13+
public class Solution {
14+
15+
// Floyd's Tortoise and Hare Algorithm
16+
// TC: O(N)
17+
// SC: O(1)
18+
public boolean hasCycle(ListNode head) {
19+
if (head == null || head.next == null) {
20+
return false;
21+
}
22+
23+
ListNode slow = head;
24+
ListNode fast = head;
25+
26+
while (fast != null && fast.next != null) {
27+
slow = slow.next;
28+
fast = fast.next.next;
29+
30+
if (slow == fast) {
31+
return true;
32+
}
33+
}
34+
return false;
35+
}
36+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
3+
// TP: O(N)
4+
// SP: O(1)
5+
// 음수 원소를 처리하기 위해 곱의 Min 값도 생각해야 했던 문제!
6+
public int maxProduct(int[] nums) {
7+
8+
int minProd = nums[0];
9+
int maxProd = nums[0];
10+
int result = nums[0];
11+
for (int i = 1; i < nums.length; i++) {
12+
if (nums[i] < 0) {
13+
int temp = minProd;
14+
minProd = maxProd;
15+
maxProd = temp;
16+
}
17+
18+
maxProd = Math.max(nums[i], maxProd * nums[i]);
19+
minProd = Math.min(nums[i], minProd * nums[i]);
20+
result = Math.max(result, maxProd);
21+
}
22+
23+
return result;
24+
}
25+
}
26+
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class Solution {
5+
6+
// TP: O(N*N)
7+
// SP: O(N*N)
8+
// pacific에 접하는 지점으로부터 dfs를 시작해 pacific에 도달할 수 있는 지점을 체크하고,
9+
// atlantic에 접하는 지점으로부터 dfs를 시작해 atlantic에 도달할 수 있는 지점을 체크한다.
10+
// 마지막으로 두 지점 모두에 도달할 수 있는 지점을 찾아 반환한다.
11+
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
12+
13+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
14+
int rowSize = heights.length;
15+
int colSize = heights[0].length;
16+
17+
boolean[][] pacific = new boolean[rowSize][colSize];
18+
boolean[][] atlantic = new boolean[rowSize][colSize];
19+
20+
for (int i = 0; i < rowSize; i++) {
21+
dfs(i, 0, pacific, heights);
22+
dfs(i, colSize - 1, atlantic, heights);
23+
}
24+
25+
for (int i = 0; i < colSize; i++) {
26+
dfs(0, i, pacific, heights);
27+
dfs(rowSize - 1, i, atlantic, heights);
28+
}
29+
30+
List<List<Integer>> result = new ArrayList<>();
31+
for (int i = 0; i < rowSize; i++) {
32+
for (int j = 0; j < colSize; j++) {
33+
if (pacific[i][j] && atlantic[i][j]) {
34+
result.add(List.of(i, j));
35+
}
36+
}
37+
}
38+
return result;
39+
}
40+
41+
private void dfs(int row, int col, boolean[][] visited, int[][] heights) {
42+
visited[row][col] = true;
43+
44+
for (int[] direction : directions) {
45+
int newRow = row + direction[0];
46+
int newCol = col + direction[1];
47+
48+
if (newRow < 0 || newRow >= heights.length || newCol < 0 || newCol >= heights[0].length) {
49+
continue;
50+
}
51+
52+
if (visited[newRow][newCol]) {
53+
continue;
54+
}
55+
56+
if (heights[newRow][newCol] < heights[row][col]) {
57+
continue;
58+
}
59+
60+
dfs(newRow, newCol, visited, heights);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)