Leetcode - GasStation

An kind of interesting problem.  Use an array arr[] to store how many gas are left if we travel from station i to station i+1, arr[i] = gas[i] - cost[i]. Then it becomes an variance of the maximal sub array problem: if the sum
of the sub array arr[i....j] < 0, it means we can‘t start at any gas station whose index is within [i..j]. Then we should skip to j+1 and start to scan again. O(n) time and O(n) space:

public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int[] arr = new int[gas.length];
        int sum = 0;
        for(int i=0;i<gas.length;i++)
        {
        	arr[i] = gas[i] - cost[i];
        	sum+=arr[i];
        }

        if(sum < 0) return -1;

        int s;
        boolean flag;
        for(int i=0;i<gas.length;i++){
        	if(arr[i] < 0)
        		continue;

        	flag = true;
        	s=0;
        	for(int j=0;j<gas.length;j++)
        	{
        		int ind = (i+j) % gas.length;
        		s += arr[ind];
        		if(s<0)
        		{
        			i = i + j;
        			flag = false;
        			break;
        		}
        	}

        	if(flag == true)
        		return i;
        }
        return -1;
    }

    public static void main(String[] args)
    {
    	int[] gas= new int[]{1,2,3,3};
    	int[] cost = new int[]{2,1,5,1};

    	Solution sol = new Solution();
    	System.out.print(sol.canCompleteCircuit(gas, cost));

    }
}
时间: 2024-10-29 19:11:42

Leetcode - GasStation的相关文章

[leetcode]Gas Station @ Python

原题地址:https://oj.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

LeetCode题目总结分类

注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/probl

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 e

【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

[LeetCode]题解(python):134-Gas Station

题目来源: https://leetcode.com/problems/gas-station/ 题意分析: 在一个圈子路线里面有N个汽油站,i站的汽油有gas[i]汽油.现在有一辆无限容量的车,它从i站开到(i+1)需要耗费cost[i]汽油.如果这辆车可以走完这个圈,那么返回这个车的起点,否者返回-1. 题目思路: 不难发现,如果gas的总和大于或等于cost的总和,必然存在一种路线使得走完整个圈子.那么只要找到一个起点i,从这个起点出发的所有gas的和总比cost的和大就可以了. 代码(p

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个