|
| 1 | +package com.leosanqing.leetcode.medium.array; |
| 2 | + |
| 3 | +/** |
| 4 | + * @Author: rtliu |
| 5 | + * @Date: 2020/8/4 上午9:38 |
| 6 | + * @Package: com.leosanqing.leetcode.medium.array |
| 7 | + * @Description: 1 |
| 8 | + * ` There are N gas stations along a circular route, |
| 9 | + * ` where the amount of gas at station i is gas[i]. |
| 10 | + * ` You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its |
| 11 | + * next station (i+1). |
| 12 | + * ` You begin the journey with an empty tank at one of the gas stations. |
| 13 | + * ` Return the starting gas station's index |
| 14 | + * ` if you can travel around the circuit once in the clockwise direction, otherwise return -1. |
| 15 | + * ` |
| 16 | + * ` Note: |
| 17 | + * ` If there exists a solution, it is guaranteed to be unique. |
| 18 | + * ` Both input arrays are non-empty and have the same length. |
| 19 | + * ` Each element in the input arrays is a non-negative integer. |
| 20 | + * ` |
| 21 | + * ` 沿循环路线有N个加油站, |
| 22 | + * ` 其中,第i个站点的天然气量为gas [i]。 |
| 23 | + * ` 您有一辆带无限油箱的汽车,从第i站到下一个(i + 1)的行车成本为[i]。 |
| 24 | + * ` 您可以从其中一个加油站的空罐开始旅程。 |
| 25 | + * ` 返回起始加油站的索引 |
| 26 | + * ` 如果您可以沿顺时针方向绕过电路一次,否则返回-1。 |
| 27 | + * ` 注意: |
| 28 | + * ` 如果存在解决方案,则保证是唯一的。 |
| 29 | + * ` 两个输入数组都是非空的,并且具有相同的长度。 |
| 30 | + * ` 输入数组中的每个元素都是非负整数。 |
| 31 | + * ` Example 1: |
| 32 | + * ` Input: |
| 33 | + * ` gas = [1,2,3,4,5] |
| 34 | + * ` cost = [3,4,5,1,2] |
| 35 | + * ` Output: 3 |
| 36 | + * ` Explanation: |
| 37 | + * ` Start at station 3 (index 3) and fill up with 4 unit of gas. |
| 38 | + * ` Your tank = 0 + 4 = 4 Travel to station 4. Your tank = 4 - 1 + 5 = 8 Travel to station 0. |
| 39 | + * ` Your tank = 8 - 2 + 1 = 7 Travel to station 1. |
| 40 | + * ` Your tank = 7 - 3 + 2 = 6 Travel to station 2. |
| 41 | + * ` Your tank = 6 - 4 + 3 = 5 Travel to station 3. |
| 42 | + * ` The cost is 5. Your gas is just enough to travel back to station 3. |
| 43 | + * ` Therefore, return 3 as the starting index. |
| 44 | + * ` Example 2: |
| 45 | + * ` Input: |
| 46 | + * ` gas = [2,3,4] |
| 47 | + * ` cost = [3,4,3] |
| 48 | + * ` Output: -1 |
| 49 | + * ` Explanation: |
| 50 | + * ` You can't start at station 0 or 1, as there is not enough gas to travel to the next station. |
| 51 | + * ` Let's start at station 2 and fill up with 4 unit of gas. |
| 52 | + * ` Your tank = 0 + 4 = 4 Travel to station 0. |
| 53 | + * ` Your tank = 4 - 3 + 2 = 3 Travel to station 1. |
| 54 | + * ` Your tank = 3 - 3 + 3 = 3 You cannot travel back to station 2, as it requires 4 unit of gas but you |
| 55 | + * only have 3. |
| 56 | + * ` Therefore, you can't travel around the circuit once no matter where you start. |
| 57 | + * @Version: 1.0 |
| 58 | + */ |
| 59 | +public class _134_gas_station { |
| 60 | + |
| 61 | + public static void main(String[] args) { |
| 62 | + |
| 63 | + int[] gas = {1, 2, 3, 4, 5}; |
| 64 | + int[] cost = {3, 4, 5, 1, 2}; |
| 65 | + System.out.println(canCompleteCircuit(gas, cost)); |
| 66 | + } |
| 67 | + |
| 68 | + public static int canCompleteCircuit(int[] gas, int[] cost) { |
| 69 | + int total = 0; |
| 70 | + |
| 71 | + for (int i = 0; i < gas.length; i++) { |
| 72 | + total += gas[i] - cost[i]; |
| 73 | + } |
| 74 | + if (total < 0) { |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + for (int i = 0; i < gas.length; i++) { |
| 79 | + if (canDoIt(gas, cost, i)) { |
| 80 | + return i; |
| 81 | + } |
| 82 | + } |
| 83 | + return -1; |
| 84 | + } |
| 85 | + |
| 86 | + private static boolean canDoIt(int[] gas, int[] cost, int start) { |
| 87 | + int g = 0; |
| 88 | + for (int j = 0; j < gas.length; j++) { |
| 89 | + int position = (j + start) % gas.length; |
| 90 | + g += gas[position] - cost[position]; |
| 91 | + if (g < 0) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +} |
0 commit comments