当前位置:网站首页>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
边栏推荐
- Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
- 手写ORM(对象关系映射)增删改查
- 高并发介绍及应对
- [shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
- Oracle cursor
- Perceptron model and Application
- 《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
- php优化foreach中的sql查询
- Pointer and string
- Mathematical modeling -- graph and network models and methods (I)
猜你喜欢
![[LeetCode] 数组中的第K个最大元素【215】](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[LeetCode] 数组中的第K个最大元素【215】

Wait to solve the zombie process

Oracle-PL/SQL编程
![[foreign journal] sleep and weight loss](/img/81/42dcfae19e72a0bc761cb7a40fe5d5.jpg)
[foreign journal] sleep and weight loss

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

New feature of go1.18: introduce new netip Network Library

任务和特权级保护

Niuke: Dragon and dungeon games

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

Oracle cursor
随机推荐
Oracle cursor
[LeetCode] 数组中的第K个最大元素【215】
Golang面试整理 三 简历如何书写
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
Perceptron model and Application
[foreign journal] sleep and weight loss
钟薛高回应产品1小时不化:含固体成分 融化不能变成水
Dynamic memory allocation (malloc calloc realloc free)
电商系统微服务架构
Oracle-PL/SQL编程
New feature of go1.18: introduce new netip Network Library
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
Server response status code
Oracle-游标
【洛谷P1541】乌龟棋【DP】
How should programmers write logs
Regular expression (2)
Storage unit conversion
U++ learning notes - relaxation
wait解决僵尸进程