Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save apurvanandan1997/9a89039a18be343439ac8005fef3f792 to your computer and use it in GitHub Desktop.
Save apurvanandan1997/9a89039a18be343439ac8005fef3f792 to your computer and use it in GitHub Desktop.
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
int c[26] = {0};
priority_queue<int> pq;
queue<vector<int>> q;
for(int i = 0; i < tasks.size(); i++)
c[tasks[i]-'A']++;
for(int i = 0; i < 26; i++)
if(c[i])
pq.push(c[i]);
int cpu_cycles = 0;
while(pq.size() || q.size()) {
if(q.size()) {
vector<int> to_sch = q.front();
if (to_sch[1] <= cpu_cycles) {
q.pop();
pq.push(to_sch[0]);
}
}
vector<int> sch_task;
cpu_cycles++;
if(pq.size()) {
int to_run = pq.top();
pq.pop();
to_run--;
if(to_run) {
sch_task.push_back(to_run);
sch_task.push_back(cpu_cycles + n);
q.push(sch_task);
}
}
}
return cpu_cycles;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment