Maximize the Profit as the Salesman - Problem

You're a real estate salesman working on a street with n houses numbered from 0 to n-1. Multiple buyers have made offers to purchase contiguous blocks of houses, and each buyer is willing to pay a specific amount of gold for their desired range.

Given a 2D array offers where offers[i] = [starti, endi, goldi], the i-th buyer wants to purchase all houses from starti to endi (inclusive) for goldi amount of gold.

Your goal: Strategically select offers to maximize your total earnings. Remember that no house can be sold to multiple buyers, and some houses may remain unsold.

This is essentially a weighted interval scheduling problem where you need to find the optimal combination of non-overlapping offers that gives you maximum profit.

Input & Output

example_1.py โ€” Basic case
$ Input: n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]]
โ€บ Output: 3
๐Ÿ’ก Note: We can select offers [0,0,1] and [1,3,2] without overlap, giving us profit 1+2=3. The offer [0,2,2] overlaps with [1,3,2], so we can't take both.
example_2.py โ€” No overlaps
$ Input: n = 5, offers = [[0,1,3],[2,3,4],[4,4,2]]
โ€บ Output: 9
๐Ÿ’ก Note: All three offers are non-overlapping, so we can take all of them: 3+4+2=9.
example_3.py โ€” Choose wisely
$ Input: n = 3, offers = [[0,2,10],[1,1,5]]
โ€บ Output: 10
๐Ÿ’ก Note: We can either take [0,2,10] for profit 10, or [1,1,5] for profit 5. Since they overlap, we choose the more profitable one.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค offers.length โ‰ค 105
  • offers[i].length == 3
  • 0 โ‰ค starti โ‰ค endi โ‰ค n-1
  • 1 โ‰ค goldi โ‰ค 103
  • No house can be sold to multiple buyers

Visualization

Tap to expand
๐Ÿ  Real Estate Salesman StrategyStreet with Houses (0 to n-1)01234Buyer Offers (sorted by end time)Houses 0-1: $50Houses 1-2: $30Houses 2-4: $80Decision ProcessTake Offer 1 ($50)DP[1] = max(0, 50) = 50Skip Offer 2 (overlaps)DP[2] = max(50, 30+0) = 50Take Offer 3 ($80)DP[3] = max(50, 80+50) = 130๐ŸŽฏ Key InsightSort by end time + DP with binary search = Optimal O(n log n) solutionEach decision considers both immediate profit and future opportunities!
Understanding the Visualization
1
Organize Offers
Sort all buyer offers by their end house number - this helps us process them in a logical order
2
Make Strategic Decisions
For each offer, decide: should I take this deal or skip it? Consider both the immediate profit and future opportunities
3
Find Compatible Deals
Use binary search to quickly find the latest deal that doesn't conflict with the current one
4
Build Optimal Portfolio
Combine the best deals to create your maximum profit portfolio
Key Takeaway
๐ŸŽฏ Key Insight: By sorting offers by end time and using dynamic programming with binary search, we can efficiently find the optimal combination of non-overlapping deals in O(n log n) time, much better than the exponential brute force approach.
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 15
48.2K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen