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
| class Solution { public int minimumIndex(List<Integer> nums) { n = nums.size(); this.nums = nums.stream().mapToInt(i->i).toArray(); Map<Integer, Integer> count = new HashMap<>(); TreeMap<Integer, Integer> treeMap = new TreeMap<>(); for (int i = 0; i < n; ++i) { count.put(this.nums[i], count.getOrDefault(this.nums[i], 0) + 1); treeMap.put(count.get(this.nums[i]), this.nums[i]); } primary = treeMap.lastEntry().getValue(); int[] cnt = new int[n]; cnt[0] = this.nums[0] == primary ? 1 : 0; for (int i = 1; i < n; ++i) { cnt[i] = cnt[i - 1]; if (this.nums[i] == primary) cnt[i]++; } for (int i = 0; i < n - 1; ++i) { if (cnt[i] * 2 > i + 1 && (cnt[n - 1] - cnt[i]) * 2 > n - i - 1) { return i; } } return -1; }
int n, MAX = Integer.MAX_VALUE, primary; int[] nums; }
|