Skip to content

Commit a53f471

Browse files
committed
add 304 java
1 parent da7255c commit a53f471

File tree

6 files changed

+187
-12
lines changed

6 files changed

+187
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@
10491049
276| [Paint Fence](https://leetcode.com/problems/paint-fence/) | [Python](./leetcode_python/Dynamic_Programming/paint-fence.py) | _O(n)_| _O(1)_| Easy |🔒| AGAIN
10501050
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*
10511051
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
1052-
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)
1052+
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)
10531053
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)
10541054
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)
10551055
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)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-12-08
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-30
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241208: 304
12
20241202: 370(todo),1109(todo)
23
20241130: 34,767
34
20241126: 722,380

data/to_review.txt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1+
2025-02-01 -> ['304']
2+
2025-01-26 -> ['370(todo),1109(todo)']
13
2025-01-24 -> ['34,767']
24
2025-01-20 -> ['722,380']
35
2025-01-19 -> ['33,81']
46
2025-01-17 -> ['253']
57
2025-01-16 -> ['776,31']
68
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
79
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
8-
2025-01-11 -> ['394']
10+
2025-01-11 -> ['304', '394']
911
2025-01-10 -> ['833,950']
12+
2025-01-05 -> ['370(todo),1109(todo)']
1013
2025-01-04 -> ['53,210,207']
1114
2025-01-03 -> ['34,767', '444']
1215
2025-01-02 -> ['1188,130,855(again)']
1316
2024-12-30 -> ['722,380']
14-
2024-12-29 -> ['33,81']
17+
2024-12-29 -> ['304', '33,81']
1518
2024-12-28 -> ['900']
1619
2024-12-27 -> ['253', '26,27', '802,1197,26']
1720
2024-12-26 -> ['776,31']
1821
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
1922
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
20-
2024-12-21 -> ['34,767', '394', '855,846']
23+
2024-12-23 -> ['370(todo),1109(todo)']
24+
2024-12-21 -> ['304', '34,767', '394', '855,846']
2125
2024-12-20 -> ['833,950', '932']
2226
2024-12-18 -> ['951,792']
2327
2024-12-17 -> ['722,380']
24-
2024-12-16 -> ['33,81']
28+
2024-12-16 -> ['304', '33,81']
29+
2024-12-15 -> ['370(todo),1109(todo)']
2530
2024-12-14 -> ['253', '53,210,207', '163,1048']
26-
2024-12-13 -> ['34,767', '776,31', '444', '298,729']
31+
2024-12-13 -> ['304', '34,767', '776,31', '444', '298,729']
2732
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
28-
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
29-
2024-12-09 -> ['722,380']
33+
2024-12-11 -> ['304', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
34+
2024-12-10 -> ['304', '370(todo),1109(todo)']
35+
2024-12-09 -> ['304', '722,380']
3036
2024-12-08 -> ['34,767', '33,81', '394', '737']
31-
2024-12-07 -> ['833,950', '900', '686,734,737']
37+
2024-12-07 -> ['370(todo),1109(todo)', '833,950', '900', '686,734,737']
3238
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
33-
2024-12-05 -> ['34,767', '776,31', '528,334']
34-
2024-12-04 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)']
35-
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']
39+
2024-12-05 -> ['370(todo),1109(todo)', '34,767', '776,31', '528,334']
40+
2024-12-04 -> ['370(todo),1109(todo)', '722,380', '004(todo),34(todo),162(todo),275(todo)']
41+
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']
3642
2024-12-02 -> ['34,767']
3743
2024-12-01 -> ['34,767', '722,380', '253', '53,210,207']
3844
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package LeetCodeJava.DynamicProgramming;
2+
3+
// https://leetcode.com/problems/range-sum-query-2d-immutable/description/
4+
/**
5+
* 304. Range Sum Query 2D - Immutable
6+
* Solved
7+
* Medium
8+
* Topics
9+
* Companies
10+
* Given a 2D matrix matrix, handle multiple queries of the following type:
11+
*
12+
* 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).
13+
* Implement the NumMatrix class:
14+
*
15+
* NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
16+
* 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).
17+
* You must design an algorithm where sumRegion works on O(1) time complexity.
18+
*
19+
*
20+
*
21+
* Example 1:
22+
*
23+
*
24+
* Input
25+
* ["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]
26+
* [[[[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]]
27+
* Output
28+
* [null, 8, 11, 12]
29+
*
30+
* Explanation
31+
* 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]]);
32+
* numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
33+
* numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
34+
* numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)
35+
*
36+
*
37+
* Constraints:
38+
*
39+
* m == matrix.length
40+
* n == matrix[i].length
41+
* 1 <= m, n <= 200
42+
* -104 <= matrix[i][j] <= 104
43+
* 0 <= row1 <= row2 < m
44+
* 0 <= col1 <= col2 < n
45+
* At most 104 calls will be made to sumRegion.
46+
*
47+
*/
48+
public class RangeSumQuery2DImmutable {
49+
50+
51+
/**
52+
* Your NumMatrix object will be instantiated and called as such:
53+
* NumMatrix obj = new NumMatrix(matrix);
54+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
55+
*/
56+
57+
// V0
58+
// class NumMatrix {
59+
//
60+
// public NumMatrix(int[][] matrix) {
61+
//
62+
// }
63+
//
64+
// public int sumRegion(int row1, int col1, int row2, int col2) {
65+
//
66+
// }
67+
// }
68+
69+
// V1
70+
// IDEA : DP
71+
// https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-304-range-sum-query-2d-immutable/
72+
// https://www.youtube.com/watch?v=MSNSqU3BnXk
73+
class NumMatrix_1 {
74+
private int[][] sums;
75+
76+
public NumMatrix_1(int[][] matrix) {
77+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
78+
return;
79+
}
80+
81+
int m = matrix.length;
82+
int n = matrix[0].length;
83+
84+
// Initialize the sums array
85+
sums = new int[m + 1][n + 1];
86+
for (int i = 1; i <= m; i++) {
87+
for (int j = 1; j <= n; j++) {
88+
sums[i][j] = matrix[i - 1][j - 1]
89+
+ sums[i - 1][j]
90+
+ sums[i][j - 1]
91+
- sums[i - 1][j - 1];
92+
}
93+
}
94+
}
95+
96+
public int sumRegion(int row1, int col1, int row2, int col2) {
97+
return sums[row2 + 1][col2 + 1]
98+
- sums[row2 + 1][col1]
99+
- sums[row1][col2 + 1]
100+
+ sums[row1][col1];
101+
}
102+
}
103+
104+
// V2
105+
// IDEA : DP
106+
// https://leetcode.com/problems/range-sum-query-2d-immutable/solutions/2104317/dp-visualised-interview-tips/
107+
class NumMatrix_2 {
108+
private int[][] sums;
109+
110+
public NumMatrix_2(int[][] matrix) {
111+
int n = matrix.length;
112+
int m = matrix[0].length;
113+
sums = new int[n+1][m+1];
114+
for (int i=1; i<=n; i++)
115+
for (int j=1; j<=m; j++)
116+
sums[i][j] = sums[i-1][j] + sums[i][j-1] + matrix[i-1][j-1] - sums[i-1][j-1];
117+
}
118+
119+
public int sumRegion(int row1, int col1, int row2, int col2) {
120+
int RED_RECTANGLE = sums[row2+1][col2+1];
121+
int PURPLE_RECTANGLES = sums[row1][col2+1] + sums[row2+1][col1];
122+
int BLUE_RECTANGLE = sums[row1][col1];
123+
return RED_RECTANGLE - PURPLE_RECTANGLES + BLUE_RECTANGLE;
124+
}
125+
}
126+
127+
// V3
128+
// IDEA : DP
129+
// https://leetcode.com/problems/range-sum-query-2d-immutable/solutions/1204168/js-python-java-c-easy-4-rectangles-dp-solution-w-explanation/
130+
class NumMatrix_3 {
131+
long[][] dp;
132+
133+
public NumMatrix_3(int[][] M) {
134+
int ylen = M.length + 1, xlen = M[0].length + 1;
135+
dp = new long[ylen][xlen];
136+
for (int i = 1; i < ylen; i++)
137+
for (int j = 1; j < xlen; j++)
138+
dp[i][j] = M[i-1][j-1] + dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1];
139+
}
140+
141+
public int sumRegion(int R1, int C1, int R2, int C2) {
142+
return (int)(dp[R2+1][C2+1] - dp[R2+1][C1] - dp[R1][C2+1] + dp[R1][C1]);
143+
}
144+
}
145+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,26 @@ public int[] topKFrequent(int[] nums, int k) {
42524252
return res;
42534253
}
42544254

4255+
// LC 304
4256+
/**
4257+
* Your NumMatrix object will be instantiated and called as such:
4258+
* NumMatrix obj = new NumMatrix(matrix);
4259+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
4260+
*/
4261+
4262+
// V0
4263+
// 3.05 - 3.05 pm
4264+
class NumMatrix {
4265+
4266+
public NumMatrix(int[][] matrix) {
4267+
4268+
}
4269+
4270+
public int sumRegion(int row1, int col1, int row2, int col2) {
4271+
return 0;
4272+
}
4273+
}
4274+
42554275

42564276
}
42574277

0 commit comments

Comments
 (0)