Skip to content

Instantly share code, notes, and snippets.

@vegito2002
Last active March 27, 2018 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vegito2002/9b33e2566493b095cb6b432e384b3b1e to your computer and use it in GitHub Desktop.
Save vegito2002/9b33e2566493b095cb6b432e384b3b1e to your computer and use it in GitHub Desktop.
you are a frog
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