ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

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

[ํ’€์ด]

 

์šฐ์„  ํ•„์š”ํ•œ ์ •๋ณด๋“ค์„ ์„ธํŒ…ํ•ฉ๋‹ˆ๋‹ค.

  • (x,y)์ขŒํ‘œ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” KeyPadํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • Map<Integer, KeyPad> map์— ํ‚คํŒจ๋“œ์˜ ๋ชจ์–‘๋Œ€๋กœ ์ˆซ์ž๋“ค์˜ ์œ„์น˜ (x,y)๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • KeyPad right, keyPad right์— ๊ฐ๊ฐ ์†์˜ ์œ„์น˜ ์ขŒํ‘œ๋ฅผ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์œผ๋กœ, numbers[]์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ

  • 1,4,7 ์ˆซ์ž๋Š” ์™ผ์†์ด ํด๋ฆญํ•˜๋ฏ€๋กœ "L"์„ ์ €์žฅํ•˜๊ณ  ํ•ด๋‹น ์ˆซ์ž์˜ ์ขŒํ‘œ์˜ ์œ„์น˜๋กœ left๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
  • 3,6,9 ์ˆซ์ž๋Š” ์˜ค๋ฅธ์†์ด ํด๋ฆญํ•˜๋ฏ€๋กœ "R"์„ ์ €์žฅํ•˜๊ณ  ํ•ด๋‹น ์ˆซ์ž์˜ ์ขŒํ‘œ์˜ ์œ„์น˜๋กœ right๋ฅผ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‚˜๋จธ์ง€ ์ˆซ์ž๋“ค์— ๋Œ€ํ•ด์„œ๋Š”, click()์„ ํ†ตํ•ด ํ•ด๋‹น ์ˆซ์ž์™€ ๊ฐ ์†์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œํ›„, ๊ฐ€๊นŒ์šด ์†์œผ๋กœ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋งŒ์•ฝ, ๋‘์†์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค๋ฉด, hand์— ์ €์žฅ๋œ ์†์œผ๋กœ ํ•ด๋‹น ์ˆซ์ž๋ฅผ ๋ˆ„๋ฅด๊ณ  hand๋ฅผ ํ•ด๋‹น ์ขŒํ‘œ๋กœ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

import java.util.*;
class KeyPad{
    int x;
    int y;
    KeyPad(int x, int y) {
        this.x = x;
        this.y = y; 
    }
}
class Solution {
    static Map<Integer, KeyPad> map = new HashMap<Integer, KeyPad>(); // ์ˆซ์ž ๋“ค์–ด์žˆ๋Š” ์œ„์น˜ ์ €์žฅ
    static KeyPad left = new KeyPad(3,0); // ์™ผ์† ์œ„์น˜
    static KeyPad right = new KeyPad(3,2); // ์˜ค๋ฅธ์† ์œ„์น˜
    public String solution(int[] numbers, String hand) {
        String answer = "";
        map.put(0, new KeyPad(3,1));
        int index = 1;
        for(int i=0;i<3;i++) {
            for(int j=0;j<3;j++) {
                map.put(index, new KeyPad(i,j));
                index++;
            }
        }
        
        for(int i=0;i<numbers.length;i++) {
            // ์™ผ์ชฝ์ด ๋ˆ„๋ฆ„
            if(numbers[i] == 1 || numbers[i] == 4 || numbers[i] == 7) {
                answer += "L";
                left = map.get(numbers[i]);
            }
            // ์˜ค๋ฅธ์ชฝ์ด ๋ˆ„๋ฆ„
            else if(numbers[i] == 3 || numbers[i] == 6 || numbers[i] == 9) {
                answer += "R";
                right = map.get(numbers[i]);
            }
            // ๊ฐ€๊นŒ์šด ์†๊ฐ€๋ฝ์ด ๋ˆ„๋ฆ„
            else {
                answer += click(numbers[i], hand);
            }
        }
        return answer;
    }
    public static String click(int number, String hand) {
        KeyPad kp = map.get(number);
        int l = Math.abs(kp.x - left.x) + Math.abs(kp.y - left.y);
        int r = Math.abs(kp.x - right.x) + Math.abs(kp.y - right.y);
        
        if(l < r) {
            //์™ผ์†์ด ๊ฐ€๊นŒ์›€
            left = kp;
            return "L";
        } else if(l > r) {
            //์˜ค๋ฅธ์†์ด ๊ฐ€๊นŒ์›€
            right = kp;
            return "R";
        } else {
            if(hand.equals("left")) {
                left = kp;
                return "L";
            } else {
                right = kp;
                return "R";
            }
        }
    }
}