Skip to content

Commit 56d4a1d

Browse files
add 2099
1 parent 98821cc commit 56d4a1d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,4 @@ fun maxSubsequence(nums: IntArray, k: Int): IntArray {
309309
indices.sortBy { it.second }
310310

311311
return indices.map { it.first }.toIntArray()
312-
}
312+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,25 @@ fun findDiagonalOrderProdVariantII(nums: List<List<Int>>): IntArray {
5858
}
5959
}
6060

61+
/**
62+
* 2099. Find Subsequence of Length K With the Largest Sum
63+
* Prod Variant
64+
*/
65+
66+
fun maxSubsequenceProdVariant(nums: IntArray, k: Int): IntArray {
67+
val heap = PriorityQueue<Pair<Int, Int>>(compareBy { it.first })
68+
69+
nums.forEachIndexed { index, num ->
70+
heap.offer(index to num)
71+
if (heap.size > k) heap.poll()
72+
}
73+
74+
return mutableListOf<Pair<Int, Int>>()
75+
.apply { heap.forEach { add(it) } }
76+
.sortedBy { it.second }
77+
.map { it.first }
78+
.toIntArray()
79+
}
80+
6181

6282

0 commit comments

Comments
 (0)