-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1493-LongestSubarrayOf1sAfterDeletingOneElement.go
More file actions
137 lines (124 loc) · 4.58 KB
/
1493-LongestSubarrayOf1sAfterDeletingOneElement.go
File metadata and controls
137 lines (124 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
// 1493. Longest Subarray of 1's 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.
import "fmt"
func longestSubarray(nums []int) int {
res,l := 0, len(nums)
for i := 0; i < l; i++ {
delet, count := 1, 0
for j := i; j < l; j++ {
if nums[j] == 1 {// 是 1 就累加
count++
} else if delet == 1 { // 遇到 0,有1次删除机会使用掉
delet--
continue
} else { // 遇到 0,没有删除机会了
break
}
}
if count > res { // 取最大的
res = count
}
}
if res == l { // 处理全是 1的情况(必须做一上删除操作)
return l - 1
}
return res
}
func longestSubarray1(nums []int) int {
sum, l, r := 0, 0, 0
for ; r <len(nums); r++ {
sum += nums[r]
if sum < r - l {
sum -= nums[l]
l++
}
}
return r - l - 1
}
func longestSubarray2(nums []int) int {
res, left, right, k, isKUsed := 0, 0, 0, 0, false
max := func (x, y int) int { if x > y { return x; }; return y; }
for right < len(nums) {
if nums[right] == 1 {
res = max(res, right - left + 1 - k)
right++
} else {
if k == 0 { // use k
k++
res = max(res, right - left + 1 - k)
right++
isKUsed = true
} else {
// skip the first non zero
for nums[left] == 1 {
left++
}
left++
k--
}
}
}
if !isKUsed {
res--
}
return res
}
func main() {
// 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.
fmt.Println(longestSubarray([]int{1,1,0,1})) // 3
// 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].
fmt.Println(longestSubarray([]int{0,1,1,1,0,1,1,0,1})) // 5
// Example 3:
// Input: nums = [1,1,1]
// Output: 2
// Explanation: You must delete one element.
fmt.Println(longestSubarray([]int{1,1,1})) // 2
fmt.Println(longestSubarray([]int{1,1,1,1,1,1,1,1,1,1})) // 9
fmt.Println(longestSubarray([]int{0,0,0,0,0,0,0,0,0,0})) // 0
fmt.Println(longestSubarray([]int{1,0,1,0,1,0,1,0,1,0})) // 2
fmt.Println(longestSubarray([]int{0,1,0,1,0,1,0,1,0,1})) // 2
fmt.Println(longestSubarray([]int{0,0,0,0,0,1,1,1,1,1})) // 5
fmt.Println(longestSubarray([]int{1,1,1,1,1,0,0,0,0,0})) // 5
fmt.Println(longestSubarray1([]int{1,1,0,1})) // 3
fmt.Println(longestSubarray1([]int{0,1,1,1,0,1,1,0,1})) // 5
fmt.Println(longestSubarray1([]int{1,1,1})) // 2
fmt.Println(longestSubarray1([]int{1,1,1,1,1,1,1,1,1,1})) // 9
fmt.Println(longestSubarray1([]int{0,0,0,0,0,0,0,0,0,0})) // 0
fmt.Println(longestSubarray1([]int{1,0,1,0,1,0,1,0,1,0})) // 2
fmt.Println(longestSubarray1([]int{0,1,0,1,0,1,0,1,0,1})) // 2
fmt.Println(longestSubarray1([]int{0,0,0,0,0,1,1,1,1,1})) // 5
fmt.Println(longestSubarray1([]int{1,1,1,1,1,0,0,0,0,0})) // 5
fmt.Println(longestSubarray2([]int{1,1,0,1})) // 3
fmt.Println(longestSubarray2([]int{0,1,1,1,0,1,1,0,1})) // 5
fmt.Println(longestSubarray2([]int{1,1,1})) // 2
fmt.Println(longestSubarray2([]int{1,1,1,1,1,1,1,1,1,1})) // 9
fmt.Println(longestSubarray2([]int{0,0,0,0,0,0,0,0,0,0})) // 0
fmt.Println(longestSubarray2([]int{1,0,1,0,1,0,1,0,1,0})) // 2
fmt.Println(longestSubarray2([]int{0,1,0,1,0,1,0,1,0,1})) // 2
fmt.Println(longestSubarray2([]int{0,0,0,0,0,1,1,1,1,1})) // 5
fmt.Println(longestSubarray2([]int{1,1,1,1,1,0,0,0,0,0})) // 5
}