leetcode 656. Coin Path
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The integer B denotes that from any place (suppose the index is i) in the array A, you can jump to any one of the place in the array A indexed i+1, i+2, …, i+B if this place can be jumped to. Also, if you step on the index i, you have to pay Ai coins. If Ai is -1, it means you can’t jump to the place indexed i in the array.
Now, you start from the place indexed 1 in the array A, and your aim is to reach the place indexed N using the minimum coins. You need to return the path of indexes (starting from 1 to N) in the array you should take to get to the place indexed N using minimum coins.
If there are multiple paths with the same cost, return the lexicographically smallest such path.
If it's not possible to reach the place indexed N then you need to return an empty array.
Example 1:
Input: [1,2,4,-1,2], 2
Output: [1,3,5]
Example 2:
Input: [1,2,4,-1,2], 1
Output: []
Note:
- Path Pa1, Pa2, ..., Pan is lexicographically smaller than Pb1, Pb2, ..., Pbm, if and only if at the first
iwhere Pai and Pbi differ, Pai < Pbi; when no suchiexists, thenn<m. - A1 >= 0. A2, ..., AN (if exist) will in the range of [-1, 100].
- Length of A is in the range of [1, 1000].
- B is in the range of [1, 100].
dp 输出路径
class Solution {
public:
const int inf = 0x3f3f3f3f;
vector<int> cheapestJump(vector<int>& A, int B) {
vector<int> v;
int n = A.size();
if (n == || A[n - ] == -) return v;
vector<int> dp(n, inf);
vector<int> pre(n, -);
dp[n - ] = A[n - ];
for (int i = n - ; i >= ; --i) {
if (A[i] == -) continue;
for (int j = ; j <= B; ++j) {
if (i + j < n && dp[i + j] + A[i] < dp[i]) {
dp[i] = dp[i + j] + A[i];
pre[i] = i + j;
}
}
}
int m = ;
if (dp[] == inf ) return v;
while (m != -) {
v.push_back(m + );
m = pre[m];
}
return v;
}
};
leetcode 656. Coin Path的更多相关文章
- [LeetCode] 656. Coin Path 硬币路径
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
- LC 656. Coin Path 【lock, Hard】
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] 518. Coin Change 2 硬币找零 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- [LeetCode] Coin Path 硬币路径
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
随机推荐
- Mac 10.10 配置apache
配置php 命令行工具:http://blog.csdn.net/evane1890/article/details/38759073 自从系统从OS X Mavericks 10.9升级到OS X ...
- XTU 二分图和网络流 练习题 J. Drainage Ditches
J. Drainage Ditches Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d Ja ...
- zoj 2818 Root of the Problem
Root of the Problem Time Limit: 2 Seconds Memory Limit: 65536 KB Given positive integers B and ...
- 洛谷P1276 校门外的树(增强版)未完工
题目描述 校门外马路上本来从编号0到L,每一编号的位置都有1棵树.有砍树者每次从编号A到B处连续砍掉每1棵树,就连树苗也不放过(记 0 A B ,含A和B):幸运的是还有植树者每次从编号C到D 中凡是 ...
- [转]制作一个64M的U盘启动盘(mini linux + winpe +dos toolbox)
自己动手定制winpe+各类dos工具箱U盘启动盘+minilinux 由于一个64M老U盘,没什么用,拿来发挥余热.如果U盘够大,可以使用功能更强大的mini linux和带更多工具的winpe.这 ...
- 命令行模式直接下载jar包到本地库
命令行下,直接使用 dependency:get -DrepoUrl=仓库地址 -Dartifact=groupId:artifactId:version[:packaging][:classifi ...
- Codeforces Round Edu 36
A.B.C 略 D(dfs+强连通分量) 题意: 给出一个n(n<=500)点m(m<=100000)边的有向图,问能否通过删去一条边使得该图无环. 分析: 最简单的想法就是枚举一条边删去 ...
- java内存区域和对象的产生
一直被java中内存组成弄的头晕眼花,这里总结下都有哪些,先上图片 程序计数器 小块内存,线程执行字节码的信号指示器,以此获取下一条需要执行的字节码指令,分支,循环,跳转,异常处理,线程恢复都要依赖他 ...
- Ubuntu 16.04关闭Alt+鼠标左键移动窗口(转)
1.打开终端,菜单-编辑-配置文件首选项-命令,勾上“以登录Shell方式运行命令”,重启终端. 2.在终端输入 gsettings get org.gnome.desktop.wm.preferen ...
- Java的finally语句在try或catch中的return语句执行之后还是之前?
import java.util.HashMap; import java.util.Map; public class FinallyDemo1 { public static void main( ...