Skip to content

Commit 08dff96

Browse files
committed
add 498 java
1 parent b97e76f commit 08dff96

File tree

6 files changed

+262
-21
lines changed

6 files changed

+262
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@
251251
422| [Valid Word Square](https://leetcode.com/problems/valid-word-square/) | [Python](./leetcode_python/Array/valid-word-square.py) | _O(m * n)_ | _O(1)_ | Easy |🔒, `basic`, `matrix`| AGAIN*
252252
442| [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [Python](./leetcode_python/Array/find-all-duplicates-in-an-array.py) | _O(n)_ | _O(1)_ | Medium || OK*
253253
448| [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Python](./leetcode_python/Array/find-all-numbers-disappeared-in-an-array.py)| _O(n)_ | _O(1)_ | Easy || OK
254-
498 | [Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/) | [Python](./leetcode_python/Array/diagonal_traverse.py) | _O(n)_ | _O(1)_ | Medium |`matrix`,`google`,`fb`| AGAIN*** (3) (not start)
254+
498 | [Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/) | [Python](./leetcode_python/Array/diagonal_traverse.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/DiagonalTraverse.java) | _O(n)_ | _O(1)_ | Medium |`matrix`,`google`,`fb`| AGAIN*** (4)
255255
531 | [Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i/) | [Python](./leetcode_python/Array/lonely-pixel-i.py) | _O(m * n)_ | _O(m + n)_ | Medium | 🔒, `matrix`, `basic`| AGAIN*
256256
533 | [Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii/) | [Python](./leetcode_python/Array/lonely-pixel-ii.py) | _O(m * n)_ | _O(m * n)_ | Medium |🔒| AGAIN (not start*)
257257
565 | [Array Nesting](https://leetcode.com/problems/array-nesting/) | [Python](./leetcode_python/Array/array-nesting.py) | _O(n)_ | _O(1)_ | Medium |`union find`,`dfs`,`apple`| AGAIN**** (1)

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-29
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-12-28
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+
20241229: 498
12
20241228: 379,173,855(todo)
23
20241227: 079,212(todo),362,849
34
20241222: 369,311

data/to_review.txt

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1-
2025-02-21 -> ['379,173']
2-
2025-02-20 -> ['079,212(todo),362,849,855(todo)']
1+
2025-02-22 -> ['498']
2+
2025-02-21 -> ['379,173,855(todo)']
3+
2025-02-20 -> ['079,212(todo),362,849']
34
2025-02-15 -> ['369,311']
45
2025-02-14 -> ['370']
56
2025-02-13 -> ['815,871,593,1109']
67
2025-02-07 -> ['560,523']
7-
2025-02-01 -> ['304,853,325']
8-
2025-01-31 -> ['379,173']
9-
2025-01-30 -> ['079,212(todo),362,849,855(todo)']
8+
2025-02-01 -> ['498', '304,853,325']
9+
2025-01-31 -> ['379,173,855(todo)']
10+
2025-01-30 -> ['079,212(todo),362,849']
1011
2025-01-26 -> ['370(todo)']
1112
2025-01-25 -> ['369,311']
1213
2025-01-24 -> ['370', '34,767']
1314
2025-01-23 -> ['815,871,593,1109']
1415
2025-01-20 -> ['722,380']
15-
2025-01-19 -> ['33,81']
16-
2025-01-18 -> ['379,173']
17-
2025-01-17 -> ['079,212(todo),362,849,855(todo)', '560,523', '253']
16+
2025-01-19 -> ['498', '33,81']
17+
2025-01-18 -> ['379,173,855(todo)']
18+
2025-01-17 -> ['079,212(todo),362,849', '560,523', '253']
1819
2025-01-16 -> ['776,31']
1920
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2021
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2122
2025-01-12 -> ['369,311']
22-
2025-01-11 -> ['370', '304,853,325', '394']
23-
2025-01-10 -> ['379,173', '815,871,593,1109', '833,950']
24-
2025-01-09 -> ['079,212(todo),362,849,855(todo)']
25-
2025-01-05 -> ['379,173', '370(todo)']
26-
2025-01-04 -> ['079,212(todo),362,849,855(todo)', '369,311', '560,523', '53,210,207']
27-
2025-01-03 -> ['370', '34,767', '444']
28-
2025-01-02 -> ['379,173', '815,871,593,1109', '1188,130,855(again)']
29-
2025-01-01 -> ['079,212(todo),362,849,855(todo)']
30-
2024-12-31 -> ['379,173']
31-
2024-12-30 -> ['379,173', '079,212(todo),362,849,855(todo)', '369,311', '722,380']
32-
2024-12-29 -> ['379,173', '079,212(todo),362,849,855(todo)', '370', '304,853,325', '33,81']
33-
2024-12-28 -> ['079,212(todo),362,849,855(todo)', '815,871,593,1109', '900']
23+
2025-01-11 -> ['498', '370', '304,853,325', '394']
24+
2025-01-10 -> ['379,173,855(todo)', '815,871,593,1109', '833,950']
25+
2025-01-09 -> ['079,212(todo),362,849']
26+
2025-01-06 -> ['498']
27+
2025-01-05 -> ['379,173,855(todo)', '370(todo)']
28+
2025-01-04 -> ['079,212(todo),362,849', '369,311', '560,523', '53,210,207']
29+
2025-01-03 -> ['498', '370', '34,767', '444']
30+
2025-01-02 -> ['379,173,855(todo)', '815,871,593,1109', '1188,130,855(again)']
31+
2025-01-01 -> ['498', '079,212(todo),362,849']
32+
2024-12-31 -> ['498', '379,173,855(todo)']
33+
2024-12-30 -> ['498', '379,173,855(todo)', '079,212(todo),362,849', '369,311', '722,380']
34+
2024-12-29 -> ['379,173,855(todo)', '079,212(todo),362,849', '370', '304,853,325', '33,81']
35+
2024-12-28 -> ['079,212(todo),362,849', '815,871,593,1109', '900']
3436
2024-12-27 -> ['369,311', '560,523', '253', '26,27', '802,1197,26']
3537
2024-12-26 -> ['370', '776,31']
3638
2024-12-25 -> ['369,311', '815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/diagonal-traverse/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
8+
/**
9+
* 498. Diagonal Traverse
10+
* Solved
11+
* Medium
12+
* Topics
13+
* Companies
14+
* Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.
15+
*
16+
*
17+
*
18+
* Example 1:
19+
*
20+
*
21+
* Input: mat = [[1,2,3],[4,5,6],[7,8,9]]
22+
* Output: [1,2,4,7,5,3,6,8,9]
23+
* Example 2:
24+
*
25+
* Input: mat = [[1,2],[3,4]]
26+
* Output: [1,2,3,4]
27+
*
28+
*
29+
* Constraints:
30+
*
31+
* m == mat.length
32+
* n == mat[i].length
33+
* 1 <= m, n <= 104
34+
* 1 <= m * n <= 104
35+
* -105 <= mat[i][j] <= 105
36+
* Seen this question in a real interview before?
37+
*
38+
*/
39+
public class DiagonalTraverse {
40+
41+
// V0
42+
// TODO : implement
43+
// public int[] findDiagonalOrder(int[][] mat) {
44+
//
45+
// }
46+
47+
// V1-1
48+
// https://leetcode.com/problems/diagonal-traverse/editorial/
49+
// IDEA: Diagonal Iteration and Reversal
50+
public int[] findDiagonalOrder_1_1(int[][] matrix) {
51+
52+
// Check for empty matrices
53+
if (matrix == null || matrix.length == 0) {
54+
return new int[0];
55+
}
56+
57+
// Variables to track the size of the matrix
58+
int N = matrix.length;
59+
int M = matrix[0].length;
60+
61+
// The two arrays as explained in the algorithm
62+
int[] result = new int[N * M];
63+
int k = 0;
64+
ArrayList<Integer> intermediate = new ArrayList<Integer>();
65+
66+
// We have to go over all the elements in the first
67+
// row and the last column to cover all possible diagonals
68+
for (int d = 0; d < N + M - 1; d++) {
69+
70+
// Clear the intermediate array every time we start
71+
// to process another diagonal
72+
intermediate.clear();
73+
74+
// We need to figure out the "head" of this diagonal
75+
// The elements in the first row and the last column
76+
// are the respective heads.
77+
int r = d < M ? 0 : d - M + 1;
78+
int c = d < M ? d : M - 1;
79+
80+
// Iterate until one of the indices goes out of scope
81+
// Take note of the index math to go down the diagonal
82+
while (r < N && c > -1) {
83+
84+
intermediate.add(matrix[r][c]);
85+
++r;
86+
--c;
87+
}
88+
89+
// Reverse even numbered diagonals. The
90+
// article says we have to reverse odd
91+
// numbered articles but here, the numbering
92+
// is starting from 0 :P
93+
if (d % 2 == 0) {
94+
Collections.reverse(intermediate);
95+
}
96+
97+
for (int i = 0; i < intermediate.size(); i++) {
98+
result[k++] = intermediate.get(i);
99+
}
100+
}
101+
return result;
102+
}
103+
104+
// V1-2
105+
// https://leetcode.com/problems/diagonal-traverse/editorial/
106+
// IDEA: Simulation
107+
public int[] findDiagonalOrder_1_2(int[][] matrix) {
108+
109+
// Check for empty matrices
110+
if (matrix == null || matrix.length == 0) {
111+
return new int[0];
112+
}
113+
114+
// Variables to track the size of the matrix
115+
int N = matrix.length;
116+
int M = matrix[0].length;
117+
118+
// Incides that will help us progress through
119+
// the matrix, one element at a time.
120+
int row = 0, column = 0;
121+
122+
// As explained in the article, this is the variable
123+
// that helps us keep track of what direction we are
124+
// processing the current diaonal
125+
int direction = 1;
126+
127+
// The final result array
128+
int[] result = new int[N * M];
129+
int r = 0;
130+
131+
// The uber while loop which will help us iterate over all
132+
// the elements in the array.
133+
while (row < N && column < M) {
134+
135+
// First and foremost, add the current element to
136+
// the result matrix.
137+
result[r++] = matrix[row][column];
138+
139+
// Move along in the current diagonal depending upon
140+
// the current direction.[i, j] -> [i - 1, j + 1] if
141+
// going up and [i, j] -> [i + 1][j - 1] if going down.
142+
int new_row = row + (direction == 1 ? -1 : 1);
143+
int new_column = column + (direction == 1 ? 1 : -1);
144+
145+
// Checking if the next element in the diagonal is within the
146+
// bounds of the matrix or not. If it's not within the bounds,
147+
// we have to find the next head.
148+
if (new_row < 0 || new_row == N || new_column < 0 || new_column == M) {
149+
150+
// If the current diagonal was going in the upwards
151+
// direction.
152+
if (direction == 1) {
153+
154+
// For an upwards going diagonal having [i, j] as its tail
155+
// If [i, j + 1] is within bounds, then it becomes
156+
// the next head. Otherwise, the element directly below
157+
// i.e. the element [i + 1, j] becomes the next head
158+
row += (column == M - 1 ? 1 : 0);
159+
column += (column < M - 1 ? 1 : 0);
160+
161+
} else {
162+
163+
// For a downwards going diagonal having [i, j] as its tail
164+
// if [i + 1, j] is within bounds, then it becomes
165+
// the next head. Otherwise, the element directly below
166+
// i.e. the element [i, j + 1] becomes the next head
167+
column += (row == N - 1 ? 1 : 0);
168+
row += (row < N - 1 ? 1 : 0);
169+
}
170+
171+
// Flip the direction
172+
direction = 1 - direction;
173+
174+
} else {
175+
176+
row = new_row;
177+
column = new_column;
178+
}
179+
}
180+
return result;
181+
}
182+
183+
// V2
184+
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,4 +969,55 @@ private void getValues(TreeNode root){
969969
}
970970
}
971971

972+
// LC 498
973+
// https://leetcode.com/problems/diagonal-traverse/
974+
// 4.26 pm - 4,40 pm
975+
/**
976+
*
977+
* Given an m x n matrix mat,
978+
* return an array of all the elements of the array
979+
* in a diagonal order.
980+
*
981+
* Exp 1
982+
* Input: mat = [[1,2,3],[4,5,6],[7,8,9]]
983+
* Output: [1,2,4,7,5,3,6,8,9]
984+
*
985+
*
986+
* Exp 2
987+
* Input: mat = [[1,2],[3,4]]
988+
* Output: [1,2,3,4]
989+
*
990+
*/
991+
/**
992+
* IDEA : ARRAY OP
993+
*
994+
* Input: mat = [[1,2,3],[4,5,6],[7,8,9]]
995+
*
996+
* -> (0,0) -> (1,0) -> (0,1) -> (2,0) -> (1,1) -> (2,0)
997+
* -> (2,1)
998+
*
999+
* ↖ ↘
1000+
*
1001+
*
1002+
* ->, ↙.
1003+
*
1004+
*/
1005+
public int[] findDiagonalOrder(int[][] mat) {
1006+
// edge
1007+
if(mat.length == 1 && mat[0].length == 1){
1008+
return new int[]{mat[0][0]};
1009+
}
1010+
1011+
int l = mat.length;
1012+
int w = mat[0].length;
1013+
1014+
int[] res = new int[l * w];
1015+
1016+
int[][] rightUpMove = new int[][]{{1,1}};
1017+
int[][] leftDownMove = new int[][]{{-1,1}};
1018+
1019+
1020+
return res;
1021+
}
1022+
9721023
}

0 commit comments

Comments
 (0)