Skip to content

Commit 7670664

Browse files
authored
Merge pull request #1 from iamAntimPal/Leetcode-75
Leetcode 75
2 parents 405e4dd + 19079a2 commit 7670664

File tree

49 files changed

+4067
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+4067
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def longestOnes(self, nums: List[int], k: int) -> int:
3+
l = cnt = 0
4+
for x in nums:
5+
cnt += x ^ 1
6+
if cnt > k:
7+
cnt -= nums[l] ^ 1
8+
l += 1
9+
return len(nums) - l
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# 1004. Max Consecutive Ones III
2+
3+
**Difficulty:** Medium
4+
**Tags:** Array, Binary Search, Prefix Sum, Sliding Window
5+
**Source:** [Leetcode](https://leetcode.com/problems/max-consecutive-ones-iii) | Weekly Contest 126 Q3
6+
7+
---
8+
9+
## Description
10+
11+
Given a binary array `nums` and an integer `k`, return *the maximum number of consecutive* `1`'s in the array if you can flip at most `k` `0`'s.
12+
13+
---
14+
15+
## Examples
16+
17+
**Example 1:**
18+
19+
```
20+
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
21+
Output: 6
22+
Explanation: [1,1,1,0,0,1,1,1,1,1]
23+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
30+
Output: 10
31+
Explanation: [1,1,1,1,1,1,1,1,1,1]
32+
```
33+
34+
---
35+
36+
## Constraints
37+
38+
- `1 <= nums.length <= 10^5`
39+
- `nums[i]` is either `0` or `1`.
40+
- `0 <= k <= nums.length`
41+
42+
---
43+
44+
## Approach
45+
46+
### Sliding Window
47+
48+
We use a sliding window to maintain a subarray that contains at most `k` zeros. When the count of zeros exceeds `k`, we shrink the window from the left. The window size gives the count of maximum 1s after flipping at most `k` zeros.
49+
50+
- Time Complexity: O(n)
51+
- Space Complexity: O(1)
52+
53+
---
54+
55+
## Solutions
56+
57+
### Python
58+
59+
```python
60+
class Solution:
61+
def longestOnes(self, nums: List[int], k: int) -> int:
62+
l = cnt = 0
63+
for r in range(len(nums)):
64+
cnt += nums[r] == 0
65+
if cnt > k:
66+
cnt -= nums[l] == 0
67+
l += 1
68+
return len(nums) - l
69+
```
70+
71+
### Java
72+
73+
```java
74+
class Solution {
75+
public int longestOnes(int[] nums, int k) {
76+
int l = 0, cnt = 0;
77+
for (int r = 0; r < nums.length; r++) {
78+
if (nums[r] == 0) cnt++;
79+
if (cnt > k) {
80+
if (nums[l++] == 0) cnt--;
81+
}
82+
}
83+
return nums.length - l;
84+
}
85+
}
86+
```
87+
88+
### C++
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int longestOnes(vector<int>& nums, int k) {
94+
int l = 0, cnt = 0;
95+
for (int r = 0; r < nums.size(); r++) {
96+
if (nums[r] == 0) cnt++;
97+
if (cnt > k) {
98+
if (nums[l++] == 0) cnt--;
99+
}
100+
}
101+
return nums.size() - l;
102+
}
103+
};
104+
```
105+
106+
### Go
107+
108+
```go
109+
func longestOnes(nums []int, k int) int {
110+
l, cnt := 0, 0
111+
for r := 0; r < len(nums); r++ {
112+
if nums[r] == 0 {
113+
cnt++
114+
}
115+
if cnt > k {
116+
if nums[l] == 0 {
117+
cnt--
118+
}
119+
l++
120+
}
121+
}
122+
return len(nums) - l
123+
}
124+
```
125+
126+
### TypeScript
127+
128+
```ts
129+
function longestOnes(nums: number[], k: number): number {
130+
let l = 0, cnt = 0;
131+
for (let r = 0; r < nums.length; r++) {
132+
if (nums[r] === 0) cnt++;
133+
if (cnt > k) {
134+
if (nums[l] === 0) cnt--;
135+
l++;
136+
}
137+
}
138+
return nums.length - l;
139+
}
140+
```
141+
142+
---
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
l, r = 0, len(height) - 1
4+
ans = 0
5+
while l < r:
6+
t = min(height[l], height[r]) * (r - l)
7+
ans = max(ans, t)
8+
if height[l] < height[r]:
9+
l += 1
10+
else:
11+
r -= 1
12+
return ans

0 commit comments

Comments
 (0)