leetcode face面试题 Minimal run time scheduler

来源:互联网 发布:360全景源码 编辑:程序博客网 时间:2024/06/05 01:05

题意:

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++;elsebreak;}unsigned ret = cnt[0] + (cnt[0] - 1) * K + frequency - 1;return max(ret, (unsigned)vec.size()); }


0 0
原创粉丝点击