Skip to content

Commit 1d170af

Browse files
committed
add 311 java
1 parent 9ea1b14 commit 1d170af

File tree

6 files changed

+357
-44
lines changed

6 files changed

+357
-44
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@
240240
289| [Game of Life](https://leetcode.com/problems/game-of-life/) | [Python](./leetcode_python/Array/game-of-life.py) | _O(m * n)_ | _O(1)_ | Medium |simultaneous,matrix,`complex`, `google`, `amazon`| AGAIN*** (4)
241241
293| [Flip Game](https://leetcode.com/problems/flip-game/) | [Python](./leetcode_python/Array/flip-game.py) | _O(n * (c+1))_ | _O(1)_ | Easy |🔒, `basic`| AGAIN*
242242
308| [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/) | [Python](./leetcode_python/Array/range-sum-query-2d-mutable.py) | _O(n * (c+1))_ | _O(1)_ | Hard |LC 303, pre-sum, matrix, tree, 🔒, `google`| not start
243-
311| [Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/) | [Python](./leetcode_python/Array/sparse-matrix-multiplication.py) | _O(m * n * l)_ | _O(m * l)_ | Medium |🔒, matrix, `basic`, `fb`| OK**** (4)
243+
311| [Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/) | [Python](./leetcode_python/Array/sparse-matrix-multiplication.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/SparseMatrixMultiplication.java)| _O(m * n * l)_ | _O(m * l)_ | Medium |🔒, matrix, `basic`, `fb`,google| OK**** (5)
244244
334| [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [Python](./leetcode_python/Array/increasing-triplet-subsequence.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/IncreasingTripletSubsequence.java) | _O(n)_ | _O(1)_ | Medium |AGAIN, check `# 300 Longest Increasing Subsequence`, `trick`, `fb`, google|OK***** (but again) (4)
245245
370| [Range Addition](https://leetcode.com/problems/range-addition/) | [Python](./leetcode_python/Array/range-addition.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/RangeAddition.java) | _O(k + n)_ | _O(1)_ | Medium |prefix, diff array, check `# 598 Range Addition II`, `amazon`, LC 1109, google| AGAIN************** (4) (MUST)
246246
384| [Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [Python](./leetcode_python/Array/shuffle-an-array.py) | _O(n)_ | _O(n)_ | Medium | EPI, `trick` | OK*

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241222: 369
1+
20241222: 369,311
22
20241221: 370
33
20241220: 815,871,593,1109
44
20241214: 560,523

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
2025-02-15 -> ['369']
1+
2025-02-15 -> ['369,311']
22
2025-02-14 -> ['370']
33
2025-02-13 -> ['815,871,593,1109']
44
2025-02-07 -> ['560,523']
55
2025-02-01 -> ['304,853,325']
66
2025-01-26 -> ['370(todo)']
7-
2025-01-25 -> ['369']
7+
2025-01-25 -> ['369,311']
88
2025-01-24 -> ['370', '34,767']
99
2025-01-23 -> ['815,871,593,1109']
1010
2025-01-20 -> ['722,380']
@@ -13,21 +13,21 @@
1313
2025-01-16 -> ['776,31']
1414
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
1515
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
16-
2025-01-12 -> ['369']
16+
2025-01-12 -> ['369,311']
1717
2025-01-11 -> ['370', '304,853,325', '394']
1818
2025-01-10 -> ['815,871,593,1109', '833,950']
1919
2025-01-05 -> ['370(todo)']
20-
2025-01-04 -> ['369', '560,523', '53,210,207']
20+
2025-01-04 -> ['369,311', '560,523', '53,210,207']
2121
2025-01-03 -> ['370', '34,767', '444']
2222
2025-01-02 -> ['815,871,593,1109', '1188,130,855(again)']
23-
2024-12-30 -> ['369', '722,380']
23+
2024-12-30 -> ['369,311', '722,380']
2424
2024-12-29 -> ['370', '304,853,325', '33,81']
2525
2024-12-28 -> ['815,871,593,1109', '900']
26-
2024-12-27 -> ['369', '560,523', '253', '26,27', '802,1197,26']
26+
2024-12-27 -> ['369,311', '560,523', '253', '26,27', '802,1197,26']
2727
2024-12-26 -> ['370', '776,31']
28-
2024-12-25 -> ['369', '815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
29-
2024-12-24 -> ['369', '370', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
30-
2024-12-23 -> ['369', '370', '815,871,593,1109', '370(todo)']
28+
2024-12-25 -> ['369,311', '815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
29+
2024-12-24 -> ['369,311', '370', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
30+
2024-12-23 -> ['369,311', '370', '815,871,593,1109', '370(todo)']
3131
2024-12-22 -> ['370', '815,871,593,1109', '560,523']
3232
2024-12-21 -> ['815,871,593,1109', '304,853,325', '34,767', '394', '855,846']
3333
2024-12-20 -> ['833,950', '932']
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/sparse-matrix-multiplication/description/
4+
// https://leetcode.ca/all/311.html
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
/**
11+
* 311. Sparse Matrix Multiplication
12+
* Given two sparse matrices A and B, return the result of AB.
13+
*
14+
* You may assume that A's column number is equal to B's row number.
15+
*
16+
* Example:
17+
*
18+
* Input:
19+
*
20+
* A = [
21+
* [ 1, 0, 0],
22+
* [-1, 0, 3]
23+
* ]
24+
*
25+
* B = [
26+
* [ 7, 0, 0 ],
27+
* [ 0, 0, 0 ],
28+
* [ 0, 0, 1 ]
29+
* ]
30+
*
31+
* Output:
32+
*
33+
* | 1 0 0 | | 7 0 0 | | 7 0 0 |
34+
* AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
35+
* | 0 0 1 |
36+
* Difficulty:
37+
* Medium
38+
* Lock:
39+
* Prime
40+
* Company:
41+
* Amazon Apple Bloomberg Facebook Goldman Sachs Google LinkedIn Microsoft Snapchat
42+
*
43+
*/
44+
public class SparseMatrixMultiplication {
45+
46+
// V0
47+
// IDEA : ARRAY OP (fix by gpt)
48+
/**
49+
* Why there is 3 loop ?
50+
*
51+
* -> Matrix Multiplication: Ci,j = Sigma(Aik * Bkj)
52+
*
53+
* -> so we have 3 layer loop as below:
54+
* - i : Iterates over the rows of A (outer loop).
55+
* - j : Iterates over the columns of B (second loop).
56+
* - k : Iterates over the “shared dimension” (columns of A or rows of B ) to compute the dot product (inner loop).
57+
*
58+
*
59+
* ->
60+
*
61+
* The Role of the Loops
62+
* 1. Outer loop ( i ): Iterates over the rows of mat1 to calculate each row of the result matrix.
63+
* 2. Middle loop ( j ): Iterates over the columns of mat2 to compute each element in a row of the result matrix.
64+
* 3. Inner loop ( k ): Iterates over the “shared dimension” to compute the dot product of the i^{th} row of mat1 and the j^{th} column of mat2.
65+
*
66+
*
67+
* -> Why the Inner Loop ( k ) Exists ?
68+
*
69+
* -> The inner loop is necessary
70+
* because each element of the result matrix
71+
* is computed as the dot product of a
72+
* row from mat1 and a column from mat2.
73+
* Without this loop, the computation of the dot product would be incomplete.
74+
*/
75+
public static int[][] multiply(int[][] mat1, int[][] mat2) {
76+
// Edge case: Single element matrices
77+
if (mat1.length == 1 && mat1[0].length == 1 && mat2.length == 1 && mat2[0].length == 1) {
78+
return new int[][]{{mat1[0][0] * mat2[0][0]}};
79+
}
80+
81+
int l_1 = mat1.length; // Number of rows in mat1
82+
int w_1 = mat1[0].length; // Number of columns in mat1 (and rows in mat2)
83+
84+
int w_2 = mat2[0].length; // Number of columns in mat2
85+
86+
// Initialize the result matrix
87+
int[][] res = new int[l_1][w_2];
88+
89+
// Perform matrix multiplication
90+
for (int i = 0; i < l_1; i++) {
91+
for (int j = 0; j < w_2; j++) {
92+
int sum = 0; // Sum for res[i][j]
93+
for (int k = 0; k < w_1; k++) {
94+
sum += mat1[i][k] * mat2[k][j];
95+
}
96+
res[i][j] = sum;
97+
}
98+
}
99+
100+
return res;
101+
}
102+
103+
// V1
104+
// IDEA : ARRAY OP (gpt)
105+
public int[][] multiply_1(int[][] A, int[][] B) {
106+
int m = A.length; // Number of rows in A
107+
int n = A[0].length; // Number of columns in A
108+
int p = B[0].length; // Number of columns in B
109+
110+
int[][] result = new int[m][p];
111+
112+
// Iterate through rows of A
113+
for (int i = 0; i < m; i++) {
114+
for (int k = 0; k < n; k++) {
115+
if (A[i][k] != 0) { // Skip zero entries in A
116+
for (int j = 0; j < p; j++) {
117+
if (B[k][j] != 0) { // Skip zero entries in B
118+
result[i][j] += A[i][k] * B[k][j];
119+
}
120+
}
121+
}
122+
}
123+
}
124+
125+
return result;
126+
}
127+
128+
// V2
129+
// https://leetcode.ca/2016-10-06-311-Sparse-Matrix-Multiplication/
130+
public int[][] multiply_2(int[][] mat1, int[][] mat2) {
131+
int m = mat1.length, n = mat2[0].length;
132+
int[][] ans = new int[m][n];
133+
List<int[]>[] g1 = f(mat1);
134+
List<int[]>[] g2 = f(mat2);
135+
for (int i = 0; i < m; ++i) {
136+
for (int[] p : g1[i]) {
137+
int k = p[0], x = p[1];
138+
for (int[] q : g2[k]) {
139+
int j = q[0], y = q[1];
140+
ans[i][j] += x * y;
141+
}
142+
}
143+
}
144+
return ans;
145+
}
146+
147+
private List<int[]>[] f(int[][] mat) {
148+
int m = mat.length, n = mat[0].length;
149+
List<int[]>[] g = new List[m];
150+
Arrays.setAll(g, i -> new ArrayList<>());
151+
for (int i = 0; i < m; ++i) {
152+
for (int j = 0; j < n; ++j) {
153+
if (mat[i][j] != 0) {
154+
g[i].add(new int[] {j, mat[i][j]});
155+
}
156+
}
157+
}
158+
return g;
159+
}
160+
161+
// V3
162+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -515,32 +515,89 @@ public ListNode plusOne(ListNode head) {
515515
return null;
516516
}
517517

518-
// v1
519-
// public ListNode plusOne_1(ListNode head) {
520-
// if (head == null){
521-
// return new ListNode(1); // ??
522-
// }
523-
// List<Integer> list = new ArrayList<>();
524-
// while(head != null){
525-
// list.add(head.val);
526-
// head = head.next;
527-
// }
528-
// // ???
529-
// int cur = 0;
530-
// for (int j = list.size(); j >= 0; j--){
531-
// cur += (10 ^ j) * list.get(j);
532-
// }
533-
// cur += 1;
534-
// ListNode res = new ListNode();
535-
// String string = String.valueOf(cur);
536-
// for (String x : string.split("")){
537-
// ListNode tmp = new ListNode();
538-
// tmp.val = Integer.parseInt(x);
539-
// res.next = tmp;
540-
// res = res.next;
541-
// }
542-
//
543-
// return res;
544-
// }
518+
// v1
519+
// public ListNode plusOne_1(ListNode head) {
520+
// if (head == null){
521+
// return new ListNode(1); // ??
522+
// }
523+
// List<Integer> list = new ArrayList<>();
524+
// while(head != null){
525+
// list.add(head.val);
526+
// head = head.next;
527+
// }
528+
// // ???
529+
// int cur = 0;
530+
// for (int j = list.size(); j >= 0; j--){
531+
// cur += (10 ^ j) * list.get(j);
532+
// }
533+
// cur += 1;
534+
// ListNode res = new ListNode();
535+
// String string = String.valueOf(cur);
536+
// for (String x : string.split("")){
537+
// ListNode tmp = new ListNode();
538+
// tmp.val = Integer.parseInt(x);
539+
// res.next = tmp;
540+
// res = res.next;
541+
// }
542+
//
543+
// return res;
544+
// }
545+
546+
// LC 311
547+
// https://leetcode.ca/2016-10-06-311-Sparse-Matrix-Multiplication/
548+
// 4.39 - 4.50 pm
549+
/**
550+
* IDEA 1: ARRAY OP (brute force)
551+
*
552+
* Input:
553+
*
554+
* A = [
555+
* [ 1, 0, 0],
556+
* [-1, 0, 3]
557+
* ]
558+
*
559+
* B = [
560+
* [ 7, 0, 0 ],
561+
* [ 0, 0, 0 ],
562+
* [ 0, 0, 1 ]
563+
* ]
564+
*
565+
* Output:
566+
*
567+
* | 1 0 0 | | 7 0 0 | | 7 0 0 |
568+
* AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
569+
* | 0 0 1 |
570+
*
571+
*/
572+
public int[][] multiply(int[][] mat1, int[][] mat2) {
573+
574+
// edge case
575+
if (mat1.length == 1 && mat1[0].length == 1 && mat2.length == 1 && mat2[0].length == 1){
576+
int[][] res = new int[][]{};
577+
res[0][0] = mat1[0][0] * mat2[0][0];
578+
return res;
579+
}
580+
581+
int l_1 = mat1.length;
582+
int w_1 = mat1[0].length;
583+
584+
int l_2 = mat2.length;
585+
int w_2 = mat2[0].length;
586+
587+
int[][] res = new int[l_1][w_2]; // ???
588+
589+
for(int i = 0; i < l_1; i++){
590+
int tmp = 0;
591+
for (int j = 0; j < w_2; j++){
592+
System.out.println(">>> i = " + i + ", j = " + j + ", tmp = " + tmp);
593+
tmp += (mat1[i][j] * mat2[j][i]);
594+
res[i][j] = tmp;
595+
}
596+
//res[i][j] = tmp; // ?
597+
}
598+
599+
return res;
600+
}
601+
545602

546603
}

0 commit comments

Comments
 (0)