当前位置:网站首页>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
边栏推荐
- 加油站[问题分析->问题转换->贪心]
- Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
- php优化foreach中的sql查询
- 'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
- Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
- 基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
- Pointer array parameter passing, pointer parameter passing
- NC24325 [USACO 2012 Mar S]Flowerpot
- [QT] QT multithreading development - reentrancy and thread safety
- Jerry's modification does not require long press the boot function [chapter]
猜你喜欢

`${}`的用法
![[001] [arm-cortex-m3/4] internal register](/img/49/a0eceac1a67267216dd9b2566033a1.jpg)
[001] [arm-cortex-m3/4] internal register

图形视图框架

Socket socket c/s end process

任务和特权级保护
![[LeetCode] 反转字符串中的单词 III【557】](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[LeetCode] 反转字符串中的单词 III【557】

Oracle PL / SQL programming

Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed

Pointer and string

uniapp微信登录返显用户名和头像
随机推荐
NC24325 [USACO 2012 Mar S]Flowerpot
Niuke: Dragon and dungeon games
NC50965 Largest Rectangle in a Histogram
kubernetes 使用主机名将 pod 分配在指定节点上
杰理之充电拔出,无法触摸开机【篇】
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
What is the'function'keyword used in some bash scripts- What is the 'function' keyword used in some bash scripts?
建立自己的网站(22)
杰理之、产线装配环节【篇】
Golang的学习路线
540. Single element in ordered array
Market Research - current market situation and future development trend of aircraft front wheel steering system
NC24325 [USACO 2012 Mar S]Flowerpot
phpcms实现订单直接支付宝支付功能
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
佩服,竟然有人把高等数学这么晦涩难懂的科目,讲解得如此通俗易懂
位的高阶运算
Golang面试整理 三 简历如何书写
Market Research - current market situation and future development trend of third-party data platform
杰理之快速触摸不响应问题【篇】