当前位置:网站首页>Gas station [problem analysis - > problem conversion - > greed]
Gas station [problem analysis - > problem conversion - > greed]
2022-07-02 22:50:00 【REN_ Linsen】
Preface
The solution to many problems is not obvious / Combination of multiple knowledge points , Especially the scene question , Then it is more necessary to extract key problems , And analyzed .
Analyze the core requirements of the topic , We need to make appropriate knowledge contact / Combine / improvement , To complete the requirements .
One 、 Gas station

Two 、 logic analysis -> greedy
package everyday.greed;
// Gas station
public class CanCompleteCircuit {
/* target: Choose a place to start walking , Arrive at each location , You can get the amount of oil at each location ( Can be installed , There is no upper limit ), Without taking one step, a certain amount of fuel will be consumed ( It can be understood as the distance ) You can see the amount of oil in each position , You should go from the place with the largest cumulative fuel . */
public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
// Calculate the difference between the amount of oil obtained at each position and the amount of oil it takes to take one step .
int[] gap = new int[n];
int surplus = 0;// The amount of oil left
for (int i = 0; i < n; i++) {
gap[i] = gas[i] - cost[i];
surplus += gap[i];
}
// As long as the total amount of fuel Greater than or equal to Total consumption , Then we must find a starting point , You can walk around , On the contrary, you can't find .
// Greedy thinking , One road has negative , Because the amount obtained is greater than or equal to the consumption , So the sum of the remaining half way must be positive , And positive + negative >=0
if (surplus < 0) return -1;
// Double pointer to find the starting point .
int begin = 0, sum = 0;
for (int i = 0; i < n; i++) {
if ((sum += gap[i]) < 0) {
begin = i + 1;
sum = 0;
}
}
return begin;
}
}
summary
1) Not all questions are to examine an obvious knowledge point , There are many problems that need to be analyzed / mining , Can be converted into relevant knowledge points .
2) Greedy thoughts .
reference
边栏推荐
- PHP微信抢红包的算法
- Zhong Xuegao responded that the product will not melt for 1 hour: it contains solid components and cannot melt into water
- 基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
- Socket套接字C/S端流程
- 杰理之快速触摸不响应问题【篇】
- Market Research - current situation and future development trend of cell-based seafood market
- 存储单位换算
- UE4 UI adaptive screen
- Socket socket c/s end process
- [ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
猜你喜欢

附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)

基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习

It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #

Jatpack------LiveData

PMP项目整合管理

Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
![[LeetCode] 反转字符串中的单词 III【557】](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[LeetCode] 反转字符串中的单词 III【557】

UE4 game architecture learning notes

Struct, bit segment, enumeration, union
![[shutter] shutter application theme (themedata | dynamic modification theme)](/img/77/6b0082368943aee7108ac550141f28.gif)
[shutter] shutter application theme (themedata | dynamic modification theme)
随机推荐
Market Research - current situation and future development trend of carob chocolate market
Source code analysis - lightweight asynchronous crawler framework Ruia
Golang的学习路线
Perceptron model and Application
附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
Market Research - current situation and future development trend of herringbone gear Market
What is the'function'keyword used in some bash scripts- What is the 'function' keyword used in some bash scripts?
Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
U++ 学习笔记 ----松弛
建立自己的网站(22)
Market Research - current market situation and future development trend of aircraft front wheel steering system
原生js添加样式的方法
LxC terminal login method
杰理之样机无触摸,拆机之后重新安装变正常【篇】
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
[001] [arm-cortex-m3/4] internal register
Server response status code
Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
图形视图框架