https://leetcode.com/problems/gas-station/

题目:

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. class Solution {
  2. public:
  3. int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
  4. int n = gas.size();
  5. if(n == ) return -;
  6. if(n == ) {
  7. if(gas[] - cost[] >= ) return ;
  8. return -;
  9. }
  10.  
  11. vector<int> dif; dif.clear();
  12. for(int i=;i<n;++i) dif.push_back(gas[i] - cost[i]);
  13. for(int i=;i<n-;++i) dif.push_back(gas[i] - cost[i]);
  14.  
  15. vector<int> sum(*n-, );
  16. vector<int> dp(*n-, );
  17. sum[] = dif[];
  18. dp[] = ;
  19.  
  20. for(int i=;i<*n-;++i) {
  21. if(sum[i-] < ) {
  22. sum[i] = dif[i];
  23. dp[i] = ;
  24. }
  25. else {
  26. sum[i] = sum[i-] + dif[i];
  27. dp[i] = dp[i-] + ;
  28. }
  29.  
  30. if(dp[i] == n && sum[i] >= ) return i-n+;
  31. }
  32.  
  33. return -;
  34. }
  35. };

这里正好可以引申一个:环形数组的最大子数组和问题。解决这个问题就是把“环形” 变成 “直线型”。比如: 原来的环形数组为 v[0], v[1], ..., v[n-1]. 可以在v[n-1] 后面继续放置 v[0], v[1], ..., v[n-1]. 然后我们对这个新的数组求解:最大子数组和问题。但是这里也有一个问题,就是求解出来的最大子数组可能长度会超过n,所以我们维护两个变量:sum[i] 和 len[i]。sum[i] 记录以a[i] 为结束位置的最大子数组和,而len[i] 记录以a[i] 为结束位置的最大子数组的长度。最终的答案可以找当len[i] <= n 的时候, sum[i] 最大的值。

  1. int res = INT_MIN;
  2. vector<int> sum(n, );
  3. vector<int> len(n, );
  4.  
  5. sum[] <- v[]; len[] <- ;
  6.  
  7. for(i<- TO *n) {
  8. if(sum[i-] < ) {
  9. sum[i] <- v[i]
  10. len[i] <-
  11. }
  12. else {
  13. sum[i] <- sum[i-] + v[i]
  14. len[i] <- len[i-] +
  15. }
  16. if(len[i] <= n) res <- max{res, sum[i]);
  17. }
  18.  
  19. return res;

leetcode@ [134] Gas station (Dynamic Programming)的更多相关文章

  1. [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 ...

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

  3. leetcode 134. Gas Station ----- java

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

  4. [leetcode]134. Gas Station加油站

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

  5. Java for 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 ...

  6. [leetcode] 134. Gas Station (medium)

    原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...

  7. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  8. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. PHP截取字符串,获取长度,获取字符位置的函数

    strstr(string,string) = strchr(,) //从前面第一次出现某个字符串的地方截取到最后strrchr(string,string) //从某个字符串从最后出现的位置截取到结 ...

  2. 软考类----编码、ASII码等

    淘米2014实习生笔试,今年是淘米第一年招暑期实习生,笔试好大部分考的是软考的题目啊啊啊啊(劳资后悔当年没考软考刷加权),其他是浅而泛的风格,C++,SQL语句,数据结构(哈夫曼树,二叉查找树,栈后缀 ...

  3. POJ2187Beauty Contest

    http://poj.org/problem?id=2187 题意 :有一个农场有N个房子,问最远的房子相距多少距离 . 思路 :凸包,旋转卡壳,通过寻找所有的对锺点,找出最远的点对. #includ ...

  4. iOS中关于KVC与KVO知识点

    iOS中关于KVC与KVO知识点 iOS中关于KVC与KVO知识点  一.简介 KVC/KVO是观察者模式的一种实现,在Cocoa中是以被万物之源NSObject类实现的NSKeyValueCodin ...

  5. 使用头文件climits中的符号常量获知整型数据的表数范围---gyy整理

    在头文件climits(limits.h)以宏定义的方式定义了各种符号常量来表示各种整型类型表示数的范围,如int的最大最小值,long的最大最小值等. 符号常量 表示 CHAR_BIT char 的 ...

  6. 轻量级Java_EE企业应用实战-第5章Hibernate的基本用法-001

    1. package org.crazyit.app.domain; import javax.persistence.*; /** * Description: <br/> * ��վ: ...

  7. 新的HTTP框架:Daraja Framework

    https://www.habarisoft.com/daraja_framework.html

  8. XST综合、实现过程包含哪些步骤

    2013-06-25 18:53:50 在ISE的主界面的处理子窗口的synthesis的工具可以完成下面的任务: 查看RTL原理图(View RTL schematic) 查看技术原理图(View ...

  9. DIV内英文或者数字不换行的问题 解决办法

    word-wrap:break-word; word-break:break-all;

  10. JavaScript DOM高级程序设计 3.-DOM2和HTML2--我要坚持到底!

    由一个HTML进行说明,我就不敲了,直接copy <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " ...