当前位置:网站首页>[dynamic planning]dp27 jumping game (II) - medium
[dynamic planning]dp27 jumping game (II) - medium
2022-07-19 12:50:00 【51CTO】
DP27 Jumping game ( Two )
describe
Given an array of nonnegative integers nums, Assume that the initial subscript is 0 The location of , Each element in the array represents the maximum length of the next hop , If you can skip to the last position of the array , Please find out the most integral you can get in the jumping path .
1. If you can jump to the last position of the array , To calculate the integral obtained , Otherwise, the integral value is -1
2. If you can't jump ( That is, the length of the array is 0), Please also return to -1
3. The data guarantees that the returned result will not exceed the shaping range , That is, it will not exceed
Data range :
Input description :
Enter a positive integer in the first line n Represents an array nums The length of
Second line input n It's an integer , Represents an array nums The values of all elements of
Output description :
Output the most points you can get
Example 1
Input :
Output :
explain :
Example 2
Input :
Output :
explain :
Example 3
Input :
Output :
explain :
Answer key
Link to my Niuke website ~~
This question is a variant of jumping game one . Require maximum points , In fact, it is to find the maximum sum of all points on the path . Now we know the following conditions ( If there is any solution ):
- Finally, it must stop at n-1 coordinate
- If the coordinates of a node are i, The value of this coordinate is nums[i], For any greater than i The positive integer k, If you want to be able to jump to this position , There has to be i+nums[i]>=k
According to the above conditions, we can deduce in reverse , Use the greedy method to solve . Suppose we are in position k On , Then if you can from the position x < k Jump up to the position k On , We can nums[x] Add up to the total integral .
So how to determine whether it can be from x Jump up to k How about it ? According to paragraph above 2 Point analysis , as long as x+nums[x] >= k that will do . That is to say nums[x] >= k - x, and k For the last stop ,x For the current coordinates we want to judge .k Starting at n-1,x Starting at n-2, As long as the above equation can be satisfied, it will x--, And then update k value . We don't use coordinates here , But use from k and x Between gap To solve it .gap Indicates the current coordinates i And the previous coordinate k Difference between .
The code is as follows :
边栏推荐
猜你喜欢

2022 global developer salary exposure: China ranks 19th, with an average annual salary of $23790

音频常见端子剖析图---再也不会搞错了

结构体内存对齐、位段、联合

基于STM32设计的云端健康管理系统(采用阿里云物联网平台)

Acwing4405. Statistical submatrix

mysql中对于数据库的基本操作

Create a custom palette in swiftui color tutorial

市场“不确定性”中的投资逻辑 2020-03-18

Yunxi and Tencent cloud have reached a strategic cooperation to accelerate the expansion of the global live broadcast market

The difference and use between get request and post request
随机推荐
GET 请求和 POST 请求的区别和使用
HICP first day notes
Can you view MySQL data table structure in two ways?
The leader of the new generation of cloud database -- AWS
脉冲函数、阶跃函数和斜坡函数及脉冲响应
国产API工具哪家强?原来是它…
S32K148_CAN驱动(裸机开发)
Hcip fourth day notes
RingBuffer
Binary tree 2-symmetry recursion problem
Pytorch version: yolov4 integrating attention and mobilenet
超声波传感器(CH101&ch201) - Ⅱ
Opencv based on DLCO descriptor matching
ros(26):ros::Time::now(),ros::Duration,toSec(),toNSec(); Calculate program execution time
R language -- principle of Cox model calibration curve (I) data source
mysql如何删除数据表,被关联的数据表如何删除呢
运维小白成长记—架构第6周
Yu Meimei, Ji Gongdu
Relationship and difference between wav and PCM
Deep learning parameter initialization (II) Kaiming initialization with code