diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_10.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_10.py new file mode 100644 index 00000000..cf05b635 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_10.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/two_num_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py similarity index 100% rename from src/my_project/interviews/amazon_high_frequency_23/common_algos/two_num_round_3.py rename to src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_10.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_10.py new file mode 100644 index 00000000..1ed43720 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_10.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/sample.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/sample.py new file mode 100644 index 00000000..fa308366 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/sample.py @@ -0,0 +1,33 @@ +from typing import List, Union, Collection, Mapping, Optional + +class Solution: + def jump(self, nums: List[int]) -> int: + """ + Greedy approach: At each position, jump to the farthest reachable index + + Example: [2,3,1,1,4] + - From index 0 (value=2): can reach indices 1,2 + - Greedy choice: Jump to index 1 (value=3) because it reaches farthest + - From index 1: can reach indices 2,3,4 (end) + - Answer: 2 jumps + """ + + if len(nums) <= 1: + return 0 + + jumps = 0 + current_end = 0 # End of current jump range + farthest = 0 # The farthest position we can reach + + for i in range(len(nums) - 1): + # Update farthest position reachable + farthest = max(farthest, i + nums[i]) + + # If we've reached the end of current jump range + if i == current_end: + jumps += 1 + current_end = farthest # Make the greedy choice + + if current_end >= len(nums) - 1: + break + return jumps \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/the_kth_factor.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/the_kth_factor.py new file mode 100644 index 00000000..1aed3e42 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/the_kth_factor.py @@ -0,0 +1,22 @@ +class Solution: + def kthFactor(self, n, k): + """ + :type n: int + :type k: int + :rtype: int + """ + + + for i in range(1, n+1): + + if n % i == 0: + k -= 1 + + if k == 0: + return i + + return -1 + + + + diff --git a/src/my_project/interviews/top_150_questions_round_21/path_sum.py b/src/my_project/interviews/top_150_questions_round_21/path_sum.py new file mode 100644 index 00000000..f6735abd --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/path_sum.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +# Definition for a binary tree node. +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + + def hasPathSum(self, root: TreeNode, targetSum: int) -> bool: + + if not root: + return False + if not root.left and not root.right and root.val == targetSum: + return True + else: + temp_target = targetSum - root.val + return self.hasPathSum(root.left, temp_target) \ + or self.hasPathSum(root.right, temp_target) \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_path_sum_round_21.py b/tests/test_150_questions_round_21/test_path_sum_round_21.py new file mode 100644 index 00000000..0d78dd27 --- /dev/null +++ b/tests/test_150_questions_round_21/test_path_sum_round_21.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.path_sum import TreeNode, Solution + +class HasPathSumTestCase(unittest.TestCase): + + def test_is_path_sum(self): + solution = Solution() + tree = TreeNode(1, TreeNode(2), TreeNode(3)) + output = solution.hasPathSum(root=tree, targetSum=3) + self.assertTrue(output) + + def test_is_no_path_sum(self): + solution = Solution() + tree = TreeNode(1, TreeNode(2), TreeNode(3)) + output = solution.hasPathSum(root=tree, targetSum=10) + self.assertFalse(output) \ No newline at end of file