[TIL] 2023년 2월 16일

susu·2023년 2월 16일
1

Algorithm & Coding Test

목록 보기
2/6
post-thumbnail

📌 자바 Queue 구현

선언

Queue<Integer> visit = new LinkedList<>();

LinkedList와 ArrayList의 차이는 양방향 순회가 가능한지.
ArrayList는 음수 인덱스에 대한 순회가 불가능하다.
따라서 큐에는 LinkedList를 사용하는 것.

메소드

  • 값 추가 : add() vs offer()
    큐의 맨 뒤에 삽입되며, 추가에 성공하면 true를 리턴하는 건 동일
    but 큐가 꽉 차서 삽입할 수 없을 때
    add는 IllegalStateException 에러를 발생시키고,
    offer은 false를 리턴

  • 값 삭제 : remove() vs poll() vs clear()
    remove와 poll은 맨 앞의 값을 제거
    but 비어있는 큐에 삭제를 시도하면 remove는 NoSuchElementException 에러를 발생시키고,
    poll은 false를 리턴
    clear는 큐를 싹 비운다.

  • 맨앞 값 확인하기 : element() vs peek()
    둘 다 큐의 맨앞 값을 리턴
    but 비어있는 큐를 확인하려 하면 element는 NoSuchElementException 에러 발생,
    peek는 null을 리턴

  • ✅ 정리
    에러 발생: add, remove, element
    뭔가를 리턴: offer, poll, peek

📌 [프로그래머스] 조이스틱 - 브루트 포스

그리디라 생각하고 풀다가 도저히 안 풀리길래..
브루트포스임을 인정하고 나서야 풀린 문제.

예외 케이스가 생각보다 여러 가지였다.
최대한 빠르게 경우의 수를 파악하는 게 중요할 것 같다.

// 풀이
public class JoyStick {

    public int solution(String name) {
        int answer = 0;

        List<String> splitChar = Arrays.asList(name.split(""));
        List<Integer> notAIdx = new ArrayList<>();
        int length = splitChar.size();
        int horizontalCount, verticalCount = 0;

        String[] alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        for (int i=1; i<length; i++) {
            if (!splitChar.get(i).equals("A")) notAIdx.add(i); // A 아닌 곳의 인덱스 기록
        }

        // 경우의 수 파악
        if (!splitChar.contains("A")) horizontalCount = length-1; // A가 없으면 그냥 쭉 이동
        else if (notAIdx.isEmpty()) horizontalCount = 0; // 전부 A이면 좌우 이동 x
        else { // 그게 아니면, 중앙을 기준으로 
            List<Integer> front = new ArrayList<>();
            List<Integer> back = new ArrayList<>();
            for (int i : notAIdx) {
                if (i<(length+1)/2) front.add(i);
                else back.add(i);
            }
            System.out.println("front : " + front + ", back : " + back);
            if (front.isEmpty() && !back.isEmpty()) horizontalCount=length-Collections.min(back);
            else if (!front.isEmpty() && back.isEmpty()) horizontalCount = Collections.max(front);
            else { // 오른쪽, 오왼, 왼오 비교
                int r = Collections.max(notAIdx); //오른쪽 이동
                int l = length - Collections.min(front); //왼쪽 이동
                int rl = Collections.max(front)*2 + (length - Collections.min(back)); //오왼
                int lr = (length - Collections.min(back))*2 + Collections.max(front);
                List<Integer> compare = new ArrayList<>();
                compare.add(count1);
                compare.add(count2);
                compare.add(count3);
                compare.add(count4);
                horizontalCount = Collections.min(compare);
            }
        }

        System.out.println("horizontal : " + horizontalCount);

        // 2. 세로 이동횟수 계산 -> N을 기준으로 케이스 분류
        for (String s : splitChar) {
            if (s.equals("A")) verticalCount += 0;
            else if (s.equals("N")) verticalCount += 13;
            for (int i=1; i<13; i++) {
                if (s.equals(alphabet[i]) || s.equals(alphabet[26-i])) verticalCount += i;
            }
        }

        answer = horizontalCount + verticalCount;
        return answer;
    }
}
profile
블로그 이사했습니다 ✈ https://jennairlines.tistory.com

0개의 댓글