Skip to content

Conversation

Siddhram
Copy link

This PR introduces a complete and well-documented implementation of the House Robber problem in R.

Overview

The implementation provides solutions for maximizing the amount of money that can be robbed from a row of houses without alerting the police. It includes a space-optimized O(1) DP approach that tracks only two rolling states and an optional tabulation variant for clarity, along with embedded examples.

Features

  • Space-optimized O(1) DP:
    For each house i:
    • take = nums[i] + prev2 (rob current, skip previous)
    • notTake = prev1 (skip current)
    • curr = max(take, notTake)
    • Update prev2 = prev1; prev1 = curr
  • Tabulation DP (O(n)) for pedagogical completeness
  • Handles empty and single-element arrays
  • Consistent comments and structure with other dynamic_programming scripts

Complexity

  • Time Complexity: O(n)
  • Space Complexity:
    • O(1) for the rolling DP solution
    • O(n) for tabulation

Directory

  • Updated DIRECTORY.md to include “House Robber” under Dynamic Programming

Demonstration

Run the script to execute built-in examples:

From Windows command line:

Rscript "R/dynamic_programming/house_robber.r"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant