Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 27 additions & 0 deletions container-with-most-water/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
func maxArea(_ height: [Int]) -> Int {
var heights = height
Copy link
Contributor

Choose a reason for hiding this comment

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

코드가 깔끔하고 좋네요 👍 height를 안쓰고, heights를 새로 만들어서 쓰시는 이유가 있을까요?

Copy link
Contributor Author

@sonjh1217 sonjh1217 Aug 30, 2025

Choose a reason for hiding this comment

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

func maxArea(_ height: [Int]) -> Int { 라인은 leetCode에서 자동생성된건데.. 제가 습관이 되어서 Collection 타입들은 s가 붙어야 Collection으로 읽혀서요ㅎㅎ Swift에서 권장되는 방식입니답!

var start = 0
var end = heights.count - 1
var maxAmount = 0

while start < end {
let startHeight = heights[start]
let endHeight = heights[end]
let amount = min(startHeight, endHeight) * (end - start)
maxAmount = max(amount, maxAmount)

if startHeight < endHeight {
start += 1
} else {
end -= 1
}
}

return maxAmount

//시간 O(n)
//공간 O(1)
}
}

55 changes: 55 additions & 0 deletions design-add-and-search-words-data-structure/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class WordDictionary {
class TrieNode {
var children: [Character: TrieNode] = [:]
var isEndOfWord = false
}

private var root: TrieNode

init() {
root = TrieNode()
}

// O(n) time / O(n) space
func addWord(_ word: String) {
var node = root

for character in word {
if node.children[character] == nil {
node.children[character] = TrieNode()
}

node = node.children[character]!
}

node.isEndOfWord = true
}

// O(m) ~ O(26^m) time / O(1) space
Copy link
Contributor

Choose a reason for hiding this comment

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

공간복잡도가 왜 O(1)이라고 생각하셨는지 여쭤봐도 될까요?? word의 길이만큼 공간이 필요하지 않나요..? 제가 잘 몰라서 여쭤봅니다..!!! 코드가 엄청 깔끔하고 좋네요 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

e90ae4f 와 감사합니다! Array(word)며 재귀 스택 쌓이는거며 다 O(m)인데.. 왜 저렇게 생각했는지 모르겠네요ㅠ 아직 재귀에 대해서 잘 파악을 못해서 그랬던 것 같아요. 감사합니답!

func search(_ word: String) -> Bool {
return dfs(word: Array(word), index: 0, node: root)
}

private func dfs(word: [Character], index: Int, node: TrieNode) -> Bool {
if index == word.count {
return node.isEndOfWord
}

let character = word[index]

if character == "." {
for child in node.children.values {
if dfs(word: word, index: index + 1, node: child) {
return true
}
}
return false
} else {
guard let child = node.children[character] else {
return false
}
return dfs(word: word, index: index + 1, node: child)
}
}
}

2 changes: 1 addition & 1 deletion group-anagrams/sonjh1217.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Solution {
func groupAnagrams(_ strs: [String]) -> [[String]] {
var stringsByCount = [[Int]: [String]]()

strs.map { str in
strs.forEach { str in
var countsByAlphabet = Array(repeating: 0, count: 26)
for char in str.unicodeScalars {
countsByAlphabet[Int(char.value) - 97] += 1
Expand Down
22 changes: 22 additions & 0 deletions valid-parentheses/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
func isValid (_ s: String) -> Bool {
var brackets: [Character: Character] = ["(": ")", "[": "]", "{": "}"]
var closers = [Character]()

for character in s {
if let closer = brackets[character] {
closers.append(closer)
} else if character == closers.last {
closers.removeLast()
} else {
return false
}
}

return closers.isEmpty

//시간 O(n) (string의 길이)
//공간 O(n)
}
}