been_dev
been_archive
been_dev
전체 방문자
오늘
어제
  • 분류 전체보기 (34)
    • f-lab (3)
    • project (2)
    • solve (29)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 자바 백엔드
  • f-lab 1개월 후기
  • Eclipse
  • specify location
  • 이진변환반복하기
  • 실행창 작음
  • 에프랩
  • Downloading from external resources is disabled
  • 문자열
  • 자바
  • 프로그래머스
  • f-lab
  • f-lab 2개월 후기
  • 완전탐색
  • 그리디
  • Lombok
  • 버튼미노출
  • jadencase만들기
  • 스택
  • JWT
  • MYSQL
  • 에프랩 2개월 후기
  • 백준
  • 에프랩 1개월 후기
  • 큐
  • 탐욕법
  • 해시
  • 코딩테스트
  • 숫자의표현
  • 후기

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
been_dev

been_archive

solve

[프로그래머스/그리디] Lv1. 키패드 누르기(자바)

2024. 1. 10. 16:07

클래스를 활용한 풀이

참고 : 개발자로 취직하기

class Solution {
    Position left;
    Position right;
    Position numPos;

    public String solution(int[] numbers, String hand) {
        String answer = "";
        // 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 += finger;

            if (finger.equals("L"))
                left = numPos;
            else
                right = numPos;
        }

        return answer;
    }

    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);
        }
    }
}

 

내 풀이...

 

 

더보기
class Solution {
    String hand;
    String[] key = new String[]{"1","2","3","4","5","6","7","8","9","*","0","#"};
    String[][] keypad = new String[4][3];
    int[] currentLeft = new int[]{3,0}; // y,x (*)
    int[] currentRight = new int[]{3,2}; // y,x (#)

    public String solution(int[] numbers, String hand) {
        this.hand = hand;
        StringBuilder answer = new StringBuilder();

        // 1. 키패드 배열 초기화
        int num = 0;
        for(int i=0; i<4; i++) {
            for(int j=0; j<3; j++) {
                keypad[i][j] = key[num++];
            }
        }

        // 2. number 별 좌표 찾기
        for(int number : numbers) {
            int[] numberYX = findYXByNumber(number);

            if(numberYX[1] == 0) {
                // X 축이 0이면 왼손
                answer.append("L");
                currentLeft = numberYX;
            }
            // X 축이 2이면 오른손
            else if(numberYX[1]==2) {
                answer.append("R");
                currentRight = numberYX;
            }
            // X 축이 1이면 가까운 손
            else if (numberYX[1] == 1) {
                String closedHand = findHand(numberYX);
                if ("right".equals(closedHand)) {
                    answer.append("R");
                    currentRight = numberYX;
                } else if ("left".equals(closedHand)){
                    answer.append("L");
                    currentLeft = numberYX;
                }
            }
        }
        return answer.toString();
    }

    private int[] findYXByNumber(int number) {
        int[] result = new int[2];
        for (int i=0; i<4; i++) {
            for(int j=0; j<3; j++) {
                if(Integer.toString(number).equals(keypad[i][j])){
                    result = new int[]{i, j};
                }
            }
        }
        return result;
    }

    private String findHand(int[] position) {

        int right = Math.abs(position[1] - currentRight[1]) + Math.abs(position[0] - currentRight[0]);
        int left = Math.abs(position[1] - currentLeft[1]) + Math.abs(position[0] - currentLeft[0]);

        if(right < left) {
            return "right";
        } else if (left < right) {
            return "left";
        } else {
            return hand;
        }
    }

}

 

 

'solve' 카테고리의 다른 글

[프로그래머스/해시] Lv2. 순위 검색 (자바)  (1) 2024.01.11
[프로그래머스/해시] Lv2. 메뉴 리뉴얼 (자바)  (0) 2024.01.11
[프로그래머스/그리디] Lv1. 체육복(자바)  (0) 2024.01.09
[프로그래머스/완전탐색] Lv2. 소수 찾기 (자바)  (0) 2024.01.09
[프로그래머스] Lv2. 카펫 (자바)  (0) 2024.01.09
    'solve' 카테고리의 다른 글
    • [프로그래머스/해시] Lv2. 순위 검색 (자바)
    • [프로그래머스/해시] Lv2. 메뉴 리뉴얼 (자바)
    • [프로그래머스/그리디] Lv1. 체육복(자바)
    • [프로그래머스/완전탐색] Lv2. 소수 찾기 (자바)
    been_dev
    been_dev

    티스토리툴바