File tree Expand file tree Collapse file tree 5 files changed +125
-27
lines changed
0100-0199/0134.Gas Station
1100-1199/1187.Make Array Strictly Increasing Expand file tree Collapse file tree 5 files changed +125
-27
lines changed Original file line number Diff line number Diff line change 62
62
63
63
开始行驶,移动 $j$。若发现当前剩余汽油小于 $0$,说明当前 $i$ 作为起点不符合要求,我们将起点 $i$ 循环左移,并且更新剩余汽油,直至剩余汽油是非负数。
64
64
65
- 当行驶过的加油站数量达到 $n$ 时,结束。判断此时的剩余汽油是否非负,是则返回当前的 $i$ 作为答案;否则返回 $ -1$,表示无解 。
65
+ 当行驶过的加油站数量达到 $n$ 时,结束行驶过程。若此时剩余汽油 $s$ 仍然小于 $0$,说明不存在这样的起点,返回 $ -1$。否则,返回起点 $i$ 。
66
66
67
- 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 表示加油站的数量 。
67
+ 时间复杂度 $O(n)$,其中 $n$ 表示加油站的数量。 空间复杂度 $O(1)$。
68
68
69
69
<!-- tabs:start -->
70
70
@@ -162,6 +162,52 @@ func canCompleteCircuit(gas []int, cost []int) int {
162
162
}
163
163
```
164
164
165
+ ### ** TypeScript**
166
+
167
+ ``` ts
168
+ function canCompleteCircuit(gas : number [], cost : number []): number {
169
+ const n = gas .length ;
170
+ let i = n - 1 ;
171
+ let j = n - 1 ;
172
+ let s = 0 ;
173
+ let cnt = 0 ;
174
+ while (cnt < n ) {
175
+ s += gas [j ] - cost [j ];
176
+ ++ cnt ;
177
+ j = (j + 1 ) % n ;
178
+ while (s < 0 && cnt < n ) {
179
+ -- i ;
180
+ s += gas [i ] - cost [i ];
181
+ ++ cnt ;
182
+ }
183
+ }
184
+ return s < 0 ? - 1 : i ;
185
+ }
186
+ ```
187
+
188
+ ### ** C#**
189
+
190
+ ``` cs
191
+ public class Solution {
192
+ public int CanCompleteCircuit (int [] gas , int [] cost ) {
193
+ int n = gas .Length ;
194
+ int i = n - 1 , j = n - 1 ;
195
+ int s = 0 , cnt = 0 ;
196
+ while (cnt < n ) {
197
+ s += gas [j ] - cost [j ];
198
+ ++ cnt ;
199
+ j = (j + 1 ) % n ;
200
+ while (s < 0 && cnt < n ) {
201
+ -- i ;
202
+ s += gas [i ] - cost [i ];
203
+ ++ cnt ;
204
+ }
205
+ }
206
+ return s < 0 ? - 1 : i ;
207
+ }
208
+ }
209
+ ```
210
+
165
211
### ** ...**
166
212
167
213
```
Original file line number Diff line number Diff line change @@ -143,6 +143,52 @@ func canCompleteCircuit(gas []int, cost []int) int {
143
143
}
144
144
```
145
145
146
+ ### ** TypeScript**
147
+
148
+ ``` ts
149
+ function canCompleteCircuit(gas : number [], cost : number []): number {
150
+ const n = gas .length ;
151
+ let i = n - 1 ;
152
+ let j = n - 1 ;
153
+ let s = 0 ;
154
+ let cnt = 0 ;
155
+ while (cnt < n ) {
156
+ s += gas [j ] - cost [j ];
157
+ ++ cnt ;
158
+ j = (j + 1 ) % n ;
159
+ while (s < 0 && cnt < n ) {
160
+ -- i ;
161
+ s += gas [i ] - cost [i ];
162
+ ++ cnt ;
163
+ }
164
+ }
165
+ return s < 0 ? - 1 : i ;
166
+ }
167
+ ```
168
+
169
+ ### ** C#**
170
+
171
+ ``` cs
172
+ public class Solution {
173
+ public int CanCompleteCircuit (int [] gas , int [] cost ) {
174
+ int n = gas .Length ;
175
+ int i = n - 1 , j = n - 1 ;
176
+ int s = 0 , cnt = 0 ;
177
+ while (cnt < n ) {
178
+ s += gas [j ] - cost [j ];
179
+ ++ cnt ;
180
+ j = (j + 1 ) % n ;
181
+ while (s < 0 && cnt < n ) {
182
+ -- i ;
183
+ s += gas [i ] - cost [i ];
184
+ ++ cnt ;
185
+ }
186
+ }
187
+ return s < 0 ? - 1 : i ;
188
+ }
189
+ }
190
+ ```
191
+
146
192
### ** ...**
147
193
148
194
```
Original file line number Diff line number Diff line change 1
1
public class Solution {
2
2
public int CanCompleteCircuit ( int [ ] gas , int [ ] cost ) {
3
- if ( gas . Length == 0 ) return - 1 ;
4
- var startIndex = 0 ;
5
- var i = 0 ;
6
- var gasLeft = 0 ;
7
- while ( true )
8
- {
9
- gasLeft += gas [ i ] - cost [ i ] ;
10
- ++ i ;
11
- if ( i >= gas . Length ) i = 0 ;
12
- if ( gasLeft < 0 )
13
- {
14
- if ( startIndex >= i )
15
- {
16
- return - 1 ;
17
- }
18
- startIndex = i ;
19
- gasLeft = 0 ;
20
- }
21
- else
22
- {
23
- if ( startIndex == i )
24
- {
25
- return startIndex ;
26
- }
3
+ int n = gas . Length ;
4
+ int i = n - 1 , j = n - 1 ;
5
+ int s = 0 , cnt = 0 ;
6
+ while ( cnt < n ) {
7
+ s += gas [ j ] - cost [ j ] ;
8
+ ++ cnt ;
9
+ j = ( j + 1 ) % n ;
10
+ while ( s < 0 && cnt < n ) {
11
+ -- i ;
12
+ s += gas [ i ] - cost [ i ] ;
13
+ ++ cnt ;
27
14
}
28
15
}
16
+ return s < 0 ? - 1 : i ;
29
17
}
30
18
}
Original file line number Diff line number Diff line change
1
+ function canCompleteCircuit ( gas : number [ ] , cost : number [ ] ) : number {
2
+ const n = gas . length ;
3
+ let i = n - 1 ;
4
+ let j = n - 1 ;
5
+ let s = 0 ;
6
+ let cnt = 0 ;
7
+ while ( cnt < n ) {
8
+ s += gas [ j ] - cost [ j ] ;
9
+ ++ cnt ;
10
+ j = ( j + 1 ) % n ;
11
+ while ( s < 0 && cnt < n ) {
12
+ -- i ;
13
+ s += gas [ i ] - cost [ i ] ;
14
+ ++ cnt ;
15
+ }
16
+ }
17
+ return s < 0 ? - 1 : i ;
18
+ }
Original file line number Diff line number Diff line change 59
59
60
60
最后,如果 $f[ n-1] \geq \infty$,说明无法转换为严格递增数组,返回 $-1$,否则返回 $f[ n-1] $。
61
61
62
- 时间复杂度 $(n^2 )$,空间复杂度 $O(n)$。其中 $n$ 为 $arr1$ 的长度。
62
+ 时间复杂度 $(n \times (\log m + \min(m, n) )$,空间复杂度 $O(n)$。其中 $n$ 为 $arr1$ 的长度。
63
63
64
64
<!-- tabs:start -->
65
65
You can’t perform that action at this time.
0 commit comments