From 0fecaa3fb6753df63463d7095a9b1867fe7fe35c Mon Sep 17 00:00:00 2001 From: Suchitra Giri <60110218+SUCHITRAGIRI@users.noreply.github.com> Date: Sun, 3 Oct 2021 13:19:56 +0530 Subject: [PATCH 1/2] Added code of Cyclically Rotating Grid --- Java/CyclicallyRotatingGrid.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Java/CyclicallyRotatingGrid.java diff --git a/Java/CyclicallyRotatingGrid.java b/Java/CyclicallyRotatingGrid.java new file mode 100644 index 00000000..27d927c0 --- /dev/null +++ b/Java/CyclicallyRotatingGrid.java @@ -0,0 +1,30 @@ +//https://leetcode.com/contest/weekly-contest-247/problems/cyclically-rotating-a-grid/ + +// Very nice problem! + +class Solution { + public int[][] rotateGrid(int[][] a, int K) { + int n = a.length, m = a[0].length; + for(int T = 0, D = n-1, L = 0, R = m-1;L <= R && T <= D;T++,D--,L++,R--){ + int pe = 2*(n-1-T*2) + 2*(m-1-T*2); + int lk = K % pe; + for(int t = 0;t < lk;t++){ + int temp = a[T][L]; + for(int s = L+1;s <= R;s++){ + a[T][s-1] = a[T][s]; + } + for(int s = T+1;s <= D;s++){ + a[s-1][R] = a[s][R]; + } + for(int s = R-1;s >= L;s--){ + a[D][s+1] = a[D][s]; + } + for(int s = D-1;s >= T+1;s--){ + a[s+1][L] = a[s][L]; + } + a[T+1][L] = temp; + } + } + return a; + } +} From 01606122165f2e2a30c9365a4bfe945812dce373 Mon Sep 17 00:00:00 2001 From: Suchitra Giri <60110218+SUCHITRAGIRI@users.noreply.github.com> Date: Sun, 3 Oct 2021 19:30:19 +0530 Subject: [PATCH 2/2] Update the README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e6e5d46e..130c9ec6 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Java](./Java/valid-sudoku.java) | O(N^2) | O(N) | Medium | Array, 2D Matrix | | 1512 | [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) | [Java](./Java/Number-of-Good-Pairs.java) | O(N^2) | O(1) | Easy | Array | | 162 | [Find Peak element](https://leetcode.com/problems/find-peak-element/) | [javascript](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/JavaScript/findPeakElement.js) | o(Logn) | O(1) | Medium | Array | +| 1914 | [Cyclically Rotating a Grid](https://leetcode.com/problems/cyclically-rotating-a-grid/) | [Java](./Java/CyclicallyRotatingGrid.java)| O((m+n)^3) | O(1) | Medium | Array, Matrix |