当前位置:网站首页>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
边栏推荐
- Golang的学习路线
- PHP implements querying the data matching the date of birth according to the entered age
- Dynamic memory allocation (malloc calloc realloc free)
- E-commerce system microservice architecture
- 小鹏P7出事故,安全气囊未弹出,这正常吗?
- 杰理之如何测试按键的误触率【篇】
- Commodity information management system (C language document version)
- 原生js添加样式的方法
- Build your own website (22)
- [autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
猜你喜欢

540. Single element in ordered array

电商系统微服务架构
![Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)](/img/b7/0f543829b57cf2f2544efec4910c17.png)
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)

wait解决僵尸进程

Oracle PL / SQL programming

分享 10 个 JS 闭包面试题(图解),进来看看你能答对多少

任务和特权级保护

百度智能云-创建人脸识别应用

世界环境日 | 周大福用心服务推动减碳环保

New feature of go1.18: introduce new netip Network Library
随机推荐
kubernetes 使用主机名将 pod 分配在指定节点上
Golang的学习路线
Perceptron model and Application
[shutter] shutter application theme (themedata | dynamic modification theme)
Storage unit conversion
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
#include errors detected. Please update your includePath.
540. Single element in ordered array
杰理之直接触摸样机的顶针反应不正常【篇】
Market Research - current situation and future development trend of anti-counterfeiting label market
Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
[QT] QT multithreading development - reentrancy and thread safety
NC50965 Largest Rectangle in a Histogram
New feature of go1.18: trylock, which has been tossed n times
[LeetCode] 回文数【9】
NC24325 [USACO 2012 Mar S]Flowerpot
对象与对象变量
Task and privilege level protection
Market Research - current market situation and future development trend of aircraft front wheel steering system
【外刊】睡眠与减肥