Skip to content

Commit

Permalink
add 727 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 17, 2024
1 parent a6dfe80 commit 23b04fa
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,7 @@
688 | [Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard/) | [Python](./leetcode_python//Dynamic_Programming/knight-probability-in-chessboard.py) | _O(k * n^2)_ | _O(n^2)_ | Medium |`dp`,`dp basic`,`AGAIN`, `M$`,`Goldman Sachs`, `google`, `fb`, `amazon`| AGAIN********* (5)
712 | [Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/) | [Python](./leetcode_python//Dynamic_Programming/minimum-ascii-delete-sum-for-two-strings.py) | _O(m * n)_ | _O(n)_ | Medium |`dp`| AGAIN (not start)
714 | [Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/) | [Python](./leetcode_python//Dynamic_Programming/best-time-to-buy-and-sell-stock-with-transaction-fee.py) | _O(n)_ | _O(1)_ | Medium |AGAIN, `good basic`, `dp`, `greedy`,`fb`| AGAIN******** (3)
727 | [Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/description/) | [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MinimumWindowSubsequence.java) | _O(n)_ | _O(1)_ | Hard |`dp`,`google`| AGAIN (not start)
740 | [Delete and Earn](https://leetcode.com/problems/delete-and-earn/) | [Python](./leetcode_python//Dynamic_Programming/delete-and-earn.py) | _O(n)_ | _O(1)_ | Medium || AGAIN (not start)
746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [Python](./leetcode_python//Dynamic_Programming/min-cost-climbing-stairs.py) | _O(n)_ | _O(1)_ | Easy |`good dp basic`,`dp`, `amazon`| AGAIN**** (2)
750 | [Number Of Corner Rectangles](https://leetcode.com/problems/number-of-corner-rectangles/) | [Python](./leetcode_python//Dynamic_Programming/number-of-corner-rectangles.py) | _O(n * m^2)_ | _O(n * m)_ | Medium |`dp`,`fb`| AGAIN (3) (not start)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240917:
20240917: 727
20240910: 659
20240909: 801,552
20240908: 1057,1066,1110
Expand Down
12 changes: 9 additions & 3 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
2024-11-11 -> ['727']
2024-11-04 -> ['659']
2024-11-03 -> ['801,552']
2024-11-02 -> ['1057,1066,1110']
2024-10-25 -> ['1110, 1055']
2024-10-21 -> ['727']
2024-10-18 -> ['359,1057,1055(todo)']
2024-10-14 -> ['659']
2024-10-13 -> ['801,552']
2024-10-12 -> ['1057,1066,1110']
2024-10-08 -> ['727']
2024-10-04 -> ['315', '1110, 1055']
2024-10-01 -> ['659']
2024-09-30 -> ['801,552']
2024-09-30 -> ['727', '801,552']
2024-09-29 -> ['1057,1066,1110']
2024-09-27 -> ['359,1057,1055(todo)']
2024-09-25 -> ['727']
2024-09-23 -> ['659']
2024-09-22 -> ['801,552']
2024-09-22 -> ['727', '801,552']
2024-09-21 -> ['1057,1066,1110', '1110, 1055']
2024-09-18 -> ['659']
2024-09-20 -> ['727']
2024-09-19 -> ['727']
2024-09-18 -> ['727', '659']
2024-09-17 -> ['801,552']
2024-09-16 -> ['1057,1066,1110']
2024-09-15 -> ['659', '809']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package LeetCodeJava.DynamicProgramming;

// https://leetcode.com/problems/minimum-window-subsequence/description/
// https://leetcode.ca/all/727.html

/**
*
* 727. Minimum Window Subsequence
* Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W.
*
* If there is no such window in S that covers all characters in T, return the empty string "". If there are multiple such minimum-length windows, return the one with the left-most starting index.
*
* Example 1:
*
* Input:
* S = "abcdebdde", T = "bde"
* Output: "bcde"
* Explanation:
* "bcde" is the answer because it occurs before "bdde" which has the same length.
* "deb" is not a smaller window because the elements of T in the window must occur in order.
*
*
* Note:
*
* All the strings in the input will only contain lowercase letters.
* The length of S will be in the range [1, 20000].
* The length of T will be in the range [1, 100].
*
*
* Difficulty:
* Hard
* Lock:
* Prime
* Company:
* Amazon Bloomberg eBay Google Houzz Microsoft
*
*/

public class MinimumWindowSubsequence {

// V0
// TODO : implement
// public String minWindow(String s1, String s2) {
// }

// V1
// https://leetcode.ca/2017-11-26-727-Minimum-Window-Subsequence/
// IDEA : DP
public String minWindow_1(String s1, String s2) {
int m = s1.length(), n = s2.length();
int[][] f = new int[m + 1][n + 1];
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
f[i][j] = j == 1 ? i : f[i - 1][j - 1];
} else {
f[i][j] = f[i - 1][j];
}
}
}
int p = 0, k = m + 1;
for (int i = 1; i <= m; ++i) {
if (s1.charAt(i - 1) == s2.charAt(n - 1) && f[i][n] > 0) {
int j = f[i][n] - 1;
if (i - j < k) {
k = i - j;
p = j;
}
}
}
return k > m ? "" : s1.substring(p, p + k);
}


// V2
// https://github.com/doocs/leetcode/blob/main/solution/0700-0799/0727.Minimum%20Window%20Subsequence/README.md
public String minWindow_2(String s1, String s2) {
int m = s1.length(), n = s2.length();
int[][] f = new int[m + 1][n + 1];
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
f[i][j] = j == 1 ? i : f[i - 1][j - 1];
} else {
f[i][j] = f[i - 1][j];
}
}
}
int p = 0, k = m + 1;
for (int i = 1; i <= m; ++i) {
if (s1.charAt(i - 1) == s2.charAt(n - 1) && f[i][n] > 0) {
int j = f[i][n] - 1;
if (i - j < k) {
k = i - j;
p = j;
}
}
}
return k > m ? "" : s1.substring(p, p + k);
}

// V3
// https://www.cnblogs.com/grandyang/p/8684817.html
// IDEA : DP (modified by GPT)
// TODO : validate below
public String minWindow_3_1(String S, String T) {
int m = S.length();
int n = T.length();
int start = -1;
int minLen = Integer.MAX_VALUE;
int[][] dp = new int[m + 1][n + 1];

// Initialize dp array with -1
for (int[] row : dp) {
java.util.Arrays.fill(row, -1);
}

// Base case: empty T can be matched starting at any index in S
for (int i = 0; i <= m; ++i) {
dp[i][0] = i;
}

// Fill the dp array
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= Math.min(i, n); ++j) {
dp[i][j] = (S.charAt(i - 1) == T.charAt(j - 1)) ? dp[i - 1][j - 1] : dp[i - 1][j];
}
if (dp[i][n] != -1) {
int len = i - dp[i][n];
if (minLen > len) {
minLen = len;
start = dp[i][n];
}
}
}

