LeetCode 198 House Robber

标签: 动态规划问题 LeetCode 发布于:2022-02-20 10:38:31 编辑于:2022-02-20 10:39:38 浏览量:962

概述

https://leetcode.com/problems/house-robber/

动态规划问题

class Solution {
public:
    vector<int> dp;
    int rob(vector<int>& nums) {
        dp.resize(nums.size(), -1);
        return helper(nums, 0);
    }
    
    int helper(vector<int>& nums, int i) {
        if (i >= nums.size()) return 0;
        if (dp[i] != -1) return dp[i];
        dp[i] = max(nums[i] + helper(nums, i+2), helper(nums, i+1));
        return dp[i];
    }
};

未经允许,禁止转载,本文源站链接:https://iamazing.cn/