Leetcode Weekly Contest 287
我的第一場 Leetcode Contest,這天半路想到才跑回來寫,好玩的是,我先寫了 Medium 才回去寫 Easy,然後 Easy 還卡超久,結果是因為題目沒看清楚 lol。
我不會寫程式,所以這系列就純粹記錄一下我的解題歷程。
2224. Minimum Number of Operations to Convert Time
Difficulty: Easy
You are given two strings current
and correct
representing two 24-hour times.
24-hour times are formatted as "HH:MM"
, where HH
is between 00
and 23
, and MM
is between 00
and 59
. The earliest 24-hour time is 00:00
, and the latest is 23:59
.
In one operation you can increase the time current
by 1
, 5
, 15
, or 60
minutes. You can perform this operation any number of times.
Return the minimum number of operations needed to convert current
to correct
.
1 | Input: current = "02:30", correct = "04:35" |
1 | Input: current = "11:00", correct = "11:01" |
current
andcorrect
are in the format"HH:MM"
current <= correct
Solution
- 既然是要步驟,先初始一個 counter
operations
- 把字串拆成數字的 list 方便操作
- 把時間全部換算為
分
- 相減算出差異時間
diff_time
後,除以60
、15
、5
、1
,並加至operations
;
同時將diff_time
指定為除法後的餘數。
1 | class Solution: |
2225. Find Players With Zero or One Losses
Difficulty: Medium
You are given an integer array matches
where matches[i] = [winner_i, loser_i]
indicates that the player winner_i
defeated player loser_i
in a match.
Return a list answer
of size 2
where:
answer[0]
is a list of all players that have not lost any matches.answer[1]
is a list of all players that have lost exactly one match.
The values in the two lists should be returned in increasing order.
Note:
- You should only consider the players that have played at least one match.
- The testcases will be generated such that no two matches will have the same outcome.
1 | Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]] |
1 | Input: matches = [[2,3],[1,3],[5,4],[6,4]] |
1 <= matches.length <= 105
matches[i].length == 2
1 <= winneri, loseri <= 105
winneri != loseri
- All
matches[i]
are unique.
Solution
- 把每一組跑過一次,並將 winner 和 loser 的編號與出現次數存在各自的 dict 中
- 將
winner_dict
跑過一次,只要 key 沒在loser_dict
中出現,代表沒輸過,並存到no_lost
- 將
loser_dict
跑過一次,只要 value == 1,表示只輸過一次,並存到lost_one
- 最後將兩個 list 排序後存到
result
中回傳
1 | class Solution: |