【发布时间】:2017-03-08 15:41:44
【问题描述】:
我得到了一个由 1 和 0 组成的数组,以及一个数字 k。我最多可以翻转数组的元素 k 次。(0 到 1 或 1 到 0)。
我需要编写一个算法来翻转最多 K 个元素的状态,以使具有相同元素的最大连续元素块的大小最小化。
ex:
Array : 110001111 and k=2;
Solution is 2 , because: we can change the string to: 110101011, and the length of the maximum consecutive length is minimised to 2.
Array : 1001 k=1
Solution is 2, because: If we don't change the input string at all, the answer will be 2. It is the best value we can achieve under the given conditions.
有人可以为我提供解决此问题的方法吗?
【问题讨论】:
-
对于希望标记此内容的用户,please read a consensus view 就与活跃竞赛相关的问题。仅根据其提出的质量来判断此问题,而不是根据其他网站的道德准则。
标签: arrays algorithm mathematical-optimization