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
  • 相关阅读:
    01 输出字符串中字符的所有组合
    04 Redis主从同步
    03 Redis发布与订阅
    02 Redis防止入侵
    01 Redis基础
    MySQL索引优化 笔记
    SQL 基础语句整理
    jstl用法 简介
    type=file 上传图片限制 类型和尺寸 方法
    js 判断图片和视频是否加载成功
  • 原文地址:https://www.cnblogs.com/feiling/p/3350098.html
Copyright © 2011-2022 走看看