2830. Maximize the Profit as the Salesman

Problem Statement

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

Intuition

Basically

Sort the array according to start index
Then start the take/not_take approach on the array

If we take, the keep in mind that we take the next index whose start is greater than
The taken end

As we cannot sell the same house to different people

NlogN

https://leetcode.com/problems/maximize-the-profit-as-the-salesman/description/

https://www.youtube.com/watch?v=PJnLvCc5p8o&ab_channel=ThinkCode

Approach 1:

Memoization
C++
class Solution {
public:
    
    vector<int> memo;
    
    // next non-conflicting house range index that can be sold
    int search(vector<vector<int>>& offers, int end, int idx){
        int n=offers.size();
        int l=idx, r=n-1;
        int i=-1;
        while(l<=r){
            int m=l+(r-l)/2;
            int start=offers[m][0];
            if(start>end){
                i=m;
                r=m-1;
            }else{
                l=m+1;
            }
        }
        return i;
    }
    
    int getProfit(vector<vector<int>>& offers, int n, int idx){
        if(idx == n) return 0;
        
        if(memo[idx]!=-1) return memo[idx];
        
        // not sell
        int maxGold = getProfit(offers, n, idx+1);
        
        // sell
        int next_possible_idx = search(offers, offers[idx][1], idx+1);
        maxGold = max(maxGold, offers[idx][2] + (next_possible_idx==-1 ? 0:getProfit(offers,n,next_possible_idx)));
        
        return memo[idx]=maxGold;
    }
    
    int maximizeTheProfit(int n, vector<vector<int>>& offers) {
        sort(offers.begin(),offers.end());
        
        int m=offers.size();
        memo.resize(m,-1);
        
        return getProfit(offers,m,0);
    }
};

Approach 2:

C++

Approach 3:

C++

Approach 4:

C++

Similar Problems

Last updated