Skip to content

Instantly share code, notes, and snippets.

@atiq-cs
Last active January 30, 2019 01:42
Show Gist options
  • Save atiq-cs/d4e5e37114d84e4afd72b129e932056f to your computer and use it in GitHub Desktop.
Save atiq-cs/d4e5e37114d84e4afd72b129e932056f to your computer and use it in GitHub Desktop.
do max robbing with DP
// O(N), O(N)
public class Solution {
public int Rob(int[] nums) {
var dp = new int[nums.Length];
for (int i=0; i<nums.Length; i++)
dp[i] = Math.Max((i < 2? 0 : dp[i-2]) + nums[i], i<1? 0 : dp[i-1]);
return nums.Length == 0 ? 0 : dp[nums.Length-1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment