Skip to content
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

[byteho0n] Week 09 #989

Merged
merged 5 commits into from
Feb 9, 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
56 changes: 56 additions & 0 deletions find-minimum-in-rotated-sorted-array/ekgns33.java
ekgns33 marked this conversation as resolved.
Show resolved Hide resolved
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];
}
}
35 changes: 35 additions & 0 deletions linked-list-cycle/ekgns33.java
ekgns33 marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
45 changes: 45 additions & 0 deletions maximum-product-subarray/ekgns33.java
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

*/
30 changes: 30 additions & 0 deletions minimum-window-substring/ekgns33.java
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);
}
}
62 changes: 62 additions & 0 deletions pacific-atlantic-water-flow/ekgns33.java
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);
}
}
}