<나의풀이>
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | class Solution { public String solution(int[] numbers, String hand) { String answer = ""; int left=4,right=4; hand = hand.toUpperCase(); char hand1 = hand.charAt(0); int leftleng=1, rightleng=1; for(int i=0; i<numbers.length;i++){ if(numbers[i]==0) numbers[i]=11; if(numbers[i]%3==1){ answer+="L"; left = (numbers[i]+2)/3; leftleng=1; } else if(numbers[i]%3==0){ answer+="R"; right = numbers[i]/3; rightleng=1; } else{ int j =(numbers[i]+1)/3; if(Math.abs(j-right)+rightleng > Math.abs(j-left)+leftleng){ left=j; answer+="L"; leftleng=0; } else if(Math.abs(j-right)+rightleng < Math.abs(j-left)+leftleng){ right=j; answer+="R"; rightleng=0; } else{ if(hand1=='R'){ right=j; answer+="R"; rightleng=0; } else{ left=j; answer+="L"; leftleng=0; } } } } return answer; } } | cs |
한칸씩 움직이므로 이를 표현하기 위해 총 12개의 문자를 윗열은 모두 1 다음열 2 다다음열3 마지막열 4로 만들어 풀음
<다른사람풀이>
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 32 33 34 35 36 37 38 39 40 41 | class Solution { int tempL = 10; int tempR = 12; String myhand; public String solution(int[] numbers, String hand) { myhand = ((hand.equals("right"))? "R": "L"); String answer = ""; for(int i=0 ; i< numbers.length ; i++) { switch(numbers[i]) { case 1: case 4: case 7: answer += "L"; tempL = numbers[i]; break; case 3: case 6: case 9: answer += "R"; tempR = numbers[i]; break; default: String tempHand = checkHand(numbers[i]); if(tempHand.equals("R")) tempR = numbers[i] + ((numbers[i] == 0)? 11:0); else tempL = numbers[i] + ((numbers[i] == 0)? 11:0); answer += tempHand; break; } } return answer; } private String checkHand(int tempNum) { int leftDistance = 0; int rightDistance = 0; if(tempNum == 0) tempNum = 11; leftDistance = Math.abs((tempNum-1)/3 - (tempL-1)/3) + Math.abs((tempNum-1)%3 - (tempL-1)%3); rightDistance = Math.abs((tempNum-1)/3 - (tempR-1)/3) + Math.abs((tempNum-1)%3 - (tempR-1)%3); System.out.println(tempNum + ": " + leftDistance + ", " + rightDistance); return ((leftDistance == rightDistance)? myhand: (leftDistance > rightDistance)? "R": "L"); } } | cs |