当前位置:网站首页>LeetCode 1696. 跳跃游戏 VI 每日一题
LeetCode 1696. 跳跃游戏 VI 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
给你一个下标从 0 开始的整数数组 nums 和一个整数 k 。
一开始你在下标 0 处。每一步,你最多可以往前跳 k 步,但你不能跳出数组的边界。也就是说,你可以从下标 i 跳到 [i + 1, min(n - 1, i + k)] 包含 两个端点的任意位置。
你的目标是到达数组最后一个位置(下标为 n - 1 ),你的 得分 为经过的所有数字之和。
请你返回你能得到的 最大得分 。
示例 1:
输入:nums = [1,-1,-2,4,-7,3], k = 2
输出:7
解释:你可以选择子序列 [1,-1,4,3] (上面加粗的数字),和为 7 。
示例 2:输入:nums = [10,-5,-2,4,0,3], k = 3
输出:17
解释:你可以选择子序列 [10,4,3] (上面加粗数字),和为 17 。
示例 3:输入:nums = [1,-5,-20,4,-1,3,-6,-3], k = 2
输出:0
提示:
1 <= nums.length, k <= 105
-104 <= nums[i] <= 104来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/jump-game-vi
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
java
class Solution {
public int maxResult(int[] nums, int k) {
int n = nums.length;
int[] dp = new int[n];
Arrays.fill(dp,Integer.MIN_VALUE);
dp[0] = nums[0];
for(int i = 0;i < n;i++){
//跳k次
for(int j = i + 1;j < n && j <= i + k;j++){
//跳跃后的分数
int nextScore = dp[i] + nums[j];
//更新为跳跃后更高的分数
if(nextScore > dp[j]) dp[j] = nextScore;
//过滤
if(dp[j] >= dp[i]) break;
}
}
return dp[n - 1];
}
}
边栏推荐
- Vs2019 configuration matrix library eigen
- How can laravel get the public path
- [Android -- data storage] use SQLite to store data
- The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
- 【DesignMode】外观模式 (facade patterns)
- Sqlserver2014+: create indexes while creating tables
- The difference and working principle between compiler and interpreter
- os、sys、random标准库主要功能
- null == undefined
- The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
猜你喜欢

Have fun | latest progress of "spacecraft program" activities

字节跳动高工面试,轻松入门flutter

AutoLISP series (3): function function 3

Master this promotion path and share interview materials
正在准备面试,分享面经

掌握这套精编Android高级面试题解析,oppoAndroid面试题
![[medical segmentation] attention Unet](/img/f4/cf5b8fe543a19a5554897a09b26e68.png)
[medical segmentation] attention Unet

数据中台落地实施之法

Horizontal and vertical centering method and compatibility
作为Android开发程序员,android高级面试
随机推荐
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
【DesignMode】外观模式 (facade patterns)
Cesium(3):ThirdParty/zip. js
Advanced C language -- function pointer
Personal notes of graphics (1)
AutoLISP series (3): function function 3
A tour of gRPC:03 - proto序列化/反序列化
pycharm 终端部启用虚拟环境
Three. JS series (1): API structure diagram-1
【DesignMode】享元模式(Flyweight Pattern)
Usage of config in laravel
【图像传感器】相关双采样CDS
二叉搜索树(特性篇)
Laravel service provider instance tutorial - create a service provider test instance
PHP has its own filtering and escape functions
Direct dry goods, 100% praise
《产品经理必读:五种经典的创新思维模型》的读后感
Personal notes of graphics (4)
ATM system
爬虫(17) - 面试(2) | 爬虫面试题库