Ctrl K

Sell Plots to Bidders

Medium
There are plots land plots in a row along a road, numbered 0 to plots - 1. Buyers have sent bids bids: the bid [s, e, price] means a buyer wants all plots from s to e inclusive, as one deal, and will pay price for them. You decide which bids to accept. A plot cannot be sold to two buyers, and a bid can only be accepted as a whole. Some plots may stay unsold. Return the most money you can make.
Example 1
plots=6, bids=[[0, 1, 5], [1, 3, 7], [2, 5, 6], [4, 5, 3]] → 11

Accept [0, 1] for 5 and [2, 5] for 6: they do not overlap, 11 in total. The priciest bid, [1, 3] for 7, only combines with [4, 5] for 3, which gives 10.

Example 2
plots=3, bids=[[0, 2, 4], [0, 0, 2], [1, 1, 2], [2, 2, 1]] → 5

The three small bids together bring 2 + 2 + 1 = 5, more than 4 for the whole row.

Constraints
  • 1 ≤ plots ≤ 10⁵
  • 1 ≤ len(bids) ≤ 10⁵
  • bids[i] = [s, e, price], 0 ≤ s ≤ e ≤ plots - 1
  • 1 ≤ price ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords Hidden in a Text MediumShuttle Driver Earnings MediumSquaring Ladder MediumSpells Without Neighbors MediumClimbing Along Rows and Columns Hard
loading Python…
Input
plots=6, bids=[[0, 1, 5], [1, 3, 7], [2, 5, 6], [4, 5, 3]]
Expected
11

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