Shawn's Blog

  • Home
  • Tags
  • Categories
  • Archives
  • Search
  • Table of Contents
  • Overview

Shawn

若有恒,何必三更眠五更起;最无益,莫过一日曝十日寒。
392 posts
13 categories
114 tags

LeetCode 134. Gas Station

Posted on 2020-12-23 Edited on 2024-11-24 In LeetCode
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int start = 0, total = 0, tank = 0;
for (int i = 0; i < gas.size(); ++i)
if ((tank += gas[i] - cost[i]) < 0) {
start = i + 1;
total += tank;
tank = 0;
}
return (total + tank) < 0 ? -1 : start;
}
};
# Greedy
LeetCode 103. Binary Tree Zigzag Level Order Traversal
LeetCode 387. First Unique Character in a String
© 2024 Shawn
Powered by Hexo & NexT.Mist
0%