-
Notifications
You must be signed in to change notification settings - Fork 0
/
_20220704.kt
278 lines (228 loc) · 7.01 KB
/
_20220704.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
@file:Suppress("DuplicatedCode", "ClassName")
package _2022._07
import Testable
import utils.*
import java.util.EmptyStackException
import java.util.LinkedList
import java.util.Stack
// 239. Sliding Window Maximum
// https://leetcode.com/problems/sliding-window-maximum/
private interface Leetcode_239 {
fun maxSlidingWindow(nums: IntArray, k: Int): IntArray
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(intArrayOf(1,3,-1,-3,5,3,6,7), 3, intArrayOf(3,3,5,5,6,7)),
tupleOf(intArrayOf(1), 1, intArrayOf(1)),
)
listOf(
M1()::maxSlidingWindow,
).runTimedTests {
tests.forEachTuple { first, second, third ->
invoke(first, second).assertEqualTo(third)
}
}
}
}
private class M1 : Leetcode_239 {
override fun maxSlidingWindow(nums: IntArray, k: Int): IntArray {
val res = IntArray(nums.size - k + 1)
// the index of the max of the current cell and (k-1) previous cells
val maxes = IntArray(nums.size)
var l = 0
var r = k - 1
for (i in nums.indices) {
val c = nums[i]
if (i == 0) {
maxes[i] = c
} else {
val preMax = maxes[i-1]
if (c > preMax) {
maxes[i] = c
} else {
}
}
}
TODO("Not yet implemented")
}
}
}
// 20. Valid Parentheses
// https://leetcode.com/problems/valid-parentheses/
private interface Leetcode_20 {
fun isValid(s: String): Boolean
companion object : Testable {
override fun test() {
val tests = listOf(
"()" to true,
"()[]{}" to true,
"()[{}" to false,
"()][{}" to false,
)
listOf(
M1()::isValid,
).runTimedTests {
tests.forEachPair { first, second ->
invoke(first).assertEqualTo(second)
}
}
}
}
// https://leetcode.com/submissions/detail/738135480/
// Runtime: 149 ms, faster than 96.79% of Kotlin online submissions for Valid Parentheses.
// Memory Usage: 33.7 MB, less than 94.25% of Kotlin online submissions for Valid Parentheses.
private class M1 : Leetcode_20 {
override fun isValid(s: String): Boolean {
return Stack<Int>().apply {
s.forEach {
when(it) {
'(' -> add(0)
'[' -> add(1)
'{' -> add(2)
else -> {
if (isEmpty()) return false
if (when (it) {
')' -> 0
']' -> 1
'}' -> 2
else -> return false
} != pop()
) return false
}
}
}
}.isEmpty()
}
}
}
// 155. Min Stack
// https://leetcode.com/problems/min-stack/
private interface Leetcode_155 {
fun push(`val`: Int)
fun pop()
fun top(): Int
fun getMin(): Int
companion object : Testable {
override fun test() {
listOf(
S1(), S2(),
).runTimedTests {
push(-2)
top().assertEqualTo(-2)
push(0)
top().assertEqualTo(0)
push(-3)
top().assertEqualTo(-3)
getMin().assertEqualTo(-3)
pop()
top().assertEqualTo(0)
getMin().assertEqualTo(-2)
}
listOf(
S1(), S2(),
).runTimedTests {
push(-2)
top().assertEqualTo(-2)
push(0)
top().assertEqualTo(0)
push(-1)
top().assertEqualTo(-1)
getMin().assertEqualTo(-2)
top().assertEqualTo(-1)
pop()
getMin().assertEqualTo(-2)
}
}
}
// Not working
private class M1 : Leetcode_155 {
private data class Node<T>(
val `val`: T,
var pre: Node<T>? = null,
var preMin: Node<T>? = null
)
private var min: Node<Int>? = null
private var head: Node<Int>? = null
// TODO: Not done,
// this keep preMin sorted
override fun push(`val`: Int) {
val node = Node(`val`, pre = head)
if (min == null) {
min = node
} else {
var p: Node<Int>? = min!!
var m = p
while (node.`val` < p!!.`val`) {
if (p.preMin == null) break
m = p
p = p.preMin
}
node.preMin = p
m?.preMin = node
}
head = node
}
override fun pop() {
val node = head ?: throw EmptyStackException()
head = node.pre
min = node.preMin
}
override fun top(): Int {
return head?.`val` ?: throw EmptyStackException()
}
override fun getMin(): Int {
return min?.`val` ?: throw EmptyStackException()
}
}
// MARK: minStack
private class S1 : Leetcode_155 {
private val stack = Stack<Int>()
private val minStack = Stack<Int>()
override fun push(`val`: Int) {
stack.push(`val`)
if (minStack.isEmpty() || minStack.peek() > `val`) {
minStack.push(`val`)
} else {
// !!!
minStack.push(minStack.peek())
}
}
override fun pop() {
stack.pop()
minStack.pop()
}
override fun top(): Int {
return stack.peek()
}
override fun getMin(): Int {
return minStack.peek()
}
}
private class S2 : Leetcode_155 {
private val stack = Stack<Int>()
private val minStack = Stack<Int>()
override fun push(`val`: Int) {
stack.push(`val`)
if (minStack.isEmpty() || minStack.peek() >= `val`) {
minStack.push(`val`)
}
}
override fun pop() {
if (minStack.peek() == stack.peek()) {
minStack.pop()
}
stack.pop()
}
override fun top(): Int {
return stack.peek()
}
override fun getMin(): Int {
return minStack.peek()
}
}
}
fun main() {
// Leetcode_239.test()
// Leetcode_20.test()
Leetcode_155.test()
}