Skip to content

Commit 16d9ab3

Browse files
authored
Merge pull request #118 from bhyun-kim/main
[bhyun-kim, 김병현] Week 6 solutions
2 parents 6b99c02 + c653d51 commit 16d9ab3

File tree

7 files changed

+233
-6
lines changed

7 files changed

+233
-6
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
11. Container With Most Water
3+
https://leetcode.com/problems/container-with-most-water
4+
5+
Solution: Two Pointers
6+
- Use two pointers to traverse the list
7+
- Calculate the area between the two pointers
8+
- Update the maximum area
9+
- Move the pointer with the shorter height
10+
- Return the maximum area
11+
12+
Time complexity: O(n)
13+
- The two pointers traverse the list only once
14+
Space complexity: O(1)
15+
- No extra space is used
16+
"""
17+
18+
from typing import List
19+
20+
21+
class Solution:
22+
def maxArea(self, height: List[int]) -> int:
23+
left, right = 0, len(height) - 1
24+
max_area = 0
25+
26+
while left < right:
27+
shorter_height = min(height[left], height[right])
28+
distance = right - left
29+
area = shorter_height * distance
30+
max_area = max(area, max_area)
31+
32+
if height[left] <= height[right]:
33+
left += 1
34+
else:
35+
right -= 1
36+
37+
return max_area

counting-bits/bhyun-kim.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ def countBits(self, n: int) -> List[int]:
5555

5656
from typing import List
5757

58+
5859
class Solution:
5960
def countBits(self, n: int) -> List[int]:
60-
output = [0] * (n+1)
61+
output = [0] * (n + 1)
6162

62-
for i in range(1, n+1):
63+
for i in range(1, n + 1):
6364
output[i] = output[i >> 1] + (i & 1)
6465

6566
return output
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
153. Find Minimum in Rotated Sorted Array
3+
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
4+
5+
Solution: Binary Search
6+
- Find the center of the array
7+
- Divide the array into two parts
8+
- Compare the left and right values of each part
9+
- If these values are in increasing order, return the left value of the first part
10+
- Otherwise, find the minimum value of the left and right values of each part
11+
- If the minimum value is the left value of the first part, recursively call the function with the first part
12+
- Otherwise, recursively call the function with the second part
13+
14+
Time complexity: O(log n)
15+
Space complexity: O(n)
16+
17+
"""
18+
19+
20+
from typing import List
21+
22+
23+
class Solution:
24+
def findMin(self, nums: List[int]) -> int:
25+
if len(nums) == 1:
26+
return nums[0]
27+
28+
center = len(nums) // 2
29+
arr1, arr2 = nums[:center], nums[center:]
30+
31+
arr1_left, arr1_right = arr1[0], arr1[-1]
32+
arr2_left, arr2_right = arr2[0], arr2[-1]
33+
34+
if arr1_left < arr1_right < arr2_left < arr2_right:
35+
return arr1_left
36+
37+
min_val = min(arr1_left, arr1_right)
38+
min_val = min(min_val, arr2_left)
39+
min_val = min(min_val, arr2_right)
40+
41+
if min_val in [arr1_left, arr1_right]:
42+
return self.findMin(arr1)
43+
else:
44+
return self.findMin(arr2)

group-anagrams/bhyun-kim.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,16 @@
2929

3030
class Solution:
3131
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
32-
3332
hash_table = dict()
3433
output = []
3534

3635
for s in strs:
37-
count_s = ''.join(sorted(s))
36+
count_s = "".join(sorted(s))
3837
if count_s in hash_table:
3938
idx = hash_table[count_s]
4039
output[idx].append(s)
4140
else:
4241
hash_table[count_s] = len(output)
4342
output.append([s])
44-
45-
return output
4643

