Skip to content
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
22 changes: 22 additions & 0 deletions container-with-most-water/delight010.swift
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 design-add-and-search-words-data-structure/delight010.swift
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
}
}

21 changes: 21 additions & 0 deletions longest-increasing-subsequence/delight010.swift
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 }) {
Copy link
Contributor

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를 구현해보시는 것도 좋을 것 같습니다.

tails[index] = num
}
}
} else {
tails.append(num)
}
}

return tails.count
}
}

38 changes: 38 additions & 0 deletions spiral-matrix/delight010.swift
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
}
}

19 changes: 19 additions & 0 deletions valid-parentheses/delight010.swift
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
}
}