Re: [閒聊] 每日leetcode

作者: dont   2024-09-16 12:28:56
539. Minimum Time Difference
## 思路
把HH:MM轉成minute存到buckets (60*24)
如果之前已經存在過就直接回傳0
掃bucket檢查前後的差值, 最後再檢查頭尾
## Code
```python
class Solution:
def findMinDifference(self, timePoints: List[str]) -> int:
buckets = [False] * 1440
for time in timePoints:
hour, minute = map(int, time.split(':'))
val = 60 * hour + minute
if buckets[val]:
return 0
buckets[val] = True
res = 1440
first = prev = None
for i in range(1440):
if not buckets[i]:
continue
if first is None:
first = prev = i
continue
res = min(res, i-prev)
prev = i
res = min(res, 1440 + first - prev)
return res
```

Links booklink

Contact Us: admin [ a t ] ucptt.com