Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b83f1c2

Browse files
Add Gas Station C#
1 parent c5fd3de commit b83f1c2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public int CanCompleteCircuit(int[] gas, int[] cost) {
3+
int sumGas = 0, sumCost = 0, tank = 0, pos = 0;
4+
5+
for(int i = 0; i<gas.Length; i++){
6+
sumGas += gas[i];
7+
sumCost += cost[i];
8+
tank += gas[i] - cost[i];
9+
if(tank < 0){
10+
pos = i+1;
11+
tank = 0;
12+
}
13+
}
14+
15+
if(sumGas >= sumCost){
16+
return pos;
17+
}
18+
19+
return -1;
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /