Skip to content

Latest commit

 

History

History
 
 

2830

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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.

 

Example 1:

Input: n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]]
Output: 3
Explanation: 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 for 1 gold and houses in the range [1,3] to 3rd buyer for 2 golds.
It can be proven that 3 is the maximum amount of gold we can achieve.

Example 2:

Input: n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]]
Output: 10
Explanation: 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 for 10 golds.
It can be proven that 10 is the maximum amount of gold we can achieve.

 

Constraints:

  • 1 <= n <= 105
  • 1 <= offers.length <= 105
  • offers[i].length == 3
  • 0 <= starti <= endi <= n - 1
  • 1 <= goldi <= 103

Companies: Amazon

Related Topics:
Array, Binary Search, Dynamic Programming, Sorting

Solution 1.

Let dp[i+1] be the maximum earnings if we sell 0~ith houses.

dp[0] = 0

Sort A in ascending order of end[i].

Traverse A, and update dp[end[i] + 1] = rollingMax = max(rollingMax, dp[start[i]] + gold[i])

// OJ: https://leetcode.com/problems/maximize-the-profit-as-the-salesman
// Author: github.com/lzl124631x
// Time: O(OlogO + N)
// Space: O(N)
class Solution {
public:
    int maximizeTheProfit(int n, vector<vector<int>>& A) {
        vector<int> dp(n + 1);
        sort(begin(A), end(A), [&](auto &a, auto &b) { return a[1] < b[1]; });
        int ans = 0, end = 0;
        for (auto &o : A) {
            for (int i = end + 1; i < o[1]; ++i) dp[i + 1] = ans; // fill the gaps of DP values
            dp[o[1] + 1] = ans = max(ans, dp[o[0]] + o[2]);
            end = o[1];
        }
        return ans;
    }
};

We can also use a map to store the DP values, and use binary search to find the maximum house index that has shown up in an offer and is smaller than the current start[i].

// OJ: https://leetcode.com/problems/maximize-the-profit-as-the-salesman
// Author: github.com/lzl124631x
// Time: O(OlogO + NlogO)
// Space: O(O)
class Solution {
public:
    int maximizeTheProfit(int n, vector<vector<int>>& A) {
        map<int, int, greater<>> dp{{-1,0}};
        sort(begin(A), end(A), [&](auto &a, auto &b) { return a[1] < b[1]; });
        int ans = 0;
        for (auto &o : A) {
            dp[o[1]] = ans = max(ans, dp.upper_bound(o[0])->second + o[2]);
        }
        return ans;
    }
};