House Robber Problem

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 101:-  House Robber Problem

The House Robber problem is a classic dynamic programming problem. In this problem, you are given an array representing the wealth in each house on a street. The goal is to find the maximum amount of money you can rob without robbing adjacent houses, as robbing two adjacent houses will trigger an alarm.

Here's a Python function that solves the House Robber problem using dynamic programming:

pythonCopy code

def house_robber(nums):    if not nums:        return 0    n = len(nums)    if n == 1:        return nums[0]    # Create a dp array to store the maximum amount of money that can be robbed up to the current house    dp = [0] * n    dp[0] = nums[0]    dp[1] = max(nums[0], nums[1])    for i in range(2, n):        # The maximum amount of money that can be robbed at the current house is the maximum of:        # 1. The maximum amount of money robbed up to the previous house (dp[i-1])        # 2. The current house's wealth plus the maximum amount of money robbed up to two houses ago (dp[i-2])        dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])    return dp[-1] # Test the function house_wealth = [1, 2, 3, 1] print(house_robber(house_wealth))  # Output: 4 (Rob the first and the third house) house_wealth = [2, 7, 9, 3, 1] print(house_robber(house_wealth))  # Output: 12 (Rob the second and the fourth house)

In this code, the house_robber() function takes an array nums representing the wealth in each house. It creates a dp array to store the maximum amount of money that can be robbed up to the current house. The dynamic programming approach iterates through the houses, and at each step, it calculates the maximum amount of money that can be robbed at the current house based on the previous results stored in the dp array.

The final result will be the maximum amount of money that can be robbed without robbing adjacent houses. The time complexity of this solution is O(n), where n is the number of houses.

14. Week7 - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?