Ctrl K

The k-th Cheapest Meal From the Menu

Hard
A canteen menu is given as a matrix menu: each row is a section (soups, mains, drinks, …) and holds the prices of its dishes in non-decreasing order. A meal is exactly one dish from every section, and its price is the sum of the chosen dishes. Two meals are different if at least one section uses a different dish (a different position in the row), even when the prices match. Sort all possible meals by price and return the price of the k-th cheapest one (1-based).
Example 1
menu=[[2, 5, 6], [1, 3, 4]], k=3 → 6

The cheapest meals are 2+1=3, 2+3=5, 2+4=6, 5+1=6, 6+1=7, … The third cheapest costs 6.

Example 2
menu=[[7], [3], [10]], k=1 → 20

Every row has a single dish, so there is just one meal: 7+3+10=20.

Example 3
menu=[[1, 1, 8], [2, 6, 9], [1, 4, 4]], k=5 → 7

The cheapest meals cost 4, 4, 7, 7, 7, … so the fifth one costs 7. Different meals with the same price count separately.

Constraints
  • 1 ≤ len(menu) ≤ 40
  • All rows have the same length n, 1 ≤ n ≤ 40
  • 1 ≤ menu[i][j] ≤ 5000
  • Every row menu[i] is sorted in non-decreasing order
  • 1 ≤ k ≤ min(200, nᵐ), where m = len(menu)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCrossing at High Tide HardK-th Smallest in a Sorted Grid MediumRoute With the Highest Bottleneck MediumThe Dimmest Rows EasyThe Gentlest Climb Medium
loading Python…
Input
menu=[[2, 5, 6], [1, 3, 4]], k=3
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 82 tests on the server, hidden ones included.