diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py new file mode 100644 index 00000000..987b8bf9 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py @@ -0,0 +1,21 @@ +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 [] + + + + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py new file mode 100644 index 00000000..6d40b5d0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.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 + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_6/minimum_number_of_keypresses.py b/src/my_project/interviews/amazon_high_frequency_23/round_6/minimum_number_of_keypresses.py new file mode 100644 index 00000000..32006f75 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_6/minimum_number_of_keypresses.py @@ -0,0 +1,41 @@ +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