-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathShift2DGrid.java
More file actions
59 lines (50 loc) · 1.48 KB
/
Shift2DGrid.java
File metadata and controls
59 lines (50 loc) · 1.48 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
58
59
// Author: Shobhit Behl (LC: shobhitbruh)
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
int n=grid.length;
int m=grid[0].length;
LinkedList<Integer> li=new LinkedList<>();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
li.addLast(grid[i][j]);
}
}
while(k-->0){
int f=li.removeLast();
li.addFirst(f);
}
List<List<Integer>> al=new ArrayList<>();
for(int i=0;i<n;i++){
List<Integer> g=new ArrayList<>();
for(int j=0;j<m;j++){
g.add(li.removeFirst());
}
al.add(g);
}
return al;
}
}
// @saorav21994
// TC : O(n^2)
// SC : O(n^2)
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
int m = grid.length;
int n = grid[0].length;
int ele = m * n;
k = k % ele;
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < m; i++) {
List<Integer> sublist = new ArrayList<>();
for (int j = 0; j < n; j++) {
int idx = findShiftedIndex(i, j, n, k, ele);
sublist.add(grid[idx/n][idx%n]);
}
res.add(sublist);
}
return res;
}
public int findShiftedIndex(int i, int j, int col, int k, int ele) {
return (i * col + j - k + ele) % ele;
}
}