当前位置:网站首页>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
边栏推荐
- [001] [arm-cortex-m3/4] internal register
- PHP implements querying the data matching the date of birth according to the entered age
- Market Research - current market situation and future development trend of high tibial osteotomy plate
- [shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
- Regular expression (2)
- [autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
- Market Research - current market situation and future development trend of marine wet exhaust hose
- 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
- Baidu AI Cloud - create a face recognition application
猜你喜欢
Jatpack------LiveData
Oracle PL / SQL programming
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
大话云原生之负载均衡篇-小饭馆客流量变大了
NC24325 [USACO 2012 Mar S]Flowerpot
[LeetCode] 数组中的第K个最大元素【215】
Mathematical modeling -- graph and network models and methods (I)
[shutter] shutter application theme (themedata | dynamic modification theme)
SimpleITK使用——4. 奇怪的問題
SimpleITK使用——3. 常见操作
随机推荐
送给即将工作的自己
Server response status code
Introduction to database system Chapter 1 short answer questions - how was the final exam?
杰理之如何测试按键的误触率【篇】
Market Research - current market situation and future development trend of high tibial osteotomy plate
杰理之样机无触摸,拆机之后重新安装变正常【篇】
Oracle PL / SQL programming
`${}`的用法
New feature of go1.18: introduce new netip Network Library
Build your own website (22)
php实现根据输入的年龄查询出生日期符合的数据
Dynamic memory allocation (malloc calloc realloc free)
JS solution for obtaining the width and height of hidden elements whose display is none
【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
小鹏P7出事故,安全气囊未弹出,这正常吗?
[shutter] shutter application theme (themedata | dynamic modification theme)
建立自己的网站(22)
傑理之修改不需要長按開機功能【篇】
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
How should programmers write logs