Last active
March 27, 2018 05:42
-
-
Save vegito2002/9b33e2566493b095cb6b432e384b3b1e to your computer and use it in GitHub Desktop.
you are a frog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
int solve (int[] staircase, int[] possible_steps) { | |
int min_step = Integer.MAX_VALUE, N = staircase.length; | |
for (int step : possible_steps) | |
min_step = Math.min (min_step, step); | |
if (min_step > N) | |
return 0; | |
int[] dp = new int[N]; | |
for (int i = -1 + min_step; i < N; i++) { | |
int cur = Integer.MIN_VALUE; | |
for (int step : possible_steps) { | |
if (i - step >= -1) | |
cur = Math.max (cur, i - step >= 0 ? dp[i - step] : 0); | |
} | |
cur += staircase[i]; | |
for (int j = i; i - j < min_step; j--) | |
cur = Math.max (cur, dp[j]); | |
dp[i] = cur; | |
} | |
return dp[N - 1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment