★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-zedvwtnk-md.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given an array A
of 0s and 1s, we may change up to K
values from 0 to 1.git
Return the length of the longest (contiguous) subarray that contains only 1s. github
Example 1:數組
Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Example 2:微信
Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Note:app
1 <= A.length <= 20000
0 <= K <= A.length
A[i]
is 0
or 1
給定一個由若干 0
和 1
組成的數組 A
,咱們最多能夠將 K
個值從 0 變成 1 。spa
返回僅包含 1 的最長(連續)子數組的長度。 debug
示例 1:code
輸入:A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 輸出:6 解釋: [1,1,1,0,0,1,1,1,1,1,1] 粗體數字從 0 翻轉到 1,最長的子數組長度爲 6。
示例 2:htm
輸入:A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 輸出:10 解釋: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] 粗體數字從 0 翻轉到 1,最長的子數組長度爲 10。
提示:
1 <= A.length <= 20000
0 <= K <= A.length
A[i]
爲 0
或 1
1 class Solution { 2 func longestOnes(_ A: [Int], _ K: Int) -> Int { 3 var left = 0, right = 0, res = 0, zeroCount = 0 4 5 while right < A.count { 6 if A[right] == 0 { zeroCount += 1 } 7 right += 1 8 while(zeroCount > K) { 9 if A[left] == 0 { zeroCount -= 1 } 10 left += 1 11 } 12 res = max(right - left, res) 13 } 14 return res 15 } 16 }
432ms
1 class Solution { 2 func longestOnes(_ A: [Int], _ K: Int) -> Int { 3 var i = 0, k = K 4 var result = 0 5 for j in 0..<A.count { 6 if A[j] == 0 { 7 k -= 1 8 } 9 while k < 0 { 10 if A[i] == 0 { 11 k += 1 12 } 13 i += 1 14 } 15 result = max(result, j - i + 1) 16 } 17 return result 18 } 19 }
460ms
1 class Queue<T>: CustomDebugStringConvertible, CustomStringConvertible { 2 private var storage: Array<T> 3 4 private var head = 0 5 private var last = 0 6 7 init(_ capacity: Int) { 8 storage = Array() 9 storage.reserveCapacity(capacity) 10 } 11 12 func isEmpty() -> Bool { 13 return head == last 14 } 15 16 func push(_ val: T) { 17 storage.append(val) 18 last += 1 19 } 20 21 func pop() -> T { 22 head += 1 23 24 return storage[head - 1] 25 } 26 27 func forEach(_ block: (T)->Void) { 28 for i in head..<last { 29 block(storage[i]) 30 } 31 } 32 33 func first() -> T { 34 return storage[head] 35 } 36 37 func count() -> Int { 38 return last - head 39 } 40 41 var description: String { 42 return debugDescription 43 } 44 45 var debugDescription: String { 46 var str = "[" 47 forEach { str += "\($0)," } 48 str += "]" 49 return str 50 } 51 } 52 53 54 class Solution { 55 func longestOnes(_ A: [Int], _ K: Int) -> Int { 56 57 let queue = Queue<Int>(A.count) 58 var current = 0 59 var ans = 0 60 61 for i in A.indices { 62 if A[i] > 0 { 63 current += 1 64 } else if queue.count() < K { 65 current += 1 66 queue.push(i) 67 } else if K > 0{ 68 current = i - queue.pop() 69 queue.push(i) 70 } 71 72 ans = max(ans, current) 73 } 74 75 return ans 76 } 77 }
Runtime: 476 ms
1 class Solution { 2 func longestOnes(_ A: [Int], _ K: Int) -> Int { 3 var res:Int = 0 4 var zero:Int = 0 5 var left:Int = 0 6 for right in 0..<A.count 7 { 8 if A[right] == 0 9 { 10 zero += 1 11 } 12 while (zero > K) 13 { 14 if A[left] == 0 15 { 16 zero -= 1 17 } 18 left += 1 19 } 20 res = max(res, right - left + 1) 21 } 22 return res 23 } 24 }
Runtime: 576 ms
1 class Solution { 2 func longestOnes(_ A: [Int], _ K: Int) -> Int { 3 var n:Int = A.count 4 var pre:[Int] = [Int](repeating:0,count:n) 5 for i in 0..<n 6 { 7 if A[i] == 0 8 { 9 pre[i] = 1 10 } 11 } 12 for i in 1..<n 13 { 14 pre[i] = pre[i - 1] + pre[i] 15 } 16 var fans:Int = 0 17 for i in -1..<(n - 1) 18 { 19 var lo:Int = i + 1 20 var hi:Int = n - 1 21 var ans:Int = i 22 while(lo <= hi) 23 { 24 var mid:Int = (lo + hi) / 2 25 var val:Int = pre[mid] 26 if i >= 0 27 { 28 val -= pre[i] 29 } 30 if val <= K 31 { 32 ans = mid 33 lo = mid + 1 34 } 35 else 36 { 37 hi = mid - 1 38 } 39 } 40 fans = max(fans, ans - i) 41 } 42 return fans 43 } 44 }