Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions c/0621-task-scheduler.c
Original file line number Diff line number Diff line change
@@ -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;
}
26 changes: 15 additions & 11 deletions python/0134-gas-station.py
Original file line number Diff line number Diff line change
@@ -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