diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_16.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_16.py new file mode 100644 index 00000000..e03fe46f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_16.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_16.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_16.py new file mode 100644 index 00000000..dfc57cd7 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_16.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=r'[^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_4/minimum_adjacent_swapt_to_make_valid_array.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/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_4/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_4/minimum_number_of_keypresses.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/minimum_number_of_keypresses.py new file mode 100644 index 00000000..ce4ea1e0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_4/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/plus_one.py b/src/my_project/interviews/top_150_questions_round_21/plus_one.py new file mode 100644 index 00000000..668fbc4a --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/plus_one.py @@ -0,0 +1,20 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def plusOne(self, digits: List[int]) -> List[int]: + + len_digits = len(digits) + + for i in range(len_digits - 1, -1, -1): + + if digits[i] != 9: + digits[i] += 1 + break + else: + digits[i] = 0 + + if digits[0] == 0: + return [1] + digits + else: + return digits diff --git a/tests/test_150_questions_round_21/test_plus_one_round_21.py b/tests/test_150_questions_round_21/test_plus_one_round_21.py new file mode 100644 index 00000000..43707d73 --- /dev/null +++ b/tests/test_150_questions_round_21/test_plus_one_round_21.py @@ -0,0 +1,19 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.plus_one import Solution + +class PlusOneTestCase(unittest.TestCase): + + def test_leading_one(self): + solution = Solution() + output = solution.plusOne(digits=[9]) + target = [1, 0] + for k, v in enumerate(target): + self.assertEqual(v, output[k]) + + def test_no_leading_one(self): + solution = Solution() + output = solution.plusOne(digits=[1, 2]) + target = [1, 3] + for k, v in enumerate(target): + self.assertEqual(v, output[k]) \ No newline at end of file