File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * MinPriorityQueue | Sorting
3
+ * Time O(n*log(n)) | space O(n)
4
+ * https://leetcode.com/problems/maximum-subsequence-score/
5
+ * @param {number[] } nums1
6
+ * @param {number[] } nums2
7
+ * @param {number } k
8
+ * @return {number }
9
+ */
10
+ var maxScore = function ( nums1 , nums2 , k ) {
11
+
12
+ const minQ = new MinPriorityQueue ( {
13
+ compare : ( a , b ) => {
14
+ return a - b ;
15
+ }
16
+ } ) ;
17
+
18
+ let maxScore = 0 ;
19
+ let runningTotal = 0 ;
20
+ let takenElements = 0 ;
21
+
22
+ const nums12 = nums1 . map ( ( num , idx ) => {
23
+ return [ num , nums2 [ idx ] ] ;
24
+ } ) ;
25
+
26
+ nums12 . sort ( ( a , b ) => {
27
+ return b [ 1 ] - a [ 1 ] ;
28
+ } ) ;
29
+
30
+ for ( let i = 0 ; i < nums12 . length ; i ++ ) {
31
+ const n1 = nums12 [ i ] [ 0 ] ;
32
+ const n2 = nums12 [ i ] [ 1 ] ;
33
+
34
+ runningTotal += n1 ;
35
+ minQ . enqueue ( n1 ) ;
36
+
37
+ if ( minQ . size ( ) === k ) {
38
+ maxScore = Math . max ( maxScore , runningTotal * n2 ) ;
39
+ }
40
+ if ( minQ . size ( ) > k ) {
41
+ runningTotal -= minQ . dequeue ( ) ;
42
+ maxScore = Math . max ( maxScore , runningTotal * n2 ) ;
43
+ }
44
+ }
45
+
46
+ return maxScore ;
47
+ } ;
You can’t perform that action at this time.
0 commit comments