GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/code/lc134.java at master · mJackie/leetcode Expand file tree/ Copy pathlc134.javaMore file actions 27 lines (26 loc) · 938 Bytes / Copy pathlc134.javaFile metadata and controls27 lines (26 loc) · 938 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package code; /* * 134. Gas Station * 题意:给两个数组,一个表示在该点能加多少油,一个表示该点到下个点消耗多少油,加油站连成一个环,问能否走遍所有加油站,从哪一站开始 * 难度:Medium * 分类:Greedy * 思路:计算两个数组的差,再把每个位置加起来,如果>=0,则能跑完。位置为最大连续子数组的开始位置。 * Tips:lc53 */ public class lc134 { public int canCompleteCircuit(int[] gas, int[] cost) { int[] arr = new int[gas.length]; int sum = 0, pos = 0, dp =0; for (int i = 0; i < arr.length ; i++) { arr[i] = gas[i] - cost[i]; sum += arr[i]; if(dp<=0) { //最大连续子数组,并记录位置 dp = arr[i]; pos = i; } else dp += arr[i]; } return sum<0? -1: pos; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |