문제를 풀다가 같은 로직이지만 결과가 다르게 나오는 녀석이 발견됐다.
List로 풀면 90점이 나오고 Array로 풀면 100점이 나오는 기이한 현상이었지비.
알고보니 다른 것들은 괜찮은데 list에서 get한 녀석끼리 비교할 때 문제가 발생한 것이었다.
기존에는 list.get(i) == list.get(i+1)
로 작성을 했으나 알고보니 list에서 get하면 주소값을 가져오기 때문에 다른 값을 반환하는 문제였다.
이후에 list.get(i).equals(list.get(i+1))
로 바꿔주고나니 나의 코드도 이젠 100점~!
고치고난 뒤의 코드들은 아래에 기재한다요.
아래는 두 가지의 코드
for (int i = 0; i <= N-1; i++) {
int surplus = cnt;
if (arr[i] != 0) {
int length = 1;
for (int j = i; j < N-1; j++) {
if (arr[j] + 1 == arr[j+1]) {
length++;
} else if (arr[j] == arr[j+1]) {
continue;
} else if (surplus >= arr[j+1] - arr[j] - 1) {
surplus -= arr[j+1] - arr[j] - 1;
length += arr[j+1] - arr[j];
} else {
break;
}
}
length += surplus;
if (length > max) max = length;
}
}
while (cnt != N && idx <= list.size()-1) {
int surplus = cnt;
int length = 1;
for (int i = idx; i < list.size()-1; i++) {
if (list.get(i) + 1 == list.get(i+1)) {
length++;
} else if (list.get(i).equals(list.get(i+1))) {
continue;
} else if (surplus >= list.get(i+1) - list.get(i) - 1) {
surplus -= list.get(i+1) - list.get(i) - 1;
length += list.get(i+1) - list.get(i);
} else {
break;
}
}
length += surplus;
if (length > max) max = length;
idx++;
}
호진이랑 고민하던게 이거였구나 정올 조커치니까 글 나온다!!