-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathDiagonalTraverse.java
More file actions
57 lines (47 loc) · 1.88 KB
/
Copy pathDiagonalTraverse.java
File metadata and controls
57 lines (47 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Time Complexity : O(n*m) where n is the number of rows and m is the number of columns in the matrix
// Space Complexity : O(1)
// Did this code successfully run on Leetcode :Yes
// Three line explanation of solution in plain english
// 1. We traverse the matrix in a diagonal manner, starting from the top-left corner.
// 2. We use a boolean variable to keep track of the direction of traversal (up-right or down-left).
// 3. We update the row and column indices based on the current direction
// and the boundaries of the matrix. We store the elements in the result array as we traverse.
// The auxiliary space is O(1) as we are using only a few variables to keep track of the current position and direction,
// and the result array is not considered auxiliary space as it is required for the output.
// Your code here along with comments explaining your approac
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
boolean direction = true; // true = up-right, false = down-left
int row = mat.length;
int col = mat[0].length;
int[] result = new int[row * col];
int r = 0, c = 0;
for (int i = 0; i < row * col; i++) {
result[i] = mat[r][c];
if (direction) {
if (c == col - 1) {
r++;
direction = false;
} else if (r == 0) {
c++;
direction = false;
} else {
r--;
c++;
}
} else {
if (r == row - 1) {
c++;
direction = true;
} else if (c == 0) {
r++;
direction = true;
} else {
r++;
c--;
}
}
}
return result;
}
}