Untitled
unknown
plain_text
a year ago
2.0 kB
4
Indexable
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
// Define the Worker class
class Worker {
public:
long long time;
int cnt;
long long workerTime;
Worker(long long time, int cnt, long long workerTime) {
this->time = time;
this->cnt = cnt;
this->workerTime = workerTime;
}
// Define the comparison operator for the priority queue (min-heap)
bool operator<(const Worker& other) const {
return this->time > other.time; // We reverse it for a min-heap in C++
}
};
// Define the Solution class
class Solution {
public:
long long minNumberOfSeconds(int mountainHeight, vector<int>& workerTimes) {
// Initialize the priority queue (min-heap)
priority_queue<Worker> pq;
// Add initial workers to the heap
for (int workerTime : workerTimes) {
pq.push(Worker(workerTime, 1, workerTime));
}
// Process until the mountainHeight is reduced to 0
while (mountainHeight > 0) {
// Get the worker with the minimum time
Worker worker = pq.top();
pq.pop();
long long time = worker.time;
int cnt = worker.cnt;
long long workerTime = worker.workerTime;
// Decrease the mountain height
mountainHeight--;
if (mountainHeight == 0) {
return time;
}
// Update the worker's time and push it back into the priority queue
cnt++;
pq.push(Worker(time + cnt * workerTime, cnt, workerTime));
}
return 0;
}
};
int main() {
Solution solution;
vector<int> workerTimes = {5, 10, 3}; // Example worker times
int mountainHeight = 10;
long long result = solution.minNumberOfSeconds(mountainHeight, workerTimes);
cout << "Minimum number of seconds: " << result << endl;
return 0;
}
Editor is loading...
Leave a Comment