diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_12.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_12.py new file mode 100644 index 00000000..e03fe46f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_12.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_12.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_12.py new file mode 100644 index 00000000..1c0734a7 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_12.py @@ -0,0 +1,22 @@ +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_4/jump_game_ii.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/jump_game_ii.py new file mode 100644 index 00000000..1ef43897 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_4/jump_game_ii.py @@ -0,0 +1,33 @@ +from typing import List + +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_4/jump_game_vii.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/jump_game_vii.py new file mode 100644 index 00000000..483e38c5 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_4/jump_game_vii.py @@ -0,0 +1,61 @@ +from typing import List, Union, Collection, Mapping, Optional + + +class Solution: + def minCost(self, nums: List[int], costs: List[int]) -> int: + """ + Example: nums = [3, 1, 2, 4], costs = [1, 2, 3, 4] + + From index 0 (value=3): + - Next smaller: index 1 (value=1) + - Next larger: index 3 (value=4) + + DP builds cost backwards: + - dp[3] = 0 (at end, no cost) + - dp[2] = dp[3] + costs[3] = 0 + 4 = 4 (can jump to 4) + - dp[1] = dp[2] + costs[2] = 4 + 3 = 7 (can jump to 2) + - dp[0] = min(dp[1]+costs[1], dp[3]+costs[3]) = min(7+2, 0+4) = 4 + """ + + smallStack = [] # Monotonic increasing (next smaller element) + largeStack = [] # Monotonic decreasing (next larger element) + dp = [0] * len(nums) # dp[i] = min cost from i to end + + # Process backwards (right to left) + for i in range(len(nums) - 1, -1, -1): + + # Maintain monotonic increasing stack (for next smaller) + # Remove elements >= current (they can't be "next smaller") + while smallStack and nums[smallStack[-1]] >= nums[i]: + smallStack.pop() + + # Maintain monotonic decreasing stack (for next larger) + # Remove elements < current (they can't be "next larger") + while largeStack and nums[largeStack[-1]] < nums[i]: + largeStack.pop() + + # Calculate minimum cost for this position + nxtCost = [] + + if largeStack: + lid = largeStack[-1] # Next larger index + nxtCost.append(dp[lid] + costs[lid]) + + if smallStack: + sid = smallStack[-1] # Next smaller index + nxtCost.append(dp[sid] + costs[sid]) + + # Min cost from current position to end + dp[i] = min(nxtCost) if nxtCost else 0 + + # Add current index to both stacks + largeStack.append(i) + smallStack.append(i) + + print(smallStack) + print(largeStack) + + + print(dp) + + return dp[0] # Minimum cost from start \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_21/reverse_bits.py b/src/my_project/interviews/top_150_questions_round_21/reverse_bits.py new file mode 100644 index 00000000..e3b8dc4f --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/reverse_bits.py @@ -0,0 +1,6 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def reverseBits(self, n: int) -> int: + return int((('{0:032b}'.format(n))[::-1]),2) diff --git a/tests/test_150_questions_round_21/test_reverse_bits_round_21.py b/tests/test_150_questions_round_21/test_reverse_bits_round_21.py new file mode 100644 index 00000000..7981d6d2 --- /dev/null +++ b/tests/test_150_questions_round_21/test_reverse_bits_round_21.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.reverse_bits import Solution + +class ReverseBitsTestCase(unittest.TestCase): + + def test_reverse_bits(self): + solution = Solution() + output = solution.reverseBits(2) + target = 1073741824 + self.assertEqual(output, target) \ No newline at end of file