Skip to content

[Hyun] Week 1 Solutions #26

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
Apr 28, 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
28 changes: 28 additions & 0 deletions best-time-to-buy-and-sell-stock/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
15 changes: 15 additions & 0 deletions contains-duplicate/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
31 changes: 31 additions & 0 deletions two-sum/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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]
}
}
15 changes: 15 additions & 0 deletions valid-anagram/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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: +)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR에 친절하게 설명해주셔서 이 코드를 가까스로 이해했네요 ㅋㅋ
면접관이 Swfit를 쓰는지 여부에 따라서 이런 코드는 호불호가 갈릴 수도 있겠다는 생각이 들었습니다.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다음부터는 숏코딩 보다는 읽기 쉬운 코드로 변경해보겠습니다.
조언 감사드립니다. 🙇

}
}
32 changes: 32 additions & 0 deletions valid-palindrome/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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: /(?<alphabet>[a-zA-Z0-9]+)/) {
alphabets += match.alphabet.lowercased()
}
return String(alphabets.reversed()) == alphabets
}
}