Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.1] ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ (js) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.1] ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ (js)

Lennon 2022. 1. 24. 14:25
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/67256?language=javascript 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ

[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

 

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๋กœ ๋‹ค์‹œ ๋ฐ›์•„์ฃผ๋ฉด ๋!

728x90
๋ฐ˜์‘ํ˜•
Comments