Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

House Robber #141

Closed
Tracked by #101
fkdl0048 opened this issue Oct 20, 2024 · 0 comments
Closed
Tracked by #101

House Robber #141

fkdl0048 opened this issue Oct 20, 2024 · 0 comments
Assignees
Labels
Milestone

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Oct 20, 2024

class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        if (n == 0) return 0;
        if (n == 1) return nums[0];

        int prev2 = 0;
        int prev1 = nums[0];

        for (int i = 1; i < n; i++) {
            int curr = max(prev1, prev2 + nums[i]);
            prev2 = prev1;
            prev1 = curr;
        }

        return prev1;
    }
};
  • 점화식 도출이 관건인 DP문제
  • p[i] = max(dp[i-1], dp[i-2] + nums[i])
    • dp[i-1]: i-1 번째 집까지의 최대 수익을 의미.
    • dp[i-2] + nums[i]: 현재 집 i를 포함해서 i-2까지의 최대 수익
  • 최대값 도출이기 때문에 공간도 2개만 사용하여 계산
@fkdl0048 fkdl0048 mentioned this issue Oct 20, 2024
75 tasks
@fkdl0048 fkdl0048 self-assigned this Oct 20, 2024
@fkdl0048 fkdl0048 added this to Todo Oct 20, 2024
@github-project-automation github-project-automation bot moved this to Todo in Todo Oct 20, 2024
@fkdl0048 fkdl0048 added this to the LeetCode milestone Oct 20, 2024
@fkdl0048 fkdl0048 moved this from Todo to In Progress in Todo Oct 20, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in Todo Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

1 participant