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 ith 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.
Input: n =5, offers =[[0,0,1],[0,2,2],[1,3,2]]Output: 3Explanation: There are 5 houses numbered from 0 to 4 and there are 3 purchase offers.We sell houses in the range [0,0] to 1st buyer for1 gold and houses in the range [1,3] to 3rd buyer for2 golds.It can be proven that 3is the maximum amount of gold we can achieve.
Input: n =5, offers =[[0,0,1],[0,2,10],[1,3,2]]Output: 10Explanation: There are 5 houses numbered from 0 to 4 and there are 3 purchase offers.We sell houses in the range [0,2] to 2nd buyer for10 golds.It can be proven that 10is the maximum amount of gold we can achieve.
This is a classic weighted interval scheduling problem. To maximize profit, we can use dynamic programming: for each house, decide whether to take an offer ending at that house (and add its profit to the best up to its start-1), or skip it. Binary search helps efficiently find the best previous non-overlapping offer.
classSolution {
publicintmaximizeTheProfit(int n, int[][] offers) {
List<int[]>[] endAt =new ArrayList[n];
for (int i = 0; i < n; i++) endAt[i]=new ArrayList<>();
for (int[] o : offers) endAt[o[1]].add(newint[]{o[0], o[2]});
int[] dp =newint[n+1];
for (int i = 0; i < n; i++) {
dp[i+1]= Math.max(dp[i+1], dp[i]);
for (int[] p : endAt[i]) {
dp[i+1]= Math.max(dp[i+1], dp[p[0]]+ p[1]);
}
}
return dp[n];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funmaximizeTheProfit(n: Int, offers: Array<IntArray>): Int {
val endAt = Array(n) { mutableListOf<Pair<Int, Int>>() }
for (o in offers) endAt[o[1]].add(o[0] to o[2])
val dp = IntArray(n+1)
for (i in0 until n) {
dp[i+1] = maxOf(dp[i+1], dp[i])
for ((l, g) in endAt[i]) {
dp[i+1] = maxOf(dp[i+1], dp[l] + g)
}
}
return dp[n]
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defmaximizeTheProfit(self, n: int, offers: list[list[int]]) -> int:
end_at = [[] for _ in range(n)]
for l, r, g in offers:
end_at[r].append((l, g))
dp = [0] * (n+1)
for i in range(n):
dp[i+1] = max(dp[i+1], dp[i])
for l, g in end_at[i]:
dp[i+1] = max(dp[i+1], dp[l] + g)
return dp[n]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnmaximize_the_profit(n: i32, offers: Vec<Vec<i32>>) -> i32 {
let n = n asusize;
letmut end_at =vec![vec![]; n];
for o in&offers {
end_at[o[1] asusize].push((o[0] asusize, o[2]));
}
letmut dp =vec![0; n+1];
for i in0..n {
dp[i+1] = dp[i+1].max(dp[i]);
for&(l, g) in&end_at[i] {
dp[i+1] = dp[i+1].max(dp[l] + g);
}
}
dp[n]
}
}
⏰ Time complexity: O(n + m) where n is the number of houses and m is the number of offers (since each offer is processed once, and each house is visited once).
🧺 Space complexity: O(n + m) — for the DP array and offer storage.