621. Task Scheduler - #116
Open
kazuki-official wants to merge 1 commit into
Open
Conversation
nodchip
reviewed
Jul 9, 2026
| } | ||
|
|
||
| std::priority_queue<std::pair<int, char>> executables; | ||
| for (const auto& [label, num_tasks] : label_to_num_tasks) { |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
hemispherium/LeetCode_Arai60#10 (comment)
Owner
Author
There was a problem hiding this comment.
ありがとうございます。今回は値渡しの方がよさそうですね
|
書いてみたところ、延々とバグってひどいコードになってしまいました。供養のために掲載します。 class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
map<char, int> task_to_count;
for (char task : tasks) {
++task_to_count[task];
}
map<int, vector<char>> remaining_tasks;
for (auto [task, count] : task_to_count) {
remaining_tasks[count].push_back(task);
}
vector<int> last_clocks(128, std::numeric_limits<int>::min() / 2);
int clock = 0;
while (!remaining_tasks.empty()) {
++clock;
// for (const auto& [count, tasks] : remaining_tasks) {
// std::cout << count;
// for (char task : tasks) {
// std::cout << " " << task;
// }
// std::cout << std::endl;
// }
ComsumeTask(remaining_tasks, last_clocks, clock, n);
}
return clock;
}
void ComsumeTask(
map<int, vector<char>>& remaining_tasks, vector<int>& last_clocks,
int clock, int n) {
for (auto rit = remaining_tasks.rbegin(); rit != remaining_tasks.rend(); ++rit) {
int count = rit->first;
for (auto it = rit->second.begin(); it != rit->second.end(); ++it) {
char task = *it;
// std::cout << task << "?" << std::endl;
if (clock - last_clocks[task] <= n) {
continue;
}
// std::cout << task << std::endl;
swap(*it, rit->second.back());
rit->second.pop_back();
if (rit->second.empty()) {
remaining_tasks.erase(count);
}
if (count > 1) {
remaining_tasks[count - 1].push_back(task);
}
last_clocks[task] = clock;
return;
}
}
// std::cout << "idle" << std::endl;
}
}; |
Owner
Author
clockをインクリメントしながら実際の動作に近い形で実装したのですね。ぱっと見変なところはないのですが、バグっているのですね... |
説明足らずで申し訳ありません。上記のコードは何度か修正して AC をもらったものです。途中、 if (rit->second.empty()) {
remaining_tasks.erase(count);
}が抜けており、無限ループしたりしていました。 あと、 rit と it の変数名がひどいと感じています。 |
Owner
Author
|
ああ、そういうことですね。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/task-scheduler/description/