All problems
0288MediumArrayHeap (Priority Queue)

Smallest Coupled Trims

Tracked in this browser only
Write code

Trains the technique from

LeetCode 373Find K Pairs with Smallest Sums

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A test rig couples one damper from the left tray with one damper from the right tray. Tray contents arrive as left and right, each a list of signed trim values in microns sorted in non-decreasing order. Trims may be negative.

A coupling picks one position in left and one position in right, and its coupled trim is the sum of the two values. Positions matter, not values: if a tray holds the same trim at two positions, each position gives its own coupling.

Return the k couplings whose coupled trim is smallest, each written as [left trim, right trim]. Order the answer by coupled trim ascending; where two couplings share a coupled trim, put the smaller left trim first, and if those match too, put the smaller right trim first. It is guaranteed that k does not exceed the number of couplings.

Examples

Example 1

Input
left = [-4,-1,0], right = [-2,3], k = 4
Output
[[-4,-2],[-1,-2],[0,-2],[-4,3]]

The listed couplings have coupled trims -6, -3, -2 and -1, which are the four smallest, and they appear in ascending order of coupled trim.

Example 2

Input
left = [1,3], right = [1,3], k = 3
Output
[[1,1],[1,3],[3,1]]

Coupled trims are 2, 4 and 4. The two couplings that share the trim 4 are ordered by their left trim, so 1 with 3 precedes 3 with 1.

Constraints

  • 1 <= left.length, right.length <= 10^5
  • -10^9 <= left[i], right[i] <= 10^9
  • left and right are both sorted in non-decreasing order.
  • 1 <= k <= 10^4
  • k <= left.length * right.length

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def smallest_coupled_trims(left: list[int], right: list[int], k: int) -> list[list[int]]:
Java
public List<List<Integer>> smallestCoupledTrims(int[] left, int[] right, int k)
September 7
Apply