-
-
Notifications
You must be signed in to change notification settings - Fork 247
[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
Merged
+156
−0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1176146
solve problem
delight010 85295ba
solve problem
delight010 ef1712e
solve problem
delight010 12fc06f
solve problem
delight010 421f406
solve problem
delight010 fdfded7
rename closeBucket to openBucket
delight010 e4d0892
refactor
delight010 7d34787
refactor
delight010 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
56 changes: 56 additions & 0 deletions
56
design-add-and-search-words-data-structure/delight010.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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] | ||
} | ||
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 | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) { | ||
tails[index] = num | ||
} | ||
} | ||
} else { | ||
tails.append(num) | ||
} | ||
} | ||
|
||
return tails.count | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 openBucket = dictionary[char] { | ||
if stack.isEmpty == false, stack.removeLast() == openBucket { | ||
continue | ||
} else { | ||
return false | ||
} | ||
} | ||
stack.append(char) | ||
} | ||
|
||
return stack.isEmpty | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
firstIndex는 O(n)의 함수라 O(logn)인 binarySearch를 구현해보시는 것도 좋을 것 같습니다.