-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1450.py
47 lines (34 loc) · 1.28 KB
/
p1450.py
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
47
import unittest
from typing import List
# 差分
class Solution1:
@staticmethod
def busyStudent(startTime: List[int], endTime: List[int], queryTime: int) -> int:
from sortedcontainers import SortedDict
n = len(startTime)
diff = SortedDict()
for i in range(n):
diff[startTime[i]] = diff.get(startTime[i], 0) + 1
diff[endTime[i] + 1] = diff.get(endTime[i] + 1, 0) - 1
ans = 0
for k in diff:
if k <= queryTime:
ans += diff[k]
return ans
# 直接枚举也行,但是这题是个学差分的不错例子
class Solution2:
@staticmethod
def busyStudent(startTime: List[int], endTime: List[int], queryTime: int) -> int:
ans = 0
for i in range(len(startTime)):
if queryTime >= startTime[i] and queryTime <= endTime[i]:
ans += 1
return ans
class Test(unittest.TestCase):
def test(self) -> None:
self.assertEqual(Solution2.busyStudent([1, 2, 3], [3, 2, 7], 4), 1)
self.assertEqual(Solution2.busyStudent([4], [4], 4), 1)
self.assertEqual(Solution2.busyStudent([4], [4], 5), 0)
self.assertEqual(Solution2.busyStudent([1, 1, 1, 1], [1, 3, 2, 4], 7), 0)
if __name__ == "__main__":
unittest.main()