Simple DP solution not working


  • 0

    Hello, the straightforward dp solution below is working for all custom test cases but failing to submit with an empty runtime exception.

    class Solution {
    public:
        void count(string& s, int& m, int& n){        
            int l=s.size();
            for(int i=0;i<l;++i)
                if(s[i]=='0')
                    ++m;
                else if(s[i]=='1')
                    ++n;
        }    
        int findMaxForm(vector<string>& s, int m, int n) {        
            int l = s.size();             
            int dp[l+1][m+1][n+1];
            for(int i=0;i<l+1;++i){                        
                int _m=0,_n=0;
                if(i>0)
                    count(s[i-1],_m,_n);
                for(int j=0;j<m+1;++j)
                    for(int k=0;k<n+1;++k)
                        if(i==0)
                            dp[i][j][k]=0;                    
                        else if(j>=_m && k>=_n)
                            dp[i][j][k]=max(1+dp[i-1][j-_m][k-_n],dp[i-1][j][k]);                
                        else 
                            dp[i][j][k]=dp[i-1][j][k];
            }
            return dp[l][m][n];
        }
    };
    

    Pls help me debug it. Going crazy !!T_T


  • 0

    Changing int[][][] to a 3D vector solved the issue


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.