[Leetcode]853. Car Fleet(x)
2024. 8. 2. 09:39ㆍAlgorithm
1. problem :
https://leetcode.com/problems/car-fleet/
2. solution 1 :
class Solution:
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
cars = sorted(zip(position,speed),reverse=True)
times = [(target - p)/s for p,s in cars]
current_time = 0
fleet = 0
for time in times:
if time > current_time:
fleet +=1
current_time = time
return fleet
나는 너무 복잡하게 생각했다. 하지만, chat gpt 4o는 너무 간단명료하게 답을 냈다. 외우자.
position을 기준으로 reverse sorted 해서 , 도식화한다. 그런 다음, current_time 보다 time이 크다? --> 그럼 겹칠일이 없다. 외우자.
'Algorithm' 카테고리의 다른 글
[Leetcode]704. Binary Search (0) | 2024.08.02 |
---|---|
[Leetcode]84. Largest Rectangle in Histogram(x) (0) | 2024.08.02 |
[Leetcode]739. Daily Temperatures(x) (0) | 2024.08.02 |
[Leetcode]22. Generate Parentheses(x) (0) | 2024.08.02 |
[Leetcode]150. Evaluate Reverse Polish Notation (0) | 2024.08.01 |