Skip to content
Open
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
20 changes: 20 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions Problem-3.py
Original file line number Diff line number Diff line change
@@ -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