Skip to content

Commit c41c9d9

Browse files
authored
Merge pull request #1535 from ivanpenaloza/feature/python
adding algo
2 parents bf0a103 + f152563 commit c41c9d9

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 []
17+
18+
19+
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=r'[^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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
3+
class Solution:
4+
def maximumSubarraySum(self, nums: List[int], k: int) -> int:
5+
max_sum = 0
6+
current_sum = 0
7+
freq_sum = dict()
8+
left = 0
9+
10+
for right in range(len(nums)):
11+
current_sum += nums[right]
12+
13+
freq_sum[nums[right]] = freq_sum.get(nums[right], 0) + 1
14+
15+
if right - left > k - 1:
16+
freq_sum[nums[left]] -= 1
17+
if freq_sum[nums[left]] == 0:
18+
del freq_sum[nums[left]]
19+
current_sum -= nums[left]
20+
left += 1
21+
22+
if right - left == k - 1 and len(freq_sum) == k:
23+
max_sum = max(max_sum, current_sum)
24+
25+
return max_sum
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
6+
7+
list_answer = list()
8+
9+
for i in sorted(intervals, key=lambda x: x[0]):
10+
11+
if list_answer and i[0] <= list_answer[-1][-1]:
12+
list_answer[-1][-1] = max(list_answer[-1][-1], i[-1])
13+
else:
14+
list_answer.append(i)
15+
16+
return list_answer
17+

0 commit comments

Comments
 (0)