-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
[eunhwa99] Week 9 #981
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution { | ||
|
||
// TP: O(N) | ||
// SP: O(1) | ||
// 음수 원소를 처리하기 위해 곱의 Min 값도 생각해야 했던 문제! | ||
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}; | ||
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.