-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_20220802.kt
583 lines (502 loc) · 18.4 KB
/
_20220802.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
@file:Suppress("DuplicatedCode", "ClassName", "ObjectPropertyName")
package _2022._08
import Testable
import utils.assertEqualTo
import utils.runTimedTests
import utils.tupleOf
import java.util.*
// 208. Implement Trie (Prefix Tree)
// https://leetcode.com/problems/implement-trie-prefix-tree/
private interface Leetcode_208 {
fun insert(word: String)
fun search(word: String): Boolean
fun startsWith(prefix: String): Boolean
companion object : Testable {
override fun test() {
listOf(
M1::class.java,
M2::class.java,
M3::class.java,
).runTimedTests() {
this.getDeclaredConstructor().newInstance().apply {
insert("apple")
search("apple").assertEqualTo(true)
search("app").assertEqualTo(false)
startsWith("app").assertEqualTo(true)
insert("app")
search("app").assertEqualTo(true)
search("appi").assertEqualTo(false)
}
// ["Trie","insert","insert","insert","insert","insert","insert","search","search","search","search","search","search","search","search","search","startsWith","startsWith","startsWith","startsWith","startsWith","startsWith","startsWith","startsWith","startsWith"]
// [[],["app"],["apple"],["beer"],["add"],["jam"],["rental"],["apps"],["app"],["ad"],["applepie"],["rest"],["jan"],["rent"],["beer"],["jam"],["apps"],["app"],["ad"],["applepie"],["rest"],["jan"],["rent"],["beer"],["jam"]]
//[null,null,null,null,null,null,null,false,true,false,false,false,false,false,true,true,false,true,true,false,false,false,true,true,true]
this.getDeclaredConstructor().newInstance().apply {
insert("app")
insert("apple")
insert("beer")
insert("rental")
search("apps").assertEqualTo(false)
search("app").assertEqualTo(true)
}
}
}
}
// kotlin build in
private class M1 : Leetcode_208 {
private val pq = PriorityQueue<String>()
override fun insert(word: String) {
pq.offer(word)
}
override fun search(word: String): Boolean {
return pq.contains(word)
}
override fun startsWith(prefix: String): Boolean {
return pq.any { it.startsWith(prefix) }
}
}
// sorted list
private class M2 : Leetcode_208 {
private val list = ArrayList<String>()
private fun findIndexOf(word: String) : Int {
if (list.isEmpty()) return 0
var l = 0
var r = list.lastIndex
while (l <= r) {
val m = l + (r - l) / 2
val mid = list[m]
val diff = mid.compareTo(word)
if (diff == 0) {
l = m
r = m
break
} else if (diff > 0) {
r = m - 1
} else {
l = m + 1
}
}
return l
}
override fun insert(word: String) {
val i = findIndexOf(word)
list.add(i, word)
}
override fun search(word: String): Boolean {
val index = findIndexOf(word)
return list.getOrNull(index) == word
}
override fun startsWith(prefix: String): Boolean {
val index = findIndexOf(prefix)
return list.getOrNull(index)?.startsWith(prefix) ?: false
}
}
// using neetcode.io method
// linked nodes
// Runtime: 715 ms, faster than 59.80% of Kotlin online submissions for Implement Trie (Prefix Tree).
// Memory Usage: 93.8 MB, less than 29.65% of Kotlin online submissions for Implement Trie (Prefix Tree).
// https://leetcode.com/submissions/detail/762799057/
private class M3 : Leetcode_208 {
private class Node {
// or this could've been a hashmap too
val next = Array<Node?>(26) { null }
var isEnd: Boolean = false
}
private val head = Node()
override fun insert(word: String) {
var h = head
word.forEach{
h = if (h.next[it - 'a'] == null) {
val n = Node()
h.next[it - 'a'] = n
n
} else h.next[it - 'a']!!
}
h.isEnd = true
}
override fun search(word: String): Boolean {
var h = head
for (w in word) {
h = h.next.getOrNull(w - 'a') ?: return false
}
return h.isEnd
}
override fun startsWith(prefix: String): Boolean {
var h = head
for (w in prefix) {
h = h.next.getOrNull(w - 'a') ?: return false
}
return true
}
}
}
// 981. Time Based Key-Value Store
// https://leetcode.com/problems/time-based-key-value-store/
private interface Leetcode_981 {
fun set(key: String, value: String, timestamp: Int)
fun get(key: String, timestamp: Int): String
companion object : Testable {
override fun test() {
listOf(
M1::class.java,
).runTimedTests {
getDeclaredConstructor().newInstance().apply {
val k = "love"
set(k, "h", 10)
set(k, "l", 20)
get(k, 5).assertEqualTo("")
get(k, 10).assertEqualTo("h")
get(k, 15).assertEqualTo("h")
get(k, 20).assertEqualTo("l")
get(k, 25).assertEqualTo("l")
}
}
}
}
private class M1 : Leetcode_981 {
// key timestamp to value
private val map = HashMap<String, ArrayList<Pair<Int, String>>>()
override fun set(key: String, value: String, timestamp: Int) {
map.getOrPut(key){
ArrayList()
}.add(timestamp to value)
}
override fun get(key: String, timestamp: Int): String {
val list = map[key] ?: return ""
if (list.isEmpty()) return ""
if (list[0].first > timestamp) return ""
if (list.last().first <= timestamp) return list.last().second
var l = 0
var r = list.lastIndex
while(l <= r) {
val m = l + (r - l) / 2
val (t, v) = list[m]
when {
timestamp == t -> return v
(l == r - 1) -> break
timestamp < t -> {
r = m - 1
}
else -> {
l = m + 1
}
}
}
return list[l].second
}
}
}
// 973. K Closest Points to Origin
// https://leetcode.com/problems/k-closest-points-to-origin/
private interface Leetcode_973 {
fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray>
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(
arrayOf(intArrayOf(1,3), intArrayOf(-2,2)),
1,
arrayOf(intArrayOf(-2,2))
),
tupleOf(
arrayOf(intArrayOf(3,3), intArrayOf(5,-1), intArrayOf(-2,4)),
2,
arrayOf(intArrayOf(3,3), intArrayOf(-2,4))
),
)
listOf(
M1()::kClosest,
).runTimedTests(tests) { a, b, c ->
invoke(a, b).assertEqualTo(
c,
toStr = { joinToString(prefix = "[", postfix = "]"){ it.joinToString(prefix = "[", postfix = "]") } },
// not ignoring orders
// checker = { exp, act ->
// exp.contentDeepEquals(act)
// }
)
}
}
}
// Runtime: 1241 ms, faster than 82.06% of Kotlin online submissions for K Closest Points to Origin.
// Memory Usage: 102.7 MB, less than 61.44% of Kotlin online submissions for K Closest Points to Origin.
// https://leetcode.com/submissions/detail/763038195/
private class M1 : Leetcode_973 {
private data class N(
val coords: IntArray
) {
val distance = coords.run { (get(0) * get(0)) + (get(1) * get(1)) }
}
private lateinit var pq : PriorityQueue<N>
override fun kClosest(points: Array<IntArray>, k: Int): Array<IntArray> {
pq = PriorityQueue<N>(k) { o1, o2 -> o2.distance - o1.distance }
for (p in points) {
pq.offer(N(p))
if (pq.size > k){
pq.poll()
}
}
return Array<IntArray>(k){ pq.poll().coords }
}
}
}
// 215. Kth Largest Element in an Array
// https://leetcode.com/problems/kth-largest-element-in-an-array/
private interface Leetcode_215 {
fun findKthLargest(nums: IntArray, k: Int): Int
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(
intArrayOf(3,2,1,5,6,4), 2, 5
),
tupleOf(
intArrayOf(3,2,3,1,2,4,5,5,6), 4, 4
)
)
listOf(
M1()::findKthLargest,
M2()::findKthLargest,
S1()::findKthLargest,
).runTimedTests(tests) { a, b, c ->
invoke(a, b).assertEqualTo(c)
}
}
}
// Runtime: 800 ms, faster than 6.69% of Kotlin online submissions for Kth Largest Element in an Array.
// Memory Usage: 70.7 MB, less than 8.41% of Kotlin online submissions for Kth Largest Element in an Array.
// https://leetcode.com/submissions/detail/763069863/
private class M1 : Leetcode_215 {
override fun findKthLargest(nums: IntArray, k: Int): Int {
val pq = PriorityQueue<Int>() // { a, b -> b - a }
nums.forEach {
pq.offer(it)
if (pq.size > k) pq.poll()
}
return pq.poll()
}
}
private class M2 : Leetcode_215 {
override fun findKthLargest(nums: IntArray, k: Int): Int {
val queue = PriorityQueue<Int>()
for(i in 0 until nums.size){
if(queue.size < k){
queue.add(nums[i])
}else{
if(queue.peek() < nums[i]){
queue.poll()
queue.add(nums[i])
}
}
}
return queue.peek()
}
}
// sample 181 ms submission
private class S1 : Leetcode_215 {
override fun findKthLargest(nums: IntArray, k: Int): Int {
val heap: MyPriorityQueue = MyPriorityQueueImpl()
heap.buildMaxHeap(nums)
var i = 1
while (i < k) {
heap.poll()
i++
}
return heap.peek()!!
}
interface MyPriorityQueue {
fun buildMaxHeap(array: IntArray)
fun peek(): Int?
fun poll(): Int?
fun insert(item: Int)
fun increasePriority(index: Int, priority: Int)
}
class MyPriorityQueueImpl : MyPriorityQueue {
private var capacity = 20000
private var heapSize = 0
private var A = IntArray(capacity) { Int.MIN_VALUE }
override fun buildMaxHeap(array: IntArray) {
heapSize = array.size
for (i in 0 until array.size) {
A[i] = array[i]
}
for (i in array.size / 2 downTo 0) {
heapifyDown(i)
}
}
override fun peek(): Int? = A[0]
override fun poll(): Int? {
val peek = peek()
A[0] = A[heapSize - 1]
heapSize--
heapifyDown(0)
return peek
}
override fun insert(item: Int) {
ensureCapacity()
A[heapSize] = item
heapSize++
heapifyUp(heapSize - 1)
}
override fun increasePriority(index: Int, priority: Int) {
if (A[index] < priority) {
A[index] = priority
heapifyUp(index)
}
}
private fun leftIndex(index: Int) = index * 2 + 1
private fun rightIndex(index: Int) = index * 2 + 2
private fun parentIndex(index: Int) = (index - 1) / 2
private fun hasLeft(index: Int) = leftIndex(index) in (0 until heapSize)
private fun hasRight(index: Int) = rightIndex(index) in (0 until heapSize)
private fun hasParent(index: Int) = parentIndex(index) in (0 until heapSize)
private fun leftChild(index: Int) = if (hasLeft(index)) A[leftIndex(index)] else null
private fun rightChild(index: Int) = if (hasRight(index)) A[rightIndex(index)] else null
private fun parent(index: Int) = if (hasParent(index)) A[parentIndex(index)] else null
private fun heapifyDown(index: Int) {
val leftIndex = index * 2 + 1
val rightIndex = index * 2 + 2
var maxIndex = index
if (hasLeft(index) && A[leftIndex] > A[maxIndex]) {
maxIndex = leftIndex
}
if (hasLeft(index) && A[rightIndex] > A[maxIndex]) {
maxIndex = rightIndex
}
if (maxIndex != index) {
swap(maxIndex, index)
heapifyDown(maxIndex)
}
}
private fun heapifyUp(index: Int) {
var i = index
while (hasParent(i) && A[parentIndex(i)] < A[i]) {
swap(parentIndex(i), i)
i = parentIndex(i)
}
}
private fun swap(i: Int, j: Int) {
val temp = A[i]
A[i] = A[j]
A[j] = temp
}
private fun ensureCapacity() {
if (heapSize == capacity) {
capacity *= 2
A = Arrays.copyOf(A, capacity)
}
}
}
}
}
// 78. Subsets
// https://leetcode.com/problems/subsets/
private interface Leetcode_78 {
fun subsets(nums: IntArray): List<List<Int>>
companion object : Testable {
override fun test() {
val tests = listOf(
intArrayOf(1,2,3)
)
listOf(
M1()::subsets,
).runTimedTests {
tests.forEach {
val r = invoke(it)
println("r size:${r.size}, content:${r.joinToString()}")
}
}
}
}
// Runtime: 326 ms, faster than 41.07% of Kotlin online submissions for Subsets.
// Memory Usage: 36.1 MB, less than 98.57% of Kotlin online submissions for Subsets.
// https://leetcode.com/submissions/detail/763136249/
private class M1 : Leetcode_78 {
val res = ArrayList<List<Int>>()
private fun helper(nums: IntArray, index: Int, list: ArrayList<Int>) {
if (index == nums.size) {
res.add(list)
return
}
helper(nums, index + 1, list)
val l = ArrayList<Int>(list)
val i = nums[index]
l.add(i)
helper(nums, index + 1, l)
}
override fun subsets(nums: IntArray): List<List<Int>> {
helper(nums, 0, ArrayList())
return res
}
}
}
// 39. Combination Sum
// https://leetcode.com/problems/combination-sum/
private interface Leetcode_39 {
companion object : Testable {
override fun test() {
}
}
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>>
// Runtime: 278 ms, faster than 91.02% of Kotlin online submissions for Combination Sum.
// Memory Usage: 40.7 MB, less than 89.39% of Kotlin online submissions for Combination Sum.
// https://leetcode.com/submissions/detail/763196038/
private class M1 : Leetcode_39 {
private val res = ArrayList<List<Int>>()
private val l = ArrayList<Int>()
override fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
dfs(0, 0, candidates, target)
return res
}
private fun dfs(sum: Int, index: Int, candidates: IntArray, target: Int) {
if (sum == target) {
res.add(ArrayList(l))
}
if (sum < target){
for (i in index until candidates.size) {
val c = candidates[i]
val s = sum + c
if (s <= target) {
l += c
dfs(s, i, candidates, target)
l.removeAt(l.size - 1)
}
}
}
}
}
private class S1 : Leetcode_39 {
override fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
val ans: MutableList<List<Int>> = ArrayList()
val cur: MutableList<Int> = ArrayList()
backtrack(candidates, target, ans, cur, 0)
return ans
}
fun backtrack(
candidates: IntArray,
target: Int,
ans: MutableList<List<Int>>,
cur: MutableList<Int>,
index: Int
) {
if (target == 0) {
ans.add(ArrayList<Int>(cur))
} else if (target < 0 || index >= candidates.size) {
return
} else {
cur.add(candidates[index])
backtrack(candidates, target - candidates[index], ans, cur, index)
cur.remove(cur[cur.size - 1])
backtrack(candidates, target, ans, cur, index + 1)
}
}
}
}
val _20220802 = listOf<Testable>(
// Leetcode_208,
// Leetcode_211,
// Leetcode_981,
// Leetcode_973,
// Leetcode_215,
Leetcode_78,
Leetcode_39
)