0%

第 36 场双周赛

5515. 设计停车系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ParkingSystem:

def __init__(self, big: int, medium: int, small: int):
self.big = big
self.medium = medium
self.small = small

def addCar(self, carType: int) -> bool:
if carType == 1:
if self.big == 0:
return False
self.big -= 1
return True
elif carType == 2:
if self.medium == 0:
return False
self.medium -= 1
return True
else:
if self.small == 0:
return False
self.small -= 1
return True

5516. 警告一小时内使用相同员工卡大于等于三次的人

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
class Solution:
def alertNames(self, keyName: List[str], keyTime: List[str]) -> List[str]:
res = set()
record = defaultdict(list)
for name, time in zip(keyName, keyTime):
if name in res:
continue
tmp = self.to_minute(time)
loc = bisect.bisect_left(record[name], tmp)
record[name].insert(loc, tmp)
if loc - 2 >= 0 and tmp - record[name][loc - 2] <= 60:
res.add(name)
continue
if loc - 1 >= 0 and loc + 1 < len(record[name]) and record[name][
loc + 1] - record[name][loc - 1] <= 60:
res.add(name)
continue
if loc + 2 < len(record[name]) and record[name][loc + 2] - tmp <= \
60:
res.add(name)
continue
res = list(res)
res.sort()
return res

def to_minute(self, time_str):
h, m = map(int, time_str.split(":"))
return h * 60 + m

5518. 给定行和列的和求可行矩阵

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
def restoreMatrix(self, rowSum: List[int],
colSum: List[int]) -> List[List[int]]:
m = len(rowSum)
n = len(colSum)
res = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
res[i][j] = min(rowSum[i], colSum[j])
rowSum[i] -= res[i][j]
colSum[j] -= res[i][j]
return res

5517. 找到处理最多请求的服务器

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
class Solution:
def busiestServers(self, ks: int, a: List[int], d: List[int]) -> List[int]:
from sortedcontainers import SortedList
ans = [0] * ks
pq = []
fu = SortedList(range(ks))
v = 0
for i, j in zip(a, d):
heappush(pq, [i, 1, i + j, v])
v += 1
# print(pq,fu)
while pq:
i, k, j, num = heappop(pq)
if k == 1 and fu:
v = fu.bisect_left(num % ks)
if v == len(fu):
v = 0
v = fu[v]
fu.remove(v)
ans[v] += 1
heappush(pq, [j, 0, v, num])
elif k == 0:
fu.add(j)
# print(pq,fu)
# print(ans)
t = max(ans)
return [i for i in range(ks) if ans[i] == t]