Skip to content

Commit

Permalink
add 152 java, update cheatsheet, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 7, 2024
1 parent fa5df78 commit 895de9f
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@
096| [Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/) | [Python](./leetcode_python/Dynamic_Programming/unique-binary-search-trees.py) | _O(n)_| _O(1)_| Medium| dp, Math, fb, amazon| AGAIN* (2) (not start)
120| [Triangle](https://leetcode.com/problems/triangle/) | [Python](./leetcode_python/Dynamic_Programming/triangle.py) | _O(m * n)_ | _O(n)_ | Medium |`amazon`| AGAIN* (not start) (2)
123| [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)| [Python](./leetcode_python/Dynamic_Programming/best-time-to-buy-and-sell-stock-iii.py) | _O(m * n)_ | _O(n)_ | Hard |`amazon`, uber, apple| AGAIN* (not start) (1)
152| [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)|[Python](./leetcode_python/Dynamic_Programming/maximum-product-subarray.py)| _O(n)_ | _O(1)_ | Medium |dp, brute force, Curated Top 75, google, amazon, linkedin| AGAIN***** (1)
152| [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)|[Python](./leetcode_python/Dynamic_Programming/maximum-product-subarray.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximumProductSubarray.java)| _O(n)_ | _O(1)_ | Medium |dp, brute force, Curated Top 75, google, amazon, linkedin| AGAIN***** (1)
198| [House Robber](https://leetcode.com/problems/house-robber/)| [Python](./leetcode_python/Dynamic_Programming/house-robber.py) | _O(n)_ | _O(1)_ | Easy|Curated Top 75, dp basic,`amazon`| AGAIN*** (2)
213| [House Robber II](https://leetcode.com/problems/house-robber-ii/)| [Python](./leetcode_python/Dynamic_Programming/house-robber-ii.py) | _O(n)_| _O(1)_| Medium|brute force, recursion, dp, Curated Top 75, google, amazon| AGAIN (not start)
221| [Maximal Square](https://leetcode.com/problems/maximal-square/)|[Python](./leetcode_python/Dynamic_Programming/maximal-square.py) | _O(n^2)_ | _O(n)_ | Medium | EPI, dp, `amazon`,`fb` | AGAIN** (3) (not start)
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240308: 152
20240307: 139(again),141,143(again)
20240306: 121,124(again),125,128,133(again)
20240305: 100,102,104,105(again)
Expand Down
15 changes: 9 additions & 6 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2024-05-02 -> ['152']
2024-05-01 -> ['139(again),141,143(again)']
2024-04-30 -> ['121,124(again),125,128,133(again)']
2024-04-29 -> ['100,102,104,105(again)']
Expand All @@ -6,6 +7,7 @@
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-11 -> ['152']
2024-04-10 -> ['139(again),141,143(again)']
2024-04-09 -> ['121,124(again),125,128,133(again)']
2024-04-08 -> ['100,102,104,105(again)']
Expand All @@ -14,24 +16,25 @@
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-01 -> ['1,3,5,4,19']
2024-03-29 -> ['152']
2024-03-28 -> ['139(again),141,143(again)']
2024-03-27 -> ['121,124(again),125,128,133(again)']
2024-03-26 -> ['100,102,104,105(again)']
2024-03-25 -> ['73,76,79(again),91,25']
2024-03-24 -> ['55(again),56,62,70']
2024-03-21 -> ['39,48(again),49,53,54']
2024-03-21 -> ['152', '39,48(again),49,53,54']
2024-03-20 -> ['139(again),141,143(again)', '20,21,23,33(again)']
2024-03-19 -> ['121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['100,102,104,105(again)']
2024-03-17 -> ['73,76,79(again),91,25']
2024-03-16 -> ['55(again),56,62,70']
2024-03-16 -> ['152', '55(again),56,62,70']
2024-03-15 -> ['139(again),141,143(again)']
2024-03-14 -> ['121,124(again),125,128,133(again)']
2024-03-13 -> ['100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-13 -> ['152', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['139(again),141,143(again)', '100,102,104,105(again)']
2024-03-09 -> ['139(again),141,143(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25']
2024-03-11 -> ['152', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['152', '139(again),141,143(again)', '100,102,104,105(again)']
2024-03-09 -> ['152', '139(again),141,143(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25']
2024-03-08 -> ['139(again),141,143(again)', '121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-07 -> ['121,124(again),125,128,133(again)', '100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-06 -> ['100,102,104,105(again)', '73,76,79(again),91,25', '55(again),56,62,70', '1,3,5,4,19']
Expand Down
10 changes: 10 additions & 0 deletions doc/cheatsheet/java_trick.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,14 @@ public List<List<String>> partition_1(String s) {
void dfs_1(int start, List<List<String>> result, List<String> currentList, String s) {
// ..
}
```

### 2-2) Get max, min from 3 numbers

```java
// java

// LC 152
max = Math.max(Math.max(max * nums[i], min * nums[i]), nums[i]);
min = Math.min(Math.min(temp * nums[i], min * nums[i]), nums[i]);
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package LeetCodeJava.DynamicProgramming;

// https://leetcode.com/problems/maximum-product-subarray/description/

public class MaximumProductSubarray {

// V0
// IDEA : DP
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Dynamic_Programming/maximum-product-subarray.py#L69
// IDEA : cur max = max (cur, cur * dp[k-1])
// But, also needs to consider "minus number"
// -> e.g. (-1) * (-3) = 3
// -> so we NEED to track maxSoFar, and minSoFar
public int maxProduct(int[] nums) {

// null check
if (nums.length == 0){
return 0;
}
// init
int maxSoFar = nums[0];
int minSoFar = nums[0];
int res = maxSoFar;

for (int i = 1; i < nums.length; i++){

int cur = nums[i];
/**
* or, can use below trick to get max in 3 numbers
*
* max = Math.max(Math.max(max * nums[i], min * nums[i]), nums[i]);
* min = Math.min(Math.min(temp * nums[i], min * nums[i]), nums[i]);
*
*/
int tmpMax = findMax(cur, maxSoFar * cur, minSoFar * cur);
minSoFar = findMin(cur, maxSoFar * cur, minSoFar * cur);
maxSoFar = tmpMax;

res = Math.max(maxSoFar, res);
}

return res;
}

private int findMax(int a, int b, int c){
if (a >= b && a >= c){
return a;
}
else if (b >= a && b >= c){
return b;
}else{
return c;
}
}

private int findMin(int a, int b, int c){
if (a <= b && a <= c){
return a;
}
else if (b <= a && b <= c){
return b;
}else{
return c;
}
}

// V1
// IDEA : For each index i keep updating the max and min. We are also keeping min because on multiplying with any negative number your min will become max and max will become min. So for every index i we will take max of (i-th element, prevMax * i-th element, prevMin * i-th element).
// -> max of (i-th element, prevMax * i-th element, prevMin * i-th element)
// https://leetcode.com/problems/maximum-product-subarray/solutions/1608862/java-3-solutions-detailed-explanation-using-image/
public int maxProduct_1(int[] nums) {

int max = nums[0], min = nums[0], ans = nums[0];

for (int i = 1; i < nums.length; i++) {

int temp = max; // store the max because before updating min your max will already be updated

max = Math.max(Math.max(max * nums[i], min * nums[i]), nums[i]);
min = Math.min(Math.min(temp * nums[i], min * nums[i]), nums[i]);

if (max > ans) {
ans = max;
}
}

return ans;
}

// V2
// IDEA : Just the slight modification of previous approach. As we know that on multiplying with negative number max will become min and min will become max, so why not as soon as we encounter negative element, we swap the max and min already.
// https://leetcode.com/problems/maximum-product-subarray/solutions/1608862/java-3-solutions-detailed-explanation-using-image/
public int maxProduct_2(int[] nums) {

int max = nums[0], min = nums[0], ans = nums[0];
int n = nums.length;

for (int i = 1; i < n; i++) {

// Swapping min and max
if (nums[i] < 0){
int temp = max;
max = min;
min = temp;
}

max = Math.max(nums[i], max * nums[i]);
min = Math.min(nums[i], min * nums[i]);

ans = Math.max(ans, max);
}

return ans;
}

// V3
// IDEA : 2 POINTERS
// https://leetcode.com/problems/maximum-product-subarray/solutions/1608862/java-3-solutions-detailed-explanation-using-image/
public int maxProduct_3(int[] nums) {

int n = nums.length;
int l=1,r=1;
int ans=nums[0];

for(int i=0;i<n;i++){

//if any of l or r become 0 then update it to 1
l = l==0 ? 1 : l;
r = r==0 ? 1 : r;

l *= nums[i]; //prefix product
r *= nums[n-1-i]; //suffix product

ans = Math.max(ans,Math.max(l,r));

}

return ans;
}

}

0 comments on commit 895de9f

Please sign in to comment.