forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagonalMatrixPrint.java
141 lines (115 loc) · 4.18 KB
/
diagonalMatrixPrint.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in
diagonal order as shown in the below image.
Example:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,4,7,5,3,6,8,9]
INTUITION,
go along to diaonals and incrememnt but i-1, j+1
to do in reverse order, store all elements in arraylist and then reverse
REGULAR DIAONGAL MATRIX
class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
int m= matrix.length;
int n= matrix[0].length;
int index =0;
int[] res = new int[m*n];
for(int k=0; k<=m-1; k++){
int i=k; // to print all the diagonals starting on left row
int j=0; //starts on first col always
while(i>=0){ //go until we reach the upper bord
res[index++]=matrix[i][j];
i=i-1;
j=j+1;
}
}
for(int k=1; k<=n-1; k++){ //HAVE TO START FROM K=1 to skip over the diagonl that gets counted twice
int i = m-1; //starts on last rol always
int j= k; //to print all diagonals starting at bottom row
while(j<=n-1){ //go until we reach the right border
res[index++]=matrix[i][j];
i=i-1;
j=j+1;
}
}
return res;
}
}
SAME AS DIAOGONAL, but if the diagonl we are on is odd, we will reverse all the elemetnts,
THEN add to result
//TC: O(N*M) to go thorugh the entire matrix, O(K) to clear the matrix
//SC: O(min(N,M)) to store elements in arraylist
class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) return new int[0];
int m = matrix.length;
int n = matrix[0].length;
int[] res = new int[m*n];
int resIndex = 0;
ArrayList<Integer> temp = new ArrayList<>();
for(int k=0; k<m; k++){
temp.clear(); //clear out this temp array
int i = k;
int j =0;
while(i>=0){
temp.add(matrix[i][j]); //copy this elemeent
i= i-1;
j=j+1;
}
if(k%2 == 1){
Collections.reverse(temp);
}
for(int x: temp){
res[resIndex++] = x;
}
}
for(int k=1; k<n; k++){ //NOTE, need to go from k=1 to skip of, BIGGEST DIAGONAL counted twice
temp.clear(); //clear out this temp array
int i = m-1;
int j = k;
while(j< n){
temp.add((matrix[i][j]));
i=i-1;
j=j+1;
}
if(k%2 == 1){
Collections.reverse(temp);
}
for(int x: temp){
res[resIndex++] = x;
}
}
return res;
}
}
// If out of bottom border (row >= m) then row = m - 1; col += 2; change walk direction.
// if out of right border (col >= n) then col = n - 1; row += 2; change walk direction.
// if out of top border (row < 0) then row = 0; change walk direction.
// if out of left border (col < 0) then col = 0; change walk direction.
// Otherwise, just go along with the current direction.
BEST SOLUTION
//O(M*N)
//O(1) constant space solutioN!!!!
public class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
if (matrix == null || matrix.length == 0) return new int[0];
int m = matrix.length, n = matrix[0].length;
int[] result = new int[m * n];
int row = 0, col = 0, d = 0; //first direction we go is upright
int[][] dirs = {{-1, 1}, {1, -1}};
for (int i = 0; i < m * n; i++) {
result[i] = matrix[row][col];
row += dirs[d][0];
col += dirs[d][1];
if (row >= m) { row = m - 1; col += 2; d = 1 - d;}
if (col >= n) { col = n - 1; row += 2; d = 1 - d;} //out on right botder
if (row < 0) { row = 0; d = 1 - d;} //out on top border, reset
if (col < 0) { col = 0; d = 1 - d;} // out on left border, reset col
}
return result;
}
}