우테코 프리코스 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);
}
}
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();
}
}