Skip to content

Commit 033766c

Browse files
committed
day 28
1 parent a62c421 commit 033766c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Maximum Profit in Job Scheduling
3+
================================
4+
5+
We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i].
6+
7+
You're given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.
8+
9+
If you choose a job that ends at time X you will be able to start another job that starts at time X.
10+
11+
Example 1:
12+
Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
13+
Output: 120
14+
Explanation: The subset chosen is the first and fourth job.
15+
Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.
16+
17+
Example 2:
18+
Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
19+
Output: 150
20+
Explanation: The subset chosen is the first, fourth and fifth job.
21+
Profit obtained 150 = 20 + 70 + 60.
22+
23+
Example 3:
24+
Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]
25+
Output: 6
26+
27+
Constraints:
28+
1 <= startTime.length == endTime.length == profit.length <= 5 * 104
29+
1 <= startTime[i] < endTime[i] <= 109
30+
1 <= profit[i] <= 104
31+
*/
32+
33+
class Solution {
34+
public:
35+
int memo[50001];
36+
37+
int find(vector<vector<int>>& arr, int target, int l, int r){
38+
while(l < r){
39+
int m = l+(r-l)/2;
40+
if(arr[m][0] >= target)
41+
r = m;
42+
else l = m+1;
43+
}
44+
if(arr[r][0] >= target) return r;
45+
return -1;
46+
}
47+
48+
int dfs(vector<vector<int>>& jobs, int index) {
49+
if(index >= jobs.size()) return 0;
50+
51+
if(memo[index] != -1) return memo[index];
52+
53+
int ans = 0;
54+
ans = max(ans, dfs(jobs, index+1));
55+
56+
int next = find(jobs, jobs[index][1], index+1, jobs.size()-1);
57+
58+
ans = max(ans, jobs[index][2] + dfs(jobs, next));
59+
return memo[index] = ans;
60+
}
61+
62+
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
63+
vector<vector<int>> jobs;
64+
for(int i = 0; i < startTime.size(); ++i) {
65+
jobs.push_back({startTime[i], endTime[i], profit[i]});
66+
}
67+
68+
memset(memo, -1, sizeof memo);
69+
70+
sort(jobs.begin(), jobs.end());
71+
return dfs(jobs, 0);
72+
}
73+
};

Leetcode Daily Challenge/August-2021/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929
| 25. | [Sum of Square Numbers](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3918/) | [cpp](./25.%20Sum%20of%20Square%20Numbers.cpp) |
3030
| 26. | [Verify Preorder Serialization of a Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3920/) | [cpp](./26.%20Verify%20Preorder%20Serialization%20of%20a%20Binary%20Tree.cpp) |
3131
| 27. | [Longest Uncommon Subsequence II](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3921/) | [cpp](./27.%20Longest%20Uncommon%20Subsequence%20II.cpp) |
32+
| 28. | [Maximum Profit in Job Scheduling](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3950/) | [cpp](./28.%20Maximum%20Profit%20in%20Job%20Scheduling.cpp) |
33+
| 28. | []() | [cpp](./28.%20.cpp) |
3234
| 28. | []() | [cpp](./28.%20.cpp) |

0 commit comments

Comments
 (0)