Maximize the Profit as the Salesman

You are given an integer n representing the number of houses on a number line, numbered from 0 to n - 1.

Additionally, you are given a 2D integer array offers where offers[i] = [starti, endi, goldi], indicating that the i^th buyer wants to buy all the houses from starti to endi for goldi amount of gold.

As a salesman, your goal is to maximize your earnings by strategically selecting and selling houses to buyers.

Return the maximum amount of gold you can earn.

Note that different buyers can't buy the same house, and some houses may remain unsold.

Example 1
Inputn = 5, offers = [[0,0,1],[0,2,2],[1,3,2]]
Output3
We sell houses in the range [0, 0] to the 1st buyer for 1 gold and houses in the range [1, 3] to the 3rd buyer for 2 golds, for a maximum total of 3 gold.
Example 2
Inputn = 5, offers = [[0,0,1],[0,2,10],[1,3,2]]
Output10
We sell houses in the range [0, 2] to the 2nd buyer for 10 golds, which is the maximum amount of gold achievable.

Constraints

  • 1 <= n <= 10^5
  • 1 <= offers.length <= 10^5
  • offers[i].length == 3
  • 0 <= starti <= endi <= n - 1
  • 1 <= goldi <= 10^3

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate