[Swift Weekly Contest 126]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III

[Swift Weekly Contest 126]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III,第1张

概述Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.   Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0],

Given an array A of 0s and 1s,we may change up to K values from 0 to 1.

Return the length of the longest (contiguous) subarray that contains only 1s.  

Example 1:

input: A = [1,1,0],K = 2 Output: 6 Explanation: [1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.

Example 2:

input: A = [0,1],K = 3 Output: 10 Explanation: [0,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. 

Note:

1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1 

给定一个由若干 0 和 1 组成的数组 A,我们最多可以将 K 个值从 0 变成 1 。

返回仅包含 1 的最长(连续)子数组的长度。 

示例 1:

输入:A = [1,K = 2输出:6解释: [1,1]粗体数字从 0 翻转到 1,最长的子数组长度为 6。

示例 2:

输入:A = [0,K = 3输出:10解释:[0,1]粗体数字从 0 翻转到 1,最长的子数组长度为 10。 

提示:

1 <= A.length <= 20000 0 <= K <= A.length A[i] 为 0 或 1  Runtime: 476 ms Memory Usage: 18.9 MB
 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 += 111             }12             while (zero > K)13             {14                 if A[left] == 015                 {16                     zero -= 117                 }18                 left += 119             }20             res = max(res,right - left + 1)21         }22         return res        23     }24 }

Runtime: 576 ms

Memory Usage: 18.9 MB
 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] = 110             }11         }12         for i in 1..<n13         {14             pre[i] = pre[i - 1] + pre[i]15         }16         var fans:Int = 017         for i in -1..<(n - 1)18         {19             var lo:Int = i + 120             var hi:Int = n - 121             var ans:Int = i22             while(lo <= hi)23             {24                 var mID:Int = (lo + hi) / 225                 var val:Int = pre[mID]26                 if i >= 027                 {28                     val -= pre[i]29                 }30                 if val <= K31                 {32                     ans = mID33                     lo = mID + 134                 }35                 else36                 {37                     hi = mID - 138                 }                39             }40             fans = max(fans,ans - i)41         }42         return fans    43     }44 }
总结

以上是内存溢出为你收集整理的[Swift Weekly Contest 126]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III全部内容,希望文章能够帮你解决[Swift Weekly Contest 126]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1019550.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存