Skip to content

Commit 56583bb

Browse files
add yet prod variant
1 parent 56d4a1d commit 56583bb

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,26 @@ fun maxSubsequenceProdVariant(nums: IntArray, k: Int): IntArray {
7878
.toIntArray()
7979
}
8080

81+
fun maxSubsequenceProdVariantII(nums: IntArray, k: Int): IntArray =
82+
nums.mapIndexed { index, num -> num to index }
83+
.let { pairs ->
84+
PriorityQueue<Pair<Int, Int>>(compareBy { it.first })
85+
.apply {
86+
pairs.forEach {
87+
offer(it)
88+
if (size > k) poll()
89+
}
90+
}.let { heap ->
91+
List(heap.size) { heap.poll() }
92+
.sortedBy { it.second }
93+
.map { it.first }
94+
.toIntArray()
95+
}
96+
}
97+
98+
99+
100+
101+
81102

82103

0 commit comments

Comments
 (0)