-
-
Notifications
You must be signed in to change notification settings - Fork 245
[SeongA] WEEK 06 solutions #1863
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 5 commits
1176146
85295ba
ef1712e
12fc06f
421f406
fdfded7
e4d0892
7d34787
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,22 @@ | ||
class Solution { | ||
func maxArea(_ height: [Int]) -> Int { | ||
var maxArea = 0 | ||
var startPointIndex = 0 | ||
var endPointIndex = height.count - 1 | ||
while startPointIndex < endPointIndex { | ||
let minHeight = min(height[startPointIndex], height[endPointIndex]) | ||
let area = minHeight * (endPointIndex - startPointIndex) | ||
maxArea = max(maxArea, area) | ||
if height[startPointIndex] < height[endPointIndex] { | ||
startPointIndex += 1 | ||
} else { | ||
endPointIndex -= 1 | ||
} | ||
} | ||
return maxArea | ||
} | ||
} | ||
|
||
// Time Complexity O(N) | ||
// Space Complexity O(1) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
class WordDictionary { | ||
|
||
class TrieNode { | ||
var children: [Character: TrieNode] = [:] | ||
var isEndOfWord: Bool = false | ||
} | ||
|
||
var root: TrieNode? | ||
|
||
init() { | ||
root = TrieNode() | ||
} | ||
|
||
func addWord(_ word: String) { | ||
var currentNode = root | ||
for (index, char) in word.enumerated() { | ||
if currentNode?.children[char] == nil { | ||
currentNode?.children[char] = TrieNode() | ||
} | ||
currentNode = currentNode?.children[char] | ||
if index == word.count - 1 { | ||
currentNode?.isEndOfWord = true | ||
|
||
} | ||
} | ||
} | ||
|
||
func search(_ word: String) -> Bool { | ||
return dfs(root, word: word, index: 0) | ||
} | ||
|
||
func dfs(_ node: TrieNode?, word: String, index: Int) -> Bool { | ||
guard let currentNode = node else { | ||
return false | ||
} | ||
if index == word.count { | ||
return currentNode.isEndOfWord | ||
} | ||
|
||
let char = Array(word)[index] | ||
if char == "." { | ||
for child in currentNode.children.values { | ||
let result = dfs(child, word: word, index: index + 1) | ||
if result { | ||
return true | ||
} | ||
} | ||
} else { | ||
if let child = currentNode.children[char] { | ||
return dfs(child, word: word, index: index + 1) | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
func lengthOfLIS(_ nums: [Int]) -> Int { | ||
var tails: [Int] = [] | ||
for num in nums { | ||
if let lastValue = tails.last { | ||
if num > lastValue { | ||
tails.append(num) | ||
} else { | ||
if let index = tails.firstIndex(where: { $0 >= num }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. firstIndex는 O(n)의 함수라 O(logn)인 binarySearch를 구현해보시는 것도 좋을 것 같습니다. |
||
tails[index] = num | ||
} | ||
} | ||
} else { | ||
tails.append(num) | ||
} | ||
} | ||
|
||
return tails.count | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Solution { | ||
func spiralOrder(_ matrix: [[Int]]) -> [Int] { | ||
var answer: [Int] = [] | ||
var top = 0 | ||
var bottom = matrix.endIndex - 1 | ||
var left = 0 | ||
var right = matrix[0].endIndex - 1 | ||
while top <= bottom && left <= right { | ||
for column in left...right { | ||
answer.append(matrix[top][column]) | ||
} | ||
top += 1 | ||
|
||
if top <= bottom { | ||
for row in top...bottom { | ||
answer.append(matrix[row][right]) | ||
} | ||
} | ||
right -= 1 | ||
|
||
if top <= bottom { | ||
for column in stride(from: right, through: left, by: -1) { | ||
answer.append(matrix[bottom][column]) | ||
} | ||
} | ||
bottom -= 1 | ||
|
||
if left <= right { | ||
for row in stride(from: bottom, through: top, by: -1) { | ||
answer.append(matrix[row][left]) | ||
} | ||
} | ||
left += 1 | ||
} | ||
return answer | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
func isValid(_ s: String) -> Bool { | ||
let dictionary: [Character: Character] = [")":"(", "]":"[", "}":"{"] | ||
var stack: [Character] = [] | ||
for char in s { | ||
if let closeBucket = dictionary[char] { | ||
|
||
if stack.isEmpty == false, stack.removeLast() == closeBucket { | ||
continue | ||
} else { | ||
return false | ||
} | ||
} | ||
stack.append(char) | ||
} | ||
|
||
return stack.isEmpty | ||
} | ||
} | ||
|
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.
init일 때 TrieNode()로 넣어주기 때문에 optional일 필요가 없을 것 같아요