Skip to content

[SAM] Week 6 solutions #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 9, 2024
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
20 changes: 20 additions & 0 deletions container-with-most-water/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# TC : O(n)
# SC : O(1)
class Solution:
def maxArea(self, height: List[int]) -> int:
low = 0
high = len(height) - 1
max_val = 0

while low < high:
val = min(height[low], height[high]) * (high - low)
max_val = max(val, max_val)
if height[low] < height[high]:
low += 1
elif height[low] > height[high]:
high -= 1
# increment low or decrement high
else:
low += 1

return max_val
15 changes: 15 additions & 0 deletions find-minimum-in-rotated-sorted-array/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
# TC: O(log(n)) - The search space is halved each round until the minimum is found
# SC: O(1) - Only a few extra variables are used regardless of input size
def findMin(self, nums: List[int]) -> int:
low, high = 0, len(nums) - 1

while low < high:
mid = (high + low) // 2

if nums[mid] > nums[high]:
low = mid + 1
else:
high = mid

return nums[low]
20 changes: 20 additions & 0 deletions longest-repeating-character-replacement/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# TC : O(n)
# SC : O(1)
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
count = {}
result = 0

left = 0
maxFrequency = 0
for right in range(len(s)):
count[s[right]] = 1 + count.get(s[right], 0)
maxFrequency = max(maxFrequency, count[s[right]])

while (right - left + 1) - maxFrequency > k:
count[s[left]] -= 1
left += 1

result = max(result, right - left + 1)

return result
16 changes: 16 additions & 0 deletions longest-substring-without-repeating-characters/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TC : O(n)
# SC : O(n)
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
charSet = set()
left = 0
longest_length = 0

for right in range(len(s)):
while s[right] in charSet:
charSet.remove(s[left])
left += 1
charSet.add(s[right])
longest_length = max(longest_length, right - left + 1)

return longest_length
28 changes: 28 additions & 0 deletions search-in-rotated-sorted-array/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# O(log n)
# O(1)


class Solution:
def search(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1

while left <= right:
mid = (left + right) // 2

if nums[mid] == target:
return mid

# Check if left half is sorted
if nums[left] <= nums[mid]:
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
# Otherwise, right half is sorted
else:
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1

return -1