Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k. You must choose a subsequence of indices from nums1 of length k.
For chosen indices i0, i1, ..., ik - 1, your score is defined as:
nums1 multiplied with the minimum of the selected elements from nums2.(nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]).Return the maximum possible score.
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.
Example 1:
Input: nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3 Output: 12 Explanation: The four possible subsequence scores are: - We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7. - We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6. - We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12. - We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8. Therefore, we return the max score, which is 12.
Example 2:
Input: nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1 Output: 30 Explanation: Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score.
Constraints:
n == nums1.length == nums2.length1 <= n <= 1050 <= nums1[i], nums2[j] <= 1051 <= k <= nProblem summary: You are given two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k. You must choose a subsequence of indices from nums1 of length k. For chosen indices i0, i1, ..., ik - 1, your score is defined as: The sum of the selected elements from nums1 multiplied with the minimum of the selected elements from nums2. It can defined simply as: (nums1[i0] + nums1[i1] +...+ nums1[ik - 1]) * min(nums2[i0] , nums2[i1], ... ,nums2[ik - 1]). Return the maximum possible score. 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,3,3,2] [2,1,3,4] 3
[4,2,3,1,1] [7,5,10,9,6] 1
ipo)minimum-cost-to-hire-k-workers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2542: Maximum Subsequence Score
class Solution {
public long maxScore(int[] nums1, int[] nums2, int k) {
int n = nums1.length;
int[][] nums = new int[n][2];
for (int i = 0; i < n; ++i) {
nums[i] = new int[] {nums1[i], nums2[i]};
}
Arrays.sort(nums, (a, b) -> b[1] - a[1]);
long ans = 0, s = 0;
PriorityQueue<Integer> q = new PriorityQueue<>();
for (int i = 0; i < n; ++i) {
s += nums[i][0];
q.offer(nums[i][0]);
if (q.size() == k) {
ans = Math.max(ans, s * nums[i][1]);
s -= q.poll();
}
}
return ans;
}
}
// Accepted solution for LeetCode #2542: Maximum Subsequence Score
func maxScore(nums1 []int, nums2 []int, k int) int64 {
type pair struct{ a, b int }
nums := []pair{}
for i, a := range nums1 {
b := nums2[i]
nums = append(nums, pair{a, b})
}
sort.Slice(nums, func(i, j int) bool { return nums[i].b > nums[j].b })
q := hp{}
var ans, s int
for _, e := range nums {
a, b := e.a, e.b
s += a
heap.Push(&q, a)
if q.Len() == k {
ans = max(ans, s*b)
s -= heap.Pop(&q).(int)
}
}
return int64(ans)
}
type hp struct{ sort.IntSlice }
func (h hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
a := h.IntSlice
v := a[len(a)-1]
h.IntSlice = a[:len(a)-1]
return v
}
# Accepted solution for LeetCode #2542: Maximum Subsequence Score
class Solution:
def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int:
nums = sorted(zip(nums2, nums1), reverse=True)
q = []
ans = s = 0
for a, b in nums:
s += b
heappush(q, b)
if len(q) == k:
ans = max(ans, s * a)
s -= heappop(q)
return ans
// Accepted solution for LeetCode #2542: Maximum Subsequence Score
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2542: Maximum Subsequence Score
// class Solution {
// public long maxScore(int[] nums1, int[] nums2, int k) {
// int n = nums1.length;
// int[][] nums = new int[n][2];
// for (int i = 0; i < n; ++i) {
// nums[i] = new int[] {nums1[i], nums2[i]};
// }
// Arrays.sort(nums, (a, b) -> b[1] - a[1]);
// long ans = 0, s = 0;
// PriorityQueue<Integer> q = new PriorityQueue<>();
// for (int i = 0; i < n; ++i) {
// s += nums[i][0];
// q.offer(nums[i][0]);
// if (q.size() == k) {
// ans = Math.max(ans, s * nums[i][1]);
// s -= q.poll();
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2542: Maximum Subsequence Score
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2542: Maximum Subsequence Score
// class Solution {
// public long maxScore(int[] nums1, int[] nums2, int k) {
// int n = nums1.length;
// int[][] nums = new int[n][2];
// for (int i = 0; i < n; ++i) {
// nums[i] = new int[] {nums1[i], nums2[i]};
// }
// Arrays.sort(nums, (a, b) -> b[1] - a[1]);
// long ans = 0, s = 0;
// PriorityQueue<Integer> q = new PriorityQueue<>();
// for (int i = 0; i < n; ++i) {
// s += nums[i][0];
// q.offer(nums[i][0]);
// if (q.size() == k) {
// ans = Math.max(ans, s * nums[i][1]);
// s -= q.poll();
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.