Skip to content

Commit ec6ae4a

Browse files
authored
Update 1235.Maximum-Profit-in-Job-Scheduling.cpp
1 parent 972b256 commit ec6ae4a

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

Sort/1235.Maximum-Profit-in-Job-Scheduling/1235.Maximum-Profit-in-Job-Scheduling.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@ class Solution {
22
public:
33
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit)
44
{
5-
vector<vector<int>>p;
5+
vector<vector<int>>jobs;
6+
set<int>endTimes({0});
67
for (int i=0; i<startTime.size(); i++)
7-
p.push_back({endTime[i], startTime[i], profit[i]});
8-
9-
sort(p.begin(), p.end());
10-
vector<pair<int,int>>q;
11-
q.push_back({0,0});
12-
13-
for (int i=0; i<p.size(); i++)
14-
{
15-
int end = p[i][0];
16-
int start = p[i][1];
17-
int pro = p[i][2];
18-
19-
auto iter = upper_bound(q.begin(),q.end(), make_pair(start, INT_MAX));
20-
iter = prev(iter,1);
21-
int newPro = iter->second + p[i][2];
22-
if (q.size()>0 && q.back().second < newPro)
23-
q.push_back({end, newPro});
8+
{
9+
jobs.push_back({endTime[i],startTime[i],profit[i]});
10+
endTimes.insert(endTime[i]);
2411
}
25-
26-
return q.back().second;
12+
sort(jobs.begin(), jobs.end());
13+
14+
unordered_map<int,int>time2val;
15+
time2val[0] = 0;
16+
int curVal = 0;
17+
18+
for (auto job: jobs)
19+
{
20+
int end = job[0];
21+
int start = job[1];
22+
int val = job[2];
23+
24+
auto iter = endTimes.upper_bound(start);
25+
int lastEnd = *prev(iter,1);
26+
time2val[end] = max(curVal, time2val[lastEnd]+val);
27+
28+
curVal = time2val[end];
29+
}
30+
return curVal;
2731
}
2832
};

0 commit comments

Comments
 (0)