-
Notifications
You must be signed in to change notification settings - Fork 1
/
1094-Car-Pooling.py
43 lines (41 loc) · 1.15 KB
/
1094-Car-Pooling.py
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
36
37
38
39
40
41
42
43
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
acc=0
pool=[]
for num,start,end in trips:
pool.append((start,1,num))
pool.append((end,0,num))
pool.sort()
for loc,state,num in pool:
if state==0:
acc-=num
else:
acc+=num
if acc>capacity:
return False
return True
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
acc=0
pool=[]
for num,start,end in trips:
pool.append((start,num))
pool.append((end,-num))
pool.sort()
for loc,num in pool:
acc+=num
if acc>capacity:
return False
return True
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
loc=[0]*1001
for num,start,end in trips:
loc[start]+=num
loc[end]-=num
acc=capacity
for item in loc:
acc-=item
if acc<0:
return False
return True