当前位置:网站首页>每日一题(2022-07-02)——最低加油次数
每日一题(2022-07-02)——最低加油次数
2022-07-04 17:30:00 【用脑袋装水】
871. 最低加油次数
问题描述:
示例 1:
输入:target = 1, startFuel = 1, stations = []
输出:0
解释:我们可以在不加油的情况下到达目的地。
示例 2:
输入:target = 100, startFuel = 1, stations = [[10,100]]
输出:-1
解释:我们无法抵达目的地,甚至无法到达第一个加油站。
示例 3:
输入:target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
输出:2
解释:
我们出发时有 10 升燃料。
我们开车来到距起点 10 英里处的加油站,消耗 10 升燃料。将汽油从 0 升加到 60 升。
然后,我们从 10 英里处的加油站开到 60 英里处的加油站(消耗 50 升燃料),
并将汽油从 10 升加到 50 升。然后我们开车抵达目的地。
我们沿途在1两个加油站停靠,所以返回 2 。
解题思路:
我们不要理解成加油站,就当路上的不是加油站,而是一桶桶的油,每次经过的时候,就把油带上,为了保证最少加油次数,当油不够的时候我们就取身上最大的那桶油加上,这样如果身上没油了,那么就到不了了
简单来说就是一条直线,从起点到终点,距离起点station[i][0]
的位置各有一个容量为station[i][1]
的汽油桶,如果可以到达终点返回最少的加油次数
,否则返回-1
;
题解:
func minRefuelStops(target int, startFuel int, stations [][]int) int {
// 如果初始状态的油量就可与到达,返回0
if target <= startFuel {
return 0
}
remainOil := startFuel // 剩下的汽油
pos := make([]int, 0) // 身上的油桶
visited := make(map[int]bool) // 该位置的油桶是否已经拾起,如果已经拾起,就忽略,否则就带到身上。
ans := 0 // 加油次数
for {
// 如果没有油了
if target > remainOil {
// 遍历路上油桶,拾起路过的油桶
for _, distance := range stations {
// 当前油量 可以路过带上的油桶 并且 之前没有带过这个油桶
if remainOil >= distance[0] && visited[distance[0]] != true {
// 标记已经带在身上的油桶
visited[distance[0]] = true
pos = append(pos, distance[1])
}
}
// 因为 到这里的话 意味着 target > remainOil
// len(pos) == 0:此时如果路上没有可与拾起的油桶
// 代表旅程结束,终点无法到达。
if len(pos) == 0 {
return -1
}
sort.Ints(pos)
remainOil = remainOil + pos[len(pos)-1]
ans++
// 这个油桶用过了 就丢掉
pos = pos[:len(pos)-1]
} else {
return ans
}
}
}
提交结果:
边栏推荐
- [go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
- 基于NCF的多模块协同实例
- How to improve development quality
- 力扣刷题日记/day7/2022.6.29
- Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
- php伪原创api对接方法
- Li Kou brush question diary /day7/6.30
- Machine learning concept drift detection method (Apria)
- Li Kou brush question diary /day1/2022.6.23
- Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
猜你喜欢
An example of multi module collaboration based on NCF
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
Li Kou brush question diary /day2/2022.6.24
Thawte通配符SSL证书提供的类型有哪些
Torchdrug tutorial
Li Kou brush question diary /day3/2022.6.25
Scala basic tutorial -- 17 -- Collection
My colleagues quietly told me that flying Book notification can still play like this
Machine learning concept drift detection method (Apria)
Scala基础教程--20--Akka
随机推荐
Lua emmylua annotation details
基于unity的愤怒的小鸟设计
Halcon template matching
TCP waves twice, have you seen it? What about four handshakes?
删除字符串中出现次数最少的字符【JS,Map排序,正则】
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
【uniapp】uniapp开发app在线预览pdf文件
Mxnet implementation of googlenet (parallel connection network)
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
基于C语言的菜鸟驿站管理系统
What if the self incrementing ID of online MySQL is exhausted?
Scala基础教程--18--集合(二)
中国农科院基因组所汪鸿儒课题组诚邀加入
Neglected problem: test environment configuration management
线上MySQL的自增id用尽怎么办?
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
Li Kou brush question diary /day8/7.1
【云端心声 建议征集】云商店焕新升级:提有效建议,赢海量码豆、华为AI音箱2!
Li Kou brush question diary /day3/2022.6.25
Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组