diff --git a/best-time-to-buy-and-sell-stock/WhiteHyun.swift b/best-time-to-buy-and-sell-stock/WhiteHyun.swift new file mode 100644 index 000000000..b463b8061 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/WhiteHyun.swift @@ -0,0 +1,28 @@ +// +// 121. Best Time to Buy and Sell Stock.swift +// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode121 { + func maxProfit(_ prices: [Int]) -> Int { + if prices.count == 1 { return 0 } + var diff = 0 + var left = 0 + var right = 0 + + while right < prices.endIndex { + if prices[left] > prices[right] { + left = right + } else { + diff = max(diff, prices[right] - prices[left]) + } + right += 1 + } + return diff + } +} diff --git a/contains-duplicate/WhiteHyun.swift b/contains-duplicate/WhiteHyun.swift new file mode 100644 index 000000000..0017c5497 --- /dev/null +++ b/contains-duplicate/WhiteHyun.swift @@ -0,0 +1,15 @@ +// +// 217. Contains Duplicate.swift +// https://leetcode.com/problems/contains-duplicate/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode217 { + func containsDuplicate(_ nums: [Int]) -> Bool { + Set(nums).count != nums.count + } +} diff --git a/two-sum/WhiteHyun.swift b/two-sum/WhiteHyun.swift new file mode 100644 index 000000000..6293a95d5 --- /dev/null +++ b/two-sum/WhiteHyun.swift @@ -0,0 +1,31 @@ +// +// 1. Two Sum.swift +// https://leetcode.com/problems/two-sum/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode1 { + func twoSum(_ numbers: [Int], _ target: Int) -> [Int] { + let sortedNumbersWithIndex = numbers.enumerated().sorted { lhs, rhs in + lhs.element < rhs.element + } + var left = 0 + var right = sortedNumbersWithIndex.endIndex - 1 + + while left < right { + let sum = sortedNumbersWithIndex[left].element + sortedNumbersWithIndex[right].element + if sum == target { break } + if sum < target { + left += 1 + } else { + right -= 1 + } + } + + return [sortedNumbersWithIndex[left].offset, sortedNumbersWithIndex[right].offset] + } +} diff --git a/valid-anagram/WhiteHyun.swift b/valid-anagram/WhiteHyun.swift new file mode 100644 index 000000000..5e7903363 --- /dev/null +++ b/valid-anagram/WhiteHyun.swift @@ -0,0 +1,15 @@ +// +// 242. Valid Anagram.swift +// https://leetcode.com/problems/valid-anagram/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode242 { + func isAnagram(_ s: String, _ t: String) -> Bool { + Dictionary(s.map { ($0, 1) }, uniquingKeysWith: +) == Dictionary(t.map { ($0, 1) }, uniquingKeysWith: +) + } +} diff --git a/valid-palindrome/WhiteHyun.swift b/valid-palindrome/WhiteHyun.swift new file mode 100644 index 000000000..38bd64ed1 --- /dev/null +++ b/valid-palindrome/WhiteHyun.swift @@ -0,0 +1,32 @@ +// +// 125. Valid Palindrome.swift +// https://leetcode.com/problems/valid-palindrome/description/ +// Algorithm +// +// Created by 홍승현 on 2024/04/26. +// + +import Foundation + +final class LeetCode125 { + func isPalindrome(_ s: String) -> Bool { + if let regex = try? NSRegularExpression(pattern: "[a-zA-Z0-9]+") { + let string = regex + .matches(in: s, range: .init(location: 0, length: s.count)) + .map { s[Range($0.range, in: s)!].lowercased() } + .joined() + + return string == String(string.reversed()) + } + + return false + } + + func isPalindromeUsingRegexString(_ s: String) -> Bool { + var alphabets = "" + for match in s.matches(of: /(?[a-zA-Z0-9]+)/) { + alphabets += match.alphabet.lowercased() + } + return String(alphabets.reversed()) == alphabets + } +}