【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 ...
随机推荐
- Java图形界面开发—列出指定目录
代码如下: package com.packageTemp; import javax.swing.*; import java.awt.*; import java.awt.event.*; imp ...
- [转]git修改远程仓库地址
原文链接:http://www.cnblogs.com/lazb/articles/5597878.html 问:Coding远程仓库地址变了,本地git仓库地址如何更新为最新地址 git修改远程仓库 ...
- Mysql数据库操作语句总结(二)
Mysql字符串字段判断是否包含字符串的3中方法 方法一: select * from user where email like "%b@email.com%";// 这个理解起 ...
- pta 编程题7 List Leaves
其它pta数据结构编程题请参见:pta 这次的编程作业要求从上到下,从左到右输出一棵树的叶子节点,即树的层序遍历,用队列的方式来实现. 注意enqueue和dequeue函数参数为Queue & ...
- URL跨项目调用方法,获取返回的json值,并解析
package com.mshc.util; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...
- 【洛谷4657】[CEOI2017] Chase(一个玄学的树形DP)
点此看题面 大致题意: 有一棵树,树上编号为\(i\)的节点上有\(F_i\)个铁球,逃亡者有\(V\)个磁铁,当他在某个节点放下磁铁时,与这个节点相邻的所有节点上的铁球都会被吸引到这个节点.然后一个 ...
- 【BZOJ1088】[SCOI2005] 扫雷Mine(分类讨论)
点此看题面 大致题意: 给你一个\(2*n\)的扫雷棋盘,现让你根据第二列的信息确定第一列有多少种摆法. 扫雷性质 听说这是一道动态规划+数学题. 其实,根据扫雷游戏的某个性质,只要确定了第一个格子是 ...
- Flutter Json序列号和反序列化遇到问题 Missing "part 'xxx.g.dart';"
/** * * 1.@JsonSerializable() 这是表示告诉编译器这个类是需要生成Model类的 * 2,@JsonKey 由于服务器返回的部分数据名称在Dart语言中是不被允许的, * ...
- 2018.6.13 Java语言基础复习总结
Java语言基础与面向对象编程实践 第一章 初识Java 1.1机器语言 机器语言是指一台计算机全部的指令集合.机器语言室友0和1组成的二进制数,是一串串由0和1组成的指令序列,可将这些指令序列交给计 ...
- Deep Learning 优化方法总结
Stochastic Gradient Descent (SGD) SGD的参数 在使用随机梯度下降(SGD)的学习方法时,一般来说有以下几个可供调节的参数: Learning Rate 学习率 We ...