Skip to the code
cf.
+25 XP

#390 Cheapest Bundle Pairs

medium

A store sells bundles of one laptop and one accessory. Laptop prices are in laptops and accessory prices in accessories, both sorted ascending.

Write cheapestPairs(laptops, accessories, k) that returns the k cheapest bundles as [laptopPrice, accessoryPrice] pairs, cheapest first. Order bundles by total price; for equal totals, the one with the smaller laptop index i comes first, and then the smaller accessory index j. If there are fewer than k bundles, return all of them.

Example 1

Input: laptops = [100,300,500], accessories = [20,40,60], k = 3
Output: [[100,20],[100,40],[100,60]]

Totals 120, 140, 160 are the three smallest.

Example 2

Input: laptops = [1,2], accessories = [3], k = 5
Output: [[1,3],[2,3]]

Only two bundles exist.

Constraints

  • 1 <= laptops.length, accessories.length <= 10^4
  • 1 <= k <= 10^4
  • 0 <= price <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).