LeetCode in Kotlin

2305. Fair Distribution of Cookies

Medium

You are given an integer array cookies, where cookies[i] denotes the number of cookies in the ith bag. You are also given an integer k that denotes the number of children to distribute all the bags of cookies to. All the cookies in the same bag must go to the same child and cannot be split up.

The unfairness of a distribution is defined as the maximum total cookies obtained by a single child in the distribution.

Return the minimum unfairness of all distributions.

Example 1:

Input: cookies = [8,15,10,20,8], k = 2

Output: 31

Explanation: One optimal distribution is [8,15,8] and [10,20]

The unfairness of the distribution is max(31,30) = 31.

It can be shown that there is no distribution with an unfairness less than 31.

Example 2:

Input: cookies = [6,1,3,2,2,4,1,2], k = 3

Output: 7

Explanation: One optimal distribution is [6,1], [3,2,2], and [4,1,2]

The unfairness of the distribution is max(7,7,7) = 7.

It can be shown that there is no distribution with an unfairness less than 7.

Constraints:

Solution

class Solution {
    private var res = Int.MAX_VALUE
    fun distributeCookies(c: IntArray, k: Int): Int {
        val nums = IntArray(k)
        dfs(c, nums, 0)
        return res
    }

    private fun dfs(c: IntArray, nums: IntArray, cur: Int) {
        if (cur == c.size) {
            var r = 0
            for (num in nums) {
                r = r.coerceAtLeast(num)
            }
            res = res.coerceAtMost(r)
            return
        }
        for (i in nums.indices) {
            if (nums[i] + c[cur] > res) {
                continue
            }
            nums[i] += c[cur]
            dfs(c, nums, cur + 1)
            nums[i] -= c[cur]
        }
    }
}