Skip to content

Commit 930dec7

Browse files
add 1260
1 parent 144be8c commit 930dec7

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
1010
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
1111
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | | | |Easy||
12+
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | | | |Easy||
1213
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | O(m*n + k) | O(m*n) | |Easy||
1314
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | | | |Easy||
1415
|1217|[Play with Chips](https://leetcode.com/problems/play-with-chips/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1217.java) | | | |Easy||
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
7+
/**
8+
* 1260. Shift 2D Grid
9+
*
10+
* Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.
11+
*
12+
* In one shift operation:
13+
* Element at grid[i][j] becomes at grid[i][j + 1].
14+
* Element at grid[i][n - 1] becomes at grid[i + 1][0].
15+
* Element at grid[n - 1][n - 1] becomes at grid[0][0].
16+
* Return the 2D grid after applying shift operation k times.
17+
*
18+
* Example 1:
19+
* Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
20+
* Output: [[9,1,2],[3,4,5],[6,7,8]]
21+
*
22+
* Example 2:
23+
* Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
24+
* Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
25+
*
26+
* Example 3:
27+
* Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
28+
* Output: [[1,2,3],[4,5,6],[7,8,9]]
29+
*
30+
* Constraints:
31+
* m == grid.length
32+
* n == grid[i].length
33+
* 1 <= m <= 50
34+
* 1 <= n <= 50
35+
* -1000 <= grid[i][j] <= 1000
36+
* 0 <= k <= 100
37+
* */
38+
public class _1260 {
39+
public static class Solution1 {
40+
/**credit: https://leetcode.com/problems/shift-2d-grid/discuss/431102/JavaPython-3-simple-code-using-mod*/
41+
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
42+
int m = grid.length;
43+
int n = grid[0].length;
44+
int start = m * n - k % (m * n);
45+
LinkedList<List<Integer>> result = new LinkedList<>();
46+
for (int i = start; i < m * n + start; i++) {
47+
int newIndex = i % (m * n);
48+
int newRow = newIndex / n;
49+
int newColumn = newIndex % n;
50+
if ((i - start) % n == 0) {
51+
result.add(new ArrayList<>());
52+
}
53+
result.peekLast().add(grid[newRow][newColumn]);
54+
}
55+
return result;
56+
}
57+
}
58+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1260;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1260Test {
13+
private static _1260.Solution1 solution1;
14+
private static int[][] grid;
15+
private static List<List<Integer>> expected;
16+
;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _1260.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
grid = new int[][]{
26+
{1, 2, 3},
27+
{4, 5, 6},
28+
{7, 8, 9}
29+
};
30+
expected = Arrays.asList(
31+
Arrays.asList(9, 1, 2),
32+
Arrays.asList(3, 4, 5),
33+
Arrays.asList(6, 7, 8)
34+
);
35+
assertEquals(expected, solution1.shiftGrid(grid, 1));
36+
}
37+
38+
@Test
39+
public void test2() {
40+
grid = new int[][]{
41+
{1},
42+
{2},
43+
{3},
44+
{4},
45+
{7},
46+
{6},
47+
{5}
48+
};
49+
expected = Arrays.asList(
50+
Arrays.asList(6),
51+
Arrays.asList(5),
52+
Arrays.asList(1),
53+
Arrays.asList(2),
54+
Arrays.asList(3),
55+
Arrays.asList(4),
56+
Arrays.asList(7)
57+
);
58+
assertEquals(expected, solution1.shiftGrid(grid, 23));
59+
}
60+
61+
@Test
62+
public void test3() {
63+
grid = new int[][]{
64+
{3, 8, 1, 9},
65+
{19, 7, 2, 5},
66+
{4, 6, 11, 10},
67+
{12, 0, 21, 13}
68+
};
69+
expected = Arrays.asList(
70+
Arrays.asList(12, 0, 21, 13),
71+
Arrays.asList(3, 8, 1, 9),
72+
Arrays.asList(19, 7, 2, 5),
73+
Arrays.asList(4, 6, 11, 10)
74+
);
75+
assertEquals(expected, solution1.shiftGrid(grid, 4));
76+
}
77+
78+
}

0 commit comments

Comments
 (0)