-
-
Notifications
You must be signed in to change notification settings - Fork 245
[byteho0n] Week 09 #989
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
[byteho0n] Week 09 #989
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6ebd1db
feat : find-minimum-in-rotated-sorted-array
ekgns33 e2977c4
feat : liked-list-cycle
ekgns33 d991815
feat : pacific-atlantic-water-flow
ekgns33 cf5f6c2
feat : maximum subarray product
ekgns33 fddbef5
feat : minimum-window-substring
ekgns33 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,56 @@ | ||
/** | ||
input : array of integers | ||
output : minimum element's value | ||
|
||
3 4 5 1 2 | ||
draw graph | ||
|
||
5 | ||
4 | ||
3 | ||
2 | ||
1 | ||
l r | ||
l r | ||
l r | ||
-------------- | ||
5 | ||
4 | ||
3 | ||
2 | ||
1 | ||
mid right | ||
-------------- | ||
left < right ->> sorted. | ||
left < mid ->> don't have to search range [left, mid] | ||
mid > right ->> rotation point is between [mid, right]; | ||
|
||
solution 1) brute force | ||
read the array | ||
|
||
tc : O(n) | ||
sc : O(1); | ||
|
||
solution 2) binary search | ||
do binary search, move pointers with descripted conditions | ||
after binary search loop ends, the left pointer is the minimum element | ||
|
||
tc : O(logn) | ||
sc : O(1) | ||
*/ | ||
|
||
class Solution { | ||
public int findMin(int[] nums) { | ||
int l = 0; | ||
int r = nums.length - 1; | ||
while(l < r) { | ||
int mid = (r - l) / 2 + l; | ||
if(nums[mid] < nums[r]) { | ||
r = mid; | ||
} else { | ||
l = mid + 1; | ||
} | ||
} | ||
return nums[l]; | ||
} | ||
} |
ekgns33 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,35 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode(int x) { | ||
* val = x; | ||
* next = null; | ||
* } | ||
* } | ||
*/ | ||
|
||
/* | ||
* input : singly linked list and head node | ||
* output : return true if list has cycle | ||
* | ||
* solution : tortoise and hare algorithm | ||
* with two pointer (slow and fast) | ||
* tc : O(n) | ||
* sc : O(1) | ||
* | ||
* */ | ||
public class Solution { | ||
public boolean hasCycle(ListNode head) { | ||
if(head == null) return false; | ||
ListNode slow = head; | ||
ListNode fast = head; | ||
while(fast.next != null && fast.next.next != null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
if(fast == slow) 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,45 @@ | ||
class Solution { | ||
public int maxProduct(int[] nums) { | ||
int n = nums.length; | ||
int[][] product = new int[2][n]; | ||
product[0][0] = nums[0]; | ||
product[1][0] = nums[0]; | ||
int max = nums[0]; | ||
for(int i = 1; i < n; i++) { | ||
product[0][i] = Math.max(product[0][i-1]*nums[i], Math.max(nums[i], nums[i] * product[1][i-1])); | ||
product[1][i] = Math.min(product[0][i-1]*nums[i], Math.min(nums[i], nums[i] * product[1][i-1])); | ||
max =Math.max(max, product[0][i]); | ||
} | ||
return max; | ||
} | ||
} | ||
/** | ||
|
||
|
||
brute force : | ||
nested for loop | ||
tc : O(n^2) | ||
sc : O(1) | ||
|
||
better sol : | ||
maintain min and max | ||
tc : O(n) | ||
sc : O(n) | ||
|
||
compare with prev Min * cur, prevMax * cur, cur | ||
we have to keep track of minimum value that can lead to maximum value | ||
when there are negative values later. | ||
|
||
2 3 -2 4 | ||
2 6 -2 4 | ||
2 2 -12 -48 | ||
|
||
-2 0 -1 | ||
-2 0 0 | ||
-2 0 -1 | ||
|
||
2 3 -2 5 7 -100 | ||
2 6 -2 5 35 210000 | ||
2 3 -6 -30 -210. -35 | ||
|
||
*/ |
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,30 @@ | ||
class Solution { | ||
public String minWindow(String s, String t) { | ||
int[] freq = new int[128]; | ||
char[] str = s.toCharArray(); | ||
int minL = 0; | ||
int minLength = Integer.MAX_VALUE; | ||
int l = 0, r = 0, n = s.length(); | ||
for(char c : t.toCharArray()) { | ||
freq[c-'A']++; | ||
} | ||
int resolved = t.length(); | ||
while(r < n) { | ||
if(freq[str[r++] - 'A']-- > 0) { | ||
resolved--; | ||
} | ||
while(resolved == 0) { | ||
if(r - l < minLength) { | ||
minL = l; | ||
minLength = r - l; | ||
} | ||
if(freq[str[l] - 'A']++ == 0) { | ||
resolved++; | ||
} | ||
l++; | ||
} | ||
} | ||
if(minLength == Integer.MAX_VALUE) return ""; | ||
return new String(str, minL, minLength); | ||
} | ||
} |
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,62 @@ | ||
/* | ||
* input : grid represents the heights of island | ||
* output : coordinates where water flows to both pacific and atlantic | ||
* | ||
* example | ||
* | ||
* 2 2 2 1,1 >> 1,2 >> atlantic / 1,1 >> 0,1 >> pacific | ||
* 3 3 1 | ||
* 4 3 1 | ||
* | ||
* do dfs/bfs from the edge of grids. | ||
* if cell is checked from pacific and atlantic add to result | ||
* solution dfs from edges | ||
* tc : O(m*n) | ||
* sc : O(mn) | ||
* | ||
* */ | ||
class Solution { | ||
public List<List<Integer>> pacificAtlantic(int[][] heights) { | ||
int m = heights.length; | ||
int n = heights[0].length; | ||
int[][] pacific = new int[m][n]; | ||
int[][] atlantic = new int[m][n]; | ||
|
||
for(int i = 0; i < m; i++) { | ||
dfsHelper(heights, pacific, i, 0); | ||
} | ||
for(int j = 1; j < n; j++) { | ||
dfsHelper(heights,pacific, 0, j); | ||
} | ||
for(int i =0; i < m; i++) { | ||
dfsHelper(heights,atlantic, i, n-1); | ||
} | ||
for(int j = 0; j < n-1; j++) { | ||
dfsHelper(heights, atlantic, m-1, j); | ||
} | ||
List<List<Integer>> ans = new ArrayList<>(); | ||
for(int i = 0; i < m; i++) { | ||
for(int j = 0; j < n; j++) { | ||
if(pacific[i][j] == 1 && atlantic[i][j] == 1) { | ||
ans.add(List.of(i, j)); | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
static int[][] directions = { | ||
{1, 0}, {-1, 0}, {0, 1}, {0, -1} | ||
}; | ||
|
||
void dfsHelper(int[][] board, int[][] visited, int x, int y) { | ||
if(visited[x][y] > 0) return; | ||
visited[x][y] += 1; | ||
for(int[] direction : directions) { | ||
int nx = x + direction[0]; | ||
int ny = y + direction[1]; | ||
if(nx < 0 || nx >=visited.length || ny < 0 || ny >= visited[0].length || visited[nx][ny] > 0 || board[nx][ny] < board[x][y]) continue; | ||
dfsHelper(board, visited, nx, ny); | ||
} | ||
} | ||
} |
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.