44+
return output
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
424. Longest Repeating Character Replacement
3+
https://leetcode.com/problems/longest-repeating-character-replacement/
4+
5+
Solution: Sliding Window
6+
- Use a dictionary to store the number of occurrences of each character
7+
- Use two pointers to traverse the string
8+
- Increment the right pointer until the substring contains no repeating characters
9+
- Increment the left pointer until the substring contains repeating characters
10+
To do this,
11+
- Find the maximum frequency of the characters in the substring
12+
- If the length of the substring minus the maximum frequency is greater than k, increment the left pointer
13+
- Update the maximum length of the substring
14+
- Return the maximum length of the substring
15+
16+
Time complexity: O(n)
17+
- The time complexity is O(n) because the two pointers traverse the string only once
18+
19+
Space complexity: O(n)
20+
- Store the number of occurrences of each character in a dictionary
21+
"""
22+
23+
24+
from collections import defaultdict
25+
26+
27+
class Solution:
28+
def characterReplacement(self, s: str, k: int) -> int:
29+
char_freq = defaultdict(int)
30+
left, right = 0, 0
31+
max_seq = 1
32+
max_freq = 0
33+
34+
while right < len(s):
35+
r = s[right]
36+
char_freq[r] += 1
37+
38+
max_freq = 0
39+
for v in char_freq.values():
40+
max_freq = max(v, max_freq)
41+
42+
while right - left + 1 - max_freq > k and left < right:
43+
l = s[left]
44+
char_freq[l] -= 1
45+
left += 1
46+
47+
max_freq = 0
48+
for v in char_freq.values():
49+
max_freq = max(v, max_freq)
50+
51+
max_seq = max(right - left + 1, max_seq)
52+
right += 1
53+
54+
return max_seq
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
3. Longest Substring Without Repeating Characters
3+
https://leetcode.com/problems/longest-substring-without-repeating-characters/
4+
5+
Solution: Sliding Window
6+
- Use a dictionary to store the number of occurrences of each character
7+
- Use two pointers to traverse the string
8+
- Increment the right pointer until the substring contains no repeating characters
9+
- Increment the left pointer until the substring contains repeating characters
10+
- Update the maximum length of the substring
11+
- Return the maximum length of the substring
12+
13+
Time complexity: O(n)
14+
- The time complexity is O(n) because the two pointers traverse the string only once
15+
Space complexity: O(n)
16+
- Store the number of occurrences of each character in a dictionary
17+
"""
18+
19+
from collections import defaultdict
20+
21+
22+
class Solution:
23+
def lengthOfLongestSubstring(self, s: str) -> int:
24+
char_count = defaultdict(int)
25+
right, left = 0, 0
26+
max_seq = 0
27+
while right < len(s):
28+
r = s[right]
29+
char_count[r] += 1
30+
31+
while char_count[r] > 1:
32+
l = s[left]
33+
char_count[l] -= 1
34+
left += 1
35+
36+
max_seq = max(max_seq, right - left + 1)
37+
right += 1
38+
39+
return max_seq
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
33. Search in Rotated Sorted Array
3+
https://leetcode.com/problems/search-in-rotated-sorted-array
4+
5+
Solution: Binary Search
6+
- Find the center of the array
7+
- Divide the array into two parts
8+
- Compare the left and right values of each part
9+
- If the target value is in the range of the left and right values of the first part, recursively call the function with the first part
10+
- If the target value is in the range of the left and right values of the second part, recursively call the function with the second part
11+
- If the first part is rotated, recursively call the function with the first part
12+
- Otherwise, recursively call the function with the second part
13+
- If the output is not -1, add the length of the first part to the output
14+
15+
Time complexity: O(log n)
16+
- The time complexity is the same as the time complexity of the binary search algorithm
17+
Space complexity: O(n)
18+
- Store the divided array in two variables
19+
"""
20+
21+
from typing import List
22+
23+
24+
class Solution:
25+
def search(self, nums: List[int], target: int) -> int:
26+
if len(nums) == 1:
27+
if target == nums[0]:
28+
return 0
29+
else:
30+
return -1
31+
32+
center = len(nums) // 2
33+
arr1, arr2 = nums[:center], nums[center:]
34+
35+
arr1_left, arr1_right = arr1[0], arr1[-1]
36+
arr2_left, arr2_right = arr2[0], arr2[-1]
37+
38+
if arr1_left <= target <= arr1_right:
39+
output = self.search(arr1, target)
40+
41+
elif arr2_left <= target <= arr2_right:
42+
output = self.search(arr2, target)
43+
44+
if output != -1:
45+
output += len(arr1)
46+
47+
elif arr1_right < arr1_left:
48+
output = self.search(arr1, target)
49+
else:
50+
output = self.search(arr2, target)
51+
52+
if output != -1:
53+
output += len(arr1)
54+
return output

0 commit comments

Comments
 (0)