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='[^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,28 @@
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

This file was deleted.

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).
'''
18 changes: 18 additions & 0 deletions src/my_project/interviews/top_150_questions_round_21/same_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:

if p and q:
return p.val == q.val \
and self.isSameTree(p.left, q.left) \
and self.isSameTree(p.right, q.right)
else:
return p is q
20 changes: 20 additions & 0 deletions tests/test_150_questions_round_21/test_same_tree_round_21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
from src.my_project.interviews.top_150_questions_round_21\
.same_tree import TreeNode, Solution

class SameTreeTestCase(unittest.TestCase):

def test_is_same_tree(self):
solution = Solution()
tree1 = TreeNode(1, TreeNode(2), TreeNode(3))
tree2 = TreeNode(1, TreeNode(2), TreeNode(3))
output = solution.isSameTree(p=tree1, q=tree2)
return self.assertTrue(output)

def test_is_no_same_tree(self):
solution = Solution()
tree1 = TreeNode(1, TreeNode(2), TreeNode(3))
tree2 = TreeNode(1, TreeNode(3), TreeNode(2))
output = solution.isSameTree(p=tree1, q=tree2)
return self.assertFalse(output)