๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค[Java] - ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ(2020 ์นด์นด์˜ค ์ธํ„ด์‹ญ)

by ์ฃผ๋ฐœ2 2020. 10. 3.
๋ฐ˜์‘ํ˜•

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

 

 

 

 

์ฝ”๋“œ

class Solution {
	public static String solution(int[] numbers, String hand) {
		StringBuilder sb = new StringBuilder();
		int leftIndex = 10;	 // '*' => 10์œผ๋กœ ์น˜ํ™˜
		int rightIndex = 12; // '#' => 12๋กœ ์น˜ํ™˜

		for(int number : numbers) {
			if(number == 1 || number == 4 || number == 7) { // Left
				sb.append("L");
				leftIndex = number;
			} else if(number == 3 || number == 6 || number == 9) {	// Right
				sb.append("R");
				rightIndex = number;
			} else { // Center
				int leftLength = getLength(leftIndex, number);
				int rightLength = getLength(rightIndex, number);

				if(leftLength > rightLength) {
					sb.append("R");
					rightIndex = number;
				} else if(leftLength < rightLength) {
					sb.append("L");
					leftIndex = number;
				} else {
					if(hand.equals("right")) {
						sb.append("R");
						rightIndex = number;
					} else {
						sb.append("L");
						leftIndex = number;
					}
				}
			}
		}
		return sb.toString();
	}

	public static int getLength(int index, int number) {

		// ์ˆซ์ž 0์˜ ๊ฒฝ์šฐ 11๋กœ ์น˜ํ™˜
		index = (index == 0) ? 11 : index;	
		number = (number == 0) ? 11 : number;

		int x = (index - 1) / 3;
		int y = (index - 1) % 3;
		int numberX = number / 3;
		int numberY = 1;

		return Math.abs(x-numberX) + Math.abs(y-numberY);
	}
}

ํ’€์ด

์ด ๋ฌธ์ œ์—์„œ์˜ ํ•ต์‹ฌ์€ ๊ฐ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์œ„ ์‚ฌ์ง„์ฒ˜๋Ÿผ 0,0 ๋ถ€ํ„ฐ 3,2 ๊นŒ์ง€ ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ ์ค‘๊ฐ„์˜ ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ(2, 5, 8, 0) ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ค‘์•™ ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์ธก๊ณผ ์šฐ์ธก์˜ ๋ฒˆํ˜ธ๊ฐ€ ์–ด๋Š๊ฒƒ์ด ๋” ๊ฐ€๊นŒ์šด์ง€ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋งŒ์•ฝ ๋ˆŒ๋Ÿฌ์•ผํ•˜๋Š” ๋ฒˆํ˜ธ๊ฐ€ 5๋ฒˆ(1, 1), ์ขŒ์ธก์˜ ํ˜„์žฌ ์†๊ฐ€๋ฝ์€ 7๋ฒˆ(2, 0) , ์šฐ์ธก์˜ ํ˜„์žฌ ์†๊ฐ€๋ฝ์€ 6๋ฒˆ(1, 2) ๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด

์ขŒ์ธก์˜ ๊ฑฐ๋ฆฌ๋Š” |1-2| + |1-0| = 2 ๊ฐ€ ๋˜๊ณ ,

์šฐ์ธก์˜ ๊ฑฐ๋ฆฌ๋Š” |1-1| + |1-2| = 1 ์ด ๋˜์–ด์„œ

 

์šฐ์ธก์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ๊ฐ€๊นŒ์šฐ๋ฏ€๋กœ ์šฐ์ธก์˜ ์†์œผ๋กœ ๋ˆ„๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ๊ฐ๊ฐ ์ขŒ์ธก, ์šฐ์ธก์˜ ๋ฒˆํ˜ธ - 1, 4, 7, 3, 6, 9 ์ธ ๊ฒฝ์šฐ ๋ฒˆํ˜ธ๋ฅผ ๋ˆ„๋ฅธ ๋’ค ํ˜„์žฌ ์†๊ฐ€๋ฝ์˜ ์œ„์น˜๋ฅผ ์ธ๋ฑ์Šค๋กœ ์ €์žฅ์„ ํ•˜๊ณ ,

์ค‘์•™ ๋ฒˆํ˜ธ 2, 5, 8, 0 ์„ ๋ˆŒ๋Ÿฌ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ํ˜„์žฌ ์ขŒ์šฐ ์†๊ฐ€๋ฝ์˜ ์œ„์น˜๋ฅผ ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ๋” ๊ฐ€๊นŒ์šด ์†๊ฐ€๋ฝ์„ ๋ˆŒ๋Ÿฌ์ฃผ๋ฉด ๋œ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ค‘์•™์— ์žˆ๋Š” ๋ฒˆํ˜ธ๋ฅผ ๋ˆŒ๋ €์„๋•Œ ์ขŒ, ์šฐ, ์ค‘์•™ ๋ฒˆํ˜ธ์˜ x, y ์ขŒํ‘œ๋ฅผ ๊ตฌํ•˜๊ณ , ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๊ทœ์น™์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

 

Left            Center             Right

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

4  (1, 0)        5 (1, 1)            6(1, 2)

7  (2, 0)        8 (2, 1)            9(2, 2)

10 (3, 0)       11 (3, 1)          12(3, 2)

 

์—ฌ๊ธฐ์„œ ์ขŒ, ์šฐ์ธก์˜ x ์ขŒํ‘œ๋ฅผ ๋ณด๋ฉด ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

(์ˆซ์ž-1) / 3 ---> (index-1) / 3

 

์ขŒ, ์šฐ์ธก์˜ y ์ขŒํ‘œ์˜ ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

(์ˆซ์ž-1) % 3 ---> (index-1) % 3

 

์ค‘์•™ ๋ฒˆํ˜ธ์˜ x ์ขŒํ‘œ์˜ ๊ทœ์น™์€ ์ˆซ์ž/3 ---> number / 3

์ค‘์•™ ๋ฒˆํ˜ธ์˜ y ์ขŒํ‘œ๋Š” ๋ชจ๋‘ 1์ด๋‹ค.

 

๊ทธ ํ›„ ๋ฌด์Šจ ๊ฐ’์ด ๋” ํฐ์ง€ ๋ชจ๋ฅด๋ฏ€๋กœ ์ ˆ๋Œ“๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์„œ x, y ์ขŒํ‘œ์˜ ๊ฑฐ๋ฆฌ ์ฐจ๋ฅผ ํ•ฉํ•ด์„œ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” getLength ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

 

 

 * ์ดˆ๊ธฐ ์ขŒ์ธก, ์šฐ์ธก์˜ ์†๊ฐ€๋ฝ์€ *, # ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ˆซ์ž๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ๊ฐ๊ฐ 10, 12 ๋กœ ๋ณ€ํ™˜ํ–ˆ๋‹ค.

 

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€