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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:

answer = dict()

for k, v in enumerate(nums):

if v in answer:
return [answer[v], k]
else:
answer[target - v] = k

return []
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
import re

class Solution:
def isPalindrome(self, s: str) -> bool:

# To lowercase
s = s.lower()

# Remove non-alphanumeric characters
s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s)

# Determine if s is palindrome or not

len_s = len(s)

for i in range(len_s//2):

if s[i] != s[len_s - 1 - i]:
return False

return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def minimumSwaps(self, nums: List[int]) -> int:

min_num = min(nums)
max_num = max(nums)

id_min = nums.index(min_num)

new_nums = [min_num] + nums[:id_min] + nums[id_min + 1:]

id_max = new_nums[::-1].index(max_num)

return id_min + id_max

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
from collections import Counter

class Solution:
def minimumKeypresses(self, s: str) -> int:
"""
Greedy approach: Assign most frequent characters to 1-press slots

Example: s = "apple"
Frequency: {'p': 2, 'a': 1, 'l': 1, 'e': 1}

Assignment:
- 'p' (freq=2) → position 0 → 1 press → total: 2 * 1 = 2
- 'a' (freq=1) → position 1 → 1 press → total: 1 * 1 = 1
- 'l' (freq=1) → position 2 → 1 press → total: 1 * 1 = 1
- 'e' (freq=1) → position 3 → 1 press → total: 1 * 1 = 1

Total: 2 + 1 + 1 + 1 = 5
"""

# Count character frequencies
freq = Counter(s)
print(freq)

# Sort by frequency (descending) - greedy choice
counts = sorted(freq.values(), reverse=True)
print(counts)

total_presses = 0

# Calculate presses based on position
for position, frequency in enumerate(counts):
# Positions 0-8: 1 press (9 slots)
# Positions 9-17: 2 presses (9 slots)
# Positions 18-26: 3 presses (9 slots)
presses_per_char = (position // 9) + 1
total_presses += presses_per_char * frequency

return total_presses
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:

if not root:
return 0
else:
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest
from src.my_project.interviews.top_150_questions_round_21\
.maximum_depth_binary_tree import Solution, TreeNode

class MaxDepthTreeTestCase(unittest.TestCase):

def test_max_depth_null(self):
solution = Solution()
tree = None
output = solution.maxDepth(root=tree)
target = 0
self.assertEqual(output, target)

def test_max_depth(self):
solution = Solution()
tree = TreeNode(1)
output = solution.maxDepth(root=tree)
target = 1
self.assertEqual(output, target)