【问题标题】:LeetCode Dynamic Programming Robber Question MistakeLeetCode 动态规划 强盗 问题 错误
【发布时间】:2020-06-01 19:12:21
【问题描述】:

我目前正在做LeetCode's Robber Question。我通过了 68/69 个测试用例,唯一失败的测试用例如下:像这样 [0, 0, 0, 0, 0 .....] 的数组满或零。您能否向我解释如何修复我的代码以修复此错误?另外,如果您对优化有任何建议,请告诉我!我的代码:

class Solution {
public int rob(int[] nums) {

    if (nums.length == 0 ) {
        return 0;
    }

    if (nums.length == 1) {
        return nums[0];
    }

    int[] memo = new int[nums.length];

    return Math.max(dp(0, nums, memo), dp(1, nums, memo));


}


public int dp(int index, int[] nums, int[] memo)  {

    int max;

    if (memo[index] != 0) {
        return memo[index];
    }


    if (index == nums.length - 1 || index == nums.length - 2) {
        max = nums[index];
    } else if (index == nums.length - 3) {
        max = nums[index] + nums[nums.length - 1];
    }
        else {
        max = Math.max(nums[index] + dp(index + 2, nums, memo), nums[index] + dp(index + 3, nums, memo));
    }

    memo[index] = max;
    return max;









}
}

【问题讨论】:

    标签: java dynamic-programming


    【解决方案1】:

    这就是我使用处理每个测试用例的自下而上 DP 解决相同问题的方法。

    class Solution {
        public int rob(int[] nums) {
         int n=nums.length;
         if(n==0)
             return 0;
         if(n==1)
              return nums[0];
    
         int[] dp= new int[n];
         dp[0]=nums[0];
         dp[1]=Math.max(nums[0],nums[1]);
         for(int i=2;i<n;i++)
          dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i]);
          return dp[n-1];   
        }
    

    您的代码因 [0,0,0,0....] 失败的原因是答案为零,并且您的递归基本情况 if(memo[index] != 0) 永远不会满足并导致 StackOverflow 。以下是您的代码的正确版本

     class Solution {
        public int rob(int[] nums) {
    
            if (nums.length == 0 ) {
                return 0;
            }
    
            if (nums.length == 1) {
                return nums[0];
            }
    
            int[] memo = new int[nums.length];
            Arrays.fill(memo,Integer.MAX_VALUE); // add this to check if memo is not updated
            return Math.max(dp(0, nums, memo), dp(1, nums, memo));
    
    
        }
    
    
        public int dp(int index, int[] nums, int[] memo)  {
    
            int max;
    
            if (memo[index] != Integer.MAX_VALUE) { // check for this condition 
                return memo[index];
            }
    
    
            if (index == nums.length - 1 || index == nums.length - 2) {
                max = nums[index];
            } else if (index == nums.length - 3) {
                max = nums[index] + nums[nums.length - 1];
            }
                else {
                max = Math.max(nums[index] + dp(index + 2, nums, memo), nums[index] + dp(index + 3, nums, memo));
            }
    
            memo[index] = max;
            return max;
        }
        }
    

    【讨论】:

    • 谢谢!我想了一段时间自下而上的方法,但无法想出这个。仍然不确定为什么我的代码不适用于最后一种情况......(没有它,我确实得到了 100% 的准确率)
    • 对于 69 个测试用例,答案是 [0],但你的递归基本用例永远不会得到满足,导致 LeetCode 中超过“时间限制”。和我本地系统中的 StackOverflow 。标记 Arrays.fill(memo,Integer.MAX_VALUE) 并检查你的基本情况
    猜你喜欢
    • 2017-01-25
    • 1970-01-01
    • 2023-03-08
    • 2011-10-17
    • 2012-04-09
    • 2023-01-31
    • 2011-10-14
    相关资源
    最近更新 更多