LC: 1231. Divide Chocolate - spiralgo/algorithms GitHub Wiki
The Essence:
A trial-and-error method empowered by Binary Search would be a good mechanism to try a sweetness value.
-
If
the number of the splitsrequired for the currentsweetness valueisgreaterthan allowed (k cuts) we should decrease the split number, therefore wecanincreasethesweetness value. -
If
the number of the splitsrequired for the currentsweetness valueissmallerthan allowed (k cuts) we should increase the split number, therefore wemustdecreasethesweetness value.
Details:
You can find a clean but elegant Binary Search implementation and explanation here: https://github.com/spiralgo/algorithms/pull/369