4 Jul 2020

2020 카카오 인턴십 키패드 누르기

2020카카오 인턴십 수식최대화

class Solution {
    int posL;
    int posR;
    int dist(int num,int comp){
        if(num==0){
            num=10;
        }else{
            num--;
        }
        
        if(comp==0){
            comp=10;
        }else{
            comp--;
        }
        int x1=num/3;
        int y1=num%3;
        int x2=comp/3;
        int y2=comp%3;
        
        return Math.abs(x1-x2)+Math.abs(y1-y2);
    }
    String selectHand(int num,String hand){
        
        String selected="";
        switch(num){
            case 1:
            case 4:
            case 7:
                posL=num;
                selected="L";
                break;
            case 3:
            case 6:
            case 9:
                posR=num;
                selected="R";
                break;
            case 2:
            case 5:
            case 8:
            case 0:
                
                int left=dist(num,posL);
                int right=dist(num,posR);
                
                if(left==right){
                    if(hand.equals("right")){
                        posR=num;
                        selected="R";
                    }else{
                        posL=num;
                        selected="L";
                    }
                }else if(left>right){
                     posR=num;
                    selected="R";
                }else if(left<right){
                    posL=num;
                    selected="L";
                }
                break;
        }
        return selected;
    }
    public String solution(int[] numbers, String hand) {
        String answer = "";
        StringBuilder sb=new StringBuilder();
        posL=10;
        posR=12;
        for(int x:numbers){
            sb.append(selectHand(x,hand));
        }
        answer=sb.toString();
        return answer;
    }
}


Tags:
0 comments