Input - ("mon 10:00 am", mon 11:00 am) Output - [11005, 11010, 11015...11100] Output starts with 1 if the day is monday, 2 if tuesday and so on till 7 for sunday Append 5 min interval times to that till the end time So here it is 10:05 as first case, so its written as 11005 2nd is 10:10 so its written as 11010
题目大意:
DD的面经题,给定开始时间和结束时间,求5分钟的间隔时间,注意要round to 5min
解题思路:
由于非10进制,所以开一个类来计算进制
解题步骤:
N/A
注意事项:
- 实现lt函数
- 12am, 12pm要mod 12
- (0 if parts[2] == 'am' else 12)加括号
- 开始时间到到5分钟端点,结束时间加1分钟,由于只实现了lt
Python代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47DAY_DICT = {'mon': 1, 'tue': 2, 'wed': 3, 'thu': 4, 'fri': 5, 'sat': 6, 'sun': 7}
class Solution(TestCases):
def get_intervals(self, start, end) -> List:
start_time = Time(start)
end_time = Time(end)
if start_time.min % 5 > 0:
start_time.add(5 - start_time.min % 5)
end_time.add(1)
res = []
while start_time < end_time:
res.append(start_time.get_numeric())
start_time.add(5)
return res
class Time:
def __init__(self, time):
parts = time.split(' ')
day = DAY_DICT[parts[0]]
time_parts = parts[1].split(':')
hour = int(time_parts[0]) % 12 + (0 if parts[2] == 'am' else 12) # remember paren (0 ...12), and % 12
self.day = day
self.hour = hour
self.min = int(time_parts[1])
def __lt__(self, other):
if self.day < other.day or (self.day == other.day and self.hour < other.hour) or \
(self.day == other.day and self.hour == other.hour and self.min < other.min):
return True
else:
return False
def get_numeric(self):
return self.day * 10000 + self.hour * 100 + self.min
def add(self, mins):
self.min += mins
if self.min == 60:
self.min = 0
self.hour += 1
if self.hour == 24:
self.hour = 0
self.day += 1
if self.day == 7:
self.day = 0
算法分析:
时间复杂度为O(n),空间复杂度O(1)


