Skip to content

Instantly share code, notes, and snippets.

@ducalpha
Created January 4, 2016 00:29
Show Gist options
  • Select an option

  • Save ducalpha/e680352cecd691563930 to your computer and use it in GitHub Desktop.

Select an option

Save ducalpha/e680352cecd691563930 to your computer and use it in GitHub Desktop.
int FindMinTimePeriod(const vector<int>& tasks, int K) {
unordered_map<int, int> task_to_last_time;
int cur_time = 0;
for (int i = 0; i < tasks.size(); ++i) {
int cur_task = tasks[i];
auto it = task_to_last_time.find(cur_task);
if (it != task_to_last_time.end()) {
cur_time = max(cur_time, it->second + K + 1);
it->second = cur_time;
} else {
task_to_last_time[cur_task] = cur_time;
}
++cur_time;
}
return cur_time;
} // time: O(n), space: O(num different tasks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment