Skip to content

Commit 4e22af1

Browse files
authored
Create maximize-profit-from-task-assignment.cpp
1 parent 8a1bb2f commit 4e22af1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time: O(n + tlogt)
2+
// Space: O(n)
3+
4+
// freq table, sort, greedy
5+
class Solution {
6+
public:
7+
long long maxProfit(vector<int>& workers, vector<vector<int>>& tasks) {
8+
unordered_map<int, int> cnt;
9+
for (const int& x : workers) {
10+
++cnt[x];
11+
}
12+
sort(begin(tasks), end(tasks), [](const auto& a, const auto& b) {
13+
return a[1] > b[1];
14+
});
15+
int64_t result = 0;
16+
int k = 1;
17+
for (const auto& t : tasks) {
18+
if (cnt[t[0]]) {
19+
--cnt[t[0]];
20+
result += t[1];
21+
} else if (k) {
22+
--k;
23+
result += t[1];
24+
}
25+
}
26+
return result;
27+
}
28+
};

0 commit comments

Comments
 (0)