๋ฐ์ํ
๐ค๋ฌธ์ ํด๊ฒฐ
- Array.from ์ ์ด์ฉํ์ฌ 2์ฐจ์ ํ๋ ฌ ๋ง๋ค๊ธฐ
- ํ
๋๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ
- ๋ฒ์ ๋ด์์ ํ ๋๋ฆฌ๋ง ์ค๋ฅธ์ชฝ, ์๋, ์ผ์ชฝ, ์๋ก ์ฐจ๋ก๋ก ์ด๋
- ์ด๋ํ๋ฉด์ ๊ฐ์ ๋ฆฌ์คํธ์ ์ ์ฅ, 2์ฐจ์ ๋ฐฐ์ด์ ๊ฐ์ ๋ฐ๊ฟ์ค๋ค(ํ์ )
- ๋ฆฌ์คํธ์ ๊ฐ์ฅ ์์ ๊ฐ์ answer ์ ์ ์ฅ
๐จ 2์ฐจ์ ๋ฐฐ์ด ๋ง๋ค๊ธฐ Array.from,
๋ฐฐ์ด์ ์ต์๊ฐ ๊ตฌํ๊ธฐ Math.min.apply(null, []); ์ด๊ฒ ๋ณด๋ค๋ Math.min(...[]); ์ด ์ข์๋ณด์(์ผ๋จ ๊ฐ๋ ์ฑ)
๐ป์์ค ์ฝ๋
function solution(rows, columns, queries) {
var answer = [];
// 1์ฉ ์ฆ๊ฐํ๋ 2์ฐจ์ ํ๋ ฌ ๋ง๋ค๊ธฐ
const arr = Array.from(Array(rows), () => new Array(columns));
let num = 1
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j ++) {
arr[i][j] = num++;
}
}
// ํ
๋๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ
queries.forEach(v => {
const newV = v.map(e => e - 1);
const border = [];
let i = newV[0]
let j = newV[1]
let changeNumber = arr[i+1][j];
// 1. ์ค๋ฅธ์ชฝ ์ด๋
while (j <= newV[3]) {
border.push(arr[i][j]);
[arr[i][j], changeNumber] = [changeNumber, arr[i][j]];
j++;
}
j--; i++;
// 2. ์๋๋ก ์ด๋
while (i <= newV[2]) {
border.push(arr[i][j]);
[arr[i][j], changeNumber] = [changeNumber, arr[i][j]];
i++;
}
i--; j--;
// 3. ์ผ์ชฝ์ผ๋ก ์ด๋
while (j >= newV[1]) {
border.push(arr[i][j]);
[arr[i][j], changeNumber] = [changeNumber, arr[i][j]];
j--;
}
j++; i--;
// 4. ์ผ์ชฝ์ผ๋ก ์ด๋
while (i > newV[0]) {
border.push(arr[i][j]);
[arr[i][j], changeNumber] = [changeNumber, arr[i][j]];
i--;
}
answer.push(Math.min(...border));
});
return answer;
}
console.log(solution(6, 6, [[2,2,5,4],[3,3,6,6],[5,1,6,3]]));
console.log(solution(3,3,[[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]] ));
console.log(solution(100, 97, [[1,1,100,97]] ));
์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค
๋ฐ์ํ