Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 []





Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions src/my_project/interviews/top_150_questions_round_22/ex_117_ipo.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
45 changes: 45 additions & 0 deletions tests/test_150_questions_round_22/test_117_ipo_round_22.py
Original file line number Diff line number Diff line change
@@ -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()
Loading