generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
32 lines (27 loc) · 869 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <algorithm>
#include <limits>
#include <unordered_set>
#include <vector>
class Solution {
public:
int minimumTimeRequired(std::vector<int>& jobs, int k) {
std::sort(jobs.begin(), jobs.end());
std::vector<int> workers(k, 0);
return dfs(jobs.data(), jobs.size() - 1, workers.data(), k, 0);
}
int dfs(int* jobs, int i, int* workers, int k, int time) {
if (i < 0) return time;
int minTime{std::numeric_limits<int>::max()};
std::unordered_set<int> seen{};
for (int j{k - 1}; j >= 0; --j) {
if (time >= minTime) break;
if (!seen.insert(workers[j]).second) continue;
if (workers[j] + jobs[i] >= minTime) continue;
workers[j] += jobs[i];
minTime = std::min(
minTime, dfs(jobs, i - 1, workers, k, std::max(time, workers[j])));
workers[j] -= jobs[i];
}
return minTime;
}
};