Leetcode 1231. Divide Chocolate

Problem Explanation

We have a chocolate bar that comprises different chunks, each bearing a different level of sweetness represented by an array called sweetness. We want to distribute this chocolate bar among our K friends and us by making K+1 cuts in it such that we retain the piece with the least sweet chunks while all the other pieces go to friends. This problem is about finding the cut that allows us to maximize the sweetness of the piece that we retain.

Example

If we take the third example ```

sweetness = [1,2,2,1,2,2,1,2,2], K = 2```

, we can divide the chocolate to [1,2,2], [1,2,2], [1,2,2], which would have 5 as the maximum level of sweetness, which is our answer.

Approach

The solution is based on Binary Search, implemented with a condition to ascertain if we can eat the determined sweetness or not. We will search among all plausible minimum values of sweetness. Initially, ```

l```

is the mean of maximum values, and ```

r```

accumulates sweetness divided by ```

k+1```

. Binary search is used to find a plausible solution, in which search criteria is whether a plausible maximum sweetness ```

m```

is able to divide sweetness array into ```

k+1```

number of segments. In other words, it checks if ```

m```

can be the minimum total sweetness.

Working Through An Example

Take the example, ```

sweetness = [1,2,2,1,2,2,1,2,2], K = 2```

. We divide sweetness array to [1,2,2], [1,2,2], [1,2,2] because this is the pattern that gives us the maximum level of sweetness. Thus, ```

m```

would be 5.

Python Solution

1
2python
3
4class Solution:
5    def maximizeSweetness(self, sweetness: List[int], K: int) -> int:
6        
7        # Default values
8        l, r = 0, sum(sweetness) // (K + 1)
9
10        # Binary Search
11        while l < r:
12            m = (l + r + 1) // 2
13            pieces, curSweet = 0, 0
14            
15            # check if m can be the minTotalSweetness
16            for s in sweetness:
17                curSweet += s
18                if curSweet >= m:
19                    pieces += 1
20                    curSweet = 0
21            
22            # Too many pieces will only lower the minTotalSweetness
23            if pieces > K:
24                l = m
25            else:
26                r = m - 1
27        
28        return r

Java Solution

1
2java
3
4class Solution {
5    public int maximizeSweetness(int[] sweetness, int K) {
6        
7        int l = 0, r = (int) 1e9 / (K + 1);
8        
9        while (l < r) {
10            int m = (l + r + 1) / 2;
11            int curSweet = 0, cuts = 0;
12            
13            for (int sweet : sweetness) {
14                if ((curSweet += sweet) >= m) {
15                    curSweet = 0;
16                    if (++cuts > K) {
17                        break;
18                    }
19                }
20            }
21            
22            if (cuts > K) {
23                l = m;
24            } else {
25                r = m - 1;
26            }
27        }
28        
29        return r;
30    }
31}

JavaScript Solution

1
2javascript
3
4var maximizeSweetness = function(sweetness, K) {
5    let l = 0, r = sweetness.reduce((a, b) => a + b) / (K + 1);
6    
7    while (l < r) {
8        let m = l + r + 1 >> 1, curSweet = 0, pieces = 0;
9        
10        for (let sweet of sweetness) {
11            if ((curSweet += sweet) >= m) {
12                curSweet = 0;
13                if (++pieces > K) {
14                    break;
15                }
16            }
17        }
18        
19        if (pieces > K) {
20            l = m;
21        } else {
22            r = m - 1;
23        }
24    }
25    
26    return r >> 0;
27};

C++ Solution

1
2c++
3
4class Solution {
5public:
6    int maximizeSweetness(vector<int>& sweetness, int K) {
7        int l = *min_element(sweetness.begin(), sweetness.end()), r = accumulate(sweetness.begin(), sweetness.end(), 0) / (K + 1);
8        
9        while (l < r) {
10            int m = (l + r + 1) / 2;
11            int sum = 0, cuts = 0;
12            
13            for (int sweet : sweetness) {
14                if ((sum += sweet) >= m) {
15                    sum = 0;
16                    if (++cuts > K) {
17                        break;
18                    }
19                }
20            }
21            
22            if (cuts > K) {
23                l = m;
24            } else {
25                r = m - 1;
26            }
27        }
28        
29        return r;
30    }
31};

C# Solution

1
2c#
3
4public class Solution {
5    public int MaximizeSweetness(int[] sweetness, int K) {
6        int l = 0, r = (int) 1e9 / (K + 1);
7        
8        while (l < r) {
9            int m = (l + r + 1) / 2;
10            int curSweet = 0, cuts = 0;
11            
12            foreach(int sweet in sweetness) {
13                if ((curSweet += sweet) >= m) {
14                    curSweet = 0;
15                    if (++cuts > K) {
16                        break;
17                    }
18                }
19            }
20            
21            if (cuts > K) {
22                l = m;
23            } else {
24                r = m - 1;
25            }
26        }
27        
28        return r;
29    }
30}

Conclusion

The problem of maximizing the sweetness while sharing chocolate bar requires us to perform multiple binary searches as we remain in the loop while the left value is less than the right. For each possible maximum sweetness value, we iterate through the sweetness array and keep accumulating sweetness until we reach a value greater than or equal to the current maximum possible sweetness. Each time we reset the current sweetness to zero, we increment the pieces count. This represents making a cut in the chocolate bar. If we have more pieces than we need, we adjust the left value to the calculated maximum possible sweetness, else we adjust the right value to maximum possible sweetness - 1. We repeat these steps until the left value is less than the right. When l<r becomes false, that means we found the maximum possible sweetness of the chocolate piece that we can eat, and that is our answer. So, with the help of binary search, and understanding the problem requirements and how to implement them, we can solve this problem efficiently in Python, Java, JavaScript, C++, and C#.


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.


TA 👨‍🏫