[LeetCode] 1094. Car Pooling

There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).

You are given the integer capacity and an array trips where trips[i] = [numPassengersi, fromi, toi] indicates that the ith trip has numPassengersi passengers and the locations to pick them up and drop them off are fromi and toi respectively. The locations are given as the number of kilometers due east from the car's initial location.

Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.

Example 1:

Input: trips = [[2,1,5],[3,3,7]], capacity = 4
Output: false

Example 2:

Input: trips = [[2,1,5],[3,3,7]], capacity = 5
Output: true

Constraints:

  • 1 <= trips.length <= 1000
  • trips[i].length == 3
  • 1 <= numPassengersi <= 100
  • 0 <= fromi < toi <= 1000
  • 1 <= capacity <= 105

拼车。

车上最初有 capacity 个空座位。车 只能 向一个方向行驶(也就是说,不允许掉头或改变方向)

给定整数 capacity 和一个数组 trips ,  trip[i] = [numPassengersi, fromi, toi] 表示第 i 次旅行有 numPassengersi 乘客,接他们和放他们的位置分别是 fromi 和 toi 。这些位置是从汽车的初始位置向东的公里数。

当且仅当你可以在所有给定的行程中接送所有乘客时,返回 true,否则请返回 false。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/car-pooling
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题的糖衣出的很巧妙,但是看过之后居然发现核心思路其实是扫描线。具体的思路是,因为题目条件限制了 trips 的长度不大于 1000,所以创建一个长度为 1001 的数组,然后扫描这个 trips 数组。对于每一个 trip,从他的起点 start_location 到他的终点 end_location,把当前这个 trip 涉及到的乘客数量加到数组里。在遍历的过程中,如果有任何一个 location 上的乘客人数大于 capacity,则返回 false。否则遍历完 trips 数组就返回true。

时间O(n) - 可能有 N 个 trip,但是每个 trip 的 start 和 end 最多只到1000,所以O(1000n) = O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public boolean carPooling(int[][] trips, int capacity) {
 3         int[] res = new int[1001];
 4         for (int i = 0; i < trips.length; i++) {
 5             int passengers = trips[i][0];
 6             int start = trips[i][1];
 7             int end = trips[i][2];
 8             for (int j = start; j < end; j++) {
 9                 res[j] += passengers;
10                 if (res[j] > capacity) {
11                     return false;
12                 }
13             }
14         }
15         return true;
16     }
17 }

 

另一种方法跟1109题类似,属于前缀和的思想。是用一个数组 counter 记录每个位置相比于前一个位置上的差值,最后再把 counter 数组累加一遍。如果累加的过程中,有任何一个位置上的乘客数量大于 capacity,则返回 false。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public boolean carPooling(int[][] trips, int capacity) {
 3         int[] counter = new int[1001];
 4         for (int i = 0; i < trips.length; i++) {
 5             counter[trips[i][1]] += trips[i][0];
 6             counter[trips[i][2]] -= trips[i][0];
 7         }
 8         int cap = 0;
 9         for (int i = 0; i < 1001; i++) {
10             cap += counter[i];
11             if (cap > capacity) {
12                 return false;
13             }
14         }
15         return true;
16     }
17 }

 

差分数组思想实现,建议记这种思路。

 1 class Solution {
 2     public boolean carPooling(int[][] trips, int capacity) {
 3         int[] seats = new int[1000];
 4         for (int[] trip : trips) {
 5             int num = trip[0];
 6             int from = trip[1];
 7             // 说放下乘客的位置是 to,意思其实是这一批乘客还在车上的范围是[from, to - 1]
 8             int to = trip[2] - 1;
 9             seats[from] += num;
10             // 如果to的下一个位置有效,则减去这一批乘客
11             if (to + 1 < 1000) {
12                 seats[to + 1] -= num;
13             }
14         }
15 
16         for (int i = 1; i < seats.length; i++) {
17             seats[i] += seats[i - 1];
18         }
19         for (int s : seats) {
20             if (s > capacity) {
21                 return false;
22             }
23         }
24         return true;
25     }
26 }

 

相关题目

1094. Car Pooling

1109. Corporate Flight Bookings

扫描线相关题目

LeetCode 题目总结

posted @ 2020-09-08 00:10  CNoodle  阅读(527)  评论(0编辑  收藏  举报