forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2155.java
34 lines (32 loc) · 1.03 KB
/
_2155.java
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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
public class _2155 {
public static class Solution1 {
public List<Integer> maxScoreIndices(int[] nums) {
TreeMap<Integer, List<Integer>> treeMap = new TreeMap<>();
int ones = 0;
for (int num : nums) {
ones += num;
}
int zeroes = 0;
List<Integer> l = new ArrayList<>();
l.add(0);
treeMap.put(ones, l);
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
zeroes++;
} else {
ones--;
}
int score = ones + zeroes;
List<Integer> list = treeMap.getOrDefault(score, new ArrayList<>());
Integer index = i + 1;
list.add(index);
treeMap.put(score, list);
}
return treeMap.lastEntry().getValue();
}
}
}