leetcode134 Gas Station
思路:
https://leetcode.com/problems/gas-station/discuss/269604/Java-Greedy-thought-process
关键是要想清楚如果从加油站A出发到不了B,那么从A到B之间的任何一个加油站出发也到不了B。
实现:
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost)
{
int n = gas.size(), sum = , start = , ans = -;
for (int i = ; i < * n; i++)
{
sum += gas[i % n];
sum -= cost[i % n];
if (sum < ) { sum = ; start = i + ; }
if (i - start == n) { ans = start; break; }
}
return ans;
}
};
int main()
{
// int g[] = {1, 2, 3, 4, 5};
// int c[] = {3, 4, 5, 1, 2};
// int g[] = {2, 3, 4};
// int c[] = {3, 4, 3};
int g[] = {, , , , };
int c[] = {, , , , };
vector<int> gas(begin(g), end(g));
vector<int> cost(begin(c), end(c));
cout << Solution().canCompleteCircuit(gas, cost) << endl;
return ;
}
leetcode134 Gas Station的更多相关文章
- Leetcode134. Gas Station加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...
- [Swift]LeetCode134. 加油站 | Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- LeetCode134:Gas Station
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- Leetcode 134 Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
随机推荐
- COM组件开发实践(一)
Preface 因为项目需要,开始从事ActiveX方面的工作,看了一些资料,可惜都是些COM原理方面的,没有切合实际动手的东西,在CodeProject上读完David Marcionek的文章[1 ...
- 动态规划专题 01背包问题详解 HDU 2546 饭卡
我以此题为例,详细分析01背包问题,希望该题能够为大家对01背包问题的理解有所帮助,对这篇博文有什么问题可以向我提问,一同进步^_^ 饭卡 Time Limit: 5000/1000 MS (Java ...
- Hibernate的session.createSQLQuery的几种查询方式
当我们用HQL进行子查询的时候,如select * from Tree where pid in (select id from Tree,此时HIBERANTE就会报错,说什么*号错误之类的.但如果 ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- CodeForces 1110G. Tree-Tac-Toe
题目简述:给定$n$个节点的树,其中一些节点被染成了白色(其余节点未染色).黑白双方博弈,白先动.轮到黑(白)方时,选择树上的一个未染色的节点并将其染成黑(白)色.率先达成三连色(即存在三个节点$a, ...
- Attributes.Add用途与用法
Attributes.Add("javascript事件","javascript语句"); 如: this.TextBox1.Attributes.add(& ...
- LeetCode: 620 Not Boring Movies(easy)
题目: X city opened a new cinema, many people would like to go to this cinema. The cinema also gives o ...
- LeetCode: 557Reverse Words in a String III(easy)
题目: Given a string, you need to reverse the order of characters in each word within a sentence while ...
- PhpStrom之添加文件夹至左侧目录树
1.打开编辑器,点击工具栏 File,并选择Open (File -> Open) 2.选择需要添加的文件夹路径,点击 OK 3.点击OK后弹出下图窗口(第一个选项:Open in new wi ...
- unity2d 动画
1 资源test.jpg(如下)放入Resources文件夹 2 切割图片 点击图片,在inspector中,选择Texture Type为Sprite(2D and UI),然后点击Sprite E ...