Skip to content

Commit 98821cc

Browse files
add 2099
1 parent ef4c3f0 commit 98821cc

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.backtracking.findDifferentBinaryString
4+
import com.github.contest.heap.maxSubsequence
55
import java.util.TreeMap
66

77

@@ -11,8 +11,39 @@ import java.util.TreeMap
1111

1212
fun main() {
1313

14-
findDifferentBinaryString(arrayOf("00", "01"))
14+
maxSubsequence(intArrayOf(-1, -2, 3, 4), 3).apply { this.printArray() }
15+
}
16+
17+
fun generateTesting() {
18+
val sequence = sequenceOf(3, 5, 6, 7, 7, 8, 8, 8, 9, 3)
19+
sequence.map { it * 2 }
20+
.filter { it > 3 }
21+
.filter { it > 2 }
22+
.constrainOnce()
23+
24+
25+
}
26+
27+
fun generateSequence() {
28+
var counter = 0
29+
val numbers = generateSequence {
30+
if (counter < 5) {
31+
counter++
32+
counter
33+
} else null
34+
}
35+
36+
println(numbers.toList())
37+
}
38+
39+
fun doing() {
40+
val collection = mutableListOf(listOf(5), listOf(2), listOf(4))
41+
val other = mutableListOf(3, 5, 10)
42+
val res = collection.flatMap {
43+
it.asReversed()
44+
}
1545

46+
println(res)
1647
}
1748

1849

contest/src/main/java/com/github/contest/heap/HeapLeetcode.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,27 @@ fun findDiagonalOrder(nums: List<List<Int>>): IntArray {
286286
}
287287

288288
return result
289+
}
290+
291+
/**
292+
* 2099. Find Subsequence of Length K With the Largest Sum
293+
*/
294+
295+
fun maxSubsequence(nums: IntArray, k: Int): IntArray {
296+
val heap = PriorityQueue<Pair<Int, Int>>(compareBy { it.first })
297+
for (i in nums.indices) {
298+
heap.offer(Pair(nums[i], i))
299+
if (heap.size > k) {
300+
heap.poll()
301+
}
302+
}
303+
304+
val indices = mutableListOf<Pair<Int, Int>>()
305+
while (heap.isNotEmpty()) {
306+
indices.add(heap.poll())
307+
}
308+
309+
indices.sortBy { it.second }
310+
311+
return indices.map { it.first }.toIntArray()
289312
}

0 commit comments

Comments
 (0)