Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1231.html

1231. Divide Chocolate

Level

Hard

Description

You have one chocolate bar that consists of some chunks. Each chunk has its own sweetness given by the array sweetness.

You want to share the chocolate with your K friends so you start cutting the chocolate bar into K+1 pieces using K cuts, each piece consists of some consecutive chunks.

Being generous, you will eat the piece with the minimum total sweetness and give the other pieces to your friends.

Find the maximum total sweetness of the piece you can get by cutting the chocolate bar optimally.

Example 1:

Input: sweetness = [1,2,3,4,5,6,7,8,9], K = 5

Output: 6

Explanation: You can divide the chocolate to [1,2,3], [4,5], [6], [7], [8], [9]

Example 2:

Input: sweetness = [5,6,7,8,9,1,2,3,4], K = 8

Output: 1

Explanation: There is only one way to cut the bar into 9 pieces.

Example 3:

Input: sweetness = [1,2,2,1,2,2,1,2,2], K = 2

Output: 5

Explanation: You can divide the chocolate to [1,2,2], [1,2,2], [1,2,2]

Constraints:

  • 0 <= K < sweetness.length <= 10^4
  • 1 <= sweetness[i] <= 10^5

Solution

Use binary search. Initialize low to be the minimium element in sweetness and high to be sum / (K + 1), where sum is the sum of all elements in sweetness. Each time calculate mid to be the average of low and high and check whether it is possible to cut the chocolate bar into K+1 chunks such that each chunk has total sweetness at least mid. If so, update the maximum total sweetness using mid and set low = mid + 1. Otherwise, set high = mid - 1. Finally, return the maximum total sweetness.

  • class Solution {
        public int maximizeSweetness(int[] sweetness, int K) {
            int sum = 0;
            int min = Integer.MAX_VALUE;
            for (int num : sweetness) {
                sum += num;
                min = Math.min(min, num);
            }
            if (K == 0)
                return sum;
            int low = min, high = sum / (K + 1);
            int max = 0;
            while (low <= high) {
                int mid = (high - low) / 2 + low;
                if (canDivide(sweetness, K + 1, mid)) {
                    max = Math.max(max, mid);
                    low = mid + 1;
                } else
                    high = mid - 1;
            }
            return max;
        }
    
        public boolean canDivide(int[] sweetness, int chunks, int lowerBound) {
            int sum = 0;
            int count = 0;
            int length = sweetness.length;
            for (int i = 0; i < length; i++) {
                int num = sweetness[i];
                sum += num;
                if (sum >= lowerBound) {
                    count++;
                    if (count == chunks)
                        break;
                    sum = 0;
                }
            }
            return count >= chunks;
        }
    }
    
  • class Solution {
    public:
        int maximizeSweetness(vector<int>& sweetness, int k) {
            int l = 0, r = accumulate(sweetness.begin(), sweetness.end(), 0);
            auto check = [&](int x) {
                int s = 0, cnt = 0;
                for (int v : sweetness) {
                    s += v;
                    if (s >= x) {
                        s = 0;
                        ++cnt;
                    }
                }
                return cnt > k;
            };
            while (l < r) {
                int mid = (l + r + 1) >> 1;
                if (check(mid)) {
                    l = mid;
                } else {
                    r = mid - 1;
                }
            }
            return l;
        }
    };
    
  • class Solution:
        def maximizeSweetness(self, sweetness: List[int], k: int) -> int:
            def check(x: int) -> bool:
                s = cnt = 0
                for v in sweetness:
                    s += v
                    if s >= x:
                        s = 0
                        cnt += 1
                return cnt > k
    
            l, r = 0, sum(sweetness)
            while l < r:
                mid = (l + r + 1) >> 1
                if check(mid):
                    l = mid
                else:
                    r = mid - 1
            return l
    
    
  • func maximizeSweetness(sweetness []int, k int) int {
    	l, r := 0, 0
    	for _, v := range sweetness {
    		r += v
    	}
    	check := func(x int) bool {
    		s, cnt := 0, 0
    		for _, v := range sweetness {
    			s += v
    			if s >= x {
    				s = 0
    				cnt++
    			}
    		}
    		return cnt > k
    	}
    	for l < r {
    		mid := (l + r + 1) >> 1
    		if check(mid) {
    			l = mid
    		} else {
    			r = mid - 1
    		}
    	}
    	return l
    }
    
  • function maximizeSweetness(sweetness: number[], k: number): number {
        let l = 0;
        let r = sweetness.reduce((a, b) => a + b);
        const check = (x: number): boolean => {
            let s = 0;
            let cnt = 0;
            for (const v of sweetness) {
                s += v;
                if (s >= x) {
                    s = 0;
                    ++cnt;
                }
            }
            return cnt > k;
        };
        while (l < r) {
            const mid = (l + r + 1) >> 1;
            if (check(mid)) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        return l;
    }
    
    

All Problems

All Solutions