File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ 1. 아이디어: 중력 적용 문제
3+ 원래 배열에서 장애물을 만나기 전까지 '#'의 수를 계산 후, 장에물 위치-1 인덱스부터 '#'개수 만큼 채워준다.
4+ 이후, 90도 회전 공식을 적용한다.
5+
6+ 2. 시간복잡도: O(M) + O(M*N) + O(M*N) => O(M*N)
7+
8+ 3. 자료구조/알고리즘: 구현
9+
10+ */
11+
12+ class Solution {
13+ private int n ,m ;
14+ public char [][] rotateTheBox (char [][] boxGrid ) {
15+ // 90도 회전시 중력 작용
16+ // 고정물 존재
17+ // 최대 500*500
18+ // 무조건 우측으로
19+
20+ m = boxGrid .length ;
21+ n = boxGrid [0 ].length ;
22+
23+ char [][] tmp = new char [m ][n ];
24+
25+ for (int i =0 ; i <m ; i ++) {
26+ Arrays .fill (tmp [i ], '.' );
27+ }
28+
29+ for (int i =0 ; i <m ; i ++) {
30+ int cnt = 0 ;
31+ for (int j =0 ; j <n ; j ++) {
32+ char c = boxGrid [i ][j ];
33+
34+ if (c == '#' ) cnt ++;
35+ else if (c == '*' ) {
36+ tmp [i ][j ] = '*' ;
37+ int idx = j -1 ;
38+
39+ while (cnt >0 ) {
40+ tmp [i ][idx ] = '#' ;
41+ cnt --;
42+ idx --;
43+ }
44+ }
45+
46+ }
47+
48+ int idx = n -1 ;
49+ while (cnt >0 ) {
50+ tmp [i ][idx ] = '#' ;
51+ idx --;
52+ cnt --;
53+ }
54+ }
55+
56+ return rotate90 (tmp );
57+ }
58+
59+ private char [][] rotate90 (char [][] tmp ) {
60+
61+ char ans [][] = new char [n ][m ];
62+ for (int i =0 ; i <m ; i ++) {
63+ for (int j =0 ; j <n ; j ++) {
64+ ans [j ][m -1 -i ] = tmp [i ][j ];
65+ }
66+ }
67+
68+ return ans ;
69+ }
70+ }
You can’t perform that action at this time.
0 commit comments