-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path698.py
24 lines (24 loc) · 907 Bytes
/
698.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
from typing import List
class Solution:
def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
if sum(nums) % k != 0:
return False
target = sum(nums) // k
nums.sort(reverse=True)
used = [False] * len(nums)
def bt(i, k, currSum):
if k == 0:
return True
if currSum == target:
return bt(0, k - 1, 0)
for j in range(i, len(nums)):
if j > 0 and not used[j - 1] and nums[j] == nums[j - 1]:
continue
if not used[j] and currSum + nums[j] <= target:
used[j] = True
if bt(j + 1, k, currSum + nums[j]):
return True
used[j] = False
return False
return bt(0, k, 0)