【leetcode最短路】818. Race Car
https://leetcode.com/problems/race-car/description/
1. BFS剪枝 0<=current position<=2*target。为什么2*target有点不太明白
class Solution {
public:
int dp[];
struct Node{
int pos;
int speed;
int step;
Node(int p,int s,int ss){
pos=p;
speed=s;
step=ss;
}
};
int racecar(int target) {
queue<Node> Q;
set<pair<int,int> > vis;
Q.push(Node(,,));
vis.insert({,});
while(!Q.empty()){
Node q=Q.front();
Q.pop();
if(q.pos==target) return q.step;
if(vis.find({q.pos+q.speed,q.speed*})==vis.end()&&q.pos+q.speed>=&&q.pos+q.speed<=*target){
vis.insert({q.pos+q.speed,q.speed*});
Q.push(Node(q.pos+q.speed,q.speed*,q.step+));
}
if(vis.find({q.pos,q.speed>?-:})==vis.end()){
vis.insert({q.pos,q.speed>?-:});
Q.push(Node(q.pos,q.speed>?-:,q.step+));
}
}
return -;
}
};
2. Dijkstra,官方题解,没太看懂
3. DP
class Solution {
public:
int dp[];
int racecar(int target) {
if(dp[target]!=) return dp[target];
int bound;
for(int i=;i<;i++){
if((<<i)-==target) return dp[target]=i;
if((<<i)->target){
bound=i;
break;
}
}
dp[target]=bound++racecar((<<bound)--target);
for(int i=;i<bound-;i++){
dp[target]=min(dp[target],bound-++i++racecar(target-(((<<(bound-))-)-((<<i)-))));
}
return dp[target];
}
};
【leetcode最短路】818. Race Car的更多相关文章
- LeetCode 818. Race Car
原题链接在这里:https://leetcode.com/problems/race-car/ 题目: Your car starts at position 0 and speed +1 on an ...
- 818. Race Car
Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negati ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- [LeetCode] Race Car 赛车
Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negati ...
- LeetCode Triangle 三角形(最短路)
题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...
- [LeetCode] Network Delay Time 网络延迟时间——最短路算法 Bellman-Ford(DP) 和 dijkstra(本质上就是BFS的迭代变种)
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...
- leetCode 64.Minimum Path Sum (最短路) 解题思路和方法
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
随机推荐
- 使用 PHPUnit 和 Selenium 进行测试
适用于 PHP 的 NetBeans IDE 支持 PHPUnit 自动测试.通过 PHPUnit,NetBeans IDE 可为 PHP 提供代码覆盖率,这与 IDE 为 Python 提供的代码覆 ...
- 【Web应用-网络连接】Azure Web 应用对外连接数上限分析
在 Azure Web 应用中发起大量外部连接操作时,需要考虑已经建立了多少外部连接.当超过最大对外连接数时,Azure Web 应用将会产生套接字异常.Azure Web 应用对于各个级别的实例,对 ...
- Spring Boot: Spring Starter Project
好久没有创建过新项目,楼主发现Spring Boot项目创建失败了!!! 其中有两处错误: [图一不知道是哪里错,果断删掉重输入一次.成功进入下一步 其余步骤也没有错误,然而 最后一步失败了,如图 ...
- 关键字: on
关键字: on 数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成 ...
- 中国剩余定理&Lucas定理&按位与——hdu 5446
链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...
- Kruskal与Prim
一.最小生成树 在无向图中,连通且不含圈的图称为树(Tree).给定无向图G=(V,E),连接G中所有点,且边集是E的子集的树称为G的生成树(Spanning Tree),而权值最小的生成树称为最小生 ...
- 利用python进行数据分析3_Pandas的数据结构
Series #通过list构建Series ser_obj=pd.Series(range(10,20)) print(type(ser_obj))#<class 'pandas.core.s ...
- 行内元素的padding和margin是否无效
html中元素分为三种:块级元素.行内元素(也叫内联元素),内联块级元素. 常用块级元素:<div>.<p>.<h1>...<h6>.<ol> ...
- C01 C语言基础
目录 C语言简史及特点 C语言开发环境 C语言程序结构 C语言基本输入输出函数 编译 软件类型 C语言简要及特点 什么是计算机语言 计算机语言是用于人与计算机之间通讯的语言. 计算机遵照接收到的计算机 ...
- 6.python
最早的'密码本' ascii 涵盖了英文字母大小写,特殊字符,数字.01010101ascii 只能表示256种可能,太少,创办了万国码 unicode 16表示一个字符不行,32位表示一个字符. A ...