1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public int[] fullBloomFlowers(int[][] flowers, int[] people) { TreeMap<Integer, Integer> d = new TreeMap<>(); for (int[] flower : flowers) { d.merge(flower[0], 1, Integer::sum); d.merge(flower[1] + 1, -1, Integer::sum); } int sum = 0, m = people.length; Integer[] idx = new Integer[m]; for (int i = 0; i < m; ++i) { idx[i] = i; } Arrays.sort(idx, (a, b) -> people[a] - people[b]); int[] ans = new int[m]; for (int i : idx) { int p = people[i]; while (!d.isEmpty() && d.firstKey() <= p) { sum += d.pollFirstEntry().getValue(); } ans[i] = sum; } return ans; } }
|