From 825b041e3808ce846d2bd3bdf3530569b0bc1638 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 30 Oct 2025 16:51:31 -0500 Subject: [PATCH] Greedy-2 completed --- height_queue.py | 8 ++++++++ label_partition.py | 22 ++++++++++++++++++++++ task_scheduler.py | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 height_queue.py create mode 100644 label_partition.py create mode 100644 task_scheduler.py diff --git a/height_queue.py b/height_queue.py new file mode 100644 index 0000000..8f5162b --- /dev/null +++ b/height_queue.py @@ -0,0 +1,8 @@ +class Solution: + def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]: + people.sort(key= lambda x:(-x[0],x[1])) + result=[] + for p in people: + result.insert(p[1],p) + return result + \ No newline at end of file diff --git a/label_partition.py b/label_partition.py new file mode 100644 index 0000000..4bc77f1 --- /dev/null +++ b/label_partition.py @@ -0,0 +1,22 @@ +class Solution: + def partitionLabels(self, s: str) -> List[int]: + map = {} + + for i in range(len(s)): + c = s[i] + map[c] = i + + result = [] + + start = 0 + end = 0 + + for i in range(len(s)): + c = s[i] + end = max(end, map[c]) + + if i == end: + result.append(end - start + 1) + start = i + 1 + + return result \ No newline at end of file diff --git a/task_scheduler.py b/task_scheduler.py new file mode 100644 index 0000000..b9fe355 --- /dev/null +++ b/task_scheduler.py @@ -0,0 +1,21 @@ +class Solution: + def leastInterval(self, tasks: List[str], n: int) -> int: + map = {} + maxFreq = 0 + maxCount = 0 + + for c in tasks: + map[c] = map.get(c, 0) + 1 + maxFreq = max(maxFreq, map[c]) + + for c in map: + if map[c] == maxFreq: + maxCount += 1 + + partitions = maxFreq - 1 + availableSlots = partitions * (n - (maxCount - 1)) + pending = len(tasks) - (maxFreq * maxCount) + idle = max(0, availableSlots - pending) + + return len(tasks) + idle + \ No newline at end of file