Skip to content

Commit 0863666

Browse files
committed
adding algo
1 parent 6b792a6 commit 0863666

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
class Solution:
4+
def jump(self, nums: List[int]) -> int:
5+
"""
6+
Greedy approach: At each position, jump to the farthest reachable index
7+
8+
Example: [2,3,1,1,4]
9+
- From index 0 (value=2): can reach indices 1,2
10+
- Greedy choice: Jump to index 1 (value=3) because it reaches farthest
11+
- From index 1: can reach indices 2,3,4 (end)
12+
- Answer: 2 jumps
13+
"""
14+
15+
if len(nums) <= 1:
16+
return 0
17+
18+
jumps = 0
19+
current_end = 0 # End of current jump range
20+
farthest = 0 # The farthest position we can reach
21+
22+
for i in range(len(nums) - 1):
23+
# Update farthest position reachable
24+
farthest = max(farthest, i + nums[i])
25+
26+
# If we've reached the end of current jump range
27+
if i == current_end:
28+
jumps += 1
29+
current_end = farthest # Make the greedy choice
30+
31+
if current_end > len(nums) - 1:
32+
break
33+
return jumps
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
3+
4+
class Solution:
5+
def minCost(self, nums: List[int], costs: List[int]) -> int:
6+
"""
7+
Example: nums = [3, 1, 2, 4], costs = [1, 2, 3, 4]
8+
9+
From index 0 (value=3):
10+
- Next smaller: index 1 (value=1)
11+
- Next larger: index 3 (value=4)
12+
13+
DP builds cost backwards:
14+
- dp[3] = 0 (at end, no cost)
15+
- dp[2] = dp[3] + costs[3] = 0 + 4 = 4 (can jump to 4)
16+
- dp[1] = dp[2] + costs[2] = 4 + 3 = 7 (can jump to 2)
17+
- dp[0] = min(dp[1]+costs[1], dp[3]+costs[3]) = min(7+2, 0+4) = 4
18+
"""
19+
20+
smallStack = [] # Monotonic increasing (next smaller element)
21+
largeStack = [] # Monotonic decreasing (next larger element)
22+
dp = [0] * len(nums) # dp[i] = min cost from i to end
23+
24+
# Process backwards (right to left)
25+
for i in range(len(nums) - 1, -1, -1):
26+
27+
# Maintain monotonic increasing stack (for next smaller)
28+
# Remove elements >= current (they can't be "next smaller")
29+
while smallStack and nums[smallStack[-1]] >= nums[i]:
30+
smallStack.pop()
31+
32+
# Maintain monotonic decreasing stack (for next larger)
33+
# Remove elements < current (they can't be "next larger")
34+
while largeStack and nums[largeStack[-1]] < nums[i]:
35+
largeStack.pop()
36+
37+
# Calculate minimum cost for this position
38+
nxtCost = []
39+
40+
if largeStack:
41+
lid = largeStack[-1] # Next larger index
42+
nxtCost.append(dp[lid] + costs[lid])
43+
44+
if smallStack:
45+
sid = smallStack[-1] # Next smaller index
46+
nxtCost.append(dp[sid] + costs[sid])
47+
48+
# Min cost from current position to end
49+
dp[i] = min(nxtCost) if nxtCost else 0
50+
51+
# Add current index to both stacks
52+
largeStack.append(i)
53+
smallStack.append(i)
54+
55+
print(smallStack)
56+
print(largeStack)
57+
58+
59+
print(dp)
60+
61+
return dp[0] # Minimum cost from start
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def reverseBits(self, n: int) -> int:
6+
return int((('{0:032b}'.format(n))[::-1]),2)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_21\
3+
.reverse_bits import Solution
4+
5+
class ReverseBitsTestCase(unittest.TestCase):
6+
7+
def test_reverse_bits(self):
8+
solution = Solution()
9+
output = solution.reverseBits(2)
10+
target = 1073741824
11+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)