return (start != -1) ? S.substring(start, start + minLen) : "";
}

// V3-1
// https://www.cnblogs.com/grandyang/p/8684817.html
// IDEA : 2 POINTERS (modified by GPT)
// TODO : validate below
public String minWindow_3_2(String S, String T) {
int m = S.length();
int n = T.length();
int start = -1;
int minLen = Integer.MAX_VALUE;
int i = 0;
int j = 0;

while (i < m) {
if (S.charAt(i) == T.charAt(j)) {
if (++j == n) {
int end = i + 1;
while (--j >= 0) {
while (i >= 0 && S.charAt(i) != T.charAt(j)) {
i--;
}
i--;
}
i++;
j++;
if (end - i < minLen) {
minLen = end - i;
start = i;
}
}
}
i++;
}

return (start != -1) ? S.substring(start, start + minLen) : "";
}

}
13 changes: 13 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -7757,6 +7757,19 @@ public List<String> decode(String s) {
}
}

// LC 727
// https://leetcode.ca/all/727.html
// 6.18 pm - 6.30 pm
/**
* dp equation:
*
* dp[i][j] = min(dp[i-1][j-1]
*
*/
public String minWindow_2(String s1, String s2) {
return null;
}



}

0 comments on commit 23b04fa

Please sign in to comment.