모각코 17주차 모임 결과 (22.10.27 / 목요일 / 15시 ~ 18시 / Google Meet)

KIMA·2022년 10월 31일
0
post-thumbnail

목표

우테코 프리코스 1주차 문제 풀이

결과

문제 1번

import java.util.List;

class Problem1 {
    private static final int LEFT = 0;
    private static final int RIGHT = 1;

    public static int solution(List<Integer> pobi, List<Integer> crong) {
        try {
            if (pobi.get(1) - pobi.get(0) != 1) {
                throw new IllegalArgumentException();
            }
            if (crong.get(1) - crong.get(0) != 1) {
                throw new IllegalArgumentException();
            }

            int pobiScore = Math.max(maxBetweenSumAndMultipleAllDigits(pobi.get(0), LEFT), maxBetweenSumAndMultipleAllDigits(pobi.get(1), RIGHT));
            int crongScore = Math.max(maxBetweenSumAndMultipleAllDigits(crong.get(0), LEFT), maxBetweenSumAndMultipleAllDigits(crong.get(1), RIGHT));

            return pobiScore == crongScore ? 0 : (pobiScore > crongScore ? 1 : 2);

        } catch (IllegalArgumentException e) {
            return -1;
        }
    }

    private static int maxBetweenSumAndMultipleAllDigits(int page, int direction) {
        List<Integer> forbiddenPages = List.of(1, 400);
        if (forbiddenPages.contains(page)) {
            throw new IllegalArgumentException();
        }
        if (direction == LEFT && (page % 2 != 1)) {
            throw new IllegalArgumentException();
        }
        if (direction == RIGHT && (page % 2 != 0)) {
            throw new IllegalArgumentException();
        }

        int temp = page;
        int sum = 0;
        int multiple = 1;

        for (int i = 100; i >= 1; i /= 10) {
            int divide = temp / i;
            if (divide == 0) {
                continue;
            }
            sum += divide;
            multiple *= divide;
            temp %= i;
        }

        return Math.max(sum, multiple);
    }
}

문제 2번

import java.util.Stack;

public class Problem2 {
    public static String solution(String cryptogram) {
        StringBuilder answer = new StringBuilder();
        char[] chars = cryptogram.toCharArray();
        Stack<Character> answerStack = new Stack<>();
        int countSameCharacter = 0;

        for (int i = 0; i < chars.length; i++) {
            char character = chars[i];
            if (answerStack.empty()) {
                answerStack.push(character);
            } else {
                if (answerStack.peek() != character) {
                    while (countSameCharacter > 0) {
                        answerStack.pop();
                        countSameCharacter--;
                    }
                }
                if (!answerStack.isEmpty() && answerStack.peek() == character) {
                    if (countSameCharacter == 0) {
                        countSameCharacter++;
                    }
                    countSameCharacter++;
                }
                answerStack.push(character);
            }
        }
        while (countSameCharacter > 0) {
            answerStack.pop();
            countSameCharacter--;
        }

        answerStack.forEach(answer::append);
        return answer.toString();
    }
}
profile
안녕하세요.

0개의 댓글