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.

解题思路1,o(n):

选择gas[0]和cost[0]作为起始点,设current_gas_left表示当前剩余的汽油量,则初始current_gas_left = gas[0] - cost[0];

①如果gas[0] - cost[0] < 0,说明此站不是起点。但是数组中至少存在一点i,以i为起始,在经历0点时,current_gas_left不为负,

因此想象数组是一个圈,从0向前(n-1,n-2,...)寻找,即current_gas_left += gas[i] - cost[i],i从n-1取值,不断递减。

直到累加到current_gas_left不为负,或者i = 0,停止。

②如果gas[0] - cost[0] >= 0,或者经历了第①步,current_gas_left不为负后,继续计算后面的加油站。累加current_gas_left += gas[j] - cost[j],j从1取值,不断递增。

若累加过程中current_gas_left再次出现负值,则继续采用第①步的做法。最终当j <= i时,循环累加停止。

此时如果current_gas_left为负,说明不存在符合条件的加油站。如果不为负,则i即为满足条件的加油站起点。

代码如下:

 class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int n = gas.size();
int ind_front = n-;
int ind_back = ; int station_cost = gas[] - cost[]; if (n == )
return station_cost >= ? : -; while (ind_front >= ind_back) {
if (station_cost < ) {
station_cost += gas[ind_front] - cost[ind_front];
ind_front--;
continue;
} if (station_cost >= ) {
station_cost += gas[ind_back] - cost[ind_back];
ind_back++;
if (ind_back == n)
return ;
}
} return station_cost >= ? ind_back : -;
}
};

解题思路2,o(n):

将所有加油站gas[i]和cost[i]想象合并成一个数组station[i],station[i]代表汽车行驶至此加油站时,将要支付的开销。

那么station[0]至station[n-1]的值,以加油站为x轴,以开销累加值作为y轴,可以在数轴上画一个折线(如下图)。不论从哪个station开始画起,折线的走势不会改变,只是在x轴上方和下方的比例会有变化。

如果只有唯一一个加油站能满足行驶一圈的话,那么一定是从折线的最低处的加油站,因为如果从那里作为起点,不管折线走势如何下降,current_gas_left总能保持大于等于0;

如何从任意一点找到曲线走势的最低点?只需要从任一加油站开始累加其开销值(gas[i] - cost[i]),记录累加的最小值,出现最小值的点就是曲线走势的最低点;

如果最终all gases > all costs,则加油站起始点就在出现最小值的下一个加油站。

代码:

 class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int current_gas_left = ;
int lowest_station = -;
int min = ; for (int i = ; i < gas.size(); ++i) {
current_gas_left += gas[i] - cost[i];
if (current_gas_left < min) {
min = current_gas_left;
lowest_station = i;
}
} if (current_gas_left >= )
return lowest_station + ;
else
return -;
}
};

【Leetcode】【Medium】Gas Station的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【leetCode百题成就】Gas Station解题报告

    题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

  5. 【leetcode刷题笔记】Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  6. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  7. [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  8. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  9. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  10. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

随机推荐

  1. 剑指offer等算法总结归类

    从数据结构分 一.链表: 3.题目描述:输入一个链表,从尾到头打印链表每个节点的值(递归) 思路:递归调用,调一次,加一次到list中 14.题目描述:输入一个链表,输出该链表中倒数第k个结点 两个指 ...

  2. idea的插件zookeeper

    平时用惯了ZooInspector,偶然知晓了idea的这个插件,试了一下感觉挺方便的 由于开发环境在内网,所以这里介绍内网方式(外网更简单). 1.下载插件 http://plugins.jetbr ...

  3. 010-JedisUtils工具类模板

    redis.properties配置文件 redis.maxIdle=30 redis.minIdle=10 redis.maxTotal=100 redis.url=192.168.204.128 ...

  4. Stirng,Stringbuffer,Stringbuild的区别浅淡

    String 1,Stirng是对象不是基本数据类型 2,String是final类,不能被继承.是不可变对象,一旦创建,就不能修改它的值. 3,对于已经存在的Stirng对象,修改它的值,就是重新创 ...

  5. vim实战:插件安装(Vundle,NerdTree)

    一:插件管理器Vundle 1.简介 Vundle是vim的一个插件管理器, 同时它本身也是vim的一个插件.插件管理器用于方便.快速的安装.删除.Vim更新插件.vim Vundle插件官方地址:h ...

  6. 栈C++实现

    栈的核心是LIFO(Last In First Out),即后进先出 出栈和入栈只会对栈顶进行操作,栈底永远为0.如果是入栈,要将入栈元素赋值给栈数组,再将栈顶上移一位:出栈时要先将栈顶下移一位,再将 ...

  7. Codeforces 671 A——Recycling Bottles——————【思维题】

     Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. mysql 远程链接问题

    问题描述: 从一台linux远程连接另一台linux上的MySQL, 出现ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.x ...

  9. WAMP环境配置-Apache服务器的安装

    一.下载 下载地址:http://httpd.apache.org/ 在这里就可以下载想下载的版本了 二.安装 我这次环境配置安装的是Apache-2.4.23版本! (最近我在反复安装PHP的时候出 ...

  10. 会话技术Cookie

    1.会话技术 1>什么是会话技术: 从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,成为一次会话. 2>作用: 会话技术就是记录这次会话中客户端的状态与数据的. Cookie:数 ...