https://school.programmers.co.kr/learn/courses/30/lessons/67256
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제푸는데 40분정도 걸렸습니다.
그냥 구현 + 문자열 문제입니다.
기억해야할점
첫번째. 코드 복사할떄 변경할것들은 변경해야합니다.
lefthand가 아니라 righthand를 썻어야했는데
import java.util.*; import java.io.*; class Solution { static public StringBuilder strbuilder = new StringBuilder(); static public Node lefthand = new Node(3,0); static public Node righthand = new Node(3,2); public String solution(int[] numbers, String hand) { for(int i=0;i<numbers.length;i++){ if(numbers[i] == 1){ strbuilder.append("L"); lefthand.row = 0; lefthand.col = 0; } else if(numbers[i] == 4){ strbuilder.append("L"); lefthand.row = 1; lefthand.col = 0; } else if(numbers[i] == 7){ strbuilder.append("L"); lefthand.row = 2; lefthand.col = 0; } else if(numbers[i] == 2){ Node temp = new Node(0,1); int righthandabsdistance = Math.abs(temp.row - righthand.row) + Math.abs(temp.col - righthand.col); int lefthandabsdistance = Math.abs(temp.row - lefthand.row) + Math.abs(temp.col - lefthand.col); System.out.println(righthandabsdistance); System.out.println(lefthandabsdistance); if(lefthandabsdistance < righthandabsdistance){ strbuilder.append("L"); lefthand = temp; }else if(lefthandabsdistance == righthandabsdistance){ if(hand.equals("left")){ strbuilder.append("L"); lefthand = temp; }else if(hand.equals("right")){ strbuilder.append("R"); righthand = temp; } }else if(lefthandabsdistance > righthandabsdistance){ strbuilder.append("R"); righthand = temp; } } else if(numbers[i] == 5){ Node temp = new Node(1,1); int righthandabsdistance = Math.abs(temp.row - righthand.row) + Math.abs(temp.col - righthand.col); int lefthandabsdistance = Math.abs(temp.row - lefthand.row) + Math.abs(temp.col - lefthand.col); if(lefthandabsdistance < righthandabsdistance){ strbuilder.append("L"); lefthand = temp; }else if(lefthandabsdistance == righthandabsdistance){ if(hand.equals("left")){ strbuilder.append("L"); lefthand = temp; }else if(hand.equals("right")){ strbuilder.append("R"); righthand = temp; } }else if(lefthandabsdistance > righthandabsdistance){ strbuilder.append("R"); righthand = temp; } } // LRLLL _ R이 나와야함. else if(numbers[i] == 8){ Node temp = new Node(2,1); int righthandabsdistance = Math.abs(temp.row - righthand.row) + Math.abs(temp.col - righthand.col); int lefthandabsdistance = Math.abs(temp.row - lefthand.row) + Math.abs(temp.col - lefthand.col); if(lefthandabsdistance < righthandabsdistance){ strbuilder.append("L"); lefthand = temp; }else if(lefthandabsdistance == righthandabsdistance){ if(hand.equals("left")){ strbuilder.append("L"); lefthand = temp; }else if(hand.equals("right")){ strbuilder.append("R"); righthand = temp; } }else if(lefthandabsdistance > righthandabsdistance){ strbuilder.append("R"); righthand = temp; } } else if(numbers[i] == 0){ Node temp = new Node(3,1); int righthandabsdistance = Math.abs(temp.row - righthand.row) + Math.abs(temp.col - righthand.col); int lefthandabsdistance = Math.abs(temp.row - lefthand.row) + Math.abs(temp.col - lefthand.col); if(lefthandabsdistance < righthandabsdistance){ strbuilder.append("L"); lefthand = temp; }else if(lefthandabsdistance == righthandabsdistance){ if(hand.equals("left")){ strbuilder.append("L"); lefthand = temp; }else if(hand.equals("right")){ strbuilder.append("R"); righthand = temp; } }else if(lefthandabsdistance > righthandabsdistance){ strbuilder.append("R"); righthand = temp; } } else if(numbers[i] == 3){ strbuilder.append("R"); righthand.row = 0; righthand.col = 2; } else if(numbers[i] == 6){ strbuilder.append("R"); righthand.row = 1; righthand.col = 2; } else if(numbers[i] == 9){ strbuilder.append("R"); righthand.row = 2; righthand.col = 2; } } String answer = ""; answer = strbuilder.toString(); System.out.println(answer); return answer; } } class Node{ int row; int col; public Node(int row, int col){ this.row = row; this.col = col; } }
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[카카오 기출 문제] 괄호 변환 - 레벨 2,구현 + 문자열 (0) | 2022.10.05 |
---|---|
[카카오 기출 문제] 크레인 인형뽑기 - 레벨 1, 구현 (0) | 2022.09.05 |
[카카오 기출 문제] 숫자 문자열과 영단어 - 레벨 1, 구현 + 문자열 (0) | 2022.09.04 |
[카카오 기출 문제] 신규 아이디 추천 - 레벨 1, 구현 + 문자열 (0) | 2022.09.04 |
[카카오 기출 문제] 신고 결과 받기 - 레벨 1, 구현 + 문자열 (0) | 2022.09.03 |