-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path행렬_테두리_회전하기.js
161 lines (135 loc) · 4.49 KB
/
행렬_테두리_회전하기.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// https://school.programmers.co.kr/learn/courses/30/lessons/77485
/*
* 강철원
*/
function solution(rows, columns, queries) {
// 1 ~ (r*c) 까지 이중 배열로 생성
const board = Array.from({length : rows}, (_,index) => {
return Array.from({length : columns}, (_, i) => (index*columns + 1) + i )
})
const answer = queries.reduce((acc, query) => {
acc.push(rotate(query,board))
return acc
},[])
return answer
}
function rotate(query, board) {
// 배열은 0부터 시작하기에 -1
const [r1, c1, r2 ,c2] = query.map((coordinate) => coordinate - 1)
const saveStart = board[r1][c1];
let min = saveStart
const current = board
const below = board
const before = board
const above = board
// 왼쪽
for(let i = r1; i < r2; i++) {
current[i][c1] = below[i+1][c1]
min = Math.min(min, current[i][c1])
}
// 위쪽
for(let i = c1; i < c2; i++) {
current[r2][i] = before[r2][i+1]
min = Math.min(min, current[r2][i])
}
// 오른쪽
for(let i = r2; i > r1; i--) {
current[i][c2] = above[i-1][c2]
min = Math.min(min, current[i][c2])
}
// 아래쪽
for(let i = c2; i > c1; i--) {
current[r1][i]= before[r1][i-1]
min = Math.min(min, current[r1][i])
}
board[r1][c1+1] = saveStart
return min
}
/*
* 이보리
*/
function solution(rows, columns, queries) {
const answer = [];
// 2차원 배열 생성
const matrix = Array.from({ length: rows }, (_, rowIndex) => {
return Array.from(
{ length: columns },
(_, columnIndex) => columnIndex + 1 + rows * rowIndex,
);
});
queries.forEach((query) => {
// query의 각 값 - 1 해서 계산하기 용이하게 함
const [startX, startY, endX, endY] = query.map(position => position - 1);
// 회전한 값들을 저장
const rotatedMatrix = [];
for (let i = 0; i < endY - startY; i++) rotatedMatrix.push(matrix[startX][startY + i]);
for (let i = 0; i < endX - startX; i++) rotatedMatrix.push(matrix[startX + i][endY]);
for (let i = 0; i < endY - startY; i++) rotatedMatrix.push(matrix[endX][endY - i]);
for (let i = 0; i < endX - startX; i++) rotatedMatrix.push(matrix[endX - i][startY]);
// rotatedMatrix의 마지막 값을 첫 요소로 넣어주어 회전시킴
rotatedMatrix.unshift(rotatedMatrix.pop());
// rotatedMatrix의 최소값을 answer에 저장
answer.push(Math.min(...rotatedMatrix))
// 회전한 rotatedMatrix 값을 matrix에 할당
for (let i = 0; i < endY - startY; i++) matrix[startX][startY + i] = rotatedMatrix.shift();
for (let i = 0; i < endX - startX; i++) matrix[startX + i][endY] = rotatedMatrix.shift();
for (let i = 0; i < endY - startY; i++) matrix[endX][endY - i] = rotatedMatrix.shift();
for (let i = 0; i < endX - startX; i++) matrix[endX - i][startY] = rotatedMatrix.shift();
});
return answer;
}
/*
* 신현호
*/
function solution(rows, columns, queries) {
const answer = [];
const arr = getBoard(rows, columns);
const tries = queries.length;
for (let i = 0; i < tries; i++) {
const [x1, y1, x2, y2] = queries[i];
const stack = [];
// 1. 맨 위 [x1, y1] -> +y -> [x1, y2]
for (let j = y1; j < y2; j++) {
stack.push(arr[x1][j]);
}
// 2. 오른쪽 [x1, y2] -> +x -> [x2, y2]
for (let j = x1; j < x2; j++) {
stack.push(arr[j][y2]);
}
// 3. 아래쪽 [x2, y2] -> -y -> [x2, y1]
for (let j = y2; j > y1; j--) {
stack.push(arr[x2][j]);
}
// 4. 왼쪽 [x2, y1] -> -x -> [x1, y1]
for (let j = x2; j > x1; j--) {
stack.push(arr[j][y1]);
}
// spread를 사용한 정답 찾기
answer.push(Math.min(...stack));
stack.unshift(stack.pop());
for (let j = y1; j < y2; j++) {
arr[x1][j] = stack.shift();
}
for (let j = x1; j < x2; j++) {
arr[j][y2] = stack.shift();
}
for (let j = y2; j > y1; j--) {
arr[x2][j] = stack.shift();
}
for (let j = x2; j > x1; j--) {
arr[j][y1] = stack.shift();
}
}
return answer;
}
function getBoard(rows, columns) {
const arr = Array.from(new Array(rows + 1), () =>
new Array(columns + 1).fill(0)
);
for (let i = 1; i <= rows; i++) {
for (let j = 1; j <= columns; j++) {
arr[i][j] = (i - 1) * columns + j;
}
}
return arr;
}