class Solution {
public int solution(int[] arr) {
int answer = 0;
return answer;
}
}
해결법
방법 1
class Solution {
public int solution(int[] arr) {
int answer = 0;
int[] next = arr.clone();
int[] prev = arr.clone();
boolean isDiff = false;
while (true) {
prev = next.clone();
isDiff = false;
next = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
if (prev[i] == 0) {
continue;
}
if (prev[i] >= 50 && prev[i] % 2 == 0) {
next[i] = prev[i] / 2;
} else if (prev[i] < 50 && prev[i] % 2 == 1) {
next[i] = prev[i] * 2 + 1;
} else {
next[i] = prev[i];
}
if (prev[i] != next[i]) {
isDiff = true;
}
}
if (isDiff) {
answer++;
} else {
return answer;
}
}
}
}
방법 2
class Solution {
public static int solution(int[] arr) {
int answer = 0;
int cnt = 0;
while (cnt != arr.length) {
int[] tmp = arr.clone();
cnt = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 50 && arr[i] % 2 == 0) {
arr[i] /= 2;
} else if (arr[i] < 50 && arr[i] % 2 != 0) {
arr[i] = (2 * arr[i]) + 1;
}
if (tmp[i] == arr[i]) {
cnt++;
}
}
if (cnt == arr.length) {
return answer;
} else {
answer++;
tmp = arr;
}
}
return answer;
}
}
조건에 맞게 수열 변환하기 2 Lv. 0