当前位置:网站首页>加油站[问题分析->问题转换->贪心]
加油站[问题分析->问题转换->贪心]
2022-07-02 22:07:00 【REN_林森】
前言
很多题需要用到的解法不明显/多种知识点组合,尤其是场景题,那就更需要提取关键问题,并进行分析了。
分析题目的核心需求,需要做适当的知识点联系/组合/改进,才能够完成需求。
一、加油站

二、逻辑分析->贪心
package everyday.greed;
// 加油站
public class CanCompleteCircuit {
/* target:选一个地点开始走,到达每个地点时,可以得到每个地点的油量(都可装,无上限),而没走一步就会耗掉一定油量(可理解为路程量) 可以看看每个位置的油量,应该从可累计油量最大的地方走。 */
public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
// 计算每个位置得到的油数和走一步要花费的油数差。
int[] gap = new int[n];
int surplus = 0;// 剩余油量
for (int i = 0; i < n; i++) {
gap[i] = gas[i] - cost[i];
surplus += gap[i];
}
// 只要总得油量 大于等于 总消耗量,那么就一定能找到一个起点,可走一圈,反之则找不到。
// 贪心思维,一条路有负,因为得到量大于等于消耗量,所以剩下半条路和一定为正,且正+负>=0
if (surplus < 0) return -1;
// 双指针找起点。
int begin = 0, sum = 0;
for (int i = 0; i < n; i++) {
if ((sum += gap[i]) < 0) {
begin = i + 1;
sum = 0;
}
}
return begin;
}
}
总结
1)不是所有题就是考察一个明显的知识点,有很多题都是需要分析/挖掘,才能够转换成相关知识点。
2)贪心思想。
参考文献
[1] LeetCode 加油站
边栏推荐
- It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
- #include errors detected. Please update your includePath.
- SimpleITK使用——4. 奇怪的問題
- [error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
- Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
- Market Research - current market situation and future development trend of night vision goggles for pilots
- 送给即将工作的自己
- JS获取display为none的隐藏元素的宽度和高度的解决方案
- 附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
- 牛客网:最大子矩阵
猜你喜欢

New feature of go1.18: trylock, which has been tossed n times

NC50965 Largest Rectangle in a Histogram

数学建模——图与网络模型及方法(一)

建立自己的网站(22)
![[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)](/img/f7/cb41d159e5c5ef3f4f1b9468d52ccc.jpg)
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)

小鹏P7出事故,安全气囊未弹出,这正常吗?

杰理之、产线装配环节【篇】

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

Utilisation de simpletk - 4. Question étrange

对象与对象变量
随机推荐
[LeetCode] 多数元素【169】
Build your own website (22)
Promise optimized callback hell
Market Research - current market situation and future development trend of aircraft audio control panel system
Source code analysis - lightweight asynchronous crawler framework Ruia
NC24325 [USACO 2012 Mar S]Flowerpot
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
Perceptron model and Application
杰理之内置短按再长按,不管长按多长时间都是短按【篇】
PHP wechat red packet grabbing algorithm
Objects and object variables
图形视图框架
数据库系统概论第一章简答题-期末考得怎么样?
Pointer and string
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
#include errors detected. Please update your includePath.
Pointer array parameter passing, pointer parameter passing
Oracle-游标
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】