-
Notifications
You must be signed in to change notification settings - Fork 0
/
_20220811.kt
79 lines (67 loc) · 2.01 KB
/
_20220811.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@file:Suppress("DuplicatedCode", "ClassName", "ObjectPropertyName")
package _2022._08
import Testable
import utils.assertEqualTo
import utils.runTimedTests
import utils.tupleOf
// 134. Gas Station
// https://leetcode.com/problems/gas-station/
private interface Leetcode_134 {
fun canCompleteCircuit(gas: IntArray, cost: IntArray): Int
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(intArrayOf(2,3,4), intArrayOf(3,4,3), -1),
tupleOf(intArrayOf(1,2,3,4,5), intArrayOf(3,4,5,1,2), 3),
)
listOf(
// M1()::canCompleteCircuit,
S1()::canCompleteCircuit,
).runTimedTests(tests) { a, b, c ->
invoke(a, b).assertEqualTo(c)
}
}
}
private class M1 : Leetcode_134 {
override fun canCompleteCircuit(gas: IntArray, cost: IntArray): Int {
if (gas.isEmpty()) return 0
val sums = IntArray(gas.size){
gas[it] - cost[it]
}
var l = 0
var r = 0
var sum = 0
var counter = 0
while(counter<=sums.size) {
sum += sums[r]
r = (r+1)%sums.size
if (sum < 0) {
l = r
sum = 0
counter++
} else {
if (l == r) return r
}
}
return -1
}
}
private class S1 : Leetcode_134 {
override fun canCompleteCircuit(gas: IntArray, cost: IntArray): Int {
if (gas.sum() < cost.sum()) return -1
var total = 0
var start = 0
for (i in gas.indices) {
total += (gas[i] - cost[i])
if (total < 0) {
total = 0
start = i + 1
}
}
return start
}
}
}
val _20220811 = listOf<Testable>(
Leetcode_134,
)