SHARE
TWEET

Untitled

a guest Apr 26th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // SOS Dynamic Programming
  2. // O(n * 2^n)
  3.  
  4. //iterative version
  5. for(int mask = 0; mask < (1<<N); ++mask){
  6.     dp[mask][-1] = A[mask]; //handle base case separately (leaf states)
  7.     for(int i = 0;i < N; ++i){
  8.         if(mask & (1<<i))
  9.             dp[mask][i] = dp[mask][i-1] + dp[mask^(1<<i)][i-1];
  10.         else
  11.             dp[mask][i] = dp[mask][i-1];
  12.     }
  13.     F[mask] = dp[mask][N-1];
  14. }
  15. //memory optimized, super easy to code.
  16. for(int i = 0; i<(1<<N); ++i)
  17.     F[i] = A[i];
  18. for(int i = 0;i < N; ++i) for(int mask = 0; mask < (1<<N); ++mask){
  19.     if(mask & (1<<i))
  20.         F[mask] += F[mask^(1<<i)];
  21. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top