Can some one explain why its not 2-D DP problem. Getting Memory out of exception.
https://leetcode.com/problems/can-i-win/
public class Solution {
int[,] dp;
public bool CanIWin(int maxI, int left) {
if(left==0){
return true;
}
int used = 0;
dp = new int[left+1, (1 << (maxI+1))];
return CanIWin2(maxI, left, used)>0;
}
public int CanIWin2(int maxI, int left, int used) {
if(dp[left, used]!=0){
return dp[left, used];
}
int ans = -1;
for(int i=1;i<=maxI;i++){
if((used & (1<<i))==0){ // unused and can be selected
used |= (1<<i);
int canOpponentWin = i>=left? -1 : CanIWin2(maxI, left - i, used);
used^=(1<<i);
if(canOpponentWin<0){
ans= 1;
break;
}
}
}
dp[left, used]=ans;
return ans;
}
}