Skip to content

Commit

Permalink
add 304 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 8, 2024
1 parent da7255c commit a53f471
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@
276| [Paint Fence](https://leetcode.com/problems/paint-fence/) | [Python](./leetcode_python/Dynamic_Programming/paint-fence.py) | _O(n)_| _O(1)_| Easy |🔒| AGAIN
279| [Perfect Squares](https://leetcode.com/problems/perfect-squares/)| [Python](./leetcode_python/Dynamic_Programming/perfect-squares.py) | _O(n * sqrt(n))_ | _O(n)_ | Medium | dp, bfs, trick, Hash, `google` | AGAIN*
303| [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/)| [Python](./leetcode_python/Dynamic_Programming/range-sum-query-immutable.py) | ctor: _O(n)_, lookup: _O(1)_ | _O(n)_ | Easy || OK
304| [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)| [Python](./leetcode_python/Dynamic_Programming/range-sum-query-2d-immutable.py) | ctor: _O(m * n)_, lookup: _O(1)_ | _O(m * n)_ | Medium |`dp`,`trick`,`fb`| AGAIN******* (5)
304| [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)| [Python](./leetcode_python/Dynamic_Programming/range-sum-query-2d-immutable.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/RangeSumQuery2DImmutable.java) | ctor: _O(m * n)_, lookup: _O(1)_ | _O(m * n)_ | Medium |`dp`,`trick`,`fb`| AGAIN******* (5)
309| [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | [Python](./leetcode_python/Dynamic_Programming/best-time-to-buy-and-sell-stock-with-cooldown.py) | _O(n)_ | _O(1)_ | Medium || AGAIN (not start)
322| [Coin Change](https://leetcode.com/problems/coin-change/) | [Python](./leetcode_python/Dynamic_Programming/coin-change.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/CoinChange.java)| _O(n * k)_ | _O(k)_ | Medium |Curated Top 75, bfs, dfs, dp, grab, google, fb, apple, `amazon`| AGAIN************* (6) (MUST)
351| [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) | [Python](./leetcode_python/Dynamic_Programming/android-unlock-patterns.py) | _O(9^2 * 2^9)_ | _O(9 * 2^9)_ | Medium | 🔒 Backtracking, DP, `google` | AGAIN (not start)
Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-12-08
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-11-30
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

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 @@
20241208: 304
20241202: 370(todo),1109(todo)
20241130: 34,767
20241126: 722,380
Expand Down
28 changes: 17 additions & 11 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
2025-02-01 -> ['304']
2025-01-26 -> ['370(todo),1109(todo)']
2025-01-24 -> ['34,767']
2025-01-20 -> ['722,380']
2025-01-19 -> ['33,81']
2025-01-17 -> ['253']
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['394']
2025-01-11 -> ['304', '394']
2025-01-10 -> ['833,950']
2025-01-05 -> ['370(todo),1109(todo)']
2025-01-04 -> ['53,210,207']
2025-01-03 -> ['34,767', '444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-29 -> ['33,81']
2024-12-29 -> ['304', '33,81']
2024-12-28 -> ['900']
2024-12-27 -> ['253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-21 -> ['34,767', '394', '855,846']
2024-12-23 -> ['370(todo),1109(todo)']
2024-12-21 -> ['304', '34,767', '394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-18 -> ['951,792']
2024-12-17 -> ['722,380']
2024-12-16 -> ['33,81']
2024-12-16 -> ['304', '33,81']
2024-12-15 -> ['370(todo),1109(todo)']
2024-12-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['34,767', '776,31', '444', '298,729']
2024-12-13 -> ['304', '34,767', '776,31', '444', '298,729']
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-09 -> ['722,380']
2024-12-11 -> ['304', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-10 -> ['304', '370(todo),1109(todo)']
2024-12-09 -> ['304', '722,380']
2024-12-08 -> ['34,767', '33,81', '394', '737']
2024-12-07 -> ['833,950', '900', '686,734,737']
2024-12-07 -> ['370(todo),1109(todo)', '833,950', '900', '686,734,737']
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
2024-12-05 -> ['34,767', '776,31', '528,334']
2024-12-04 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)']
2024-12-03 -> ['34,767', '33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2024-12-05 -> ['370(todo),1109(todo)', '34,767', '776,31', '528,334']
2024-12-04 -> ['370(todo),1109(todo)', '722,380', '004(todo),34(todo),162(todo),275(todo)']
2024-12-03 -> ['370(todo),1109(todo)', '34,767', '33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2024-12-02 -> ['34,767']
2024-12-01 -> ['34,767', '722,380', '253', '53,210,207']
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package LeetCodeJava.DynamicProgramming;

// https://leetcode.com/problems/range-sum-query-2d-immutable/description/
/**
* 304. Range Sum Query 2D - Immutable
* Solved
* Medium
* Topics
* Companies
* Given a 2D matrix matrix, handle multiple queries of the following type:
*
* Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
* Implement the NumMatrix class:
*
* NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
* int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
* You must design an algorithm where sumRegion works on O(1) time complexity.
*
*
*
* Example 1:
*
*
* Input
* ["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]
* [[[[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]], [2, 1, 4, 3], [1, 1, 2, 2], [1, 2, 2, 4]]
* Output
* [null, 8, 11, 12]
*
* Explanation
* NumMatrix numMatrix = new NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]]);
* numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
* numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
* numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
*
*
* Constraints:
*
* m == matrix.length
* n == matrix[i].length
* 1 <= m, n <= 200
* -104 <= matrix[i][j] <= 104
* 0 <= row1 <= row2 < m
* 0 <= col1 <= col2 < n
* At most 104 calls will be made to sumRegion.
*
*/
public class RangeSumQuery2DImmutable {


/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/

// V0
// class NumMatrix {
//
// public NumMatrix(int[][] matrix) {
//
// }
//
// public int sumRegion(int row1, int col1, int row2, int col2) {
//
// }
// }

// V1
// IDEA : DP
// https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-304-range-sum-query-2d-immutable/
// https://www.youtube.com/watch?v=MSNSqU3BnXk
class NumMatrix_1 {
private int[][] sums;

public NumMatrix_1(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}

int m = matrix.length;
int n = matrix[0].length;

// Initialize the sums array
sums = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
sums[i][j] = matrix[i - 1][j - 1]
+ sums[i - 1][j]
+ sums[i][j - 1]
- sums[i - 1][j - 1];
}
}
}

public int sumRegion(int row1, int col1, int row2, int col2) {
return sums[row2 + 1][col2 + 1]
- sums[row2 + 1][col1]
- sums[row1][col2 + 1]
+ sums[row1][col1];
}
}

// V2
// IDEA : DP
// https://leetcode.com/problems/range-sum-query-2d-immutable/solutions/2104317/dp-visualised-interview-tips/
class NumMatrix_2 {
private int[][] sums;

public NumMatrix_2(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
sums = new int[n+1][m+1];
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
sums[i][j] = sums[i-1][j] + sums[i][j-1] + matrix[i-1][j-1] - sums[i-1][j-1];
}

public int sumRegion(int row1, int col1, int row2, int col2) {
int RED_RECTANGLE = sums[row2+1][col2+1];
int PURPLE_RECTANGLES = sums[row1][col2+1] + sums[row2+1][col1];
int BLUE_RECTANGLE = sums[row1][col1];
return RED_RECTANGLE - PURPLE_RECTANGLES + BLUE_RECTANGLE;
}
}

// V3
// IDEA : DP
// https://leetcode.com/problems/range-sum-query-2d-immutable/solutions/1204168/js-python-java-c-easy-4-rectangles-dp-solution-w-explanation/
class NumMatrix_3 {
long[][] dp;

public NumMatrix_3(int[][] M) {
int ylen = M.length + 1, xlen = M[0].length + 1;
dp = new long[ylen][xlen];
for (int i = 1; i < ylen; i++)
for (int j = 1; j < xlen; j++)
dp[i][j] = M[i-1][j-1] + dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1];
}

public int sumRegion(int R1, int C1, int R2, int C2) {
return (int)(dp[R2+1][C2+1] - dp[R2+1][C1] - dp[R1][C2+1] + dp[R1][C1]);
}
}
}
20 changes: 20 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -4252,6 +4252,26 @@ public int[] topKFrequent(int[] nums, int k) {
return res;
}

// LC 304
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/

// V0
// 3.05 - 3.05 pm
class NumMatrix {

public NumMatrix(int[][] matrix) {

}

public int sumRegion(int row1, int col1, int row2, int col2) {
return 0;
}
}


}

Expand Down

0 comments on commit a53f471

Please sign in to comment.