๊ด€๋ฆฌ ๋ฉ”๋‰ด

Welcome! Everything is fine.

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

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

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

๊ฐœ๋ฐœ๊ณฐ๋ฐœ 2024. 4. 23.
728x90

๐Ÿ“Œ ๋ฌธ์ œ

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

๐Ÿ“Œ ํ’€์ด

ํ‚คํŒจ๋“œ ๊ฐ„ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ *, 0, #์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ชฐ๋ผ ๋‹ต์„ ๋ณด๊ณ  ํ‘ผ ๋ฌธ์ œ์ด๋‹ค. ๋จผ์ € *, 0, #์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐจ๋ก€๋Œ€๋กœ 10, 11, 12๋กœ ์„ค์ •ํ•œ ํ›„ ๋ฌธ์ œ๋ฅผ ํ‘ผ๋‹ค.

1 2 3
4 5 6
7 8 9
*(10) 0(11) #(12)
  • numbers ๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ ์ˆœ์„œ๋Œ€๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋ˆ„๋ฅด๋ฉฐ ์™ผ์†์œผ๋กœ ๋ˆŒ๋ €๋Š”์ง€ ์˜ค๋ฅธ์†์„ ๋ˆŒ๋ €๋Š”์ง€ ํŒ๋‹จํ•œ๋‹ค.
  • ๋ˆ„๋ฅธ ๋ฒˆํ˜ธ๊ฐ€ 1, 4, 7 ์ด๋ผ๋ฉด
    • ์™ผ์†์œผ๋กœ ํ‚คํŒจ๋“œ๋ฅผ ๋ˆ„๋ฅด๊ณ  ์™ผ์†์„ ๋ˆ„๋ฅธ ์ž๋ฆฌ๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
  • ๋ˆ„๋ฅธ ๋ฒˆํ˜ธ๊ฐ€ 3, 6, 9 ๋ผ๋ฉด
    • ์˜ค๋ฅธ์†์œผ๋กœ ํ‚คํŒจ๋“œ๋ฅผ ๋ˆ„๋ฅด๊ณ  ์˜ค๋ฅธ์†์„ ๋ˆ„๋ฅธ ์ž๋ฆฌ๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
  • ๊ทธ ์™ธ ๋ฒˆํ˜ธ๋ผ๋ฉด
    • ์™ผ์†์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ์™€ ์˜ค๋ฅธ์†์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค.
    • ์™ผ์†์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ํฌ๋‹ค๋ฉด
      • ์˜ค๋ฅธ์†์œผ๋กœ ํ‚คํŒจ๋“œ๋ฅผ ๋ˆ„๋ฅด๊ณ  ์˜ค๋ฅธ์†์„ ๋ˆ„๋ฅธ ์ž๋ฆฌ๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
    • ์˜ค๋ฅธ์†์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ํฌ๋‹ค๋ฉด
      • ์™ผ์†์œผ๋กœ ํ‚คํŒจ๋“œ๋ฅผ ๋ˆ„๋ฅด๊ณ  ์™ผ์†์„ ๋ˆ„๋ฅธ ์ž๋ฆฌ๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
    • ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค๋ฉด
      • hand์˜ ๊ฐ’์— ๋”ฐ๋ผ ์ฒ˜๋ฆฌํ•œ๋‹ค.

ํ๋ฆ„๋งŒ ๋ณด๋ฉด ์–ด๋ ต์ง€ ์•Š์ง€๋งŒ "์™ผ์†์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ์™€ ์˜ค๋ฅธ์†์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค." ๋ผ๋Š” ๋ถ€๋ถ„์„ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•ด์„œ ์–ด๋ ค์› ๋‹ค. ์ด ๋ถ€๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

int leftDist = Math.abs(num - left) / 3 + Math.abs(num - left) % 3;
int rightDist = Math.abs(num - right) / 3 + Math.abs(num - right) % 3;

๐Ÿ“Œ ๋‹ค๋ฅธ ํ’€์ด -  ์ขŒํ‘œ๋กœ ๊ตฌํ•˜๊ธฐ

๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค์„ ์‚ดํŽด๋ณด๋‹ˆ ํ‚คํŒจ๋“œ๋ฅผ ์ขŒํ‘œ๋กœ ๋†“๊ณ  ๊ตฌํ•˜๋Š” ํ’€์ด๋„ ์žˆ์–ด ๊ธฐ๋กํ•ด๋†“์œผ๋ ค๊ณ  ํ•œ๋‹ค. ์ด ํ’€์ด๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋‚˜๋ˆ  ์ž‘์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋” ๊ฐ€๋…์„ฑ์žˆ๊ณ  ๊น”๋”ํ•ด๋ณด์ธ๋‹ค! ์ด ํ’€์ด์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ํ•ด๊ฒฐํ–ˆ๋‹ค.

  • Position ํด๋ž˜์Šค
    • ์†์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•  Position ์ƒ์„ฑ์ž ๋ฉ”์„œ
    • ํ‚คํŒจ๋“œ๋ฅผ ๋ˆ„๋ฅด๋Š” ์†์„ ๊ตฌํ•˜๋Š” getFinger() ๋ฉ”์„œ๋“œ
    • ํ‚คํŒจ๋“œ์™€ ์† ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” getDistance() ๋ฉ”์„œ๋“œ

๋จผ์ € ๊ฐ„๋‹จํ•œ Position ํด๋ž˜์Šค์™€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

class Position {
    int row; 
    int col;
    
    Position(int row, int col) {
        this.row = row;
        this.col = col;
    }
}

 

solution ํ•จ์ˆ˜์— ๋งŒ๋“ค์–ด๋‘” Position ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ์™ผ์†์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•  left, ์˜ค๋ฅธ์†์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•  right, ๋ˆ„๋ฅผ ๋ฒˆํ˜ธ์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•  numPos ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ left์™€ right๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์™ผ์†์€ *, ์ฆ‰ ์ขŒํ‘œ์ƒ (3, 0)์— ์œ„์น˜ํ•˜๊ณ  ์˜ค๋ฅธ์†์€ #, ์ฆ‰ ์ขŒํ‘œ์ƒ (3, 2)์— ์œ„์น˜ํ•œ๋‹ค.

Position left;
Position right;
Position numPos;
    
public String solution(int[] numbers, String hand) {
    StringBuilder answer = new StringBuilder();

    left = new Position(3, 0);
    right = new Position(3, 2);
    
    return answer.toString();
}

 

ํ—ท๊ฐˆ๋ฆฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ขŒํ‘œ๋ฅผ ์ ์€ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋†“๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.

(0, 0) (0, 1) (0, 2)
(1, 0) (1, 1) (1, 2)
(2, 0) (2, 1) (2, 2)
(3, 0) (3, 1) (3, 2)

 

๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ numbers ๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ ์ˆซ์ž๋ฅผ ๋ˆ„๋ฅผ ์†๊ฐ€๋ฝ์„ ์ •ํ•˜๊ณ , answer์— ์ •ํ•œ ์†๊ฐ€๋ฝ์„ ์ €์žฅํ•œ ํ›„ ์†๊ฐ€๋ฝ ์œ„์น˜๋ฅผ ์ด๋™ํ•˜๋Š” ๊ณผ์ •์ด๋‹ค. numPos์˜ ์ขŒํ‘œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‹์œผ๋กœ ๊ตฌํ•œ๋‹ค.(์œ„์—์„œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ–ˆ๋˜ ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•˜๋‹ค.)

for (int num : numbers) {
    numPos = new Position((num - 1) / 3, (num - 1) % 3); // numPos์˜ ์ขŒํ‘œ ๊ตฌํ•˜๊ธฐ
    if (num == 0) numPos = new Position(3, 1); // ๋ฒˆํ˜ธ๊ฐ€ 0์ด๋ผ๋ฉด ์ขŒํ‘œ๋ฅผ (3, 1)๋กœ ๋„ฃ์–ด์ฃผ๊ธฐ
    String finger = numPos.getFinger(hand); // ๋ฒˆํ˜ธ๋ฅผ ๋ˆ„๋ฅผ ์†๊ฐ€๋ฝ ์ •ํ•˜๊ธฐ
            
    answer.append(finger);
            
    if (finger.equals("L")) left = numPos;
    else right = numPos;
}

 

