started car fleet problem with enumerate method
This commit is contained in:
parent
7da5e88c64
commit
ba769e5cf8
@ -47,6 +47,7 @@ class Solution(object):
|
|||||||
for i, t in enumerate(temperatures): #looping through temperatures array and their index
|
for i, t in enumerate(temperatures): #looping through temperatures array and their index
|
||||||
while stack and t > stack[-1][0]: # is our stack empty and is our temperature greater than the top of our stack
|
while stack and t > stack[-1][0]: # is our stack empty and is our temperature greater than the top of our stack
|
||||||
stackT, stackInd = stack.pop()
|
stackT, stackInd = stack.pop()
|
||||||
|
print(f'i {i}: t {t}')
|
||||||
res[stackInd] = (i - stackInd) # compute the days it took us to find a higher temperature
|
res[stackInd] = (i - stackInd) # compute the days it took us to find a higher temperature
|
||||||
stack.append((t,i)) # append to the stack the temperature and index
|
stack.append((t,i)) # append to the stack the temperature and index
|
||||||
return res
|
return res
|
||||||
|
|||||||
30
NeetCodeRoadmap/Stack/853_Car_Fleet.py
Normal file
30
NeetCodeRoadmap/Stack/853_Car_Fleet.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
class Solution(object):
|
||||||
|
def carFleet(self, target, position, speed):
|
||||||
|
"""
|
||||||
|
There are n cars going to the same destination along a one-lane road. The destination is target miles away.
|
||||||
|
|
||||||
|
You are given two integer array position and speed, both of length n, where position[i] is the position of
|
||||||
|
the ith car and speed[i] is the speed of the ith car (in miles per hour).
|
||||||
|
|
||||||
|
A car can never pass another car ahead of it, but it can catch up to it and drive bumper to bumper at the
|
||||||
|
same speed. The faster car will slow down to match the slower car's speed. The distance between these two
|
||||||
|
cars is ignored (i.e., they are assumed to have the same position).
|
||||||
|
|
||||||
|
A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a
|
||||||
|
single car is also a car fleet.
|
||||||
|
|
||||||
|
If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.
|
||||||
|
|
||||||
|
Return the number of car fleets that will arrive at the destination.
|
||||||
|
:type target: int
|
||||||
|
:type position: List[int]
|
||||||
|
:type speed: List[int]
|
||||||
|
:rtype: int
|
||||||
|
"""
|
||||||
|
fleets = []
|
||||||
|
for i,p in enumerate(position):
|
||||||
|
print(f'{i}: {p}')
|
||||||
|
|
||||||
|
|
||||||
|
obj = Solution()
|
||||||
|
print(obj.carFleet(12,[10,8,0,5,3],[2,4,1,1,3])) # 3
|
||||||
Loading…
Reference in New Issue
Block a user