Skip to content

Commit ad4b80a

Browse files
authored
Merge pull request #112 from SamTheKorean/solution6
[SAM] Week 6 solutions
2 parents 05f0d50 + b158445 commit ad4b80a

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def maxArea(self, height: List[int]) -> int:
5+
low = 0
6+
high = len(height) - 1
7+
max_val = 0
8+
9+
while low < high:
10+
val = min(height[low], height[high]) * (high - low)
11+
max_val = max(val, max_val)
12+
if height[low] < height[high]:
13+
low += 1
14+
elif height[low] > height[high]:
15+
high -= 1
16+
# increment low or decrement high
17+
else:
18+
low += 1
19+
20+
return max_val
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
# TC: O(log(n)) - The search space is halved each round until the minimum is found
3+
# SC: O(1) - Only a few extra variables are used regardless of input size
4+
def findMin(self, nums: List[int]) -> int:
5+
low, high = 0, len(nums) - 1
6+
7+
while low < high:
8+
mid = (high + low) // 2
9+
10+
if nums[mid] > nums[high]:
11+
low = mid + 1
12+
else:
13+
high = mid
14+
15+
return nums[low]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def characterReplacement(self, s: str, k: int) -> int:
5+
count = {}
6+
result = 0
7+
8+
left = 0
9+
maxFrequency = 0
10+
for right in range(len(s)):
11+
count[s[right]] = 1 + count.get(s[right], 0)
12+
maxFrequency = max(maxFrequency, count[s[right]])
13+
14+
while (right - left + 1) - maxFrequency > k:
15+
count[s[left]] -= 1
16+
left += 1
17+
18+
result = max(result, right - left + 1)
19+
20+
return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# TC : O(n)
2+
# SC : O(n)
3+
class Solution:
4+
def lengthOfLongestSubstring(self, s: str) -> int:
5+
charSet = set()
6+
left = 0
7+
longest_length = 0
8+
9+
for right in range(len(s)):
10+
while s[right] in charSet:
11+
charSet.remove(s[left])
12+
left += 1
13+
charSet.add(s[right])
14+
longest_length = max(longest_length, right - left + 1)
15+
16+
return longest_length
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# O(log n)
2+
# O(1)
3+
4+
5+
class Solution:
6+
def search(self, nums: List[int], target: int) -> int:
7+
left, right = 0, len(nums) - 1
8+
9+
while left <= right:
10+
mid = (left + right) // 2
11+
12+
if nums[mid] == target:
13+
return mid
14+
15+
# Check if left half is sorted
16+
if nums[left] <= nums[mid]:
17+
if nums[left] <= target < nums[mid]:
18+
right = mid - 1
19+
else:
20+
left = mid + 1
21+
# Otherwise, right half is sorted
22+
else:
23+
if nums[mid] < target <= nums[right]:
24+
left = mid + 1
25+
else:
26+
right = mid - 1
27+
28+
return -1

0 commit comments

Comments
 (0)