1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

cs: add «2542. Maximum Subsequence Score»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-10 22:40:07 +02:00
parent e8bb5d909a
commit 45acbc38cf
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,20 @@
using System.Collections.Generic;
public class Solution {
public long MaxScore(int[] nums1, int[] nums2, int k) {
long prefixSum = 0, maxScore = 0;
var heap = new PriorityQueue<int, int>();
foreach (var (num, mult) in nums1.Zip(nums2).OrderBy(key => key.Second).Reverse()) {
prefixSum += num;
heap.Enqueue(num, num);
if (heap.Count == k) {
maxScore = Math.Max(maxScore, prefixSum * mult);
prefixSum -= heap.Dequeue();
}
}
return maxScore;
}
}