์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ๋ฐฑ์ค 2108 ์๋ฐ์คํฌ๋ฆฝํธ
- suspense ๋์์๋ฆฌ
- emotion RSC
- suspense ๋น๋๊ธฐ
- js ๋ฌธ์์ด ์์ถ
- ์ต์ ๋์ฒด์ด๋
- ๋ฐฑ์ค 1339๋ฒ nodejs
- ๊ตฌ๋ฆํค
- ์นด์นด์ค ์ฝํ
- ํ๋ก๊ทธ๋๋จธ์ค ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง
- ์ค์ฝํ
- ๋ฆฌ์กํธ์ฟผ๋ฆฌ suspense
- ์๋ฐ์คํฌ๋ฆฝํธ ๋ฌธ์์ด ์์ถ
- js
- ๋ฐฑ์ค 1339๋ฒ js
- ์๋ฐ์คํฌ๋ฆฝํธ ์ค์ฝํ
- ๊ตฌ๋ฆํค ์ฑ๋ฆฐ์ง ํ๊ณ
- app router emotion
- suspense ๋ณ๋ชฉํ์
- ํ๋ก๊ทธ๋๋จธ์ค ๋ฌธ์์ด ์์ถ
- ์ฌ์ฉ์ฑ ๊ฐ์
- suspense react-query
- next13 emotion
- TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more:
- js ์ค์ฝํ
- js ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ
- ๋ฐฑ์ค 2108 nodejs
- ๋ฐฑ์ค 1339๋ฒ ์๋ฐ์คํฌ๋ฆฝํธ
- emtion app router
- Today
- Total
Lennon FE
[ํ๋ก๊ทธ๋๋จธ์ค Lv.1] ํคํจ๋ ๋๋ฅด๊ธฐ (js) ๋ณธ๋ฌธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv.1] ํคํจ๋ ๋๋ฅด๊ธฐ (js)
Lennon 2022. 1. 24. 14:25https://programmers.co.kr/learn/courses/30/lessons/67256?language=javascript
function solution(numbers, hand) {
let answer = '';
let left = [3,0];
let right = [3,2];
let leftLen;
let rightLen;
const usedHands = (left, right, num) => {
leftLen = Math.abs(left[0]-num) + Math.abs(left[1]-1);
rightLen = Math.abs(right[0]-num) + Math.abs(right[1]-1);
if(leftLen < rightLen){
answer += 'L';
left = [num,1];
} else if (leftLen > rightLen) {
answer += 'R';
right = [num,1];
} else {
if(hand === 'left'){
answer += 'L';
left = [num,1];
} else {
answer += 'R';
right = [num,1];
}
}
return [left, right];
}
numbers.forEach((v,idx) => {
switch(v) {
case 1:
left = [0,0];
answer += 'L';
break;
case 4:
left = [1,0];
answer += 'L';
break;
case 7:
left = [2,0];
answer += 'L';
break;
case 3:
right = [0,2];
answer += 'R';
break;
case 6:
right = [1,2];
answer += 'R';
break;
case 9:
right = [2,2];
answer += 'R';
break;
case 2:
[left, right] = usedHands(left, right, 0);
break;
case 5:
[left, right] = usedHands(left, right, 1);
break;
case 8:
[left, right] = usedHands(left, right, 2);
break;
case 0:
[left, right] = usedHands(left, right, 3);
break;
}
})
return answer;
}
ํคํจ๋๋ฅผ ๊ฐ์ ๋ฐฐ์ด๋ก ์๊ฐํด
[ [1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[*, 0, #] ]์ผ๋ก ํ๊ณ , ๊ฐ์ ๋ฐ๋ผ ์ธ๋ฑ์ค๋ก ํํํ๋ค. ex) 1์ [0,0] 2๋ [0,1] 5๋ [1,1]
์ฒซ ์์์ด ์ผ์์ *, ์ค๋ฅธ์์ # ์ด๋ฏ๋ก left๋ฅผ [3,0], right๋ฅผ [3,2]๋ก ์ด๊ธฐํ๋ฅผ ์์ผ์ค๋ค.
๊ทธ ํ switch๋ฌธ์ ์ด์ฉํด 1,4,7๋ง๋ค answer์ L์ ๋ํด์ฃผ๊ณ , ์ผ์์ ์์น(left)๋ฅผ ์ ๊ฐ์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ก ์๋ก ์ด๊ธฐํ์์ผ์ค๋ค.
3,6,9๋ ๋ง์ฐฌ๊ฐ์ง๋ก answer์ R์ ๋ํด์ฃผ๊ณ , ์ค๋ฅธ์์ ์์น(right)๋ฅผ ์ ๊ฐ์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ก ์๋ก ์ด๊ธฐํ์์ผ์ค๋ค.
2,5,8,0 ์ฒ๋ฆฌ ์๊ณ ๋ฆฌ์ฆ
usedHands ํจ์ => ์ผ์์ ์ขํ๊ฐ, ์ค๋ฅธ์์ ์ขํ๊ฐ, ์ซ์๋ฅผ ์ธ์๋ก ๋ฐ๋๋ค.
2,5,8,0์ ๊ณตํต์ ์ x์ขํ๊ฐ 1๋ก ๊ณ ์ ์ด๋ผ๋ ๊ฒ๊ณผ ์ธ๋ก๋ง 1์ฉ ์ฆ๊ฐ๋๋ ๊ฒ์ด๋ค.
ex) 2 = [0,1] , 5 = [1,1], 8 = [2,1], 0 = [3,1] => ๋ง์ง๋ง num์ ํด๋น ์ซ์(0,1,2,3)๋ฅผ ์๋ฏธํจ.
leftLan => left์ 2,5,8,0์ ์ขํ๊ฐ์ ๋ํด ๊ธธ์ด๋ฅผ ๊ตฌํ ๊ฒ
rightLan => right์ 2,5,8,0์ ์ขํ๊ฐ์ ๋ํด ๊ธธ์ด๋ฅผ ๊ตฌํ ๊ฒ
๋๋จธ์ง๋ ๊ธธ์ด๋ฅผ ๋น๊ตํด answer๋ฐฐ์ด์ ๋ฃ์ด์ฃผ๊ณ ๋ค์ left, right์ ์ขํ๋ฅผ ์ด๊ธฐํํด์ฃผ๊ณ ๋ฆฌํดํ๋ค.
switch๋ฌธ์์ ํด๋น ๊ฐ์ left, right๋ก ๋ค์ ๋ฐ์์ฃผ๋ฉด ๋!
'๐ฅ Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv.3] ๋จ์ ์นด๋ฉ๋ผ (js) (0) | 2022.01.30 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] 2๊ฐ ์ดํ๋ก ๋ค๋ฅธ ๋นํธ (js) (0) | 2022.01.30 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ(js) (0) | 2022.01.22 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ(js) (0) | 2022.01.22 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ(js) (0) | 2022.01.22 |