C# DP Memory Limit Exceeded


  • 0
    Y
    public class Solution {
        public bool CanCross(int[] stones) {
            int n=stones.Length;
            if(n==0)
                return false;
            //DP
            List<int>[] DP = new List<int>[n];
            DP[0] = new List<int>(){0};
            for(int i=1;i<n;i++)
            {
                DP[i] = new List<int>();
                for(int j=i-1;j>=0;j--)
                {
                    foreach(var num in DP[j])
                    {
                        if(num+1+stones[j]==stones[i])
                            DP[i].Add(num+1);
                        else if(num+stones[j]==stones[i])
                            DP[i].Add(num);
                        else if(num-1+stones[j]==stones[i])
                            DP[i].Add(num-1);
                    }
                }
            }
            return DP[n-1].Count > 0;
        }
    }

Log in to reply
 

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