1004.Maximum continuity1Number III Maximum continuity1Number III
Today's daily question is too difficult,So find the problem by yourself。Today's question is a hash table+Sliding window algorithm,虽然感觉只用了Sliding window algorithm。
class Solution:
def longestOnes(self, nums: List[int], k: int) -> int:
k_mean = k
# Used to record how many arrays there are now
flag = 0
# Used to record the maximum land number
max_flag = 0
for start in range(len(nums)):
tail = start
while k >= 0 and tail <= len(nums) - 1:
if nums[tail] == 1:
tail += 1
flag += 1
elif nums[tail] == 0 and k > 0:
tail += 1
k -= 1
flag += 1
elif nums[tail] == 0 and k == 0:
k = k_mean
max_flag = max(max_flag, flag)
flag = 0
break
if tail == len(nums):
max_flag = max(max_flag, flag)
flag = 0
break
return max_flagThis is my approach at the beginning,Although the double pointer is used,But there is no flexibility,Very empty feeling,Very dry slide。 the following@Lincoln@Lincoln Big practice,Just as one record of yourself,不作为我的answer发表
class Solution:
def longestOnes(self, nums: List[int], k: int) -> int:
"""
Thinking:1. k=0It can be understood as the problem of the maximum duplication sub -string
2. If the current window value-In the window1Number <= k: Then expand the window(right+1)
If the current window value-In the window1Number > k: Swipe the window to the right(left+1)
method:Hash table + Sliding window
"""
n = len(nums)
o_res = 0
left = right = 0
while right < n:
if nums[right]== 1: o_res += 1
if right-left+1- o_res > k:
if nums[left]== 1: o_res -= 1
left += 1
right += 1
return right - leftLook atLincolnBigThinking,very clearly,remember
贡献者
最近更新
Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0
2582.Pillow
LeetCode 2582. 递枕头 题解 — 数学规律题,核心技巧是利用 n 个人传枕头时每轮间隔为 n-1,通过 time // (n-1) 计算完整轮次,再根据轮次奇偶性判断正向或反向传递位置。适合正在刷 LeetCode 数学模拟类题目的求职者与算法学习者。
1664. Number of schemes to generate balance numbers One question daily
LeetCode 1664. 生成平衡数组的方案数 题解 — 使用动态规划与奇偶性分析,核心技巧是预处理前缀奇偶和与后缀奇偶和,在枚举删除每个元素时 O(1) 判断剩余数组奇偶下标和是否相等。适合准备算法面试、刷 LeetCode 中等题的求职者和 CS 学生。