Skip to content

Commit 64e236d

Browse files
authored
Create car-fleet-ii.py
1 parent 819fc9f commit 64e236d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Python/car-fleet-ii.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def getCollisionTimes(self, cars):
6+
"""
7+
:type cars: List[List[int]]
8+
:rtype: List[float]
9+
"""
10+
stk = []
11+
result = [-1.0]*len(cars)
12+
for i in reversed(xrange(len(cars))):
13+
p, s = cars[i]
14+
while stk and (cars[stk[-1]][1] >= s or
15+
0 < result[stk[-1]] <= float(cars[stk[-1]][0]-p)/(s-cars[stk[-1]][1])):
16+
stk.pop()
17+
if stk:
18+
result[i] = float(cars[stk[-1]][0]-p)/(s-cars[stk[-1]][1])
19+
stk.append(i)
20+
return result

0 commit comments

Comments
 (0)