LeetCode in Kotlin

1655. Distribute Repeating Integers

Hard

You are given an array of n integers, nums, where there are at most 50 unique values in the array. You are also given an array of m customer order quantities, quantity, where quantity[i] is the amount of integers the ith customer ordered. Determine if it is possible to distribute nums such that:

Return true if it is possible to distribute nums according to the above conditions.

Example 1:

Input: nums = [1,2,3,4], quantity = [2]

Output: false

Explanation: The 0th customer cannot be given two different integers.

Example 2:

Input: nums = [1,2,3,3], quantity = [2]

Output: true

Explanation: The 0th customer is given [3,3]. The integers [1,2] are not used.

Example 3:

Input: nums = [1,1,2,2], quantity = [2,2]

Output: true

Explanation: The 0th customer is given [1,1], and the 1st customer is given [2,2].

Constraints:

Solution

class Solution {
    fun canDistribute(nums: IntArray, quantity: IntArray): Boolean {
        val counter = count(nums)
        quantity.sort()
        return dfs(counter, quantity, quantity.size - 1)
    }

    private fun dfs(counter: IntArray, quantity: IntArray, quantityId: Int): Boolean {
        if (quantityId < 0) {
            return true
        }
        for (i in counter.indices) {
            if (i > 0 && counter[i] == counter[i - 1]) {
                continue
            }
            if (counter[i] >= quantity[quantityId]) {
                counter[i] -= quantity[quantityId]
                if (dfs(counter, quantity, quantityId - 1)) {
                    return true
                }
                counter[i] += quantity[quantityId]
            }
        }
        return false
    }

    private fun count(nums: IntArray): IntArray {
        val counter = IntArray(1001)
        for (n in nums) {
            counter[n]++
        }
        counter.sort()
        return counter.copyOfRange(counter.size - 50, counter.size)
    }
}