Skip to content

Commit 603028a

Browse files
committed
problem: 1493. Longest Subarray of 1's After Deleting One Element
1 parent 383dbc3 commit 603028a

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

solutions/solution_1493/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 1493. [Longest Subarray of 1's After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/?envType=study-plan-v2&id=leetcode-75)
2+
3+
Given a binary array `nums`, you should delete one element from it.
4+
5+
Return _the size of the longest non-empty subarray containing only_ `1`_'s in the resulting array._ Return `0` if there is no such subarray.
6+
7+
### **Example 1:**
8+
9+
<pre>
10+
<strong>Input:</strong> nums = [1,1,0,1]
11+
<strong>Output:</strong> 3
12+
<strong>Explanation:</strong> After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
13+
</pre>
14+
15+
### **Example 2:**
16+
17+
<pre>
18+
<strong>Input:</strong> nums = [0,1,1,1,0,1,1,0,1]
19+
<strong>Output:</strong> 5
20+
<strong>Explanation:</strong> After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
21+
</pre>
22+
23+
### **Example 3:**
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [1,1,1]
27+
<strong>Output:</strong> 2
28+
<strong>Explanation:</strong> You must delete one element.
29+
</pre>
30+
31+
### **Constraints:**
32+
33+
- <code>1 <= nums.length <= 10<sup>5</sup></code>
34+
- `nums[i]` is either `0` or `1`.

solutions/solution_1493/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def longestSubarray(self, nums: List[int]) -> int:
6+
...
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import TypedDict
2+
3+
import pytest
4+
5+
from . import Solution
6+
7+
8+
class InputDict(TypedDict):
9+
nums: list[int]
10+
11+
12+
class CaseDict(TypedDict):
13+
input: InputDict
14+
expected: int
15+
16+
17+
test_cases: list[CaseDict] = [
18+
{
19+
"input": {
20+
'nums': [1, 1, 0, 1],
21+
},
22+
"expected": 3,
23+
},
24+
{
25+
"input": {
26+
'nums': [0, 1, 1, 1, 0, 1, 1, 0, 1],
27+
},
28+
"expected": 5,
29+
},
30+
{
31+
"input": {
32+
'nums': [1, 1, 1],
33+
},
34+
"expected": 2,
35+
},
36+
]
37+
38+
39+
@pytest.mark.parametrize(
40+
'test_case',
41+
test_cases,
42+
)
43+
def test_solution(test_case: CaseDict):
44+
result = Solution().longestSubarray(**test_case["input"])
45+
assert result == test_case["expected"]

0 commit comments

Comments
 (0)