-
Notifications
You must be signed in to change notification settings - Fork 0
/
_20220627.kt
419 lines (365 loc) · 13.3 KB
/
_20220627.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
@file:Suppress("DuplicatedCode", "ClassName")
package _2022._06
import Testable
import utils.*
import java.lang.Integer.min
import kotlin.collections.contentEquals
// 76. Minimum Window Substring
// https://leetcode.com/problems/minimum-window-substring/
private interface Leetcode_76 {
fun minWindow(s: String, t: String): String
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(
"ADOBECODEBANC", "ABC", "BANC"
),
tupleOf(
"a", "a", "a"
),
tupleOf(
"a", "aa", ""
),
tupleOf(
"aaflslflsldkalskaaa", "aaa", "aaa"
)
)
listOf(
// M1()::minWindow,
// M2()::minWindow,
M3()::minWindow,
).runTimedTests {
tests.forEachTuple { first, second, third ->
invoke(first, second).assertEqualTo(third)
}
}
}
}
// Does not work for test case "a", "aa" -> ""
private class M1 : Leetcode_76 {
override fun minWindow(s: String, t: String): String {
// val tMap = IntArray(t.length) { -1 }
val tMap = HashMap<Char, Int>(t.length)
var min = 0
var max = s.length
s.forEachIndexed { index, c ->
tMap[c] = index
if (tMap.keys.containsAll(t.toList())) {
var _min = -1
var _max = -1
for (tc in t) {
val value = tMap.get(tc) ?: break
if (_min == -1 || value < _min) _min = value
if (_max == -1 || value > _max) _max = value
}
if (_min != -1 && _max != -1) {
if ((_max - _min) < (max - min)) {
max = _max
min = _min
}
}
}
}
return if (max != s.length) s.substring(min, max+1) else ""
}
}
// Nope, not working
private class M2 : Leetcode_76 {
override fun minWindow(s: String, t: String): String {
val tMap = IntArray(t.length) { -1 }
var min = 0
var max = s.length
s.forEachIndexed { index, c ->
var tIndex = -1
for (ti in t.indices) {
if (t[ti] == c) {
if (tMap[ti] == -1) {
tIndex = ti
}
}
}
if (tIndex == -1) {
for (ti in t.indices) {
if (t[ti] == c) {
if (tIndex == -1 || t[ti] > t[tIndex]) {
tIndex = ti
}
}
}
}
if (tIndex != -1) {
tMap[tIndex] = index
if (tMap.all { it != -1 }) {
var _min = -1
var _max = -1
tMap.forEach {
if (_min == -1 || it < _min) _min = it
if (_max == -1 || it > _max) _max = it
}
if (_min != -1 && _max != -1) {
if ((_max - _min) < (max - min)) {
max = _max
min = _min
}
}
}
}
}
return if (max != s.length) s.substring(min, max+1) else ""
}
}
private class M3 : Leetcode_76 {
override fun minWindow(s: String, t: String): String {
val tMap = HashMap<Char, Int>().apply {
t.forEach {
set(it, (get(it) ?: 0) + 1)
}
}
val tCount = IntArray(t.length)
fun valid() : Boolean {
for (i in t.indices) {
val c = t[i]
if (tCount[i] < checkNotNull(tMap[c])) {
return false
}
}
return true
}
var left = 0
var right = 0
var resultLeft = -1
var resultRight = -1
// [left, right)
while (right < s.length) {
val rC = s[right]
tCount[right] ++
if (valid()) {
while (valid()) {
left++
tCount[left]--
}
tCount[left]++
left--
if (resultLeft == -1 || (right - left) < (resultRight - resultLeft)) {
resultLeft = left
resultRight = right
}
}
right++
}
return if (resultLeft != -1) t.substring(resultLeft, resultRight) else ""
}
}
}
// 34. Find First and Last Position of Element in Sorted Array
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
private interface Leetcode_34 {
fun searchRange(nums: IntArray, target: Int): IntArray
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(intArrayOf(5,7,7,8,8,10), 8, intArrayOf(3,4)),
tupleOf(intArrayOf(5,7,7,8,8,10), 6, intArrayOf(-1,-1)),
tupleOf(intArrayOf(), 8, intArrayOf(-1,-1)),
tupleOf(intArrayOf(1), 8, intArrayOf(-1,-1)),
tupleOf(intArrayOf(2,2), 2, intArrayOf(0,1)),
tupleOf(intArrayOf(2,2), 1, intArrayOf(-1,-1)),
)
listOf(
M1()::searchRange,
M2()::searchRange,
).runTimedTests {
tests.forEachTuple { first, second, third ->
invoke(first, second).assertEqualTo(third, toStr = {
joinToString()
}, checker = { exp, actual ->
exp.contentEquals(actual)
}, lazyMsg = { exp, actual ->
"assertion failed for input:${first.joinToString()}, exp:${exp.joinToString()} vs actual:${actual.joinToString()}"
})
}
}
}
}
// https://leetcode.com/submissions/detail/734756643/
// Runtime: 334 ms, faster than 55.04% of Kotlin online submissions for Find First and Last Position of Element in Sorted Array.
// Memory Usage: 45.9 MB, less than 6.92% of Kotlin online submissions for Find First and Last Position of Element in Sorted Array.
private class M1 : Leetcode_34 {
override fun searchRange(nums: IntArray, target: Int): IntArray {
if (nums.isEmpty()) return intArrayOf(-1, -1)
if (nums.size == 1) return if (
nums.first() == target
) intArrayOf(0,0) else intArrayOf(-1, -1)
var a = 0
var b = nums.size - 1
var i = b / 2
loop@ while (true) {
val value = nums[i]
when{
value == target -> break@loop
a + 1 == b -> {
return when{
nums[a] == target && nums[b] == target -> intArrayOf(a,b)
nums[a] == target || nums[b] == target -> {
if (nums[a] == target) intArrayOf(a,a) else intArrayOf(b,b)
}
else -> intArrayOf(-1,-1)
}
}
value > target -> {
b = i
i = ((a + i + 0.5) / 2f).toInt()
}
else -> {
a = i
i = ((b + i + 0.5) / 2f).toInt()
}
}
}
a = i
b = i
while (a >= 0 && nums[a] == target) {
a--
}
a++
while (b < nums.size && nums[b] == target) {
b++
}
if (b > nums.size) {
b = -1
} else b--
return intArrayOf(a++, b)
}
}
// 0 1 2 3 4 5
// 5,7,7,8,8,10
private class M2 : Leetcode_34 {
private fun search(nums: IntArray, target: Int, leftIndex: Int, rightIndex: Int, isLeft: Boolean) : Int {
if (leftIndex == -1 || rightIndex == -1) return -1
if (leftIndex == rightIndex) return if (nums[leftIndex] == target) leftIndex else -1
if (isLeft) {
if (nums[leftIndex] == target) return leftIndex
} else {
if (nums[rightIndex] == target) return rightIndex
}
val mid = (leftIndex + rightIndex) / 2
val midValue = nums[mid]
return when{
midValue == target -> {
if (isLeft) {
search(nums, target, leftIndex, mid - 1, isLeft)
} else {
search(nums, target, mid + 1, rightIndex, isLeft)
}.let {
if (it == -1) mid else it
}
}
midValue < target -> {
search(nums, target, mid + 1, rightIndex, isLeft)
}
else -> {
search(nums, target, leftIndex, mid - 1, isLeft)
}
}
}
override fun searchRange(nums: IntArray, target: Int): IntArray {
if (nums.isEmpty()) return intArrayOf(-1, -1)
if (nums.size == 1) return if (nums.first() == target) intArrayOf(0,0) else intArrayOf(-1, -1)
val left = search(nums, target, 0, nums.size - 1, true)
val right = search(nums, target, left, nums.size - 1, false)
return intArrayOf(left, right)
}
}
}
// 11. Container With Most Water
// https://leetcode.com/problems/container-with-most-water/
private interface Leetcode_11 {
fun maxArea(height: IntArray): Int
companion object : Testable {
override fun test() {
val tests = listOf(
intArrayOf(1,8,6,2,5,4,8,3,7) to 49,
)
listOf(
M1()::maxArea,
S3()::maxArea,
S4()::maxArea,
).runTimedTests {
tests.forEachPair { first, second ->
invoke(first).assertEqualTo(second, lazyMsg = { a, b ->
"assertion failed for input:${first.joinToString()}, exp:$a vs actual:$b"
})
}
}
}
}
// timeout
private class M1 : Leetcode_11 {
override fun maxArea(height: IntArray): Int {
var max = 0
for (i in 0 until height.size - 1) {
val left = height[i]
for(j in i + 1 until height.size) {
val right = height[j]
val volume = Math.min(left, right) * (j - i)
if (volume > max) {
max = volume
}
}
}
return max
}
}
// https://leetcode.com/submissions/detail/735453613/
// Runtime: 459 ms, faster than 94.08% of Kotlin online submissions for Container With Most Water.
// Memory Usage: 49.4 MB, less than 98.52% of Kotlin online submissions for Container With Most Water.
private class S3 : Leetcode_11 {
override fun maxArea(height: IntArray): Int {
var max = 0
var left = 0
var right = height.size - 1
while(right > left) {
val v = (right - left) * Math.min(height[left], height[right])
if (v > max) {
max = v
}
if (height[left] > height[right]) {
right--
} else left++
}
return max
}
}
// slightly improved on S3
private class S4 : Leetcode_11 {
override fun maxArea(height: IntArray): Int {
var max = 0
var left = 0
var right = height.size - 1
var maxH = 0 // track the current max height
while(right > left) {
val h = Math.min(height[left], height[right])
if (h <= maxH) {
if (height[left] > height[right]) {
right--
} else left++
continue
}
maxH = h
val v = (right - left) * h
if (v > max) {
max = v
}
if (height[left] > height[right]) {
right--
} else left++
}
return max
}
}
}
fun main() {
Leetcode_76.test()
// Leetcode_34.test()
// Leetcode_11.test()
}