Skip to content

Commit 0e2c5f1

Browse files
committed
[main] Added algos
1 parent e58d73a commit 0e2c5f1

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
6+
sorted_candidates = sorted(candidates)
7+
combos = self.find_combos(sorted_candidates, target)
8+
filtered_combos = []
9+
for eachcombo in combos:
10+
if sorted(eachcombo) in filtered_combos:
11+
continue
12+
else:
13+
filtered_combos.append(sorted(eachcombo))
14+
print(filtered_combos)
15+
return filtered_combos
16+
17+
def find_combos(self, nums: List[int], target: int, curr_total_arr: List[int] = None, combos: List[List[int]] = None) -> List[List[int]]:
18+
curr_total = 0
19+
if curr_total_arr is None:
20+
curr_total_arr = []
21+
else:
22+
curr_total = sum(curr_total_arr)
23+
if combos is None:
24+
combos = []
25+
for eachnum in nums:
26+
if eachnum + curr_total < target:
27+
acquired_combos = self.find_combos(nums, target, curr_total_arr + [eachnum], combos)
28+
for eachcombo in acquired_combos:
29+
if eachcombo not in combos:
30+
combos.append(eachcombo)
31+
elif eachnum + curr_total > target:
32+
return [] + combos
33+
else:
34+
if sorted(curr_total_arr + [eachnum]) not in combos and curr_total + eachnum == target:
35+
return combos + sorted([curr_total_arr + [eachnum]])
36+
return []
37+
return combos
38+
39+
if __name__ == '__main__':
40+
candidates = [36,21,2,3,23,24,38,22,11,14,15,25,32,19,35,26,31,13,34,29,12,37,17,20,39,30,40,28,27,33]
41+
target = 35
42+
sol = Solution()
43+
sol.combinationSum(candidates, target)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def combinationSum2(self, candidates: List[int], target: int, combos: List[List[int]] = None, curr_combo_sum: int = None, curr_combo: List[int] = None, curr_combo_indices: List[int] = None) -> List[List[int]]:
6+
if sum(candidates) < target:
7+
return []
8+
if combos is None:
9+
combos = []
10+
if curr_combo is None:
11+
curr_combo = []
12+
if curr_combo_indices is None:
13+
curr_combo_indices = []
14+
15+
curr_combo_sum = 0
16+
if not combos is None:
17+
curr_combo_sum = sum(curr_combo)
18+
19+
for ind, elem in enumerate(candidates):
20+
if elem + curr_combo_sum > target:
21+
continue
22+
if elem + curr_combo_sum == target and ind not in curr_combo_indices:
23+
combo_sorted = sorted(curr_combo + [elem])
24+
if combo_sorted not in combos:
25+
combos.append(combo_sorted)
26+
elif ind not in curr_combo_indices:
27+
acquired_combos = self.combinationSum2(candidates, target, combos, curr_combo_sum, curr_combo + [elem], curr_combo_indices + [ind])
28+
for eachcombo in acquired_combos:
29+
eachcombo_sorted = sorted(eachcombo)
30+
if eachcombo_sorted not in combos:
31+
combos.append(eachcombo_sorted)
32+
return combos
33+
34+
if __name__ == '__main__':
35+
sol = Solution()
36+
candidates = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
37+
target = 30
38+
print(sol.combinationSum2(candidates, target))
39+

pythonProblems/dp/countAndSay.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
class Solution:
3+
4+
def gen_number(self, num: str) -> str:
5+
sequence_count = {}
6+
curr_key = ''
7+
generated_string = ''
8+
for eachdigit in num:
9+
if eachdigit in sequence_count:
10+
sequence_count[eachdigit] += 1
11+
elif len(sequence_count) == 0: ## initial case, when dict is empty
12+
sequence_count[eachdigit] = 1
13+
curr_key = eachdigit
14+
else:
15+
generated_string += str(sequence_count[curr_key]) + curr_key
16+
curr_key = eachdigit
17+
sequence_count.clear()
18+
sequence_count[eachdigit] = 1
19+
if len(sequence_count) > 0:
20+
generated_string += str(sequence_count[curr_key]) + curr_key
21+
return generated_string
22+
23+
def countAndSay(self, n: int) -> str:
24+
if n == 1:
25+
return '1'
26+
else:
27+
sequence_elements = ['1']
28+
i = 0
29+
while len(sequence_elements) < n:
30+
sequence_elements.append(self.gen_number(sequence_elements[i]))
31+
i += 1
32+
print(sequence_elements)
33+
return sequence_elements[n - 1]
34+
35+
if __name__ == '__main__':
36+
sol = Solution()
37+
target = 5
38+
sol.countAndSay(target)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def firstMissingPositive(self, nums: List[int]) -> int:
6+
min_number = -1
7+
max_number = -1
8+
if 1 not in nums:
9+
return 1
10+
for eachnumber in nums:
11+
if eachnumber < 0:
12+
continue
13+
else:
14+
min_number = eachnumber if min_number == -1 else min(min_number, eachnumber)
15+
max_number = eachnumber if max_number == -1 else max(max_number, eachnumber)
16+
return max_number + 1 if min_number <= 1 else min_number - 1
17+
18+
19+
if __name__ == '__main__':
20+
sol = Solution()
21+
nums = [3, 4, -1, 1]
22+
print(sol.firstMissingPositive(nums))

pythonProblems/dp/searchInsert.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def searchInsert(self, nums: List[int], target: int) -> int:
6+
mid = len(nums) // 2
7+
l = 0
8+
r = len(nums) - 1
9+
while l < r:
10+
if nums[mid] > target:
11+
r = mid - 1
12+
elif nums[mid] < target:
13+
l = mid + 1
14+
elif nums[mid] == target:
15+
return mid
16+
mid = l + ((r - l) // 2)
17+
if l == -1 or r == -1:
18+
return 0
19+
return mid + 1 if nums[mid] < target else mid
20+
21+
22+
if __name__ == '__main__':
23+
sol = Solution()
24+
nums = [1, 3, 5, 6]
25+
target = 0
26+
print(sol.searchInsert(nums, target))

0 commit comments

Comments
 (0)