๋‹ค์Œ์€ Position ํด๋ž˜์Šค ์•ˆ์— ์žˆ๋Š” getFinger() ๋ฉ”์„œ๋“œ์™€  getDistance() ๋ฉ”์„œ๋“œ์ด๋‹ค. hand๋ฅผ ์ด์šฉํ•ด finger๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— if๋ฌธ์„ ๊ทธ๋ƒฅ ๋น ์ ธ๋‚˜์˜ค๋”๋ผ๋„ ์ƒ๊ด€์—†๋‹ค. col์ด 0์ด๋ฉด 1, 4, 7์ด๊ธฐ ๋•Œ๋ฌธ์— ์™ผ์†์œผ๋กœ ๋ˆ„๋ฅด๊ณ , col์ด 2๋ผ๋ฉด 3, 6, 9์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅธ์†์œผ๋กœ ๋ˆ„๋ฅธ๋‹ค. ๋‘˜ ๋‹ค ์•„๋‹ˆ๋ผ๋ฉด else๋ฌธ์œผ๋กœ ๋‚˜์™€ ๊ฐ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด ๋” ๊ฐ€๊นŒ์šด ์ชฝ์˜ ์†์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฐธ๊ณ ๋ž€์— ์žˆ๋Š” ์œ ํŠœ๋ธŒ ์˜์ƒ์„ ๋ณด๋ฉด ๋” ์ดํ•ด๊ฐ€ ์ž˜ ๊ฐˆ ๊ฒƒ์ด๋‹ค.

public String getFinger(String hand) {
    String finger = hand.equals("right") ? "R" : "L";
        
    if (this.col == 0) finger = "L";
    else if (this.col == 2) finger = "R";
    else {
        int leftDist = left.getDistance(this);
        int rightDist = right.getDistance(this);
            
        if (leftDist < rightDist) finger = "L";
        else if (rightDist < leftDist) finger = "R";
    }
        
    return finger;
}
    
public int getDistance(Position p) {
    return Math.abs(this.row - p.row) + Math.abs(this.col - p.col);
}

 

๐Ÿ“Œ ์ „์ฒด ์ฝ”๋“œ

์ฒซ ๋ฒˆ์งธ ์ฝ”๋“œ

class Solution {
    public String solution(int[] numbers, String hand) {
        StringBuilder answer = new StringBuilder();
        int left = 10;
        int right = 12;
        for (int num : numbers) {
            if (num == 1 || num == 4 || num == 7) {
                answer.append('L');
                left = num;
            } else if (num == 3 || num == 6 || num == 9) {
                answer.append('R');
                right = num;
            } else {
                if (num == 0) num = 11;
                int leftDist = Math.abs(num - left) / 3 + Math.abs(num - left) % 3;
                int rightDist = Math.abs(num - right) / 3 + Math.abs(num - right) % 3;
                
                if (leftDist > rightDist) {
                    answer.append('R');
                    right = num;
                } else if (leftDist < rightDist) {
                    answer.append('L');
                    left = num;
                } else {
                    if (hand.equals("right")) {
                        answer.append('R');
                        right = num;
                    } else {
                        answer.append('L');
                        left = num;
                    }
                }
            }
        }
        
        return answer.toString();
    }
}

๋‘ ๋ฒˆ์งธ ์ฝ”๋“œ

class Solution {
    Position left;
    Position right;
    Position numPos;
    
    public String solution(int[] numbers, String hand) {
        StringBuilder answer = new StringBuilder();
        // 1. ์™ผ์†, ์˜ค๋ฅธ์† ์œ„์น˜ ์ดˆ๊ธฐํ™”
        left = new Position(3, 0);
        right = new Position(3, 2);
        
        for (int num : numbers) {
            // 2. ์ˆซ์ž๋ฅผ ๋ˆ„๋ฅผ ์†๊ฐ€๋ฝ ์ •ํ•˜๊ธฐ
            numPos = new Position((num - 1) / 3, (num - 1) % 3);
            if (num == 0) numPos = new Position(3, 1);
            String finger = numPos.getFinger(hand);
            
            //3. ์ •ํ•ด์ง„ ์†๊ฐ€๋ฝ์„ answer์— ๋‹ฌ๊ณ , ์†๊ฐ€๋ฝ ์œ„์น˜ ์ด๋™
            answer.append(finger);
            
            if (finger.equals("L")) 
                left= numPos;
            else 
                right = numPos;
        }
        
        return answer.toString();
    }

class Position {
    int row; 
    int col;
    
    Position(int row, int col) {
        this.row = row;
        this.col = col;
    }
    
    public String getFinger(String hand) {
        String finger = hand.equals("right") ? "R" : "L";
        
        if (this.col == 0) finger = "L";
        else if (this.col == 2) finger = "R";
        else {
            int leftDist = left.getDistance(this);
            int rightDist = right.getDistance(this);
            
            if (leftDist < rightDist)
                finger = "L";
            else if (rightDist < leftDist)
                finger = "R";
        }
        
        return finger;
    }
    
    public int getDistance(Position p) {
        return Math.abs(this.row - p.row) + Math.abs(this.col - p.col);
    }
}
    
}

 

์ฐธ๊ณ