当前位置:网站首页>LeetCode 1696. Jumping game VI daily question
LeetCode 1696. Jumping game VI daily question
2022-07-07 16:58:00 【@Little safflower】
Problem description
I'll give you a subscript from 0 The starting array of integers nums And an integer k .
At first you were subscribing 0 It's about . Each step , You can jump forward at most k Step , But you can't jump out of the bounds of an array . in other words , You can start with the subscript i Jump to the [i + 1, min(n - 1, i + k)] contain Any position of the two endpoints .
Your goal is to get to the last position in the array ( Subscript to be n - 1 ), Yours score Is the sum of all the numbers passed .
Please return to what you can get Maximum score .
Example 1:
Input :nums = [1,-1,-2,4,-7,3], k = 2
Output :7
explain : You can choose subsequences [1,-1,4,3] ( The numbers in bold above ), And for 7 .
Example 2:Input :nums = [10,-5,-2,4,0,3], k = 3
Output :17
explain : You can choose subsequences [10,4,3] ( It's bold ), And for 17 .
Example 3:Input :nums = [1,-5,-20,4,-1,3,-6,-3], k = 2
Output :0
Tips :
1 <= nums.length, k <= 105
-104 <= nums[i] <= 104source : Power button (LeetCode)
link :https://leetcode.cn/problems/jump-game-vi
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
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++){
// jump k Time
for(int j = i + 1;j < n && j <= i + k;j++){
// Score after jump
int nextScore = dp[i] + nums[j];
// Update to a higher score after jumping
if(nextScore > dp[j]) dp[j] = nextScore;
// Filter
if(dp[j] >= dp[i]) break;
}
}
return dp[n - 1];
}
}
边栏推荐
猜你喜欢
水平垂直居中 方法 和兼容
两类更新丢失及解决办法
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
浅浅理解.net core的路由
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
Imitate the choice of enterprise wechat conference room
运算符
二叉搜索树(特性篇)
最新高频Android面试题目分享,带你一起探究Android事件分发机制
字节跳动Android面试,知识点总结+面试题解析
随机推荐
[designmode] flyweight pattern
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
AutoLISP series (1): function function 1
一文读懂数仓中的pg_stat
QT 图片背景色像素处理法
[Android -- data storage] use SQLite to store data
null == undefined
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
字节跳动高工面试,轻松入门flutter
预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
字节跳动Android面试,知识点总结+面试题解析
谈谈 SAP 系统的权限管控和事务记录功能的实现
Advanced C language -- function pointer
LeetCode 300. 最长递增子序列 每日一题
Prometheus API deletes all data of a specified job
Tidb cannot start after modifying the configuration file
skimage学习(1)
Geoserver2.18 series (5): connect to SQLSERVER database
LeetCode 213. 打家劫舍 II 每日一题
值得一看,面试考点与面试技巧