【Gas Station】cpp
题目:
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
代码:
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
if ( gas.size() != cost.size() ) return -;
int start_station = -;
int tmp_sum = ;
int total = ;
for (size_t i = ; i < gas.size(); ++i)
{
tmp_sum += gas[i] - cost[i];
total += gas[i] - cost[i];
if (tmp_sum<)
{
tmp_sum=;
start_station = i;
}
}
return total>= ? start_station+ : -;
}
};
Tips:
1. 采用贪心算法的思想:维护一个tmp_sum,计算从开始节点到当前节点损耗之差;如果小于零,则直接放弃;否则,继续累加。
2. 最终判断能不能完成行程,需要维护一个total:如果total大于等于0,则判定一定可以走完这趟旅程。这是为什么呢?具体原理可以参见鸽巢原理。
参考资料
贪心算法:http://zh.wikipedia.org/wiki/贪心法
鸽巢原理:http://zh.wikipedia.org/wiki/鴿巢原理
===========================================================
第二次过这道题,一次AC了,大体思路记得比较清楚。写法完全按照自己理解了,与原来的代码已经不太一样了。
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
const int gas_n = gas.size();
const int cost_n = cost.size();
if ( gas_n!= cost_n ) return -;
int total_compare = ;
int curr_gas = ;
int start = ;
for ( int i=; i<cost_n; ++i )
{
if ( curr_gas== ) start = i;
curr_gas = curr_gas + gas[i] - cost[i];
if ( curr_gas< ) curr_gas = ;
total_compare = total_compare + gas[i] - cost[i];
}
return total_compare>= ? start : -;
}
};
【Gas Station】cpp的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
随机推荐
- [转]git修改远程仓库地址
原文链接:http://www.cnblogs.com/lazb/articles/5597878.html 问:Coding远程仓库地址变了,本地git仓库地址如何更新为最新地址 git修改远程仓库 ...
- Eucalyptus(v4.0)系统需求
1.计算需求 Physical Machines: All Eucalyptus components must be installed on physical machines, not virt ...
- Unity光晕剑效果的Shader简单实现
最近遇到了一个需求,想要一种在刀剑上带有光晕的酷炫效果,甚至是还想要有闪烁呼吸效果,于是就写了一个简单的叫LightSwrod的Shader去实现,先上图看看效果吧. 简单展示 这是剑本身的样子 这是 ...
- 关于配置httpd2.4.18+php5.6
关于httpd2.4.18下载之前一直很烦php官网上的点半天看不到下载链接,直到看到这么几句话 大意是Apache http server 不提供二进制版本,只提供源代码.....如果你不能自己编译 ...
- pc-要实现相隔一定时间数据排序变化一次
有时候产品会有这种要求,就是展示的数据三天是正序的,一天是逆序的,解决是: 以某一个时间点为基准点,然后获取当前的时间,然后计算差值,分情况 //专利 JPView : function(Sorder ...
- 写在Github被微软收购之际 - Github的那些另类用法
这几天朋友圈被微软75亿美元收购Github的新闻刷屏了.Jerry也来贡献一篇和Github相关的文章. 这篇文章包含了Jerry平时对于Github的一些另类用法.目录如下: 1. 部署HTML应 ...
- IOS截取部分图片
截取部分图片这么简单: - (void)loadView { [[UIApplication sharedApplication] setStatusBarHidden:YES withAni ...
- MySQL binlog server
从5.6版本开始,可以利用 mysqlbinlog命令把远程机器的日志备份到本地目录,这样就更加方便快捷的实现一个binlog server. 环境介绍:192.168.56.100是备份服务器,19 ...
- hdu-1162 Eddy's picture---浮点数的MST
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 题目大意: 给n个点,求MST权值 解题思路: 直接prim算法 #include<bi ...
- Linux进程的虚拟存储器知识点
http://blog.csdn.net/yxccc_914/article/details/52665713 用libreoffice画表真时有点蛋疼,效率很低.. 深入理解计算机系统->虚拟 ...