Just a simple explanation

  • 2
    1. Calculate the average number of dresses per machine.

    2. Don't worry about each machine should borrow dresses or give out dresses, transform all to give-out => machines[i] = machines[i] - average, yes you can give out a negative amount to dresses.

    3. Max(machines[i]) should be the answer, yes at least you need to do machines[i] operations to give out machines[i] dresses.

    4. Wait, what if there is an accumulated number of give-outs that's greater than Max(machines[i])? Then use that accumulated number as the answer since those two give-outs can happen simultaneously. Example: [1, 1, 1, 0, -5, 0, 2], 1 + 1 + 1 > 2, so answer is 1 + 1 + 1 = 3.

  • 0

    @wz366 your solution will not work for this case


    in this case, the average dresses per machine is 4. and the array of machines[i]-average is


    in this case maximum give out is 7, there are no accumulated give outs that are more than 7, so according to your algorithm, the solution is 7. But the actual answer is 8.

    Can you Please explain your algorithm or correct me if i have understood it wrong.


  • 0

    @RepentThySins If we go from right to left 1 + 7 accumulate to 8. If we go from left to right, Math.abs(-4 + -4) accumulate to 8. Negative give outs are also give outs.

  • 0

    @wz366 Ah alright, now it makes sense :) Thank you.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.