Skip to content

Commit 0df5e38

Browse files
authored
Merge pull request #1459 from ivan1016017/november18
adding algo
2 parents b4fecc8 + e2652a8 commit 0df5e38

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-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 []

src/my_project/interviews/amazon_high_frequency_23/common_algos/two_num_round_3.py renamed to src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py

File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
16+
len_s = len(s)
17+
18+
for i in range(len_s//2):
19+
20+
if s[i] != s[len_s - 1 - i]:
21+
return False
22+
23+
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, Union, Collection, Mapping, Optional
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def kthFactor(self, n, k):
3+
"""
4+
:type n: int
5+
:type k: int
6+
:rtype: int
7+
"""
8+
9+
10+
for i in range(1, n+1):
11+
12+
if n % i == 0:
13+
k -= 1
14+
15+
if k == 0:
16+
return i
17+
18+
return -1
19+
20+
21+
22+
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+
4+
# Definition for a binary tree node.
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
class Solution:
12+
13+
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
14+
15+
if not root:
16+
return False
17+
if not root.left and not root.right and root.val == targetSum:
18+
return True
19+
else:
20+
temp_target = targetSum - root.val
21+
return self.hasPathSum(root.left, temp_target) \
22+
or self.hasPathSum(root.right, temp_target)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_21\
3+
.path_sum import TreeNode, Solution
4+
5+
class HasPathSumTestCase(unittest.TestCase):
6+
7+
def test_is_path_sum(self):
8+
solution = Solution()
9+
tree = TreeNode(1, TreeNode(2), TreeNode(3))
10+
output = solution.hasPathSum(root=tree, targetSum=3)
11+
self.assertTrue(output)
12+
13+
def test_is_no_path_sum(self):
14+
solution = Solution()
15+
tree = TreeNode(1, TreeNode(2), TreeNode(3))
16+
output = solution.hasPathSum(root=tree, targetSum=10)
17+
self.assertFalse(output)

0 commit comments

Comments
 (0)