function solution(numbers, hand) {
let leftPosition = [0, 3];
let rightPosition = [2, 3];
let leftColumn = [1, 4, 7];
let centerColumn = [2, 5, 8, 0];
let rightColumn = [3, 6, 9];
let total = [leftColumn, centerColumn, rightColumn];
let result = '';
for (let num of numbers) {
if (leftColumn.includes(num)) {
result += 'L';
leftPosition = [0, leftColumn.indexOf(num)];
} else if (rightColumn.includes(num)) {
result += 'R';
rightPosition = [2, rightColumn.indexOf(num)];
} else {
// 2, 5, 8, 0 ์ธ ๊ฒฝ์ฐ
let centerPosition = [1, centerColumn.indexOf(num)];
let leftDistance =
Math.abs(leftPosition[0] - centerPosition[0]) +
Math.abs(leftPosition[1] - centerPosition[1]);
let rightDistance =
Math.abs(rightPosition[0] - centerPosition[0]) +
Math.abs(rightPosition[1] - centerPosition[1]);
if (leftDistance < rightDistance) {
result += 'L';
leftPosition = [1, centerColumn.indexOf(num)];
} else if (leftDistance > rightDistance) {
result += 'R';
rightPosition = [1, centerColumn.indexOf(num)];
} else {
// ์์์ ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๊ฒฝ์ฐ
if (hand === 'left') {
result += 'L';
leftPosition = [1, centerColumn.indexOf(num)];
} else {
result += 'R';
rightPosition = [1, centerColumn.indexOf(num)];
}
}
}
}
return result;
}
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ํคํจ๋ ๋๋ฅด๊ธฐ
[1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] "right" "LRLLLRLLRRL" [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] "left" "LRLLRRLLLRR" [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] "right" "LLRLLRLLRL"
programmers.co.kr
- ํคํจ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ฐ๋ก์ ๋ํด์ x, ์ธ๋ก์ ๋ํด์ y ๋ผ๊ณ ํ ๋ ๊ฐ๊ฐ์ ์์น๊ฐ [x, y]๊ฐ ๋๋๋ก ๋ฐฐ์ด ๊ตฌ์ฑ
- ์ผ์ชฝ ๋ฐฐ์ด๊ณผ ์ค๋ฅธ์ชฝ ๋ฐฐ์ด์ ํด๋นํ ๋๋ ๊ฐ๊ฐ 'L'๊ณผ 'R'์ ๋ํ๊ณ ํด๋น ์์น์ ์ขํ๋ฅผ ์ ์ฅ
- ์ค์ ๋ฐฐ์ด์ ํด๋นํ ๋๋ ๊ฐ๊ฐ์ ์๊ฐ๋ฝ ์์น์ ๋ฐ๋ฅธ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๊ณ ๊ทธ ๊ฑฐ๋ฆฌ๋ฅผ ๋น๊ต
- ํญ์ ๋ฒํผ์ ๋๋ฅด๊ณ ๋์ ๊ทธ ์๋ฆฌ์ ํฌ์ง์ ์ ์๊ฐ๋ฝ ๊ฐ๊ฐ์ ์ง์ ํด์ฃผ์ด์ผํ๋ค๋ ์ ์ ๊ธฐ์ตํ์
๋ฐฐ์ด์ ๋ํด์ ํท๊ฐ๋ฆฌ์ง ์๊ณ , ํด๋น ํคํจ๋๋ก ์ด๋ ํ ๊ทธ ์๋ฆฌ์ ์์น ๋ฐฐ์ด์ ๋ฐ๋ก ํ ๋นํด๋์ด์ผ ๋ค์ ๊ฑฐ๋ฆฌ ๊ณ์ฐ์ด ๊ฐ๋ฅํ๋ค๋ ์ ๋ง ์ธ์งํ๊ณ
ํ๋ฉด ์ด๋ ต์ง ์์ ๋ฌธ์ ๊ฐ๋ค.
'๐ JavaScript > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] 2021์นด์นด์ค - ๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (0) | 2022.07.01 |
---|---|
[JS] 2021์นด์นด์ค - ์ ๊ท ์์ด๋ ์ถ์ฒ (0) | 2022.07.01 |
[JS] 2019์นด์นด์ค - ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ ๊ฒ์ (0) | 2022.06.30 |
[JS] ์์ ๋ง๋ค๊ธฐ (0) | 2022.06.30 |
[JS] ๊ทธ๋ฆฌ๋ - ์ฒด์ก๋ณต (0) | 2022.06.28 |