From 94ce7f6557c4f1b5cf6535dc54b0cedcabc2fcad Mon Sep 17 00:00:00 2001 From: Sahithipsl470 Date: Tue, 10 Mar 2026 20:22:51 -0400 Subject: [PATCH] "Greedy-2 done" --- Problem-1.py | 20 ++++++++++++++++++++ Problem-2.py | 19 +++++++++++++++++++ Problem-3.py | 24 ++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 Problem-1.py create mode 100644 Problem-2.py create mode 100644 Problem-3.py diff --git a/Problem-1.py b/Problem-1.py new file mode 100644 index 0000000..c84e189 --- /dev/null +++ b/Problem-1.py @@ -0,0 +1,20 @@ +# Time Complexity : O(n), n = number of tasks +# Space Complexity : O(1), fixed size array for 26 uppercase letters +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Explanation: +# 1. Count frequency of each task. +# 2. The task with maximum frequency determines the skeleton of the schedule. +# 3. Use formula: (max_freq - 1) * (n + 1) + number of tasks with max_freq. +# 4. The answer is the maximum of total tasks and the skeleton length. + +from typing import List +from collections import Counter + +class Solution: + def leastInterval(self, tasks: List[str], n: int) -> int: + count = Counter(tasks) + max_freq = max(count.values()) + max_count = sum(1 for v in count.values() if v == max_freq) + return max(len(tasks), (max_freq - 1) * (n + 1) + max_count) \ No newline at end of file diff --git a/Problem-2.py b/Problem-2.py new file mode 100644 index 0000000..cc3e449 --- /dev/null +++ b/Problem-2.py @@ -0,0 +1,19 @@ +# Time Complexity : O(n log n), sorting + insertion +# Space Complexity : O(n), for output list +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Explanation: +# 1. Sort people by height descending, then k ascending. +# 2. Insert each person into result list at index = k. +# 3. Taller people are inserted first, ensuring shorter people are correctly positioned behind taller ones. + +from typing import List + +class Solution: + def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]: + people.sort(key=lambda x: (-x[0], x[1])) + result = [] + for person in people: + result.insert(person[1], person) + return result \ No newline at end of file diff --git a/Problem-3.py b/Problem-3.py new file mode 100644 index 0000000..c21da5c --- /dev/null +++ b/Problem-3.py @@ -0,0 +1,24 @@ +# Time Complexity : O(n), n = length of string +# Space Complexity : O(1), constant size array for 26 letters +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Explanation: +# 1. Record the last occurrence of each character. +# 2. Iterate the string while tracking the farthest last occurrence of characters in current partition. +# 3. When current index reaches the farthest point, partition ends. +# 4. Record partition size and start new partition. + +from typing import List + +class Solution: + def partitionLabels(self, s: str) -> List[int]: + last = {c: i for i, c in enumerate(s)} + result = [] + start = end = 0 + for i, c in enumerate(s): + end = max(end, last[c]) + if i == end: + result.append(end - start + 1) + start = i + 1 + return result \ No newline at end of file