Advertisement
HXXXXJ

1004. Max Consecutive Ones III

Apr 3rd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.60 KB | None | 0 0
  1. func longestOnes(_ arr: [Int], _ k: Int) -> Int {
  2.         guard arr.count > 0 else {return 0}
  3.        
  4.        
  5.         var left = 0
  6.         var right = 0
  7.         var res = 0
  8.         var count = 0
  9.        
  10.         while right < arr.count{
  11.             if arr[right] == 0 {
  12.                 count += 1
  13.             }
  14.             while count > k {
  15.                 if arr[left] == 0 {
  16.                     count -= 1
  17.                 }
  18.                 left += 1
  19.             }
  20.             res = max(res, right - left + 1)
  21.             right += 1
  22.         }
  23.        
  24.         return res
  25.        
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement