11/18
思路
画个折线图,假设以0为起点,记录一下从起点到每个点为止的净收入
容易发现,更换起点只是让这个折线图上下平移。
题目要求每个时间点的总和大于0;
就是要求折线图的最底点大于0

class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int all = 0;
int minn = Integer.MAX_VALUE;
int ans = -1;
for (int i = 0; i < gas.length; i++) {
all += gas[i] - cost[i];
if (all < minn) {
minn = all;
ans = i;
}
}
return all >= 0 ? (ans+1)%gas.length : -1;
}
}
j

博客记录了11月18日LeetCode题目的解题思路,虽内容简略,但聚焦于LeetCode解题相关信息技术内容。
1031

被折叠的 条评论
为什么被折叠?



