题解 | 时空漫游者的能量跳跃

时空漫游者的能量跳跃

https://www.nowcoder.com/practice/dfe0f8ffa80d47d48053fb6a8b7ddbad

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
       public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int k = sc.nextInt();
            int n = sc.nextInt();
            long [] E = new long[n];
            for (int i = 0; i<n;i++){
               E[i] = sc.nextLong();
            }
            long dp[] = new long[n];
            dp[0] = E[0];
            for(int i = 1; i<n; i++){
                dp[i] = E[i] + maxDpNum(dp,i,k);
            }
            System.out.println(dp[n-1]);
       }
       public static long maxDpNum(long[]E,int i,int k){
            int start = Math.max(i-k,0);
            long max = E[start];
            for (int i1 = i-1; i1 >= start; i1--){
                max = Math.max(max,E[i1]);
            }
            return max;
       }
}

全部评论

相关推荐

魔法恐龙:这真得给个机会,面试的时候问问不吃饭78.5h怎么做到的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务