[1493. Longest Subarray of 1's After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/)
### 題目描述
Given a binary array `nums`, you should delete one element from it.
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.
### 範例
**Example 1:**
```
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
```
**Example 2:**
```
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: 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].
```
**Example 3:**
```
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
```
**Constraints**:
* 1 <= `nums.length` <= 10^5^
* `nums[i]` is either `0` or `1`.
### 解答
#### TypeScript
```typescript=
function longestSubarray(nums: number[]): number {
let maxStreak = 0;
let currentStreak = 0;
let previousStreak = 0;
nums.forEach((num) => {
if (num === 1) {
currentStreak++;
} else {
maxStreak = Math.max(maxStreak, previousStreak + currentStreak);
previousStreak = currentStreak;
currentStreak = 0;
}
});
maxStreak = Math.max(maxStreak, previousStreak + currentStreak);
return maxStreak === nums.length ? maxStreak - 1 : maxStreak;
}
```
> [name=Sheep][time=Wed, July 5, 2023]
#### Javascript
```javascript=
function longestSubarray(nums) {
let zeroCount = 0;
let left = 0;
let max = 0;
for (let i = 0; i < nums.length; i++) {
if (!nums[i]) zeroCount++;
while (zeroCount > 1) {
if (!nums[left]) zeroCount--;
left++;
}
max = Math.max(max, i - left);
}
return max;
}
```
> sliding window基礎題,之前也做過一樣的,但我居然還是錯了好幾次才過...
> [name=Marsgoat][time=Wed, Jul 5, 2023]
#### Java
```java=
class Solution {
public int longestSubarray(int[] nums) {
int windowStart = 0;
int longestWindow = 0;
int zeroCount = 0;
for (int windowEnd = 0; windowEnd < nums.length; windowEnd++) {
zeroCount += nums[windowEnd] == 0 ? 1 : 0;
while (zeroCount > 1) {
zeroCount -= nums[windowStart] == 0 ? 1 : 0;
windowStart++;
}
longestWindow = Math.max(longestWindow, windowEnd - windowStart);
}
return longestWindow;
}
}
```
> [name=Ron Chen][time=Wed, July 5, 2023]
#### C#
```csharp=
public class Solution {
public int LongestSubarray(int[] nums) {
int first = 0;
int second = 0;
int ans = 0;
foreach (int num in nums) {
if (num == 1) {
second++;
continue;
}
ans = Math.Max(ans, first + second);
first = second;
second = 0;
}
ans = second == nums.Length
? nums.Length - 1
: Math.Max(ans, first + second);
return ans;
}
}
```
>[name=Jim][time=Jul 5, 2023]
#### Python
```python=
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
pre, cur, ans = 0, 0, 0
for n in nums:
if n == 1:
cur += 1
else:
ans = max(ans, cur + pre)
pre = cur
cur = 0
ans = max(ans, cur + pre)
return ans - int(ans == len(nums))
```
> [name=Yen-Chi Chen][time=Thu, Jul 6, 2023]
### Reference
[回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)