71일 (1861. Rotating the Box) matrix
You are given an m x n matrix of characters box representing a side-view of a box. Each cell of the box is one of the following:
- A stone '#'
- A stationary obstacle '*'
- Empty '.'
The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and the inertia from the box's rotation does not affect the stones' horizontal positions.
It is guaranteed that each stone in box rests on an obstacle, another stone, or the bottom of the box.
Return an n x m matrix representing the box after the rotation described above.
Example 1:
Input: box = [["#",".","#"]]
Output: [["."],
["#"],
["#"]]
Example 2:
Input: box = [["#",".","*","."],
["#","#","*","."]]
Output: [["#","."],
["#","#"],
["*","*"],
[".","."]]
Example 3:
Input: box = [["#","#","*",".","*","."],
["#","#","#","*",".","."],
["#","#","#",".","#","."]]
Output: [[".","#","#"],
[".","#","#"],
["#","#","*"],
["#","*","."],
["#",".","*"],
["#",".","."]]
Constraints:
- m == box.length
- n == box[i].length
- 1 <= m, n <= 500
- box[i][j] is either '#', '*', or '.'
- 접근방법
기능은 하나하나 간단하고 직관적이게 작성하고 , 성능은 일단 동작하게 끔 만들고나서 천천히 다듬어야한다.
1. m*n 배열 box의 각 행(i)의 모든 열(j) 들에 포함된 돌들을 우측으로 밀착시켜야한다.
>> 단, 장애물에 막히거나('*') , 박스의 오른쪽 끝(n-1)에 도달하는 경우마다 반복한다.
>> 밑의 그림을 보면 검정 직사각형에 막힐때 돌을 우측으로 밀착, 배열이 끝에 도달하였을때 돌을 우측으로 밀착한다.
(파란원은 '#', 빨간x는 '.', 검정 직사각형은 '*' 일때)
{'#', '.', '*', '#', '#', '.'} -> {'.', '#', '*', '.', '#', '#'}으로 변환하면 된다.
2. box배열을 시계방향으로 90도 회전하면 각 값이 동일한 인덱스는?
>> m*n 배열을 오른쪽으로 90회전한다면 n*m배열이 되며 배열끼리 같은 값은 answer[j][m-1-i] = box[i][j] 이다.
- 코드
class Solution {
public char[][] rotateTheBox(char[][] box) {
int m = box.length;
int n = box[0].length;
char[][] answer = new char[n][m];
// 리턴할 n*m배열에 빈 공간을 나타내는 '.'로 채워넣는다.
for(char[] arr : answer) Arrays.fill(arr, '.');
for (int i = 0; i < m; i++) {
int stoneCount = 0;
for (int j = 0; j < n; j++) {
if (box[i][j] == '#') stoneCount++; // box i행의 돌의 개수를 센다.
if (box[i][j] == '*') { // 1. 현재 위치가 벽일때
// box를 시계방향으로 90도 회전시킨 answer에 벽 '*'을 넣는다.
answer[j][m-1-i] = '*';
int temp = j-1;
// 현재 벽의 왼쪽방향으로 돌의 개수만큼 '#'을 넣는다.
while(stoneCount > 0) {
answer[temp][m-1-i] = '#'; // answer에 돌 '#'을 넣는다.
stoneCount--;
temp--;
}
}
if(j == n-1) { // 2. 현재 위치가 공간의 우측 끝일때
int temp = j;
// 현재 위치를 '포함'하여 왼쪽 방향으로 돌 '#'을 채운다.
while(stoneCount > 0) {
answer[temp][m-1-i] = '#';
stoneCount--;
temp--;
}
}
}
}
return answer;
}
}
- 코드를 다듬었기 때문에
box를 수정한 후 box[i][j]를 answer[j][m-1-i]로 옮겨담지않고, box의 수정할 값을 그대로 answer[j][m-1-i]에 옮겨담았다.
주석1은 벽, 주석2는 공간의 우측 끝으로 각각 구분된 공간마다
돌을 우측부터 채운 값을, 90도 시계방향으로 회전한 배열 answer에 담고 리턴하였다.
https://leetcode.com/problems/rotating-the-box/
Rotating the Box - LeetCode
Can you solve this real interview question? Rotating the Box - You are given an m x n matrix of characters box representing a side-view of a box. Each cell of the box is one of the following: * A stone '#' * A stationary obstacle '*' * Empty '.' The box is
leetcode.com