-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_20220814.kt
More file actions
510 lines (432 loc) · 14.9 KB
/
_20220814.kt
File metadata and controls
510 lines (432 loc) · 14.9 KB
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
@file:Suppress("DuplicatedCode", "ClassName", "ObjectPropertyName")
package _2022._08
import Testable
import utils.TreeNode
import utils.assertEqualTo
import utils.runTimedTests
import utils.tupleOf
import java.util.*
// 199. Binary Tree Right Side View
// https://leetcode.com/problems/binary-tree-right-side-view/
private interface Leetcode_199 {
fun rightSideView(root: TreeNode?): List<Int>
companion object : Testable {
override fun test() {}
}
// Runtime: 292 ms, faster than 40.23% of Kotlin online submissions for Binary Tree Right Side View.
// Memory Usage: 36.3 MB, less than 30.88% of Kotlin online submissions for Binary Tree Right Side View.
// https://leetcode.com/submissions/detail/773318529/
private class M1 : Leetcode_199 {
override fun rightSideView(root: TreeNode?): List<Int> {
if (root == null) return emptyList()
val res = ArrayList<Int>()
val q = LinkedList<TreeNode>()
val level = ArrayList<TreeNode>() // Not needed, see M1_ below
q.add(root)
while(q.isNotEmpty()) {
val size = q.size
level.clear()
for (i in 0 until size) {
val n = q.pop()
level.add(n)
if (n.left != null) q.add(n.left!!)
if (n.right != null) q.add(n.right!!)
}
if (level.isNotEmpty()) {
res.add(level.last().`val`)
}
}
return res
}
}
// slightly improved
private class M1_ : Leetcode_199 {
override fun rightSideView(root: TreeNode?): List<Int> {
if (root == null) return emptyList()
val res = ArrayList<Int>()
val q = LinkedList<TreeNode>()
q.add(root)
while(q.isNotEmpty()) {
val size = q.size
var rightMost: TreeNode? = null
for (i in 0 until size) {
val n = q.pop()
rightMost = n
if (n.left != null) q.add(n.left!!)
if (n.right != null) q.add(n.right!!)
}
if (rightMost != null) {
res.add(rightMost.`val`)
}
}
return res
}
}
}
// 1448. Count Good Nodes in Binary Tree
// https://leetcode.com/problems/count-good-nodes-in-binary-tree/
//
//
private interface Leetcode_1448 {
fun goodNodes(root: TreeNode?): Int
companion object : Testable {
override fun test() {
/*
val tests = listOf(
)
listOf(
M1()::,
).runTimedTests(tests) { a, b ->
invoke(a).assertEqualTo(b)
}
*/
}
}
// Runtime: 715 ms, faster than 21.05% of Kotlin online submissions for Count Good Nodes in Binary Tree.
// Memory Usage: 73.2 MB, less than 50.88% of Kotlin online submissions for Count Good Nodes in Binary Tree.
// https://leetcode.com/submissions/detail/773335970/
private class M1 : Leetcode_1448 {
var counter = 0
override fun goodNodes(root: TreeNode?): Int {
dfs(root, Int.MIN_VALUE)
return counter
}
fun dfs(node: TreeNode?, max: Int) {
if (node == null) return
var max = max
if (node.`val` >= max) {
max = node.`val`
println("added $max")
counter++
}
dfs(node.left, max)
dfs(node.right, max)
}
}
}
// 98. Validate Binary Search Tree
// https://leetcode.com/problems/validate-binary-search-tree/
private interface Leetcode_98 {
fun isValidBST(root: TreeNode?): Boolean
private class S2 : Leetcode_98 {
var cur = Int.MIN_VALUE
override fun isValidBST(root: TreeNode?): Boolean {
return bfs(root)
}
fun bfs(node: TreeNode?) : Boolean {
if (node == null) return true
if (!bfs(node.left)) return false
if (node.`val` <= cur) return false
cur = node.`val`
if (!bfs(node.right)) return false
return true
}
}
private class S1 : Leetcode_98 {
var cur: Int? = null
override fun isValidBST(root: TreeNode?): Boolean {
return bfs(root)
}
fun bfs(node: TreeNode?) : Boolean {
if (node == null) return true
if (!bfs(node.left)) return false
if (cur != null) {
if (node.`val` <= cur!!) return false
}
cur = node.`val`
if (!bfs(node.right)) return false
return true
}
}
}
// 230. Kth Smallest Element in a BST
// https://leetcode.com/problems/kth-smallest-element-in-a-bst/
private interface Leetcode_230 {
fun kthSmallest(root: TreeNode?, k: Int): Int
// Runtime: 405 ms, faster than 29.55% of Kotlin online submissions for Kth Smallest Element in a BST.
// Memory Usage: 44.7 MB, less than 9.55% of Kotlin online submissions for Kth Smallest Element in a BST.
// https://leetcode.com/submissions/detail/773374349/
private class S : Leetcode_230 {
var counter = 0
var value: Int = 0
override fun kthSmallest(root: TreeNode?, k: Int): Int {
dfs(root, k)
return value
}
fun dfs(root: TreeNode?, k: Int) {
if (root == null) return
dfs(root.left, k)
counter++
if (counter > k) return
value = root.`val`
dfs(root.right, k)
}
}
private class Solution : Leetcode_230 {
override fun kthSmallest(root: TreeNode?, k: Int): Int {
var root = root
var k = k
val stack: Deque<TreeNode> = ArrayDeque()
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root)
root = root.left
}
root = stack.pop()
--k
if (k == 0) {
break
}
root = root.right
}
return root!!.`val`
}
}
}
// 105. Construct Binary Tree from Preorder and Inorder Traversal
private interface Leetcode_105 {
fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode?
private class M : Leetcode_105 {
override fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {
TODO("Not yet implemented")
}
}
}
// 621. Task Scheduler
// https://leetcode.com/problems/task-scheduler/
private interface Leetcode_621 {
fun leastInterval(tasks: CharArray, n: Int): Int
companion object : Testable {
override fun test() {
val tests = listOf(
tupleOf(charArrayOf('A','A','A','B','B','B'), 2, 8),
tupleOf(charArrayOf('A','A','A','A','A','A','B','C','D', 'E', 'F', 'G'), 2, 16),
)
listOf(
M1()::leastInterval,
S1()::leastInterval,
// M2()::leastInterval,
).runTimedTests(tests) { a, b, c ->
invoke(a, b).assertEqualTo(c)
}
}
}
private class M1 : Leetcode_621 {
data class C(
val c: Char,
var count: Int,
var nextI: Int = 0
)
fun useNextC(q: PriorityQueue<C>, i: Int, n: Int) : C? {
val l = ArrayList<C>()
var res: C? = null
while(res == null && q.isNotEmpty()) {
val c = q.poll()
if (c.count == 0) continue
if (c.nextI <= i) {
res = c
c.nextI = i + n + 1
c.count = (c.count - 1)
}
if (c.count != 0) {
l += c
}
}
l.forEach { q.add(it) }
return res
}
override fun leastInterval(tasks: CharArray, n: Int): Int {
val map = HashMap<Char, Int>()
val heat = PriorityQueue<C>(){ a, b ->
if (a.count == b.count) {
a.nextI - b.nextI
} else {
b.count - a.count
}
}
var total = 0
for (c in tasks) {
total++
map[c] = (map[c] ?: 0) + 1
}
map.forEach { t, u ->
heat.offer(C(t, u))
}
var i = 0
while (heat.isNotEmpty()) {
val n = useNextC(heat, i, n)
print(n?.c ?: "-")
i++
}
return i
}
}
private class S1 : Leetcode_621 {
override fun leastInterval(tasks: CharArray, n: Int): Int {
val map = HashMap<Char, Int>()
val heat = PriorityQueue<Int>(){ a, b -> b - a }
val q = LinkedList<Pair<Int, Int>>()
for (c in tasks) {
map[c] = (map[c] ?: 0) + 1
}
map.forEach { t, u -> heat.offer(u) }
// a,b,0,a
var i = 0
while (heat.isNotEmpty() || q.isNotEmpty()) {
i++
if (q.isNotEmpty() && q.peek().second == i) {
heat.add(q.pop().first)
}
if (heat.isNotEmpty()) {
val t = heat.poll() - 1
if (t != 0) {
val pair = t to i + n + 1
q.add(pair)
}
}
}
return i
}
}
// not working
private class M2 : Leetcode_621 {
private class Item(
var count: Int,
var nextI: Int = 0
)
override fun leastInterval(tasks: CharArray, n: Int): Int {
// Not working for test case:
// ["A","A","A","A","A","A","B","C","D","E","F","G"]
//
val heat = PriorityQueue<Item>(){ a, b ->
if (a.count != b.count) b.count - a.count else a.nextI - b.nextI
}
val map = HashMap<Char, Int>()
for (c in tasks) {
map[c] = (map[c] ?: 0) + 1
}
map.forEach { t, u -> heat.offer(Item(u)) }
var i = 0
while (heat.isNotEmpty()) {
val t = heat.peek()
if (t.count > 0 && t.nextI <= i) {
heat.poll().apply {
count--
nextI = i + n + 1
println("i:$i new count:$count nextI:$nextI")
if (count > 0) heat.offer(this)
}
}
i++
}
return i
}
}
}
// 355. Design Twitter
private interface Leetcode_355 {
fun postTweet(userId: Int, tweetId: Int)
fun getNewsFeed(userId: Int): List<Int>
fun follow(followerId: Int, followeeId: Int)
fun unfollow(followerId: Int, followeeId: Int)
// Runtime: 301 ms, faster than 30.91% of Kotlin online submissions for Design Twitter.
// Memory Usage: 37.2 MB, less than 38.18% of Kotlin online submissions for Design Twitter.
// https://leetcode.com/submissions/detail/773836551/
private class S : Leetcode_355 {
// userId, tweetId
val feeds = ArrayList<Pair<Int, Int>>()
val follows = HashMap<Int, HashSet<Int>>()
override fun postTweet(userId: Int, tweetId: Int) {
println("postTweet $userId-$tweetId")
feeds += userId to tweetId
}
override fun getNewsFeed(userId: Int): List<Int> {
val set = follows[userId]
var i = feeds.lastIndex
val list = ArrayList<Int>()
while(list.size != 10 && i >= 0) {
val feed = feeds[i]
println("getNewsFeed userId:$userId feed.first:${feed.first}")
if (feed.first == userId || (set != null && feed.first in set)) {
list.add(feed.second)
}
i--
}
return list
}
override fun follow(followerId: Int, followeeId: Int) {
val set = follows[followerId]
if (set == null) {
follows[followerId] = HashSet<Int>()
}
follows[followerId]!!.add(followeeId)
}
override fun unfollow(followerId: Int, followeeId: Int) {
val set = follows[followerId] ?: return
set -= followeeId
}
}
}
// 332. Reconstruct Itinerary
// https://leetcode.com/problems/reconstruct-itinerary/
private interface Leetcode_332 {
fun findItinerary(tickets: List<List<String>>): List<String>
companion object : Testable {
override fun test() {
val tests = listOf(
listOf(
// [["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]]
listOf("JFK","KUL"),listOf("JFK","NRT"),listOf("NRT","JFK")
// ["JFK","NRT","JFK","KUL"]
) to listOf("JFK","NRT","JFK","KUL"),
)
listOf(
M1()::findItinerary
).runTimedTests(tests) { a, b ->
invoke(a).assertEqualTo(b)
}
}
}
// Runtime: 266 ms, faster than 97.73% of Kotlin online submissions for Reconstruct Itinerary.
// Memory Usage: 44.4 MB, less than 93.18% of Kotlin online submissions for Reconstruct Itinerary.
// https://leetcode.com/submissions/detail/774154210/
private class M1 : Leetcode_332 {
override fun findItinerary(tickets: List<List<String>>): List<String> {
val res = ArrayList<String>()
val map = HashMap<String, ArrayList<String>>()
for (t in tickets) {
if (map[t[0]] == null) {
map[t[0]] = ArrayList<String>()
}
map[t[0]]!!.add(t[1])
}
val sorted = map.mapValues {
ArrayList(it.value.sortedBy { it })
}
fun dfs(from: String) : Boolean {
res.add(from)
if (res.size == tickets.size + 1) return true
val dests = sorted[from] ?: return false
if (dests.isEmpty()) return false
val temp = ArrayList(dests)
for (t in temp) {
dests.remove(t)
if (dfs(t)) {
return true
} else {
// res.removeLast() // not available in leetcode
res.removeAt(res.lastIndex)
dests.add(t)
}
}
return false
}
dfs("JFK")
return res
}
}
}
val _20220814 = listOf<Testable>(
// Leetcode_621,
Leetcode_332,
)