From 7d0270693858ce09bdc1d4ae1e81417c1de2e627 Mon Sep 17 00:00:00 2001 From: Mohammed Muzakkir Fazal <56503557+mdmzfzl@users.noreply.github.com> Date: Mon, 7 Aug 2023 13:33:16 +0530 Subject: [PATCH 1/2] Added 0621-task-scheduler.c --- c/0621-task-scheduler.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 c/0621-task-scheduler.c diff --git a/c/0621-task-scheduler.c b/c/0621-task-scheduler.c new file mode 100644 index 000000000..9afa014ce --- /dev/null +++ b/c/0621-task-scheduler.c @@ -0,0 +1,23 @@ +int leastInterval(char *tasks, int tasksSize, int n) { + int taskCount[26] = {0}; + + for (int i = 0; i < tasksSize; i++) { + taskCount[tasks[i] - 'A']++; + } + + int maxTaskFreq = 0; + int numMaxTasks = 0; + + for (int i = 0; i < 26; i++) { + if (taskCount[i] > maxTaskFreq) { + maxTaskFreq = taskCount[i]; + numMaxTasks = 1; + } else if (taskCount[i] == maxTaskFreq) { + numMaxTasks++; + } + } + + int intervals = (maxTaskFreq - 1) * (n + 1) + numMaxTasks; + + return (intervals > tasksSize) ? intervals : tasksSize; +} From db27e679396255d92829a48f9da1cb38ad6b7f37 Mon Sep 17 00:00:00 2001 From: Mohammed Muzakkir Fazal <56503557+mdmzfzl@users.noreply.github.com> Date: Mon, 7 Aug 2023 13:33:51 +0530 Subject: [PATCH 2/2] Update 0134-gas-station.py --- python/0134-gas-station.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/python/0134-gas-station.py b/python/0134-gas-station.py index a33d8e773..5aa28c278 100644 --- a/python/0134-gas-station.py +++ b/python/0134-gas-station.py @@ -1,14 +1,18 @@ class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: - start, end = len(gas) - 1, 0 - total = gas[start] - cost[start] + total_gas = 0 + remaining_gas = 0 + start_index = 0 - while start >= end: - while total < 0 and start >= end: - start -= 1 - total += gas[start] - cost[start] - if start == end: - return start - total += gas[end] - cost[end] - end += 1 - return -1 + for i in range(len(gas)): + total_gas += gas[i] - cost[i] + remaining_gas += gas[i] - cost[i] + + if remaining_gas < 0: + remaining_gas = 0 + start_index = i + 1 + + if total_gas >= 0: + return start_index + else: + return -1