Question

In: Computer Science

You are a professional robber planning to rob houses along a street. Each house has a...

You are a professional robber planning to rob houses along a street. Each house has

a certain amount of money stashed, the only constraint stopping you from robbing

each of them is that adjacent houses have security system connected and it will

automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house,

determine the maximum amount of money you can rob tonight without alerting the

police.

Solutions

Expert Solution

To solve this problem , we will use dynamic programming .

At each step we have 2 choices or decisions to make and we will take the best decision in order to maximize the money.

Choice-1)At every ith index or house we can  rob that house and add the money to the amount collected till previous to previous house i.e (i-2)th house

Choice -2)Skip the current house

Below is the simple python code to understand and verify the above logic.
Code:
def rob_non_adjacent_houses(money_list):
    """

    :param money_list:
    :return: maximum amount of money that can be robbed from non-adjacent houses
    """

    # If we are at the ith house then there are two options available to us, either we can
    # rob that house and add the money to the amount collected
    # till previous to previous house i.e (i-2)th house  or we skip the current house.
    dp = [money_list[0], money_list[1]]
    for i in range(2, len(money_list)):
        dp.append(max(dp[i - 1], dp[i - 2] + money_list[i]))
    return dp[len(money_list) - 1]


if __name__ == '__main__':
    """
    main method to test the above method
    """
    print(rob_non_adjacent_houses([30, 40, 50, 20, 10, 40]))


Related Solutions

You are saving for the down payment on a house. The houses in the area you...
You are saving for the down payment on a house. The houses in the area you prefer have an average selling price of $450,000 and you need a 10% down payment to ensure your mortgage payments are not too high. You have $30,000 saved that you can invest today at 6.5% (annual compounding). a) How long before you will have enough for the down payment saved? b) You want to buy the house sooner. In addition to the $30,000 saved...
You are planning to buy a house today. The house costs $350,000. You have $50,000 in...
You are planning to buy a house today. The house costs $350,000. You have $50,000 in cash that you can use as a down payment on the​ house, but you need to borrow the rest of the purchase price. The bank is offering a 30​-year mortgage that requires annual payments and has an EAR of 5% per year. What will be your annual mortgage​ payment?
The average house has 10 paintings on its walls. Is the mean smaller for houses owned...
The average house has 10 paintings on its walls. Is the mean smaller for houses owned by teachers? The data show the results of a survey of 13 teachers who were asked how many paintings they have in their houses. Assume that the distribution of the population is normal. 8, 9, 10, 10, 8, 10, 9, 8, 11, 10, 7, 7, 7 What can be concluded at the α = 0.10 level of significance? For this study, we should use...
The average house has 10 paintings on its walls. Is the mean smaller for houses owned...
The average house has 10 paintings on its walls. Is the mean smaller for houses owned by teachers? The data show the results of a survey of 14 teachers who were asked how many paintings they have in their houses. Assume that the distribution of the population is normal. 7, 11, 8, 10, 10, 7, 7, 7, 11, 10, 7, 8, 10, 7 What can be concluded at the αα = 0.10 level of significance? For this study, we should...
Jess has agreed to purchase a house on 11th Street by July 2020 but only if...
Jess has agreed to purchase a house on 11th Street by July 2020 but only if she is able to sell her house on 15th Street by June 2020. This term is called a: express condition concurrent condition condition subsequent anticipatory condition --------------------------------------------------------------------------------------------------- In the United States, sources of law include: Executive orders laws passed by a state legislature international common law the U.S. Constitution --------------------------------------------------------------------------------------------------- The main purpose of contract remedies is to put the party who is not...
You are walking to George Brown College along King Street just past Jarvis. You are on...
You are walking to George Brown College along King Street just past Jarvis. You are on your way to Business Law class and are now on George Brown campus. Person B, is a pet-walker, and is also walking to GBC with a gila monster and pelican on separate leashes. It was about 8:30 am. At this time, two Volvo transport trucks are driving down King Street with Hélio Gracie doing the "most-epic of splits" between them. Moreover, at this time,...
Implement a class Address. An address has a house number, a street, an optional apartment number,...
Implement a class Address. An address has a house number, a street, an optional apartment number, a city, a state, and a postal code (all of these are strings). Create the getters and setters. Supply two constructors that accept all the address components except one includes an apartment number and one without (refer to the tester class below to see the proper order). Supply a print method that returns void and prints to the console the address with the street...
You expect to live in a house you are planning to own for 5 years, with...
You expect to live in a house you are planning to own for 5 years, with a $300K loan. You could get a 3/1 ARM amortized over 15 years at 3.9 %. Suppose the expected interest rate of the ARM for years 4 and 5 is 4.5% and 5.5% respectively. MARR is 10% per year compounded monthly. What is your Present Cost (PC) of this loan option? What is the outstanding balance of the loan at the end of 5...
You expect to live in a house you are planning to own for 5 years, with...
You expect to live in a house you are planning to own for 5 years, with a $300K loan. You could get a 3/1 ARM amortized over 15 years at 3.9 %. Suppose the expected interest rate of the ARM for years 4 and 5 is 4.5% adn 5.5% respectively. MARR is 10% per year compounded monthly. What is your Present Cost (PC) of this loan option?
Suppose you are buying a beach house condo for $450,000. You are planning to finance the...
Suppose you are buying a beach house condo for $450,000. You are planning to finance the ENTIRE amount of the condo via a mortgage for 30 years, 5% fixed, 0-points. The HOA fees, property taxes, Annual maintenance + Upkeep of the condo = $12,000 per year. Utilities are $150/month You are planning to rent this condo, and your rental income is $40,000 per year (gross), which goes up by 2% per year. Expenses to rent out the property are 30%...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT