Class Solution
-
- All Implemented Interfaces:
public final class Solution3659 - Partition Array Into K-Distinct Groups.
Medium
You are given an integer array
numsand an integerk.Your task is to determine whether it is possible to partition all elements of
numsinto one or more groups such that:Each group contains exactly
kdistinct elements.Each element in
numsmust be assigned to exactly one group.
Return
trueif such a partition is possible, otherwise returnfalse.Example 1:
Input: nums = 1,2,3,4, k = 2
Output: true
Explanation:
One possible partition is to have 2 groups:
Group 1:
[1, 2]Group 2:
[3, 4]
Each group contains
k = 2distinct elements, and all elements are used exactly once.Example 2:
Input: nums = 3,5,2,2, k = 2
Output: true
Explanation:
One possible partition is to have 2 groups:
Group 1:
[2, 3]Group 2:
[2, 5]
Each group contains
k = 2distinct elements, and all elements are used exactly once.Example 3:
Input: nums = 1,5,2,3, k = 3
Output: false
Explanation:
We cannot form groups of
k = 3distinct elements using all values exactly once.Constraints:
<code>1 <= nums.length <= 10<sup>5</sup></code>
<code>1 <= numsi<= 10<sup>5</sup></code>
1 <= k <= nums.length
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final BooleanpartitionArray(IntArray nums, Integer k)-
-
Method Detail
-
partitionArray
final Boolean partitionArray(IntArray nums, Integer k)
-
-
-
-