<p>We have <code>n</code> jobs, where every job is scheduled to be done from <code>startTime[i]</code> to <code>endTime[i]</code>, obtaining a profit of <code>profit[i]</code>.</p>

<p>You&#39;re given the <code>startTime</code>, <code>endTime</code> and <code>profit</code> arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.</p>

<p>If you choose a job that ends at time <code>X</code> you will be able to start another job that starts at time <code>X</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/10/10/sample1_1584.png" style="width: 380px; height: 154px;" /></strong></p>

<pre>
<strong>Input:</strong> startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
<strong>Output:</strong> 120
<strong>Explanation:</strong> The subset chosen is the first and fourth job. 
Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/10/10/sample22_1584.png" style="width: 600px; height: 112px;" /> </strong></p>

<pre>
<strong>Input:</strong> startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
<strong>Output:</strong> 150
<strong>Explanation:</strong> The subset chosen is the first, fourth and fifth job. 
Profit obtained 150 = 20 + 70 + 60.
</pre>

<p><strong class="example">Example 3:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/10/10/sample3_1584.png" style="width: 400px; height: 112px;" /></strong></p>

<pre>
<strong>Input:</strong> startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]
<strong>Output:</strong> 6
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= startTime.length == endTime.length == profit.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= startTime[i] &lt; endTime[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= profit[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    int jobScheduling(vector<int>& startTime, vector<int>& endTime,
                      vector<int>& profit) {
        vector<std::tuple<int, int, int>> jobs;
        for (int i = 0; i < startTime.size(); i++) {
            jobs.push_back({startTime[i], endTime[i], profit[i]});
        }
        return jobScheduling(std::move(jobs));
    }

private:
    int jobScheduling(vector<std::tuple<int, int, int>> jobs) {
        // return helper(jobs);
        return helper_x(jobs);
    }

    int helper(vector<std::tuple<int, int, int>>& jobs) {
        std::sort(jobs.begin(), jobs.end());
        int index = 0;
        unordered_map<int, int> old_results;
        int result = 0;
        int final_result = 0;
        for (int index = jobs.size() - 1; index >= 0; index--) {
            auto [start, end, profit] = jobs[index];
            int current_max = 0;
            result = 0;
            helper(jobs, index, /*prev_end*/ std::nullopt, current_max, result,
                   old_results);
            old_results[index] = result;
            final_result = std::max(final_result, result);
        }
        return final_result;
    }

    int helper(vector<std::tuple<int, int, int>>& jobs, int index,
               std::optional<int> prev_end, int current_max, int& result,
               unordered_map<int, int>& old_results) {
        for (int i = index; i < jobs.size(); i++) {
            auto [start, end, profit] = jobs[i];
            if (prev_end.has_value() && *prev_end > start) {
                continue;
            }
            current_max += profit;
            result = std::max(result, current_max);
            helper(jobs, i + 1, end, current_max, result, old_results);
            current_max -= profit;
        }

        return 0;
    }


    typedef std::tuple<int, int, int> Job;

    int helper_x(vector<Job>& jobs) {
        unordered_map<int, int> old_results;
        std::sort(jobs.begin(), jobs.end());
        return helper_x(jobs, 0, old_results);
    }

    int helper_x(vector<Job>& jobs, int index,
                 unordered_map<int, int>& old_results) {
        if (index < 0 || index == jobs.size()) {
            return 0;
        }

        if (old_results.contains(index)) {
            return old_results[index];
        }

        auto [start, end, profit] = jobs[index];
        // int next_index = index + 1;
        int next_index =
            std::lower_bound(jobs.begin(), jobs.end(), jobs[index],
                             [](const Job& j1, const Job& j2) {
                                 return std::get<0>(j1) < std::get<1>(j2);
                             }) -
            jobs.begin();
        int without_current_node = helper_x(jobs, index + 1, old_results);
        int with_current_node = profit + helper_x(jobs, next_index, old_results);
        int max_profit = std::max(without_current_node, with_current_node);
        old_results[index] = max_profit;
        return max_profit;
    }
};

class Solution2 {
public:
    // maximum number of jobs are 50000
    int memo[50001];

    int findMaxProfit(vector<vector<int>>& jobs, vector<int>& start, int n,
                      int position) {
        // 0 profit if we have already iterated over all the jobs
        if (position == n) {
            return 0;
        }
        // we have already calculated the answer so no need to go into recursion
        if (memo[position] != -1) {
            return memo[position];
        }
        // nextIndex is the index of next non-conflicting job
        // this step is similar to the binary search as in the java solution
        // lower_bound will return the iterator to the first element which is
        // equal to or greater than the element at `jobs[position][1]`
        int nextIndex =
            lower_bound(start.begin(), start.end(), jobs[position][1]) -
            start.begin();

        // find the maximum profit of our two options: skipping or scheduling
        // the current job
        int maxProfit =
            max(findMaxProfit(jobs, start, n, position + 1),
                jobs[position][2] + findMaxProfit(jobs, start, n, nextIndex));

        // returning maximum profit and also storing it for future reference
        // (memoization)
        return memo[position] = maxProfit;
    }

    int jobScheduling(vector<int>& startTime, vector<int>& endTime,
                      vector<int>& profit) {
        vector<vector<int>> jobs;
        // marking all values to -1 so that we can differentiate
        // if we have already calculated the answer or not
        memset(memo, -1, sizeof(memo));

        // storing job's details into one list
        // this will help in sorting the jobs while maintaining the other
        // parameters
        for (int i = 0; i < profit.size(); i++) {
            jobs.push_back({startTime[i], endTime[i], profit[i]});
        }

        sort(jobs.begin(), jobs.end());

        // binary search will be used in startTime so store it as separate list
        for (int i = 0; i < profit.size(); i++) {
            startTime[i] = jobs[i][0];
        }

        return findMaxProfit(jobs, startTime, profit.size(), 0);
    }
};
106293