Skip to content

Commit 181849c

Browse files
committed
Add: Exercise 1004. Longest Subarray of 1's After Deleting One Element
1 parent 4594fb6 commit 181849c

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 1004. Max Consecutive Ones III
2+
Given a binary array `nums` and an integer `k`, return the maximum number of consecutive 1's in the
3+
array if you can flip at most `k` 0's.
4+
5+
## Examples
6+
7+
### Example 1:
8+
9+
Input: `nums` = [1,1,1,0,0,0,1,1,1,1,0], `k` = 2
10+
Output: 6
11+
Explanation: [1,1,1,0,0,1,1,1,1,1,1]
12+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
13+
14+
### Example 2:
15+
16+
Input: `nums` = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], `k` = 3
17+
Output: 10
18+
Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
19+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
20+
21+
## Constraints
22+
23+
- 1 <= `nums.length` <= 10^5
24+
- `nums[i]` is either 0 or 1.
25+
- 0 <= `k` <= `nums.length`
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Finds the maximum number of consecutive 1s in the array if you can flip at most k 0s.
3+
* @param {number[]} nums - Binary array (only contains 0s and 1s).
4+
* @param {number} k - The maximum number of 0s that can be flipped to 1s.
5+
* @return {number} - The maximum number of consecutive 1s after flipping at most k 0s.
6+
*/
7+
var longestOnes = function (nums, k) {
8+
let left = 0; // Left pointer for the sliding window
9+
let right = 0; // Right pointer for the sliding window
10+
let longest = 0; // Track the length of the longest subarray
11+
let zerosCount = 0; // Count the number of zeros in the current window
12+
13+
// Iterate through the array with the right pointer
14+
while (right < nums.length) {
15+
// Increment zerosCount if current element is 0
16+
if (nums[right] === 0) {
17+
zerosCount++;
18+
}
19+
20+
// When zerosCount exceeds k, update longest and shrink window from the left
21+
if (zerosCount > k) longest = Math.max(longest, right - left);
22+
while (zerosCount > k) {
23+
// If the left element is 0, decrement zerosCount
24+
if (nums[left] === 0) {
25+
zerosCount--;
26+
}
27+
// Move the left pointer to shrink the window
28+
left++;
29+
}
30+
31+
// Move the right pointer to expand the window
32+
right++;
33+
}
34+
35+
// Update longest for the case where the loop exits without updating longest
36+
longest = Math.max(longest, right - left);
37+
38+
return longest;
39+
};
40+
41+
var a = longestOnes([1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], 2) // expected 6
42+
43+
module.exports = longestOnes
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "1004-max-consecutive-ones-iii",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}

1004-max-consecutive-ones-iii/test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const assert = require('assert')
2+
const longestOnes = require('./index')
3+
4+
const tests = [
5+
{
6+
nums: [1,1,1,0,0,0,1,1,1,1,0],
7+
k: 2,
8+
expectedOutput: 6,
9+
},
10+
{
11+
nums: [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1],
12+
k: 3,
13+
expectedOutput: 10,
14+
},
15+
]
16+
17+
for (const t of tests) {
18+
assert.strict.deepEqual(longestOnes(t.nums, t.k), t.expectedOutput)
19+
}

0 commit comments

Comments
 (0)