目录
一、跟练视频
【值域打家劫舍 树状数组【力扣周赛 402】-哔哩哔哩】 https://b23.tv/iDc49pt
二、3184. 构成整天的下标对数目 I
暴力
class Solution:
def countCompleteDayPairs(self, hours: List[int]) -> int:
# 暴力
ans = 0
for i in range(len(hours)):
for j in range(i + 1, len(hours)):
if (hours[i] + hours[j]) % 24 == 0:
ans += 1
return ans
三、3185. 构成整天的下标对数目 II
不会,来自视频。
class Solution:
def countCompleteDayPairs(self, hours: List[int]) -> int:
ans = 0
cnt = [0 for _ in range(24)]
for h in hours:
# h %= 24
h = (h % 24 + 24) % 24
# (h % 24 + 24) % 24 可以将负数正确取模
# 在python中可以不用
ans += cnt[(24 - h) % 24] # 寻找满足条件的配对数
cnt[h] += 1
return ans
未完待续