Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Complete the maxStolenValue function below.
- static int maxStolenValue(int[] values) {
- if(values == null || values.Length == 0)
- return 0;
- if(values.Length == 1)
- return values[0];
- var DP = new int[values.Length];
- DP[0] = values[0];
- DP[1] = values[1];
- var MX = new int[values.Length]; // tells the maximum value from DP[0] to DP[i]
- MX[0] = DP[0];
- MX[1] = Math.Max(DP[0], DP[1]);
- var maxValue = Math.Max(values[0], values[1]);
- for(int i=2; i< values.Length;i++){
- DP[i] = MX[i-2] + values[i];
- maxValue = Math.Max(maxValue, DP[i]);
- MX[i] = Math.Max(MX[i-1], DP[i]);
- }
- return maxValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement