-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_20220724.kt
118 lines (106 loc) · 3.94 KB
/
_20220724.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@file:Suppress("DuplicatedCode", "ClassName")
package _2022._07
import Testable
import utils.assertEqualTo
import utils.runTimedTests
import utils.tupleOf
import java.util.*
// 739. Daily Temperatures
// https://leetcode.com/problems/daily-temperatures/
private interface Leetcode_739 {
fun dailyTemperatures(temperatures: IntArray): IntArray
companion object : Testable {
override fun test() {
val tests = listOf(
intArrayOf(73,74,75,71,69,72,76,73) to intArrayOf(1,1,4,2,1,1,0,0),
intArrayOf(30,40,50,60) to intArrayOf(1,1,1,0),
intArrayOf(30,60,90) to intArrayOf(1,1,0),
)
listOf(
S2()::dailyTemperatures,
).runTimedTests(tests) { i, o ->
invoke(i).assertEqualTo(o)
}
}
}
// not working
private class M1 : Leetcode_739 {
override fun dailyTemperatures(temperatures: IntArray): IntArray {
val result = IntArray(temperatures.size)
for (i in temperatures.size - 1 downTo 0) {
val temp = temperatures[i]
var counter = 0
while (i != temperatures.size && i + counter + 1 < temperatures.size && i + counter + 1 >= 0) {
val p = i + counter + 1
val pTemp = temperatures[p]
if (temp > pTemp) {
counter ++
break
} else {
val diff = result[p]
counter += if (diff == 0) 1 else diff
}
}
result[temperatures.size - i - 1] = counter
}
return result
}
}
private class S2 : Leetcode_739 {
override fun dailyTemperatures(temperatures: IntArray): IntArray {
if (temperatures.size <= 1) return intArrayOf(0)
val stack = Stack<Pair<Int, Int>>()
val result = IntArray(temperatures.size)
stack.push(0 to temperatures[0])
for (i in 1 until temperatures.size) {
val temp = temperatures[i]
while (stack.isNotEmpty() && stack.peek().second < temp) {
val pop = stack.pop()
result[pop.first] = i - pop.first
}
stack.push(i to temp)
}
return result
}
}
}
// 853. Car Fleet
// https://leetcode.com/problems/car-fleet/
private interface Leetcode_853 {
fun carFleet(target: Int, position: IntArray, speed: IntArray): Int
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(12, intArrayOf(10,8,0,5,3), intArrayOf(2,4,1,1,3)) to 3,
tupleOf(10, intArrayOf(3), intArrayOf(3)) to 1,
tupleOf(100, intArrayOf(0,2,4), intArrayOf(4,2,1)) to 1,
)
listOf(
S1()::carFleet,
).runTimedTests(tests) { a, b ->
invoke(a.first, a.second, a.third).assertEqualTo(b)
}
}
}
// Runtime: 1109 ms, faster than 83.33% of Kotlin online submissions for Car Fleet.
// Memory Usage: 114.7 MB, less than 71.43% of Kotlin online submissions for Car Fleet.
// https://leetcode.com/submissions/detail/755904041/
private class S1 : Leetcode_853 {
override fun carFleet(target: Int, position: IntArray, speed: IntArray): Int {
val sorted = position.zip(speed).sortedByDescending { it.first }
val stack = Stack<Pair<Pair<Int, Int>, Double>>()
for (pair in sorted) {
val (p, v) = pair
val t = (target - p) / v.toDouble()
if (stack.isEmpty() || stack.peek().second < t) {
stack.push(pair to t)
}
}
return stack.size
}
}
}
fun main() {
// Leetcode_739.test()
Leetcode_853.test()
}