๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ (Java)

by 1mj 2021. 10. 26.

https://programmers.co.kr/learn/courses/30/lessons/67256

 

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

[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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2020 ์นด์นด์˜ค ์ธํ„ด์‹ญ "ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ" ๋ฌธ์ œ ํ’€์ด์ด๋‹ค.

์™ผ์†, ์˜ค๋ฅธ์†์œผ๋กœ ๋ˆ„๋ฅด๋Š” ๊ฒƒ ์ค‘ ์–ด๋–ค ๊ฒƒ์ด ๊ฐ€๊นŒ์šด์ง€ ํ‚คํŒจ๋“œ ๊ฐ„ ์ด๋™ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด ํฌ์ธํŠธ์ธ ๋ฌธ์ œ์˜€๋‹ค.

 

ํ‚คํŒจ๋“œ ๋งจ ์•„๋žซ์ค„ *, 0, #์„ ๊ฐ๊ฐ 10, 11, 12์˜ ์ˆซ์ž๋กœ ๋Œ€์ฒดํ•˜์—ฌ ๊ฐ ๊ฑฐ๋ฆฌ๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ƒํ•˜์ขŒ์šฐ๋กœ -3, 3, -1, 1๊ณผ ๊ฐ™์ด ์›€์ง์ด๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์ขŒ์šฐ๋กœ๋Š” ์ตœ๋Œ€ 2์นธ์„ ์›€์ง์ด๋Š” ๊ฒƒ์ด ์ตœ๋‹จ ๊ฑฐ๋ฆฌ์ด๋ฏ€๋กœ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ๋”ํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

import java.util.*;

class Solution {
    static final int[] USE_LEFT = {1, 4, 7};
    static final int[] USE_RIGHT = {3, 6, 9};
    
    public String solution(int[] numbers, String hand) {
        // numbers: ์ˆœ์„œ๋Œ€๋กœ ๋ˆ„๋ฅผ ๋ฒˆํ˜ธ
        // hand: ์™ผ์†์žก์ด(left), ์˜ค๋ฅธ์†์žก์ด(right)
        // ํ‚คํŒจ๋“œ์˜ ๋ฒˆํ˜ธ๋Š” ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ์„ ์œ„ํ•ด *์€ 10, 0์€ 11, #์€ 12๋กœ ๊ณ„์‚ฐ
        
        int leftPrev = 10;   // ์™ผ์†์œผ๋กœ ๋งˆ์ง€๋ง‰ ๋ˆ„๋ฅธ ์ˆซ์ž (๋””ํดํŠธ *)
        int rightPrev = 12;  // ์˜ค๋ฅธ์†์œผ๋กœ ๋งˆ์ง€๋ง‰ ๋ˆ„๋ฅธ ์ˆซ์ž (๋””ํดํŠธ #)
        
        String answer = "";
        for (int i = 0; i < numbers.length; i++) {
            // ํ˜„์žฌ ๋ˆ„๋ฅผ ๋ฒˆํ˜ธ (0์ผ ๊ฒฝ์šฐ 11๋กœ ๋Œ€์ฒด)
            int current = numbers[i] == 0 ? 11 : numbers[i];
            
            // ์™ผ์ชฝ ์†์ด ๋ˆ„๋ฅผ ๋ฒ”์œ„์— ํฌํ•จ๋œ ๋ฒˆํ˜ธ๋Š” ๋ฐ”๋กœ ์ฒ˜๋ฆฌ (1, 4, 7)
            if (isContain(USE_LEFT, current)) {
                answer += "L";
                leftPrev = current;
            }
            // ์˜ค๋ฅธ์ชฝ ์†์ด ๋ˆ„๋ฅผ ๋ฒ”์œ„์— ํฌํ•จ๋œ ๋ฒˆํ˜ธ๋Š” ๋ฐ”๋กœ ์ฒ˜๋ฆฌ (3, 6, 9)
            else if (isContain(USE_RIGHT, current)) {
                answer += "R";
                rightPrev = current;
            }
            // ๊ฐ€์šด๋ฐ ์žˆ๋Š” ๋ฒˆํ˜ธ๋“ค ์ฒ˜๋ฆฌ (2, 5, 8, 0)
            else {
                String select = "";
                int leftDistance = calcDistance(Math.abs(current - leftPrev));
                int rightDistance = calcDistance(Math.abs(current - rightPrev));

                if (leftDistance > rightDistance) {
                    select = "R";
                } else if (leftDistance < rightDistance) {
                    select = "L";
                } else {    // ์™ผ์†, ์˜ค๋ฅธ์† ๋ชจ๋‘ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™์„ ๋•Œ
                    select = "left".equals(hand) ? "L" : "R";
                } 
                
                if ("L".equals(select)) {
                    leftPrev = current;
                } else {
                    rightPrev = current;
                }
                answer += select;
            }
        }
        return answer;
    }
    
    // ๋ฐฐ์—ด์— ์š”์†Œ๊ฐ€ ํฌํ•จ๋œ์ง€ ํ™•์ธ
    public static boolean isContain(int[] arr, int val) {
        for (int num : arr) {
            if (num == val)
                return true;
        }
        return false;
    }
    
    // ๋ฒˆํ˜ธ ๊ฐ„ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
    // ์ƒํ•˜์ขŒ์šฐ ์ˆœ์„œ๋กœ -3, 3, -1, 1๋กœ ๊ณ„์‚ฐ
    // -1, 1์€ ์ตœ๋Œ€ 2์นธ๋งŒ ์ด๋™ ๊ฐ€๋Šฅ
    public static int calcDistance(int val) {
        int q = val / 3; // 3์œผ๋กœ ๋‚˜๋ˆ ์ง€์ง€ ์•Š์œผ๋ฉด ๋‚˜๋จธ์ง€๋Š” 1 ๋˜๋Š” 2์ด๋ฏ€๋กœ ์ขŒ์šฐ๋กœ ์ด๋™ํ•œ ํšŸ์ˆ˜์— ์ถ”๊ฐ€
        int r = val % 3;
        return q + r;
    }
}

 

๊ฐ ๋ฒˆํ˜ธ๋ฅผ ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•ด ์œ„์น˜๋ฅผ ์ •ํ•ด์ฃผ๊ณ , ์ดˆ๊ธฐ ์œ„์น˜๋ฅผ ์„ธํŒ…ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•๋„ ํ™œ์šฉํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

๋Œ“๊ธ€