Skip to content

Commit b061b88

Browse files
authored
Merge pull request #1466 from ivan1016017/november25
adding algo
2 parents ab65560 + 1738e6b commit b061b88

6 files changed

Lines changed: 139 additions & 0 deletions

File tree

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: 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=r'[^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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def minimumOperations(self, nums: List[int]) -> int:
6+
7+
l, r = 0, len(nums) - 1
8+
ls = nums[l]
9+
rs = nums[r]
10+
answer = 0
11+
12+
while l < r:
13+
14+
if ls == rs:
15+
l += 1
16+
r -= 1
17+
ls = nums[l]
18+
rs = nums[r]
19+
elif ls < rs:
20+
l += 1
21+
ls += nums[l]
22+
answer += 1
23+
else:
24+
r -= 1
25+
rs += nums[r]
26+
answer += 1
27+
28+
return answer
29+
30+
31+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
3+
4+
class Solution:
5+
def minSwaps(self, data: List[int]) -> int:
6+
7+
# Set window size
8+
k = sum(data)
9+
10+
val = answer = 0
11+
12+
for i, v in enumerate(data):
13+
14+
val += v
15+
16+
if i >= k:
17+
val -= data[i - k]
18+
19+
if i >= k - 1:
20+
answer = max(answer, val)
21+
22+
return k - answer
23+
24+
'''
25+
Window size (k): Count total number of 1s in the array. This is the size of the window needed to fit all 1s.
26+
27+
Sliding window: Move a window of size k across the array and count how many 1s are already in each window position.
28+
29+
Find maximum 1s: Track the maximum number of 1s found in any window (ans).
30+
31+
Calculate swaps: The minimum swaps needed = k - ans (total 1s minus the maximum 1s already grouped in any window).
32+
'''
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def mySqrt(self, x: int) -> int:
6+
7+
left, right = 0, x
8+
9+
while left <= right:
10+
11+
mid = (left + right)//2
12+
13+
if mid ** 2 < x:
14+
left = mid + 1
15+
elif mid ** 2 > x:
16+
right = mid - 1
17+
else:
18+
return mid
19+
20+
return min(left, right)
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+
.sqrtx import Solution
4+
5+
class SqrtxTestCase(unittest.TestCase):
6+
7+
def test_even_sqrtx(self):
8+
solution = Solution()
9+
output = solution.mySqrt(x=4)
10+
target = 2
11+
self.assertEqual(output, target)
12+
13+
def test_odd_sqrtx(self):
14+
solution = Solution()
15+
output = solution.mySqrt(x=8)
16+
target = 2
17+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)