题目

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的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. 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~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

随机推荐

  1. Redis集群维护、运营的相关命令与工具介绍

    Redis集群的搭建.维护.运营的相关命令与工具介绍 一.概述 此教程主要介绍redis集群的搭建(Linux),集群命令的使用,redis-trib.rb工具的使用,此工具是ruby语言写的,用于集 ...

  2. 利用binlog2sql解析mysqlbinlog row行日志

    binlog2sql 项目作者:曹单锋 github项目地址:https://github.com/danfengcao/binlog2sql 也可在github.com上搜索“binlog2sql” ...

  3. IIS7.5配置自动添加www 及 限制通过IP访问web

    IIS7.5配置自动添加www 方法 新建一个站点2(主机名为不带www的站点),将其重定向至带www的URL即可. 注意以下几点 站点2不可与站点1的路径一致,否则会导致站点1也会添加同样的重定向, ...

  4. 关于android界面菜单,project显示问题

    刚用android studio不久,遇到了一个问题:界面菜单project不小心被关掉了,百度各种搜索无结果.我想大多数人都遇到过类似问题,此类问题其实很简单但是对于初学者来说就有点苦恼了.所以在此 ...

  5. 浅谈KD-Tree

    前言 \(KD-Tree\)是一个十分神奇的东西,其实本质上类似于一个\(K\)维的二叉搜索树. 核心思想 \(KD-Tree\)的核心思想与\(BST\)是差不多的(插入等操作也都基本上一样). 唯 ...

  6. 2018.6.21 css的应用---注册表格

    参与css样式表格的注册表单 <!DOCTYPE html> <head> <meta charset="UTF-8" /> <meta ...

  7. SecureCRT连接Linux

    一.服务端 1.在linux上安装openssh-server服务,并确认打开了22监听端口 1)安装openssh-server:apt-get install openssh-server 2)查 ...

  8. spring xml 配置文件中标签的解析

    一个springmvc配置文件的例子为: <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  9. 小波变换(wavelet transform)的通俗解释(一)

    小波变换 小波,一个神奇的波,可长可短可胖可瘦(伸缩平移),当去学习小波的时候,第一个首先要做的就是回顾傅立叶变换(又回来了,唉),因为他们都是频率变换的方法,而傅立叶变换是最入门的,也是最先了解的, ...

  10. servlet从服务器磁盘文件读出到浏览器显示,中文乱码问题,不要忘记在输入流和输出流都要设置编码格式,否则一个地方没设置不统一就会各种乱码

    package com.swift; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOE ...