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,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 []
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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




22 changes: 22 additions & 0 deletions src/my_project/interviews/top_150_questions_round_21/path_sum.py
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

# 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)
17 changes: 17 additions & 0 deletions tests/test_150_questions_round_21/test_path_sum_round_21.py
Original file line number Diff line number Diff line change
@@ -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)