From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/task_scheduler.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 other/task_scheduler.cpp (limited to 'other/task_scheduler.cpp') diff --git a/other/task_scheduler.cpp b/other/task_scheduler.cpp new file mode 100644 index 0000000..553dd5d --- /dev/null +++ b/other/task_scheduler.cpp @@ -0,0 +1,36 @@ +// 2365. Task Scheduler II +#include "leetcode.h" + +/* + * given a 0-indexed array of positive integers 'tasks', representing tasks that + * need to be completed in order, where 'tasks[i]' represents the type of the + * i'th task. you are also given a positve integer 'space' which represents the + * minimum number of days that must pass after the completion of a task before + * another task of the same type can be performed. each day until all tasks have + * been completed, you must either + * - complete the next task from 'tasks' or + * - take a break + * return the minimum number of days needed to complete all tasks. + */ + +class Solution { +public: + long long taskSchedulerII(vector &tasks, int space) { + unordered_map last; + long long ans = 0; + for (int t : tasks) { + if (last.count(t)) + last[t] = ans = max(ans, last[t] + space) + 1; + else + last[t] = ++ans; + } + return ans; + } +}; + +int main() { + Solution obj; + vector t1 = {1, 2, 1, 2, 3, 1}, t2 = {5, 8, 8, 5}; + printf("%lld\n", obj.taskSchedulerII(t1, 3)); // expect: 9 + printf("%lld\n", obj.taskSchedulerII(t2, 2)); // expect: 6 +} -- cgit v1.2.3