classSolution: defminSubArrayLen(self, target: int, nums: List[int]) -> int: # 另一种写法 n = len(nums) ans = n + 1 left = 0 cur_sum = 0 for right inrange(n): cur_sum += nums[right]
while cur_sum-nums[left] >= target: cur_sum -= nums[left] left += 1
if cur_sum >= target and ans > right-left+1: # print(cur_sum, left, right) ans = right-left+1 return ans if ans != n+1else0
# 时间复杂度 On,空间复杂度O1 n = len(nums) ans = n + 1 left = 0 cur_sum = 0 for right inrange(n): cur_sum += nums[right] while cur_sum >= target: ans = min(ans, right-left+1) cur_sum -= nums[left] left += 1 return ans if ans != n+1else0
classSolution: deflengthOfLongestSubstring(self, s: str) -> int: # 时间复杂度On,空间复杂度由字符串的不重复字符数确定 rec = set() ans = 0 left = 0 for right, x inenumerate(s): if x notin rec: rec.add(x) ans = max(ans, right-left+1) else: while s[left] != x: rec.discard(s[left]) left += 1 left += 1 return ans
classSolution: defmaximumLengthSubstring(self, s: str) -> int: # 时间复杂度On,空间复杂度由字符串的不重复字符数确定 rec = Counter() left = 0 ans = 0 for right, x inenumerate(s): rec[x] += 1 while rec[x] > 2: rec[s[left]] -= 1 left += 1 ans = max(ans, right-left+1) return ans
classSolution: deflongestSemiRepetitiveSubstring(self, s: str) -> int: # 时间复杂度On,空间复杂度O1 # 虽说确实可以用哈希表来辅助,但是题目给了限定对数,那么为啥不直接维护这个对数呢 ans = 0 left = 0 pair_count = 0 for right, x inenumerate(s): if right > 0and x == s[right-1]: pair_count += 1 while pair_count > 1: if s[left]==s[left+1]: pair_count -= 1 left += 1 ans = max(ans, right-left+1)
classSolution: deflongestOnes(self, nums: List[int], k: int) -> int: # 能翻转k个0为1,换个角度就是求最长的连续1序列长度,其中最多可以包含两个0 # 时间On,空间复杂度O1 ans = 0 left = 0 zero_cnt = 0 for right, x inenumerate(nums): if x == 0: zero_cnt += 1 while zero_cnt > k: if nums[left] == 0: zero_cnt -= 1 left += 1 ans = max(ans, right-left+1) return ans
classSolution: defcountSubarrays(self, nums: List[int], k: int) -> int: # 这题先是陷入到了之前的固定思维,想着怎么把这题转化为窗口内至多的题目,失败了 # 看了灵神的思路解析才知道,完全可以窗口滑动到不满足后,left本身左边都符合 # 时间 On,空间O1 ans = 0 max_num = max(nums) left = 0 s = 0 for x in nums: if x == max_num: s += 1
while s >= k: if nums[left] == max_num: s -= 1 left += 1 ans += left return ans
classSolution: defcountSubarrays(self, nums: List[int], k: int) -> int: # 时间On,空间复杂度O1 ans = 0 left = 0 s = 0 for right, x inenumerate(nums): s += x while s * (right-left+1) >= k: s -= nums[left] left += 1 ans += right - left + 1 return ans
s = 0 max_len = -1 left = 0 for right, x inenumerate(nums): s += x while s > target: s -= nums[left] left += 1 if s == target: max_len = max(max_len, right - left + 1) returnlen(nums)-max_len if max_len >= 0else -1
classSolution: defminLength(self, nums: List[int], k: int) -> int: # 时间复杂度O(n),空间复杂度根据不重复的数字相关 n = len(nums) ans = n + 1 rec = defaultdict(int) left = 0 s = 0 for right, x inenumerate (nums): rec[x] += 1 if rec[x] == 1: s += x while s>= k: ans = min(ans, right-left+1) rec[nums[left]] -= 1 if rec[nums[left]] == 0: s -= nums[left] left += 1
for c in t: rec[c] += 1 left = 0 for right, x inenumerate(s): if x in rec: rec[x] -= 1 if rec[x] == 0: enough_cnt += 1 while enough_cnt == len(rec): iflen(ans) > right-left+1: ans = s[left:right+1] out = s[left] if out in rec: rec[out] += 1 if rec[out] == 1: enough_cnt -= 1 left += 1 return ans iflen(ans) <= 1e5else""