public static void Main()
Console.WriteLine(RobDP(new int[]{1, 2, 3, 1}));
Console.WriteLine(RobDP(new int[]{2, 7, 9, 3, 1}));
Console.WriteLine(RobDP(new int[]{2, 1, 1, 2}));
Console.WriteLine("**********");
Console.WriteLine(RobMax(new int[]{1, 2, 3, 1}));
Console.WriteLine(RobMax(new int[]{2, 7, 9, 3, 1}));
Console.WriteLine(RobMax(new int[]{2, 1, 1, 2}));
Console.WriteLine("**********");
Console.WriteLine(Rob(new int[]{1, 2, 3, 1}));
Console.WriteLine(Rob(new int[]{2, 7, 9, 3, 1}));
Console.WriteLine(Rob(new int[]{2, 1, 1, 2}));
private static int RobDP(int[] nums)
if(len == 1) return nums[0];
dp[1] = Math.Max(nums[0], nums[1]);
for(int i = 2; i < len; i++)
dp[i] = Math.Max(dp[i - 2] + nums[i], dp[i-1]);
max = Math.Max(max, dp[i]);
private static int RobMax(int[] nums)
for(int i = 1; i < len; i++)
max = Math.Max(nums[i] + prev, max);
private static int Rob(int[] nums)
notRobbed = Math.Max(robbed, notRobbed);
return Math.Max(robbed, notRobbed);