【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 (无 ...
随机推荐
- 覆盖alert对话框-自制Jquery.alert插件
Javascript 代码: (function ($) { 'use strict'; window.alert = $.alert = function (msg) { var defaultOp ...
- SAP云平台,Netweaver,Kubernetes和C4C的用户和角色关系
SAP云平台 Netweaver 同SAP云平台一样,在事务码PFCG里维护角色: 然后在事务码SU01里将多个角色分配给用户: Kubernetes Kubernetes在1.3版本中发布了alph ...
- (转)MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
http://blog.csdn.net/yerenyuan_pku/article/details/71893689 前面对MyBatis框架的学习中,我们对Mapper.xml映射文件多少有些了解 ...
- github+hexo+themes搭建简易个性主题博客
0x00 install Node.js and git 安装Node.js:http://www.runoob.com/nodejs/nodejs-install-setup.html 安装git ...
- HTTP协议重定向
HTTP重定向:服务器无法处理浏览器发送过来的请求(request),服务器告诉浏览器跳转到可以处理请求的url上.(浏览器会自动访问该URL地址,以至于用户无法分辨是否重定向了.) 重定向的返回码3 ...
- javaEE(16)_Servlet监听器
一.监听器原理 1.监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行. 2.监听器典型案例 ...
- Nginx代理tcp端口实现负载均衡
Nginx代理tcp端口实现负载均衡 1.修改配置文件 vi /etc/nginx/nginx.conf 添加如下配置: stream { ###XXX upstream notify { has ...
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
Person类中的各种属性写法如下: package com.swift.person; import java.util.Arrays; import java.util.List; import ...
- iOS 面试集锦
是第一篇: 1.Difference between shallow copy and deep copy? 浅复制和深复制的区别? 答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身. 深层 ...
- js之数组知识
一.数组的定义(来源于Array.prototype) 1.构造函数方法: (1)var arr = new Array();//没有参数等价于 var arr = []; (2)var arr = ...