-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1403.java
More file actions
39 lines (35 loc) · 1.18 KB
/
LeetCode1403.java
File metadata and controls
39 lines (35 loc) · 1.18 KB
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
34
35
36
37
38
39
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class LeetCode1403 {
public static void main(String[] args) {
// 输入:nums = [4,3,10,9,8]
// 输出:[10,9]
System.out.println(Arrays.toString(new Solution1403().minSubsequence(new int[] { 4, 3, 10, 9, 8 }).toArray()));
// 输入:nums = [4,4,7,6,7]
// 输出:[7,7,6]
System.out.println(Arrays.toString(new Solution1403().minSubsequence(new int[] { 4, 4, 7, 6, 7 }).toArray()));
}
}
class Solution1403 {
public List<Integer> minSubsequence(int[] nums) {
Arrays.sort(nums);
int sumLeft = 0;
int sumRight = 0;
for (int i = 0; i < nums.length; i++) {
sumLeft += nums[i];
}
for (int i = nums.length - 1; i >= 0; i--) {
sumRight += nums[i];
sumLeft -= nums[i];
if (sumLeft < sumRight) {
List<Integer> result = new ArrayList<Integer>();
for (int ind = nums.length - 1; ind >= i; ind--) {
result.add(nums[ind]);
}
return result;
}
}
return null;
}
}