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
  • 相关阅读:
    洛谷 U141580 简化罗波切问题
    洛谷 U141578 维修电路
    洛谷 U140760 狭义公因子
    CF75C Modified GCD
    算法题-求解斐波那切数列的第N个数是几?
    算法题-求N的阶乘
    JAVA8新特性
    nginx启动脚本,手动编辑
    javah生成带有包名的头文件
    Matlab图像处理(03)-基本概念
  • 原文地址:https://www.cnblogs.com/feiling/p/3350098.html
Copyright © 2011-2022 走看看