题意:
Given a task sequence tasks
such as ABBABBC
, and an integer k
, which is the cool down time between two same tasks. Assume the execution for each individual task is 1 unit.
For example, if k = 3
, then tasks BB
takes a total of 5 unit time to finish, because B
takes 1 unit of time to execute, then wait for 3 unit, and execute the second B
again for another unit of time. So 1 + 3 + 1 = 5.
Given a task sequence and the cool down time, return the total execution time.
Follow up: Given a task sequence and the cool down time, rearrange the task sequence such that the execution time is minimal.
思路: 关注出现次数最多的那个字符串即可
代码:
unsigned min_runtime(vector<string>& vec, const int K) {
if (vec.empty())
return 0;
unordered_map<string, int> hashtable;
for (auto str : vec)
hashtable[str]++;
vector<int> cnt;
for (auto elem : hashtable)
cnt.push_back(elem.second);
sort(cnt.rbegin(), cnt.rend());
int frequency = 1;
for (int i = 1; i < cnt.size(); ++i) {
if (cnt[0] == cnt[i])
frequency++;
else
break;
}
unsigned ret = cnt[0] + (cnt[0] - 1) * K + frequency - 1;
return max(ret, (unsigned)vec.size());
}