diff --git a/container-with-most-water/samthekorean.py b/container-with-most-water/samthekorean.py new file mode 100644 index 000000000..081b32658 --- /dev/null +++ b/container-with-most-water/samthekorean.py @@ -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 diff --git a/find-minimum-in-rotated-sorted-array/samthekorean.py b/find-minimum-in-rotated-sorted-array/samthekorean.py new file mode 100644 index 000000000..90e58a1f8 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/samthekorean.py @@ -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] diff --git a/longest-repeating-character-replacement/samthekorean.py b/longest-repeating-character-replacement/samthekorean.py new file mode 100644 index 000000000..d8c7216dc --- /dev/null +++ b/longest-repeating-character-replacement/samthekorean.py @@ -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 diff --git a/longest-substring-without-repeating-characters/samthekorean.py b/longest-substring-without-repeating-characters/samthekorean.py new file mode 100644 index 000000000..4c0f8f632 --- /dev/null +++ b/longest-substring-without-repeating-characters/samthekorean.py @@ -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 diff --git a/search-in-rotated-sorted-array/samthekorean.py b/search-in-rotated-sorted-array/samthekorean.py new file mode 100644 index 000000000..cca39cb33 --- /dev/null +++ b/search-in-rotated-sorted-array/samthekorean.py @@ -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