1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public boolean carPooling(int[][] trips, int capacity) { Map<Integer, Integer> map = new TreeMap<>(); for (int[] trip : trips) { map.put(trip[1], map.getOrDefault(trip[1], 0) + trip[0]); map.put(trip[2], map.getOrDefault(trip[2], 0) - trip[0]); } int count = 0; for (int v : map.values()) { count += v; if (count > capacity) return false; } return true; } }
|
和meeting roomII是一样的.
时间复杂度: O(nlogn)
空间复杂度: O(n)