File tree 2 files changed +30
-1
lines changed
2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -83,7 +83,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
83
83
| 130 | [ Surrounded Regions] ( https://leetcode.com/problems/surrounded-regions/ ) | [ JavaScript] ( ./src/surrounded-regions/res.js ) | Medium |
84
84
| 131 | [ palindrome-partitioning] ( https://leetcode.com/problems/palindrome-partitioning/ ) | [ TypeScript] ( ./src/palindrome-partitioning/res.ts ) | Medium |
85
85
| 133 | [ Clone Graph] ( https://leetcode.com/problems/clone-graph/ ) | [ JavaScript] ( ./src/clone-graph/res.js ) | Medium |
86
- | 134 | [ Gas Station] ( https://leetcode.com/problems/gas-station/ ) | [ JavaScript] ( ./src/gas-station/res.js ) | Medium |
86
+ | 134 | [ Gas Station] ( https://leetcode.com/problems/gas-station/ ) | [ JavaScript] ( ./src/gas-station/res.js ) · [ TypeScript ] ( ./src/gas-station/res.ts ) | Medium |
87
87
| 135 | [ Candy] ( https://leetcode.com/problems/candy/ ) | [ JavaScript] ( ./src/candy/res.js ) | Hard |
88
88
| 136 | [ Single Number] ( https://leetcode.com/problems/single-number/ ) | [ JavaScript] ( ./src/single-number/res.js ) | Easy |
89
89
| 137 | [ Single Number II] ( https://leetcode.com/problems/single-number-ii/ ) | [ JavaScript] ( ./src/single-number-ii/res.js ) | Medium |
Original file line number Diff line number Diff line change
1
+ function canCompleteCircuit ( gas : number [ ] , cost : number [ ] ) : number {
2
+ const rest = gas . map ( ( gasItem , i ) => gasItem - cost [ i ] ) ;
3
+ let remain = 0 ;
4
+ let startIndex = 0 ;
5
+
6
+ if ( rest . reduce ( ( p , c ) => p + c , 0 ) < 0 ) {
7
+ return - 1 ;
8
+ }
9
+
10
+ let count = rest . length ;
11
+ for ( let i = 0 ; ; ) {
12
+ if ( remain + rest [ i ] < 0 ) {
13
+ remain = 0 ;
14
+ i = ( i + 1 ) % rest . length ;
15
+ startIndex = i ;
16
+ count = rest . length ;
17
+ } else {
18
+ remain += rest [ i ] ;
19
+ i = ( i + 1 ) % rest . length ;
20
+ count -- ;
21
+
22
+ if ( ! count && remain >= 0 ) {
23
+ break ;
24
+ }
25
+ }
26
+ }
27
+
28
+ return startIndex ;
29
+ } ;
You can’t perform that action at this time.
0 commit comments