Skip to content

Commit d4b8a2d

Browse files
authoredJan 6, 2024
Create 1235. Maximum Profit in Job Scheduling.cpp
1 parent bdda63b commit d4b8a2d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
 
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
4+
int c_profit,low,high;
5+
int n=startTime.size();
6+
vector<vector<int>> jobs;
7+
for(int i=0;i<n;++i)
8+
{
9+
jobs.push_back({endTime[i], startTime[i], profit[i]});
10+
}
11+
sort(jobs.begin(),jobs.end());
12+
vector<int> arr(n,0);
13+
arr[0]=jobs[0][2];
14+
for(int i=1;i<n;++i)
15+
{
16+
c_profit=jobs[i][2];
17+
low=0,high=i-1;
18+
while(low<=high)
19+
{
20+
int mid=(low+high)/2;
21+
if(jobs[mid][0]<=jobs[i][1])
22+
{
23+
if(jobs[mid+1][0]<=jobs[i][1])
24+
{
25+
low=mid+1;
26+
}
27+
else
28+
{
29+
c_profit +=arr[mid];
30+
break;
31+
}
32+
}
33+
else
34+
{
35+
high=mid-1;
36+
}
37+
}
38+
arr[i]=max(c_profit,arr[i-1]);
39+
}
40+
return arr[n - 1];
41+
}
42+
};

0 commit comments

Comments
 (0)
Please sign in to comment.