add new solutions
This commit is contained in:
parent
886b5e0a8e
commit
67cad91898
47 changed files with 1549 additions and 1 deletions
24
solutions/14/q1493/solution.go
Normal file
24
solutions/14/q1493/solution.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package q1493
|
||||
|
||||
func longestSubarray(nums []int) int {
|
||||
prev, cur := 0, 0
|
||||
longest := 0
|
||||
has0 := false
|
||||
for _, a := range nums {
|
||||
if a == 1 {
|
||||
cur++
|
||||
}
|
||||
longest = max(longest, prev+cur)
|
||||
if a == 0 {
|
||||
prev, cur = cur, 0
|
||||
has0 = true
|
||||
}
|
||||
}
|
||||
|
||||
if !has0 {
|
||||
longest--
|
||||
}
|
||||
return longest
|
||||
}
|
||||
|
||||
var _ = longestSubarray
|
||||
Loading…
Add table
Add a link
Reference in a new issue