sushantsangwan2006's blog

By sushantsangwan2006, history, 19 hours ago, In English

can anybody explain me this... i am not getting the implementation..2024B - Buying Lemonade

»
19 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

we have to find the minimum number of presses (which would determine that k cans will be collected) so if we say that x is the answer then we can say for sure that in the worst luck possible(in the case when you will press the buttons which corresponds the the minimum number of cans) x would work and any case where it's not the worst order x will suffice.

for ex : if the cans are ( 2 1 3 4 ) and we need k=4 cans whats the worst that could happen you press the button with 2nd row and get (2 0 3 4) then press it again and you don't get any so you know that this button is not good , then you press the button corresponding to 2(1st position) in the row so after 2 presses we get (0 0 3 4) then after pressing this for one more time you get the know that this button's row is also empty so you move forward .**so in this case the answer is 6 for getting k=4 , in any other order of presses you would take less than or equal to these number of presses **

**please keep in mind this solution would be complete if the number of cans can be 0 but it is not but we can modify if so it works **

for that just subtract n from k and reduce every element in array by 1 we would have that now number of cans can be zero also

so you can now greedy for the worst case possible .

greedy : we know that a[i] is atleast 1 so we press every button 1 time and reduce every element in array by one. and then follow the procedure above

if you want i can write a simple code for it if you still have some doubts