-
-
Notifications
You must be signed in to change notification settings - Fork 245
[sonjh1217] WEEK 06 solutions #1864
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution { | ||
func maxArea(_ height: [Int]) -> Int { | ||
var heights = height | ||
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) | ||
} | ||
} | ||
|
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 | ||
|
||
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) | ||
} | ||
} | ||
} | ||
|
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) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드가 깔끔하고 좋네요 👍 height를 안쓰고, heights를 새로 만들어서 쓰시는 이유가 있을까요?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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에서 권장되는 방식입니답!