Skip to content

Commit 6b99c02

Browse files
authored
Merge pull request #123 from WhiteHyun/whitehyun/week6
[Hyun] Week 6 Solution Explanation
2 parents 5379515 + c331bae commit 6b99c02

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// 11. Container With Most Water
3+
// https://leetcode.com/problems/container-with-most-water/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/08.
7+
//
8+
9+
class Solution {
10+
func maxArea(_ height: [Int]) -> Int {
11+
var totalArea = 0
12+
var left = 0
13+
var right = height.endIndex - 1
14+
15+
while left < right {
16+
let area = (right - left) * min(height[left], height[right])
17+
if area > totalArea {
18+
totalArea = area
19+
}
20+
21+
if height[left] < height[right] {
22+
left += 1
23+
} else {
24+
right -= 1
25+
}
26+
}
27+
28+
return totalArea
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// 153. Find Minimum in Rotated Sorted Array
3+
// https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/09.
7+
//
8+
9+
class Solution {
10+
func findMin(_ nums: [Int]) -> Int {
11+
var left = 0
12+
var right = nums.count - 1
13+
while left < right {
14+
let mid = (left + right) >> 1
15+
16+
if nums[mid] > nums[right] {
17+
left = mid + 1
18+
} else {
19+
right = mid
20+
}
21+
}
22+
23+
return nums[left]
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// 424. Longest Repeating Character Replacement
3+
// https://leetcode.com/problems/longest-repeating-character-replacement/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/08.
7+
//
8+
9+
final class Solution {
10+
func characterReplacement(_ s: String, _ k: Int) -> Int {
11+
var maxFrequency = 0
12+
var i = 0
13+
var counter: [Character: Int] = [:]
14+
let array = Array(s)
15+
16+
for index in array.indices {
17+
counter[array[index], default: 0] += 1
18+
if maxFrequency < counter[array[index]]! {
19+
maxFrequency = counter[array[index]]!
20+
}
21+
22+
if index - i + 1 > maxFrequency + k {
23+
counter[array[i]]! -= 1
24+
i += 1
25+
}
26+
}
27+
28+
return array.count - i
29+
}
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// 3. Longest Substring Without Repeating Characters
3+
// https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/08.
7+
//
8+
9+
class Solution {
10+
func lengthOfLongestSubstring(_ s: String) -> Int {
11+
var longest: Int = .min
12+
let array = Array(s)
13+
14+
var set: Set<Character> = []
15+
var startIndex = 0
16+
for index in array.indices {
17+
if set.contains(array[index]) == false {
18+
set.insert(array[index])
19+
continue
20+
}
21+
22+
if longest < index - startIndex {
23+
longest = index - startIndex
24+
}
25+
26+
while array[startIndex] != array[index] {
27+
set.remove(array[startIndex])
28+
startIndex += 1
29+
}
30+
startIndex += 1
31+
}
32+
33+
if longest < array.endIndex - startIndex {
34+
longest = array.endIndex - startIndex
35+
}
36+
37+
38+
return longest
39+
}
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// 33. Search in Rotated Sorted Array
3+
// https://leetcode.com/problems/search-in-rotated-sorted-array/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/10.
7+
//
8+
9+
final class Solution {
10+
func search(_ nums: [Int], _ target: Int) -> Int {
11+
guard nums.count != 1
12+
else {
13+
return nums[0] == target ? 0 : -1
14+
}
15+
16+
var left = 0
17+
var right = nums.count - 1
18+
19+
while left <= right {
20+
let mid = (left + right) >> 1
21+
22+
if nums[mid] == target { return mid }
23+
24+
if nums[left] <= nums[mid] {
25+
if target > nums[mid] || target < nums[left] {
26+
left = mid + 1
27+
} else {
28+
right = mid - 1
29+
}
30+
} else {
31+
if target < nums[mid] || target > nums[right] {
32+
right = mid - 1
33+
} else {
34+
left = mid + 1
35+
}
36+
}
37+
}
38+
39+
return -1
40+
}
41+
}

0 commit comments

Comments
 (0)