A lot of WA were there in this problem , so I thought of writing down the common mistakes done while solving it. Also in the contest the accuracy for this problem was around 5% .
In this problem, you are given a binary string .
’1’ means that particular day is a working day
’0’ means that particular day is a holiday .
The person need X continuous holidays to go to a vacation . You can take only one leave i.e at any position flip ‘1’ to ‘0’ .
And then you have to find the maximum number of vacations the person can go to.
The common mistakes that were done are :-
1 . Only considering the '0’s in the segment where the ‘1’ is flipped and not the other contiguous '0’s
2 . Selecting the longest subarray having atmost one ‘1’ and flipping that ‘1’ and simply dividing the number of zeros in that segment by X plus the other segments of '0’s that are present.
The first mistake is quite obvious why its incorrect. Its simply because you are not counting all the holidays that could have been counted in a vacation.
Now , the second mistake is quite common because it seems selecting the longest subarray having atmost one ‘1’ will be optimal but its not always the case .
This comment by @yellow_tee explains this very well.
Lets consider the following input=
N= 13
X= 5
0000001001000now if we find longest subarray with only single 1 in it, it would be from index 0 to index 8, ie: “000000100”. let’s flip the 1 according to your solution. Therefore we get 0000000001000 as X =5 only one vacation can be taken in this case.
But the solution is as follows- flip the second one so we get:
“0000001000000”, in this we see that 2 vacations can be taken.The goal should be to flip the 1 such thats the merged interval has an extra segment of X 0s, this might not be true for the longest contiguous segment of 0s.
And so, the correct approach is that you have to go to every position which is a working day i.e ‘1’ and have to flip it and check whether it increases the total vacations or not.
Your final answer is just the maximum vacation that you got in this process.