Flip consecutive zeroes to ones in k operations to have maximum number of ones, find the maximum number of ones

You are given a binary string made of 0 and 1, and a value k which represents number of operations. You can flip consecutive 0s in each operation to 1s. Find the maximum number of 1s after k operations.

For example:
input: “00010”, k=1
output: 4
explanation: we can convert first three consecutive 0s to 1s in one operation. the result is “11110”. answer is 4.

input: “1100101001”, k=2
output: 7
explanation: we can convert 0s at indices[2,3] to 1s in first operation and then 0 at index 5 to 1. the result after two operations is 1111111001. answer is 7.

I thought of using sliding window but I am unable to solve using it as we need to form a result with maximum 1s considering the consecutive 0s to be flipped to 1s which can make the result with maximum 1s.

4

A sliding window approach will work. In order to maximise the number of consecutive 1s, it only makes sense to choose adjacent groups of 0s for conversion to 1s (i.e. without any non-chosen 0s between them), otherwise we spend our k on creating disjoint islands of 1s, which is a waste. So you’d first choose the first k groups of 0s, then unselect the first one of these and append the next one, …etc, “sliding” through the input, each time checking which one of these options represents the longest result.

Here is an implementation of the sliding window approach written in JavaScript:

function solve(pattern, k) {
    let start = 0; // start index of the "window"
    let size = 0; // Longest result found so far
    for (let end = 0; end < pattern.length; end++) { // Grow window to the right
        if (pattern[end] == "0" && (end == 0 || pattern[end-1] == "1")) {
            // We encounter a first 0 in a group of 0s
            k--; // Adjust the number of 0-groups we are still allowed to consume
            if (k < 0) { // Must give up a group of 0s
                // Shrink window from the left side, dropping one 0-group
                while (pattern[start] == 1) start++;
                while (pattern[start] == 0) start++;
                k++; // ...and allow for one more 0-group at the right
            }
        }
        if (end - start >= size) size = end + 1 - start; // best so far
    }
    return size;
}

let count = solve("1100101001", 2);
console.log(count);

0

You’re looking for the longest sequence with the property that it contains at most 2 runs of zeroes. Because that’s the answer: Flip those 2 runs to ones and voila, the longest chain of 1s.

For obvious reasons, the start of this sequence is either the start of the input, or a 1 that is preceded by a 0, and the end is either the end of the input, or a 1 that is followed by a 0.

Rewrite the input to length coding. So, 11000100001 Turns into ‘0,2,3,1,4,1’ (for: 0 zeroes, then 2 ones, then 3 zeroes, 1 one, 4 zeroes, 1 one). For this algorithm we must always start with a count of zeroes, hence, we start with ‘zero zeroes’.

You’re looking for the largest available sum of 4 consecutive numbers if the start is even, and 5 consecutive numbers if the start index is odd.

This can trivially be done with sliding windows: Add up the first 4 numbers and save this value.

Then remove the first number and add the 2 subsequent numbers. If this is larger than what you stored before, save that.

Then remove 2 numbers off the front and add 1 to the back.

Then remove 1 number off the front and add 2 to the back.

Keeping going until you’re at the end. Return the largest ‘stored’ number.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật