Created
January 4, 2016 00:29
-
-
Save ducalpha/e680352cecd691563930 to your computer and use it in GitHub Desktop.
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
| 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