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
| class Solution { public int[] avoidFlood(int[] rains) { int[] ans = new int[rains.length]; Arrays.fill(ans, 1);
TreeSet<Integer> sunDaySet = new TreeSet<>(); Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < rains.length; i++) { if (rains[i] > 0) { ans[i] = -1;
if (map.containsKey(rains[i])) { Integer sunDay = sunDaySet.higher(map.get(rains[i])); if (sunDay == null) { return new int[0]; } else { ans[sunDay] = rains[i]; sunDaySet.remove(sunDay); } } map.put(rains[i], i); } else { sunDaySet.add(i); } }
return ans; } }
|
References
1488. Avoid Flood in The City