내 답안
class Solution {
public int solution(int[] arr1, int[] arr2) {
int sum1 = 0;
int sum2 = 0;
for (int num : arr1) {
sum1 += num;
}
for (int num : arr2) {
sum2 += num;
}
if (arr1.length > arr2.length || (arr1.length == arr2.length && sum1 > sum2)) {
return 1;
} else if (arr1.length < arr2.length || (arr1.length == arr2.length && sum1 < sum2)) {
return -1;
} else {
return 0;
}
}
}
Java
복사
다른사람 풀이
import java.util.stream.IntStream;
class Solution {
public int solution(int[] arr1, int[] arr2) {
int answer = Integer.compare(arr1.length, arr2.length);
if(answer == 0) {
answer = Integer.compare(IntStream.of(arr1).sum(), IntStream.of(arr2).sum());
}
return answer;
}
}
Java
복사
class Solution {
public int solution(int[] arr1, int[] arr2) {
if (arr1.length < arr2.length) return -1;
else if (arr1.length > arr2.length) return 1;
int arr1Sum = 0, arr2Sum = 0;
for (int i = 0;i < arr1.length;i++) {
arr1Sum += arr1[i];
arr2Sum += arr2[i];
}
return arr1Sum == arr2Sum ? 0 : (arr1Sum > arr2Sum ? 1 : -1);
}
}
Java
복사