LeetCode 2305: Fair Distribution of Cookies Solution

Master LeetCode problem 2305 (Fair Distribution of Cookies), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2305. Fair Distribution of Cookies

Problem Explanation

Explanation

To find the minimum unfairness of all distributions, we can use a binary search approach. We first sort the array of cookies, then iterate through possible unfairness values. For each unfairness value, we check if it is possible to distribute the cookies to children such that the maximum total cookies obtained by a child is less than or equal to the current unfairness value. If possible, we update the minimum unfairness seen so far. The goal is to find the minimum unfairness value for which a valid distribution is possible.

  • Sort the cookies array in ascending order.
  • Initialize two pointers, left and right, where left is the minimum possible unfairness (0) and right is the maximum possible unfairness (the sum of all cookies).
  • Perform binary search within the range [left, right] to find the minimum unfairness value.
  • For each mid value in the binary search, check if a valid distribution is possible by assigning cookies to children.
  • If a valid distribution is possible, update the minimum unfairness seen so far and adjust the search range accordingly.

Time Complexity: O(n log(sum)), where n is the number of cookies and sum is the sum of all cookies. Space Complexity: O(1)

Solution Code

class Solution {
    public int fairDistribution(int[] cookies, int k) {
        Arrays.sort(cookies);
        int left = 0, right = Arrays.stream(cookies).sum();
        
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (isValidDistribution(cookies, k, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        
        return left;
    }
    
    private boolean isValidDistribution(int[] cookies, int k, int unfairness) {
        int count = 1;
        int sum = 0;
        
        for (int cookie : cookies) {
            sum += cookie;
            if (sum > unfairness) {
                count++;
                sum = cookie;
            }
        }
        
        return count <= k;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2305 (Fair Distribution of Cookies)?

This page provides optimized solutions for LeetCode problem 2305 (Fair Distribution of Cookies) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2305 (Fair Distribution of Cookies)?

The time complexity for LeetCode 2305 (Fair Distribution of Cookies) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2305 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2305 in Java, C++, or Python.

Back to LeetCode Solutions