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=r'[^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,31 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def minimumOperations(self, nums: List[int]) -> int:

l, r = 0, len(nums) - 1
ls = nums[l]
rs = nums[r]
answer = 0

while l < r:

if ls == rs:
l += 1
r -= 1
ls = nums[l]
rs = nums[r]
elif ls < rs:
l += 1
ls += nums[l]
answer += 1
else:
r -= 1
rs += nums[r]
answer += 1

return answer



Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import List, Union, Collection, Mapping, Optional


class Solution:
def minSwaps(self, data: List[int]) -> int:

# Set window size
k = sum(data)

val = answer = 0

for i, v in enumerate(data):

val += v

if i >= k:
val -= data[i - k]

if i >= k - 1:
answer = max(answer, val)

return k - answer

'''
Window size (k): Count total number of 1s in the array. This is the size of the window needed to fit all 1s.

Sliding window: Move a window of size k across the array and count how many 1s are already in each window position.

Find maximum 1s: Track the maximum number of 1s found in any window (ans).

Calculate swaps: The minimum swaps needed = k - ans (total 1s minus the maximum 1s already grouped in any window).
'''
20 changes: 20 additions & 0 deletions src/my_project/interviews/top_150_questions_round_21/sqrtx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def mySqrt(self, x: int) -> int:

left, right = 0, x

while left <= right:

mid = (left + right)//2

if mid ** 2 < x:
left = mid + 1
elif mid ** 2 > x:
right = mid - 1
else:
return mid

return min(left, right)
17 changes: 17 additions & 0 deletions tests/test_150_questions_round_21/test_sqrtx_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\
.sqrtx import Solution

class SqrtxTestCase(unittest.TestCase):

def test_even_sqrtx(self):
solution = Solution()
output = solution.mySqrt(x=4)
target = 2
self.assertEqual(output, target)

def test_odd_sqrtx(self):
solution = Solution()
output = solution.mySqrt(x=8)
target = 2
self.assertEqual(output, target)