Skip to content

Commit f4bc50f

Browse files
committed
added problem 2542
1 parent f9b465a commit f4bc50f

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package problem2542
2+
3+
import (
4+
"container/heap"
5+
"sort"
6+
)
7+
8+
/*
9+
You are given two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k.
10+
You must choose a subsequence of indices from nums1 of length k.
11+
For chosen indices i0, i1, ..., ik - 1, your score is defined as:
12+
The sum of the selected elements from nums1 multiplied with the minimum of the selected elements from nums2.
13+
It can defined simply as: (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]).
14+
Return the maximum possible score.
15+
A subsequence of indices of an array is a set that can be derived from the set {0, 1, ..., n-1} by deleting some or no elements.
16+
*/
17+
18+
func maxScore(nums1, nums2 []int, k int) int64 {
19+
var res, sum int64
20+
var order = make([]int, len(nums1))
21+
for i := range order {
22+
order[i] = i
23+
}
24+
25+
sort.Slice(order, func(i, j int) bool {
26+
return nums2[order[i]] >= nums2[order[j]]
27+
})
28+
29+
var pq = MakeHeap(MinHeap)
30+
31+
for i := range order {
32+
e, s := nums2[order[i]], nums1[order[i]]
33+
heap.Push(pq, s)
34+
sum += int64(s)
35+
if pq.Len() > k {
36+
sum -= int64(heap.Pop(pq).(int))
37+
}
38+
if pq.Len() == k {
39+
res = max(res, sum*int64(e))
40+
}
41+
}
42+
43+
return res
44+
}
45+
46+
type Heap struct {
47+
Values []int
48+
LessFunc func(int, int) bool
49+
}
50+
51+
func (h *Heap) Less(i, j int) bool { return h.LessFunc(h.Values[i], h.Values[j]) }
52+
func (h *Heap) Swap(i, j int) { h.Values[i], h.Values[j] = h.Values[j], h.Values[i] }
53+
func (h *Heap) Len() int { return len(h.Values) }
54+
func (h *Heap) Peek() int { return h.Values[0] }
55+
func (h *Heap) Pop() (v interface{}) {
56+
h.Values, v = h.Values[:h.Len()-1], h.Values[h.Len()-1]
57+
return v
58+
}
59+
func (h *Heap) Push(v interface{}) { h.Values = append(h.Values, v.(int)) }
60+
61+
func MakeHeap(less func(int, int) bool) *Heap {
62+
return &Heap{LessFunc: less}
63+
}
64+
65+
func MaxHeap(i, j int) bool { return i > j }
66+
func MinHeap(i, j int) bool { return i < j }
67+
68+
func max(a, b int64) int64 {
69+
if a > b {
70+
return a
71+
}
72+
return b
73+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problem2542
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Nums1, Nums2 []int
12+
K int
13+
Expected int64
14+
}
15+
16+
var Results = []Result{
17+
{[]int{1, 3, 3, 2}, []int{2, 1, 3, 4}, 3, 12},
18+
{[]int{4, 2, 3, 1, 1}, []int{7, 5, 10, 9, 6}, 1, 30},
19+
}
20+
21+
func TestMaxScoreSubsequence(t *testing.T) {
22+
assert := assert.New(t)
23+
24+
for _, res := range Results {
25+
want := res.Expected
26+
got := maxScore(res.Nums1, res.Nums2, res.K)
27+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
28+
}
29+
}
30+
31+
func BenchmarkMaxScoreSubsequenceh(b *testing.B) {
32+
for i := 0; i < b.N; i++ {
33+
for _, res := range Results {
34+
maxScore(res.Nums1, res.Nums2, res.K)
35+
}
36+
}
37+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ Each problem is in it's own directory, with test files. There are helper package
493493
| 2477 | [Minimum Fuel Cost to Report to the Capital](https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital) | [My Solution](./problems/problem2477) ||
494494
| 2492 | [Minimum Score of a Path Between Two Cities](https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities) | [My Solution](./problems/problem2492) ||
495495
| 2512 | [Reward Top K Students](https://leetcode.com/problems/reward-top-k-students) | [My Solution](./problems/problem2512) ||
496+
| 2542 | [Maximum Subsequence Score](https://leetcode.com/problems/maximum-subsequence-score) | [My Solution](./problems/problem2542) ||
496497
| 2553 | [Separate the Digits in an Array](https://leetcode.com/problems/separate-the-digits-in-an-array) | [My Solution](./problems/problem2553) ||
497498
| 2558 | [Take Gifts From the Richest Pile](https://leetcode.com/problems/take-gifts-from-the-richest-pile) | [My Solution](./problems/problem2558) ||
498499
| 2562 | [Find the Array Concatenation Value](https://leetcode.com/problems/find-the-array-concatenation-value) | [My Solution](./problems/problem2562) ||
@@ -511,6 +512,7 @@ Each problem is in it's own directory, with test files. There are helper package
511512
| 0087 | Needs revision |
512513
| 1857 | Needs revision |
513514
| 0879 | Needs revision |
515+
| 2542 | Needs revision |
514516
| 1799 | Black magic |
515517

516518

0 commit comments

Comments
 (0)