698. Partition to K Equal Sum Subsets 發表於 2023-02-13 | 分類於 leetcode problem17. Letter Combinations of a Phone Numbersolutionoption 1 - dfs , maybe TLE1234567891011121314151617181920212223242526272829class Solution {public: bool backtracking(vector<int>& nums, vector<bool> & visited, int start, int sum, int target, int k){ if(k==0) return true; if(target == sum){ return backtracking(nums, visited, 0,0,target , k-1); } for(int i=start;i<nums.size() ; ++i){ if(visited[i] || sum+nums[i] > target) continue; sum+=nums[i]; visited[i] = true; if(backtracking(nums, visited, i+1,sum, target, k) ) return true; sum-=nums[i]; visited[i] = false; } return false; } bool canPartitionKSubsets(vector<int>& nums, int k) { int n = nums.size(); int total = 0; for(int n:nums) total +=n; if(total%k!=0) return false; vector<bool> visited(n, false); total/= k; return backtracking(nums, visited, 0, 0, total, k); }};