-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_561.java
More file actions
33 lines (29 loc) · 769 Bytes
/
Copy pathP_561.java
File metadata and controls
33 lines (29 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package leetcode.easy;
import java.util.Arrays;
public class P_561 {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int res = 0;
for (int i = 0; i < nums.length; i += 2) {
res += nums[i];
}
return res;
}
public int arrayPairSumBS(int[] nums) {
final int n = 10000;
final int[] buckets = new int[2 * n + 1];
for (int num : nums) {
buckets[num + n]++;
}
int res = 0;
boolean add = true;
for (int i = 0; i < buckets.length; i++) {
for (int j = 0; j < buckets[i]; j++, add ^= true) {
if (add) {
res += i - n;
}
}
}
return res;
}
}