<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p>

<p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p>

<p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p>

<p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" />
<pre>
<strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6.
Second day you can finish the last job, total difficulty = 1.
The difficulty of the schedule = 6 + 1 = 7 
</pre>

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

<pre>
<strong>Input:</strong> jobDifficulty = [9,9,9], d = 4
<strong>Output:</strong> -1
<strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
</pre>

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

<pre>
<strong>Input:</strong> jobDifficulty = [1,1,1], d = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3.
</pre>

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

<ul>
	<li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li>
	<li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li>
	<li><code>1 &lt;= d &lt;= 10</code></li>
</ul>

#include<bits/stdc++.h>
class Solution {
    
public:
    // unordered_map<pair<int,int>,int>mp; cant use unodered map for a pair
    map<pair<int,int>,int>mp;
    int solve(int ind,int d,vector<int>& jobDifficulty){
        // ind and day changes but d is dynamic
        // base case
        int n = jobDifficulty.size();
        
        if(d==1){
            // find the max of all the left over tasks and return
            int ans = INT_MIN;
            for(int i=ind;i<n;i++){
                ans = max(ans,jobDifficulty[i]);
            }
            return ans; // this is the maxD in this case
        }

        // find in map
        if (mp.find({ind, d}) != mp.end()) {
            return mp[{ind,d}];
        } 
        
        // this base case doesnt matter here
        // if(ind>=n){
        //     return -1;
        // }

        int maxD = jobDifficulty[ind];
        int finalResult = INT_MAX;

        // now check for all possible days
        for(int i=ind;i<=n-d;i++){
            maxD = max(maxD ,jobDifficulty[i]);
            // int result = maxD + solve(ind+1,d-1,jobDifficulty);
            // in recursive call i must be there
            int result = maxD + solve(i+1,d-1,jobDifficulty);
            finalResult = min(finalResult,result);
        }

        // before saving push in map
        mp[{ind,d}] = finalResult;
        
        return finalResult;
    }



    int minDifficulty(vector<int>& jobDifficulty, int d) {
        // dp qn 
        // fn has 2 parameters index and day 
        // index i has range 0 to n-d 
        // this is because everyday must have atleast 1 task 



        if(jobDifficulty.size() <d){
            return -1;
        }

        return solve(0,d,jobDifficulty);
    }
};
12500