diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py new file mode 100644 index 00000000..e03fe46f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py @@ -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 [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py new file mode 100644 index 00000000..1ed43720 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py @@ -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 \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_adjacent_swapt_to_make_valid_array.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_adjacent_swapt_to_make_valid_array.py new file mode 100644 index 00000000..bb358b65 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_adjacent_swapt_to_make_valid_array.py @@ -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 + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_number_of_keypresses.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_number_of_keypresses.py new file mode 100644 index 00000000..ce4ea1e0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_number_of_keypresses.py @@ -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 \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_21/maximum_depth_binary_tree.py b/src/my_project/interviews/top_150_questions_round_21/maximum_depth_binary_tree.py new file mode 100644 index 00000000..dbc93a50 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/maximum_depth_binary_tree.py @@ -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 + diff --git a/tests/test_150_questions_round_21/test_maximum_depth_binary_tree_round_21.py b/tests/test_150_questions_round_21/test_maximum_depth_binary_tree_round_21.py new file mode 100644 index 00000000..6e3a86db --- /dev/null +++ b/tests/test_150_questions_round_21/test_maximum_depth_binary_tree_round_21.py @@ -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) \ No newline at end of file