From fbff39247660f1df276e505d40f6c4a5bc78d51e Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sat, 8 Jun 2024 20:59:33 +0900 Subject: [PATCH 1/6] feat: Add solution for LeetCode problem 11 --- container-with-most-water/WhiteHyun.swift | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 container-with-most-water/WhiteHyun.swift diff --git a/container-with-most-water/WhiteHyun.swift b/container-with-most-water/WhiteHyun.swift new file mode 100644 index 000000000..4aa99adcb --- /dev/null +++ b/container-with-most-water/WhiteHyun.swift @@ -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 + } +} From c9576121e1bc67e49394f4005cd36c5f04db894e Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sat, 8 Jun 2024 21:31:09 +0900 Subject: [PATCH 2/6] feat: Add solution for LeetCode problem 3 --- .../WhiteHyun.swift | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 longest-substring-without-repeating-characters/WhiteHyun.swift diff --git a/longest-substring-without-repeating-characters/WhiteHyun.swift b/longest-substring-without-repeating-characters/WhiteHyun.swift new file mode 100644 index 000000000..8e7bc3dfe --- /dev/null +++ b/longest-substring-without-repeating-characters/WhiteHyun.swift @@ -0,0 +1,42 @@ +// +// 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 = [] + var temp: Int = 0 + var startIndex = 0 + for index in array.indices { + if set.contains(array[index]) == false { + set.insert(array[index]) + temp += 1 + continue + } + + if longest < temp { + longest = temp + } + + while array[startIndex] != array[index] { + set.remove(array[startIndex]) + temp -= 1 + startIndex += 1 + } + startIndex += 1 + } + + if longest < temp { + longest = temp + } + + return longest + } +} From a142303265e29c03b0d681525330a56d303b8a8c Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sat, 8 Jun 2024 21:40:56 +0900 Subject: [PATCH 3/6] refactor: Refactor code of problem 3 --- .../WhiteHyun.swift | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/longest-substring-without-repeating-characters/WhiteHyun.swift b/longest-substring-without-repeating-characters/WhiteHyun.swift index 8e7bc3dfe..7be1fdd14 100644 --- a/longest-substring-without-repeating-characters/WhiteHyun.swift +++ b/longest-substring-without-repeating-characters/WhiteHyun.swift @@ -12,31 +12,29 @@ class Solution { let array = Array(s) var set: Set = [] - var temp: Int = 0 var startIndex = 0 for index in array.indices { if set.contains(array[index]) == false { set.insert(array[index]) - temp += 1 continue } - if longest < temp { - longest = temp + if longest < index - startIndex { + longest = index - startIndex } while array[startIndex] != array[index] { set.remove(array[startIndex]) - temp -= 1 startIndex += 1 } startIndex += 1 } - if longest < temp { - longest = temp + if longest < array.endIndex - startIndex { + longest = array.endIndex - startIndex } + return longest } } From 24f4a4d8795f94df67049f48fcbf4c9e16cb017d Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sun, 9 Jun 2024 20:37:30 +0900 Subject: [PATCH 4/6] feat: Add solution for LeetCode problem 424 --- .../WhiteHyun.swift | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 longest-repeating-character-replacement/WhiteHyun.swift diff --git a/longest-repeating-character-replacement/WhiteHyun.swift b/longest-repeating-character-replacement/WhiteHyun.swift new file mode 100644 index 000000000..42c3928ff --- /dev/null +++ b/longest-repeating-character-replacement/WhiteHyun.swift @@ -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 + } +} From 3629c89953c1d8e5c3efe4585a5d90a4ad14e78e Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Mon, 10 Jun 2024 13:26:25 +0900 Subject: [PATCH 5/6] feat: Add solution for LeetCode problem 153 --- .../WhiteHyun.swift | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/WhiteHyun.swift diff --git a/find-minimum-in-rotated-sorted-array/WhiteHyun.swift b/find-minimum-in-rotated-sorted-array/WhiteHyun.swift new file mode 100644 index 000000000..6c189bc6c --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/WhiteHyun.swift @@ -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] + } +} From c331baecf35edf8f80f57f47c5bc7e46c215ae9b Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Mon, 10 Jun 2024 13:54:19 +0900 Subject: [PATCH 6/6] feat: Add solution for LeetCode problem 33 --- .../WhiteHyun.swift | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 search-in-rotated-sorted-array/WhiteHyun.swift diff --git a/search-in-rotated-sorted-array/WhiteHyun.swift b/search-in-rotated-sorted-array/WhiteHyun.swift new file mode 100644 index 000000000..4612ee34d --- /dev/null +++ b/search-in-rotated-sorted-array/WhiteHyun.swift @@ -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 + } +}