From f9e8ee1427d4cd071c9705969521b9384a7e1129 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 2 Apr 2026 04:35:22 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_4.py | 21 ++++++++ .../common_algos/valid_palindrome_round_4.py | 22 ++++++++ .../top_150_questions_round_22/ex_117_ipo.py | 24 +++++++++ .../top_150_questions_round_1/ex_117_ipo.ts | 53 +++++++++++++++++++ .../test_117_ipo_round_22.py | 45 ++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_117_ipo.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_117_ipo.ts create mode 100644 tests/test_150_questions_round_22/test_117_ipo_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py new file mode 100644 index 00000000..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.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_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.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=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/top_150_questions_round_22/ex_117_ipo.py b/src/my_project/interviews/top_150_questions_round_22/ex_117_ipo.py new file mode 100644 index 00000000..063e93bf --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_117_ipo.py @@ -0,0 +1,24 @@ +import heapq +from typing import List + + +class Solution: + def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int: + # Min-heap of (capital_required, profit) sorted by capital + min_cap_heap = sorted(zip(capital, profits)) + cap_idx = 0 + # Max-heap of profits (negate for max behavior) + max_profit_heap = [] + + for _ in range(k): + # Push all affordable projects into the max-profit heap + while cap_idx < len(min_cap_heap) and min_cap_heap[cap_idx][0] <= w: + heapq.heappush(max_profit_heap, -min_cap_heap[cap_idx][1]) + cap_idx += 1 + + if not max_profit_heap: + break + + w += -heapq.heappop(max_profit_heap) + + return w diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_117_ipo.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_117_ipo.ts new file mode 100644 index 00000000..f4a9d7cb --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_117_ipo.ts @@ -0,0 +1,53 @@ +function findMaximizedCapital(k: number, w: number, profits: number[], capital: number[]): number { + // Pair projects as [capital, profit] and sort by capital requirement + const projects: [number, number][] = profits.map((p, i) => [capital[i], p]); + projects.sort((a, b) => a[0] - b[0]); + + // Max-heap of profits for affordable projects (using a simple sorted insert) + const maxHeap: number[] = []; + let idx = 0; + + const heapPush = (val: number) => { + maxHeap.push(val); + let i = maxHeap.length - 1; + while (i > 0) { + const parent = (i - 1) >> 1; + if (maxHeap[parent] < maxHeap[i]) { + [maxHeap[parent], maxHeap[i]] = [maxHeap[i], maxHeap[parent]]; + i = parent; + } else break; + } + }; + + const heapPop = (): number => { + const top = maxHeap[0]; + const last = maxHeap.pop()!; + if (maxHeap.length > 0) { + maxHeap[0] = last; + let i = 0; + while (true) { + const left = 2 * i + 1; + const right = 2 * i + 2; + let largest = i; + if (left < maxHeap.length && maxHeap[left] > maxHeap[largest]) largest = left; + if (right < maxHeap.length && maxHeap[right] > maxHeap[largest]) largest = right; + if (largest === i) break; + [maxHeap[i], maxHeap[largest]] = [maxHeap[largest], maxHeap[i]]; + i = largest; + } + } + return top; + }; + + for (let i = 0; i < k; i++) { + // Push all newly affordable projects into the max-heap + while (idx < projects.length && projects[idx][0] <= w) { + heapPush(projects[idx][1]); + idx++; + } + if (maxHeap.length === 0) break; + w += heapPop(); + } + + return w; +} diff --git a/tests/test_150_questions_round_22/test_117_ipo_round_22.py b/tests/test_150_questions_round_22/test_117_ipo_round_22.py new file mode 100644 index 00000000..11ee0690 --- /dev/null +++ b/tests/test_150_questions_round_22/test_117_ipo_round_22.py @@ -0,0 +1,45 @@ +import unittest +from typing import List +from src.my_project.interviews.top_150_questions_round_22\ + .ex_117_ipo import Solution + + +class IPOTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + """k=2, w=0, profits=[1,2,3], capital=[0,1,1] -> 4""" + self.assertEqual(self.solution.findMaximizedCapital(2, 0, [1, 2, 3], [0, 1, 1]), 4) + + def test_example_2(self): + """k=3, w=0, profits=[1,2,3], capital=[0,1,2] -> 6""" + self.assertEqual(self.solution.findMaximizedCapital(3, 0, [1, 2, 3], [0, 1, 2]), 6) + + def test_no_affordable_projects(self): + """Initial capital too low for all projects -> w unchanged""" + self.assertEqual(self.solution.findMaximizedCapital(2, 0, [5, 10], [3, 5]), 0) + + def test_k_larger_than_projects(self): + """k exceeds number of projects, should pick all""" + self.assertEqual(self.solution.findMaximizedCapital(10, 0, [1, 2, 3], [0, 0, 0]), 6) + + def test_single_project_affordable(self): + """Only one project affordable""" + self.assertEqual(self.solution.findMaximizedCapital(1, 1, [5, 10], [1, 100]), 6) + + def test_large_initial_capital(self): + """Enough capital to start any project, always pick highest profit""" + self.assertEqual(self.solution.findMaximizedCapital(2, 10, [3, 7, 5], [0, 0, 0]), 22) + + def test_all_same_capital_requirement(self): + """All projects have same capital requirement""" + self.assertEqual(self.solution.findMaximizedCapital(2, 0, [4, 2, 6], [0, 0, 0]), 10) + + def test_k_is_zero(self): + """k=0, no projects selected""" + self.assertEqual(self.solution.findMaximizedCapital(0, 5, [1, 2, 3], [0, 0, 0]), 5) + + +if __name__ == '__main__': + unittest.main()