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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. Example 1:Input: nums = [1,2,3,1]Output: 4Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).Total amount you can rob = 1 + 3 = 4.
Question
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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. Example 1:Input: nums = [1,2,3,1]Output: 4Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).Total amount you can rob = 1 + 3 = 4.
Solution
This problem can be solved using dynamic programming. Here are the steps:
-
Create an array dp of the same length as nums, where dp[i] is the maximum amount of money you can rob from the first i houses. Initialize dp[0] to nums[0] and dp[1] to max(nums[0], nums[1]).
-
For each house from the third one (i = 2) to the last one, calculate the maximum amount of money you can rob from the first i houses. This is either the amount of money you can rob from the first i - 1 houses (which means you don't rob the current house), or the amount of money you can rob from the first i - 2 houses plus the money in the current house (which means you do rob the current house). So dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]).
-
The maximum amount of money you can rob from all the houses is the maximum amount of money you can rob from the first n houses, which is dp[n - 1], where n is the number of houses.
Here is the Python code for this solution:
def rob(nums):
if not nums:
return 0
if len(nums) == 1:
return nums[0]
dp = [0] * len(nums)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for i in range(2, len(nums)):
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
return dp[-1]
You can call this function with the input array to get the maximum amount of money you can rob. For example, rob([1,2,3,1]) returns 4.
Similar Questions
One day a thief plans to rob PQR bank. All the money in the bank is kept in a secret room. The secret room has N vaults arranged in a circle and the amount of money inside each vault will be displayed on the vault door. An alarm sounds If the thief attempt to break more than 2 of any 5 adjacent vaults and thief will be caught. Calculate the maximum amount of money that he can rob without sounding the alarm. Note: The vaults are arranged in a circle, the last vault is adjacent to the first one. Input Format: The first line contains an integer N which is the number of vaults. The next line has a sequence of positive integers of length N, giving the amount of cash in the vaults in order. Output Format: The maximum amount of money that the thief can rob without sounding the alarm. Constraints: N<=25, Amount in each vault <=50000 Sample Input 1: 9 1000 2000 1000 5000 9000 5000 3000 4000 1000 Sample Output 1: 15000 Explanation: The vaults 1, 5, 6 are looted, giving a total loot of (1000+5000+9000) = 15000 Sample Input 2: 10 1000 2000 3000 5000 9000 7000 6000 4000 7000 5000 Sample Output 2: 26000 Explanation: One way of getting the maximum amount is to loot vaults 4, 5, 9 and 10 giving a total of 26000. Hence the output is 26000. Note that no 5 adjacent vaults have more than 2 looted. give the code in c++
You are given an array of positive integers nums.Alice and Bob are playing a game. In the game, Alice can choose either all single-digit numbers or all double-digit numbers from nums, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is strictly greater than the sum of Bob's numbers.Return true if Alice can win this game, otherwise, return false. Example 1:Input: nums = [1,2,3,4,10]Output: falseExplanation:Alice cannot win by choosing either single-digit or double-digit numbers.Example 2:Input: nums = [1,2,3,4,5,14]Output: trueExplanation:Alice can win by choosing single-digit numbers which have a sum equal to 15.Example 3:Input: nums = [5,5,5,25]Output: trueExplanation:Alice can win by choosing double-digit numbers which have a sum equal to 25
You are given an integer array nums containing positive integers. We define a function encrypt such that encrypt(x) replaces every digit in x with the largest digit in x. For example, encrypt(523) = 555 and encrypt(213) = 333.Return the sum of encrypted elements. Example 1:Input: nums = [1,2,3]Output: 6Explanation: The encrypted elements are [1,2,3]. The sum of encrypted elements is 1 + 2 + 3 == 6.Example 2:Input: nums = [10,21,31]Output: 66Explanation: The encrypted elements are [11,22,33]. The sum of encrypted elements is 11 + 22 + 33 == 66. Constraints:1 <= nums.length <= 501 <= nums[i] <= 1000
In a village, people build houses in the same side of the road. A thief plans to loot the village. He wants maximum amount of money without having any risk of getting caught. By some means, the villagers know that their adjacent house is being looted or not and thus they become alert. So the thief cannot loot contiguous two houses. Given that the thief knows the amount of money stored in each house and the road is straight and there is no turning, which is the most efficient algorithmic strategy to solve this problem?
In a village, people build houses in the same side of the road. A thief plans to loot the village. He wants maximum amount of money without having any risk of getting caught. By some means, the villagers know that their adjacent house is being looted or not and thus they become alert. So the thief cannot loot contiguous two houses. Given that the thief knows the amount of money stored in each house and the road is straight and there is no turning, which is the most efficient algorithmic strategy to solve this problem?Brute-forceDynamic ProgrammingBacktrackingDivide and Conquer
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.