当前位置:网站首页>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
边栏推荐
- UE4 UI自适应屏幕
- 【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
- 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
- Market Research - current situation and future development trend of cell-based seafood market
- U++ learning notes - relaxation
- 540. Single element in ordered array
- 建立自己的网站(22)
- U++ 原始内存 学习笔记
- Jatpack------LiveData
- 杰理之内置短按再长按,不管长按多长时间都是短按【篇】
猜你喜欢

Build your own website (22)

Socket套接字C/S端流程

Oracle-PL/SQL编程

Oracle cursor

大话云原生之负载均衡篇-小饭馆客流量变大了

建立自己的网站(22)
![[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')](/img/02/67448df1817e8b34b654722df8ecd4.jpg)
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
![[LeetCode] 多数元素【169】](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[LeetCode] 多数元素【169】

Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts

NC50965 Largest Rectangle in a Histogram
随机推荐
Market Research - current market situation and future development trend of intravenous injection (IV) bottles
go 条件变量
Hanoi Tower problem
[001] [arm-cortex-m3/4] internal register
uniapp微信登录返显用户名和头像
U++ learning note pile
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
[LeetCode] 反转字符串中的单词 III【557】
UE4 游戏架构 学习笔记
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
【外刊】睡眠与减肥
[QT] QT multithreading development - reentrancy and thread safety
世界环境日 | 周大福用心服务推动减碳环保
U++ 学习笔记 ----松弛
原生js添加样式的方法
`${}`的用法
NC24325 [USACO 2012 Mar S]Flowerpot
[foreign journal] sleep and weight loss
分享 10 个 JS 闭包面试题(图解),进来看看你能答对多少
Jerry's modification does not require long press the boot function [chapter]