Skip to content

Latest commit

 

History

History
39 lines (34 loc) · 1.03 KB

Question_1861.md

File metadata and controls

39 lines (34 loc) · 1.03 KB

LeetCode Records - Question 1861 Rotating the Box

Attempt 1: Use a while loop to find the next empty space for each stone

class Solution {
    public char[][] rotateTheBox(char[][] box) {
        int m = box.length;
        int n = box[0].length;

        for (int i = 0; i < m; i++) {
            for (int j = n - 2; j >= 0; j--) {
                if (box[i][j] == '#') {
                    int nextJ = j;
                    while (nextJ + 1 < n && box[i][nextJ + 1] == '.') {
                        nextJ++;
                    }
                    if (j != nextJ) {
                        box[i][j] = '.';
                        box[i][nextJ] = '#';
                    }
                }
            }
        }

        char[][] ans = new char[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[i][j] = box[m - j - 1][i];
            }
        }

        return ans;
    }
}
  • Runtime: 14 ms (Beats: 28.17%)
  • Memory: 80.08 MB (Beats: 7.43%)