cs: add «2542. Maximum Subsequence Score»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
e8bb5d909a
commit
45acbc38cf
1 changed files with 20 additions and 0 deletions
20
cs/maximum-subsequence-score.cs
Normal file
20
cs/maximum-subsequence-score.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue