Skip to content

Commit 4e55065

Browse files
committed
chore: add daily leetcode post 1004_translated
1 parent 7c5d609 commit 4e55065

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: 1004.Maximum continuity1Number III Maximum continuity1Number III
3+
date: '2022.12.07-01:15'
4+
tags:
5+
- - Python
6+
- - solved
7+
- answer
8+
abbrlink: ed19b576
9+
---
10+
11+
12+
13+
14+
15+
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。
16+
```python
17+
18+
class Solution:
19+
def longestOnes(self, nums: List[int], k: int) -> int:
20+
k_mean = k
21+
# Used to record how many arrays there are now
22+
flag = 0
23+
# Used to record the maximum land number
24+
max_flag = 0
25+
for start in range(len(nums)):
26+
tail = start
27+
while k >= 0 and tail <= len(nums) - 1:
28+
if nums[tail] == 1:
29+
tail += 1
30+
flag += 1
31+
elif nums[tail] == 0 and k > 0:
32+
tail += 1
33+
k -= 1
34+
flag += 1
35+
elif nums[tail] == 0 and k == 0:
36+
k = k_mean
37+
max_flag = max(max_flag, flag)
38+
flag = 0
39+
break
40+
if tail == len(nums):
41+
max_flag = max(max_flag, flag)
42+
flag = 0
43+
break
44+
return max_flag
45+
```
46+
47+
This is my approach at the beginning,Although the double pointer is used,But there is no flexibility,Very empty feeling,Very dry slide。
48+
the following[@Lincoln](/u/lincoln)@Lincoln Big practice,Just as one record of yourself,不作为我的answer发表
49+
```
50+
class Solution:
51+
def longestOnes(self, nums: List[int], k: int) -> int:
52+
"""
53+
Thinking:1. k=0It can be understood as the problem of the maximum duplication sub -string
54+
2. If the current window value-In the window1Number <= k: Then expand the window(right+1)
55+
If the current window value-In the window1Number > k: Swipe the window to the right(left+1)
56+
method:Hash table + Sliding window
57+
"""
58+
n = len(nums)
59+
o_res = 0
60+
left = right = 0
61+
while right < n:
62+
if nums[right]== 1: o_res += 1
63+
if right-left+1- o_res > k:
64+
if nums[left]== 1: o_res -= 1
65+
left += 1
66+
right += 1
67+
return right - left
68+
```
69+
Look atLincolnBigThinking,very clearly,remember

0 commit comments

Comments
 (0)