Tap here to switch tabs
Problem
Submissions

Problem: Choose K Elements With Maximum Sum

Choose K Elements With Maximum Sum asks you to use two arrays: for each position, return the largest sum of at most k values from the second array paired with first-array values smaller than the current one.

Statement

You are given two integer arrays nums1 and nums2 of size n, along with an integer k.

For each index i from 00 to n −1- 1, find all indices j such that nums1[j] < nums1[i]. From these indices, select at most k indices such that the sum of the corresponding values in nums2 is maximized.

Return an array ans of size n, where ans[i] represents the maximum sum of at most k elements in nums2 whose corresponding elements in nums1 are strictly less than nums1[i].

Constraints:

  • n == nums1.length == nums2.length

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq nums1[i], nums2[i] ≤106\leq 10^6

  • 1≤1 \leq k ≤\leq n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Choose K Elements With Maximum Sum

Choose K Elements With Maximum Sum asks you to use two arrays: for each position, return the largest sum of at most k values from the second array paired with first-array values smaller than the current one.

Statement

You are given two integer arrays nums1 and nums2 of size n, along with an integer k.

For each index i from 00 to n −1- 1, find all indices j such that nums1[j] < nums1[i]. From these indices, select at most k indices such that the sum of the corresponding values in nums2 is maximized.

Return an array ans of size n, where ans[i] represents the maximum sum of at most k elements in nums2 whose corresponding elements in nums1 are strictly less than nums1[i].

Constraints:

  • n == nums1.length == nums2.length

  • 1≤1 \leq n ≤105\leq 10^5

  • 1≤1 \leq nums1[i], nums2[i] ≤106\leq 10^6

  • 1≤1 \leq k ≤\leq n