使用差分的思想进行解决
class Solution: def countTestedDevices(self, batteryPercentages: List[int]) -> int: diff = 0 for x in batteryPercentages: if x > diff: diff += 1 return diff
class Solution: def getGoodIndices(self, variables: List[List[int]], target: int) -> List[int]: return [i for i, (a, b, c, m) in enumerate(variables) if pow(pow(a, b, 10), c, m) == target]
这两题比较简单,我也是赛后看了灵神的代码,实在是太优雅了,贴在这里以后看一看。
第三题仍然是滑动窗口,也是在 这次周赛和双周赛后从灵神题解中学到的写法,极其优雅。
class Solution: def countSubarrays(self, nums: List[int], k: int) -> int: n = len(nums) mx = max(nums) cnt = 0 res = left = 0 for v in nums: if v == mx: cnt += 1 while cnt >= k: if nums[left] == mx: cnt -= 1 left += 1 res += left return res
class Solution: def numberOfGoodPartitions(self, nums: List[int]) -> int: r = {} for i, x in enumerate(nums): r[x] = i m = max_r = 0 for i, x in enumerate(nums): max_r = max(max_r, r[x]) if max_r == i: m += 1 return pow(2, m - 1, 10**9 + 7)
标签:周赛,cnt,nums,int,max,List,diff,滑动,375 From: https://www.cnblogs.com/zk6696/p/17893422.html