https://leetcode.com/problems/house-robber-ii/
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, 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.
Example 1: Input: [2,3,2] Output: 3 Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses. Example 2: Input: [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CirucularArray
{
/// <summary>
/// google interview
/// https://www.geeksforgeeks.org/maximum-sum-in-circular-array-such-that-no-two-elements-are-adjacent/
/// https://leetcode.com/problems/house-robber-ii/
/// </summary>
[TestClass]
public class MaximumSumCircularArrayNo2ElementsAreAdjacent
{
[TestMethod]
public void RobberTestEvenArray()
{
int[] arr = {1, 2, 3, 4, 5, 1};
int result = Rob(arr);
Assert.AreEqual(9, result);
}
[TestMethod]
public void RobberTestOddArray()
{
int[] arr = { 5, 1, 2, 10 ,5 };
int result = Rob(arr);
Assert.AreEqual(15, result);
}
[TestMethod]
public void TestMethodOneItem()
{
int[] arr = {1 };
int result = Rob(arr);
Assert.AreEqual(1, result);
}
public int Rob(int[] nums)
{
if (nums == null || nums.Length == 0)
{
return 0;
}
if (nums.Length == 1)
{
return nums[0];
}
return Math.Max(Helper(0, nums.Length - 2,nums), Helper(1, nums.Length - 1,nums));
}
private int Helper(int start, int end, int[] nums)
{
int prevMax = 0;
int currMax = 0;
for (int i = start; i <= end; i++)
{
int temp = currMax;
currMax = Math.Max(prevMax + nums[i], currMax);
prevMax = temp;
}
return currMax;
}
}
}
-
\$\begingroup\$ But your solution is not correct; try this: 11 1 2 4 1 10 1. The right result is 11 + 4 + 10 = 25. Your code would output 11 + 2 + 1 = 14. \$\endgroup\$Ilkhd– Ilkhd2019年09月22日 18:38:37 +00:00Commented Sep 22, 2019 at 18:38
-
\$\begingroup\$ It passed all of leetcode tests. But let me check what you are saying. \$\endgroup\$Gilad– Gilad2019年09月22日 19:17:28 +00:00Commented Sep 22, 2019 at 19:17
-
\$\begingroup\$ @Ilkhd you are wrong my friend my code returns 25 \$\endgroup\$Gilad– Gilad2019年09月23日 17:15:15 +00:00Commented Sep 23, 2019 at 17:15
-
\$\begingroup\$ Sorry, my fault. The code is a bit trickier than I thought. \$\endgroup\$Ilkhd– Ilkhd2019年09月23日 21:21:07 +00:00Commented Sep 23, 2019 at 21:21
-
\$\begingroup\$ What happens if the input is int[] arr = {5, 5, 5, 5, 5, 5}; \$\endgroup\$Alex Leo– Alex Leo2019年09月26日 14:17:46 +00:00Commented Sep 26, 2019 at 14:17
1 Answer 1
Here is another version of a solution
public int Rob(int[] nums)
{
int n = nums.Length;
if (n == 0)
{
return 0;
}
if (n == 1)
{
return nums[0];
}
int[] dp = new int[n];
int[] dp2 = new int[n];
dp[0] = nums[0];
dp[1] = Math.Max(nums[0], nums[1]);
dp2[0] = 0; //we "SKIP" nums[0]
dp2[1] = nums[1];
for (int i = 2; i < n; i++)
{
dp[i] = Math.Max(nums[i] + dp[i - 2], dp[i - 1]);
dp2[i] = Math.Max(nums[i] + dp2[i - 2], dp2[i - 1]);
}
//for dp we can't use n-1 since it is next to nums[0] so we only use n-2
return Math.Max(dp[n - 2], dp2[n - 1]);
}
-
\$\begingroup\$ You have presented an alternative solution, but haven't reviewed the code. Please edit to show what aspects of the question code prompted you to write this version, and in what ways it's an improvement over the original. It may be worth (re-)reading How to Answer. \$\endgroup\$Toby Speight– Toby Speight2022年05月04日 16:25:01 +00:00Commented May 4, 2022 at 16:25
-
\$\begingroup\$ While OPs are encouraged to answer their own questions bear in mind that "Every answer must make at least one insightful observation about the code in the question. Answers that merely provide an alternate solution with no explanation or justification do not constitute valid Code Review answers and may be deleted."... If you summarize what changed along with the reasons then it could satisfy as a review. \$\endgroup\$2022年05月04日 16:54:02 +00:00Commented May 4, 2022 at 16:54