Advertisement
rishu110067

Untitled

Jan 24th, 2022 (edited)
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1.     // Complete the maxStolenValue function below.
  2.     static int maxStolenValue(int[] values) {
  3.      
  4.      if(values == null || values.Length == 0)
  5.       return 0;
  6.      if(values.Length == 1)
  7.       return values[0];
  8.      var DP = new int[values.Length];
  9.      DP[0] = values[0];
  10.      DP[1] = values[1];
  11.      var MX = new int[values.Length]; // tells the maximum value from DP[0] to DP[i]
  12.      MX[0] = DP[0];
  13.      MX[1] = Math.Max(DP[0], DP[1]);
  14.      var maxValue = Math.Max(values[0], values[1]);
  15.      for(int i=2; i< values.Length;i++){
  16.         DP[i] = MX[i-2] + values[i];
  17.         maxValue = Math.Max(maxValue, DP[i]);
  18.         MX[i] = Math.Max(MX[i-1], DP[i]);
  19.      }
  20.      return maxValue;
  21.     }
  22.  
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement