forked from gouravkrosx/DSA-Revision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.txt
262 lines (237 loc) · 8.31 KB
/
Matrix.txt
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
Solutions of these questions are either on GFG or LEETCODE
---------------------------------------MATRIX-----------------------------------------
1. Spirally traversing a matrix
static ArrayList<Integer> spirallyTraverse(int m[][], int r, int c)
{
int minr=0;
int minc=0;
int maxr=r-1;
int maxc=c-1;
int count=0;
int nel=r*c;
ArrayList<Integer>list=new ArrayList<>();
while(count<nel){
for(int col=minc;col<=maxc && count<nel;col++){
list.add(m[minr][col]);
count++;
}
minr++;
for(int row=minr;row<=maxr && count<nel;row++){
list.add(m[row][maxc]);
count++;
}
maxc--;
for(int col=maxc;col>=minc && count<nel;col--){
list.add(m[maxr][col]);
count++;
}
maxr--;
for(int row=maxr;row>=minr && count<nel;row--){
list.add(m[row][minc]);
count++;
}
minc++;
}
return list;
}
-----------------------------------------------------------------------------------
2a. Search in a row wise and column wise sorted matrix
T(N)
// set indexes for top right
public boolean searchMatrix(int[][] matrix, int target) {
int n = matrix.length-1;
int m = matrix[0].length-1;
int i=0;
while(i<=n && m>=0){//terminating condition of loop
//starting from the last element of the first column
if(matrix[i][m]<target){
//increasing row
i++;
}else if(matrix[i][m]>target){
//decreasing column
m--;
}else if(matrix[i][m]==target){
return true;
}
}
return false;
}
-----------------------------------------------------------------------------------
2b. Search element in a sorted matrix->(The first integer of each row is greater than the last integer of the previous row.)
T(log n + log m)
public boolean searchMatrix(int[][] arr, int target) {
if(arr.length==0){
return false;
}
int n=arr.length;
int m=arr[0].length;
int low=0;
int high=n*m-1;
//we will use binary search as usual taking 2d array indexes and then convert the row and column indexes corresponding to their mid index.
while(low<=high){
int mid=(low+high)/2;
if(arr[mid/m][mid%m]==target){
return true;
}
if(arr[mid/m][mid%m]<target){
low=mid+1;
}else{
high=mid-1;
}
}
return false;
}
-----------------------------------------------------------------------------------
3. Row with max 1s (each row is sorted)
-> int rowWithMax1s(int arr[][], int n, int m) {
int ans=-1;
int i=0;
int j=m-1;
while(i<n && j>=0){
if(arr[i][j]==1){
ans=i;
j--; //move left
}else{
i++; //move down
}
}
return ans;
}
-----------------------------------------------------------------------------------
4. sort the matrix ->
Approach-> we took an temp array of size n*n copy the elements of matrix and the sort the array and then again update the matrix array using temp array.
-----------------------------------------------------------------------------------
5. Rotate Matrix by 90 degrees
private static void RotateMatrixby90degree(int[][] arr) {
// step 1 transpose the matrix
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr[0].length; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
// step 2 reverse every row
for (int i = 0; i < arr.length; i++) {
int l = 0, r = arr[0].length-1;
while (l <= r) {
int t = arr[i][l];
arr[i][l] = arr[i][r];
arr[i][r] = t;
l++;
r--;
}
}
}
-----------------------------------------------------------------------------------
6. Kth smallest element in sorted matrix ->(https://www.youtube.com/watch?v=oeQlc87HbbQ)
->T(NLog(max-min))
public int kthSmallest(int[][] matrix, int k) {
int n=matrix.length;
int matrix_min=matrix[0][0];
int matrix_max=matrix[n-1][n-1];
//we are using binary search here
while(matrix_min<matrix_max){
int mid=(matrix_min+matrix_max)/2;
if(countNumbers(mid,matrix,n)<k){
matrix_min=mid+1;
}else{
//mid because it can be possible that the mid which is kth element is not present in the // matrix array.
//1,5,9,10,11,12,13,13,15->here in this case once kth element comes out of 14 which is not // present in the matrix.
matrix_max=mid;
}
}
//min and max become equal therefore
return matrix_min;
}
public static int countNumbers(int mid,int[][]arr,int n){
//this will count the numbers less than the mid so that we could find the kth smallest as we get //to know how many element are smaller than the current mid.
int row=0,col=n-1;
int count=0;
while(row<n && col>=0){
if(arr[row][col]<=mid){
count+=col+1;
row=row+1;
}else{
col-=1;
}
}
return count;
}
-----------------------------------------------------------------------------------
7. Common elements in all rows
-> took a hashmap, put all the elements of first row and then check for other rows that already present elements are there in current row or not. at last count the frequency if it is equal to row that print that element
public static void CommonEleinAllRows(int[][] arr) {
int m = arr.length;
int n = arr[0].length;
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(arr[0][i], 1);
}
for (int i = 1; i < m; i++) {
for (int j = 0; j < n; j++) {
// checking if element is present in the array and is not duplicate // in the current row
if (map.containsKey(arr[i][j]) && map.get(arr[i][j]) == i) {
map.put(arr[i][j], map.get(arr[i][j]) + 1);
} else {
}
}
}
for (int val : map.keySet()) {
if (map.get(val) == m) {
System.out.println(val);
}
}
}
-----------------------------------------------------------------------------------
8. Maximum size rectangle in a Binary Matrix
Intuition-> we just break every row as a single array, simply dry run for easy explaination
public int maxArea(int mat[][], int m, int n) {
int[] arr=new int[n];
int ans=Integer.MIN_VALUE;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(mat[i][j]==1){
arr[j]=arr[j]+1;
}else{
arr[j]=0;
}
}
ans=Math.max(ans,LargestAreaHistogram(arr));
}
return ans;
}
-----------------------------------------------------------------------------------
9. Median of Row Wise Sorted Matrix
-> Median is someone who can say i have lesser than equal to number of elements on my left, and i have greater than equal to number of element on my right.
private int countSmallerThanMid(ArrayList<Integer> row, int mid) {
int l = 0, h = row.size() - 1;
while(l <= h) {
int md = (l + h) >> 1;
if(row.get(md) <= mid) {
l = md + 1;
}
else {
h = md - 1;
}
}
return l;
}
public int findMedian(ArrayList<ArrayList<Integer>> A) {
int low = Integer.MIN_VALUE;
int high = Integer.MAX_VALUE;
int n = A.size();
int m = A.get(0).size();
while(low <= high) {
int mid = (low + high) >> 1;
int cnt = 0;
for(int i = 0;i<n;i++) {
cnt += countSmallerThanMid(A.get(i), mid);
}
if(cnt <= (n * m) / 2) low = mid + 1;
else high = mid - 1;
}
return low;
}
-----------------------------------------------------------------------------------
10.