当前位置:网站首页>One question per day (2022-07-02) - Minimum refueling times
One question per day (2022-07-02) - Minimum refueling times
2022-07-04 19:11:00 【Fill your head with water】
List of articles
871. Minimum refueling times
Problem description :
Example 1:
Input :target = 1, startFuel = 1, stations = []
Output :0
explain : We can get to our destination without refueling .
Example 2:
Input :target = 100, startFuel = 1, stations = [[10,100]]
Output :-1
explain : We can't get to our destination , Not even getting to the first gas station .
Example 3:
Input :target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
Output :2
explain :
We set out with 10 Liter fuel .
We drove to the starting point 10 The gas station at mile , Consume 10 Liter fuel . Take the gas from 0 To add to 60 l .
then , We from 10 The gas station is miles away 60 The gas station at mile ( Consume 50 Liter fuel ),
And take the gas from 10 To add to 50 l . Then we drove to our destination .
We are on the way 1 Two gas stations stop , So back 2 .
Their thinking :
Let's not understand it as a gas station , It's not a gas station on the road , But barrels of oil , Every time I pass , Just take the oil , In order to ensure the minimum refueling times , When the oil is not enough, we take the largest barrel of oil and add , So if you run out of oil , Then it won't arrive
Simply put, it's a straight line , From start to end , Distance from starting pointstation[i][0]
Each location has a capacity ofstation[i][1]
Gasoline barrel , If you can reach the end, returnMinimum refueling times
, Otherwise return to-1
;
Answer key :
func minRefuelStops(target int, startFuel int, stations [][]int) int {
// If the oil quantity in the initial state can reach , return 0
if target <= startFuel {
return 0
}
remainOil := startFuel // The rest of the gasoline
pos := make([]int, 0) // The oil bucket on your body
visited := make(map[int]bool) // Whether the oil drum at this position has been picked up , If it has been picked up , Just ignore it , Otherwise, take it with you .
ans := 0 // Refueling times
for {
// If there is no oil
if target > remainOil {
// Traverse the oil bucket on the road , Pick up passing oil barrels
for _, distance := range stations {
// Current oil volume You can pass the oil bucket you brought also I haven't brought this oil bucket before
if remainOil >= distance[0] && visited[distance[0]] != true {
// The oil bucket with the mark on your body
visited[distance[0]] = true
pos = append(pos, distance[1])
}
}
// because When we get here signify target > remainOil
// len(pos) == 0: At this time, if there is no oil bucket to pick up on the road
// Represents the end of the journey , The destination cannot be reached .
if len(pos) == 0 {
return -1
}
sort.Ints(pos)
remainOil = remainOil + pos[len(pos)-1]
ans++
// This oil bucket has been used Just throw it away
pos = pos[:len(pos)-1]
} else {
return ans
}
}
}
Submit results :
边栏推荐
- Digital "new" operation and maintenance of energy industry
- 【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
- A method of using tree LSTM reinforcement learning for connection sequence selection
- 使用FTP
- Cache é JSON uses JSON adapters
- Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- 自由小兵儿
- 力扣刷题日记/day6/6.28
- 神经网络物联网应用技术学什么
猜你喜欢
Scala基础教程--20--Akka
Mxnet implementation of googlenet (parallel connection network)
How is the entered query SQL statement executed?
Angry bird design based on unity
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现
Journal des problèmes de brosse à boutons de force / day6 / 6.28
力扣刷题日记/day8/7.1
正则替换【JS,正则表达式】
What types of Thawte wildcard SSL certificates provide
随机推荐
力扣刷题日记/day2/2022.6.24
Li Kou brush question diary /day1/2022.6.23
读写关闭的channel是啥后果?
神经网络物联网应用技术就业前景【欢迎补充】
Scala basic tutorial -- 17 -- Collection
Scala基础教程--19--Actor
神经网络物联网应用技术学什么
How is the entered query SQL statement executed?
Build your own website (15)
神经网络物联网是什么意思通俗的解释
力扣刷题日记/day3/2022.6.25
Wireshark抓包TLS协议栏显示版本不一致问题
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Li Kou brush question diary /day4/6.26
Caché WebSocket
Interpretation of SIGMOD '22 hiengine paper
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0