Skip to content

[Hyun] Week 6 Solution Explanation #123

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 6 commits into from
Jun 10, 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
30 changes: 30 additions & 0 deletions container-with-most-water/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// 11. Container With Most Water
// https://leetcode.com/problems/container-with-most-water/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/08.
//

class Solution {
func maxArea(_ height: [Int]) -> Int {
var totalArea = 0
var left = 0
var right = height.endIndex - 1

while left < right {
let area = (right - left) * min(height[left], height[right])
if area > totalArea {
totalArea = area
}

if height[left] < height[right] {
left += 1
} else {
right -= 1
}
}

return totalArea
}
}
25 changes: 25 additions & 0 deletions find-minimum-in-rotated-sorted-array/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// 153. Find Minimum in Rotated Sorted Array
// https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/09.
//

class Solution {
func findMin(_ nums: [Int]) -> Int {
var left = 0
var right = nums.count - 1
while left < right {
let mid = (left + right) >> 1

if nums[mid] > nums[right] {
left = mid + 1
} else {
right = mid
}
}

return nums[left]
}
}
30 changes: 30 additions & 0 deletions longest-repeating-character-replacement/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// 424. Longest Repeating Character Replacement
// https://leetcode.com/problems/longest-repeating-character-replacement/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/08.
//

final class Solution {
func characterReplacement(_ s: String, _ k: Int) -> Int {
var maxFrequency = 0
var i = 0
var counter: [Character: Int] = [:]
let array = Array(s)

for index in array.indices {
counter[array[index], default: 0] += 1
if maxFrequency < counter[array[index]]! {
maxFrequency = counter[array[index]]!
}

if index - i + 1 > maxFrequency + k {
counter[array[i]]! -= 1
i += 1
}
}

return array.count - i
}
}
40 changes: 40 additions & 0 deletions longest-substring-without-repeating-characters/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// 3. Longest Substring Without Repeating Characters
// https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/08.
//

class Solution {
func lengthOfLongestSubstring(_ s: String) -> Int {
var longest: Int = .min
let array = Array(s)

var set: Set<Character> = []
var startIndex = 0
for index in array.indices {
if set.contains(array[index]) == false {
set.insert(array[index])
continue
}

if longest < index - startIndex {
longest = index - startIndex
}

while array[startIndex] != array[index] {
set.remove(array[startIndex])
startIndex += 1
}
startIndex += 1
}

if longest < array.endIndex - startIndex {
longest = array.endIndex - startIndex
}


return longest
}
}
41 changes: 41 additions & 0 deletions search-in-rotated-sorted-array/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// 33. Search in Rotated Sorted Array
// https://leetcode.com/problems/search-in-rotated-sorted-array/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/06/10.
//

final class Solution {
func search(_ nums: [Int], _ target: Int) -> Int {
guard nums.count != 1
else {
return nums[0] == target ? 0 : -1
}

var left = 0
var right = nums.count - 1

while left <= right {
let mid = (left + right) >> 1

if nums[mid] == target { return mid }

if nums[left] <= nums[mid] {
if target > nums[mid] || target < nums[left] {
left = mid + 1
} else {
right = mid - 1
}
} else {
if target < nums[mid] || target > nums[right] {
right = mid - 1
} else {
left = mid + 1
}
}
}

return -1
}
}