Advertisement
_no0B

Untitled

Dec 13th, 2021
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. /// problem: https://leetcode.com/problems/partition-to-k-equal-sum-subsets/
  2.  
  3. int Solve(int mask, int sum)
  4. {
  5.     if(mask == 0) return 1;
  6.     int ans = 0;
  7. //    for(int sub = 1 ; sub <= mask ; sub++){
  8. //        if((sub | mask) == mask && preCal[sub] == sum){
  9. //            ans |= Solve(mask ^ sub , k-1 , sum);
  10. //        }
  11. //    }
  12.     for(int sub = mask ; sub > 0 ; sub = (sub - 1)&mask){
  13.         if(preCal[sub] == sum){
  14.             ans |= Solve(mask ^ sub , sum);
  15.         }
  16.     }
  17.     return ans;
  18.  
  19. }
  20.  
  21. /// state: O(2^n)
  22. /// time: O(3^n) why??
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement