zoukankan      html  css  js  c++  java
  • leetcode -- 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.

    [解题思路]

    My first solution is two nested loops which will start from 0~n-1 to check whether the car can travel around the circuit.

    Unfortunatelly, my code has some problem to solve end the loop-_-!

    The following solution reference the dicussion in leetcode.

    Here we maintain two variable sum, total.

    1.total is used to check whether the car can travel around the circuit.

    2.sum is used to find the start index, when we find the sum is less than 0, it means that the last start index can not travel around the circuit,

    we need to start from the next index. So the result should be startIndex + 1

     1 public class Solution {
     2     public int canCompleteCircuit(int[] gas, int[] cost) {
     3         // Note: The Solution object is instantiated only once and is reused by each test case.
     4         int N = gas.length, startIndex = -1;
     5         int sum = 0, total = 0;
     6         for(int i = 0; i < N; i++){
     7             sum += (gas[i] - cost[i]);
     8             total += (gas[i] - cost[i]);
     9             if(sum < 0){
    10                 startIndex = i;
    11                 sum = 0;
    12             }
    13         }
    14         return total >= 0 ? startIndex + 1 : -1;
    15     }
    16 }

    My original solution:

     1 public class Solution {
     2     public int canCompleteCircuit(int[] gas, int[] cost) {
     3         // Note: The Solution object is instantiated only once and is reused by each test case.
     4         int N = gas.length, startIndex = -1;
     5         int gasLeft = 0;
     6         for(int i = 0; i < N; i++){
     7             boolean flag = true;
     8             for(int j = i; j < N; ){
     9                 gasLeft += gas[j];
    10                 
    11                 if(j == i - 1){
    12                     break;
    13                 }
    14                 if(gasLeft >= cost[j]){
    15                     gasLeft -= cost[j];
    16                 } else {
    17                     flag = false;
    18                     break;
    19                 }
    20                 
    21                 j++;
    22                 if(j == N){
    23                     j = 0;
    24                 }
    25                 if(j == i){
    26                     break;
    27                 }
    28             }
    29             if(flag){
    30                 startIndex = i;
    31                 return startIndex;
    32             }
    33         }
    34         return startIndex;
    35     }
    36 }
    View Code
  • 相关阅读:
    orm 对象关系映射 指 表与类之间的映射 # 40
    事务 视图 触发器 函数 (内置) 存储过程 流程控制 索引 # 39
    exist 存在 Python操作mysql pymysql sql注入问题 # 38
    基本查询语句与方法 多表查询 # 37
    外键 #36
    存储引擎 索引 数据类型 约束条件 # 35
    mysql安装 登录 修改密码 库,表,记录(增删改查) # 34
    进程池和线程池 协程 # 33
    GIL全局解释器锁
    # 并发编程 -进程理论-进程的方法
  • 原文地址:https://www.cnblogs.com/feiling/p/3350098.html
Copyright © 2011-2022 走看看