Maximum Subsequence Score
Choose k indexes so that the sum of their nums1 values times their smallest nums2 value is as large as possible.
Given two integer arrays nums1nums2kknums1nums2k
Example 1
Output: 12
Explanation: indexes 0, 2 and 3 give (1 + 3 + 2) × min(2, 3, 4) = 6 × 2 = 12.
Example 2
Output: 30
Explanation: index 2 alone gives 3 × 10 = 30.
The Idea
Think of a convoy of trucks. Each truck carries some cargo and has its own top speed, but the trucks drive together, so the convoy moves at the speed of its slowest truck. The cargo delivered per hour is the total cargo times that slowest speed. In this problem, nums1[i]nums2[i]k
Take Example 1, where 3 of the 4 indexes must be chosen. There are four choices. Indexes 0, 1, 2 give 7 × 1 = 7, and indexes 0, 1, 3 give 6 × 1 = 6. Indexes 1, 2, 3 give 8 × 1 = 8, and indexes 0, 2, 3 give 6 × 2 = 12. Index 1 carries 3, one of the largest loads, but its speed of 1 pulls every choice that includes it down to a multiplier of 1.
So the two parts of the score pull against each other: a big nums1nums2
We can, by deciding first which index is the slowest one in the choice. Say it is index 0, with nums2nums2nums1
Trying every index as the slowest one becomes easy when the indexes are visited in order of nums2nums2nums1
Look at the picture below. Each row is one index reached, in that order.
In the rows labelled index 3 and index 2, fewer than 3 values are kept, so there is no score yet. In the row labelled index 0, the kept values are 1, 2 and 3, their sum is 6, and the score is 6 × 2 = 12. In the last row, index 1 brings a 3, the heap would hold four values, and the smallest, 1, is dropped. The sum becomes 8, but the speed falls to 1, so the score is only 8 × 1 = 8. The best score seen is 12, the answer.
So the sorted order settles the smallest nums2
Solution Approach
Turning that idea into code means settling four things: the order in which to visit the indexes, how to keep the knums1
Trying every choice
Why not simply try every choice of kk
Visiting the indexes by nums2
What exactly gets sorted? Not nums2nums1nums2nums2nums2
Keeping the k largest nums1 values
How do we keep the knums1kPriorityQueue(a, b) -> Integer.compare(a, b)
When a score counts
A score is computed only when the heap holds exactly kknums2
What if the index just reached is the one whose value was dropped? Then every kept index has a nums2nums2
How large a score can get
Can the score outgrow an intintnums1nums2longlong
Java Solution
The method maxScore
- First, build an array
holding the indexes 0 to n - 1, asorder values so it can be sorted with a comparator. Sort it byInteger , largest first, withnums2 .Arrays.sort(order, (a, b) -> Integer.compare(nums2[b], nums2[a])) - Create the min-heap
, akept of the keptPriorityQueue values, and twonums1 variables that start at 0:long for the kept values andsum for the best score so far.best - Loop over
withorder . Addi tonums1[i] and tokept . Ifsum now holds more thankept values, take out its smallest withk and subtract it fromkept.poll() .sum - When
holds exactlykept values, computek and keep it insum * nums2[i] if it is larger.best - After the last index,
holds the largest score, and the method returns it.best
The mainklong
import java.util.Arrays;
import java.util.PriorityQueue;
public class MaximumSubsequenceScore {
public static long maxScore(int[] nums1, int[] nums2, int k) {
int n = nums1.length;
Integer[] order = new Integer[n];
for (int i = 0; i < n; i++) {
order[i] = i;
}
Arrays.sort(order, (a, b) -> Integer.compare(nums2[b], nums2[a])); // nums2, largest first
// min-heap: the smallest kept nums1 value is on top
PriorityQueue<Integer> kept = new PriorityQueue<>((a, b) -> Integer.compare(a, b));
long sum = 0;
long best = 0;
for (int i : order) {
kept.add(nums1[i]);
sum += nums1[i];
if (kept.size() > k) {
sum -= kept.poll(); // drop the smallest kept nums1 value
}
if (kept.size() == k) {
// nums2[i] is the smallest nums2 met so far
best = Math.max(best, sum * nums2[i]);
}
}
return best;
}
public static void main(String[] args) {
System.out.println(maxScore(new int[] {1, 3, 3, 2}, new int[] {2, 1, 3, 4}, 3));
System.out.println(maxScore(new int[] {4, 2, 3, 1, 1}, new int[] {7, 5, 10, 9, 6}, 1));
System.out.println(maxScore(new int[] {1, 3, 3, 2}, new int[] {2, 1, 3, 4}, 4));
System.out.println(maxScore(new int[] {5, 5, 1}, new int[] {3, 3, 2}, 2));
int big = 100000;
System.out.println(maxScore(new int[] {big, big, big}, new int[] {big, big, big}, 3));
}
}
Running it prints:
12
30
9
30
30000000000
The answers 12 and 30 match the two examples. The 9 below them is Example 1 with k
The second 30 comes from nums1nums2kbest
Look at the last line, 30000000000. With sumintint
Complexity
The work is the sort of the indexes, plus one heap step or two for each index.
- Time: O(n log n), because sorting the n indexes by
costs O(n log n), and then each index makes onenums2 and at most oneadd , each O(log k) on a heap of at most k + 1 values.poll - Space: O(n), because the
array holds all n indexes, while the heap never holds more than k + 1 values.order
Visiting items in order of one value while a heap keeps the best of another also solves