File tree Expand file tree Collapse file tree 5 files changed +156
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence Expand file tree Collapse file tree 5 files changed +156
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func maxArea( _ height: [ Int ] ) -> Int {
3
+ var maxArea = 0
4
+ var startPointIndex = 0
5
+ var endPointIndex = height. count - 1
6
+ while startPointIndex < endPointIndex {
7
+ let minHeight = min ( height [ startPointIndex] , height [ endPointIndex] )
8
+ let area = minHeight * ( endPointIndex - startPointIndex)
9
+ maxArea = max ( maxArea, area)
10
+ if height [ startPointIndex] < height [ endPointIndex] {
11
+ startPointIndex += 1
12
+ } else {
13
+ endPointIndex -= 1
14
+ }
15
+ }
16
+ return maxArea
17
+ }
18
+ }
19
+
20
+ // Time Complexity O(N)
21
+ // Space Complexity O(1)
22
+
Original file line number Diff line number Diff line change
1
+ class WordDictionary {
2
+
3
+ class TrieNode {
4
+ var children : [ Character : TrieNode ] = [ : ]
5
+ var isEndOfWord : Bool = false
6
+ }
7
+
8
+ var root : TrieNode
9
+
10
+ init ( ) {
11
+ root = TrieNode ( )
12
+ }
13
+
14
+ func addWord( _ word: String ) {
15
+ var currentNode = root
16
+ for (index, char) in word. enumerated ( ) {
17
+ if currentNode? . children [ char] == nil {
18
+ currentNode? . children [ char] = TrieNode ( )
19
+ }
20
+ currentNode = currentNode? . children [ char]
21
+ }
22
+ currentNode? . isEndOfWord = true
23
+ }
24
+
25
+ func search( _ word: String ) -> Bool {
26
+ return dfs ( root, word: word, index: 0 )
27
+ }
28
+
29
+ func dfs( _ node: TrieNode ? , word: String , index: Int ) -> Bool {
30
+ guard let currentNode = node else {
31
+ return false
32
+ }
33
+ if index == word. count {
34
+ return currentNode. isEndOfWord
35
+ }
36
+
37
+ let char = Array ( word) [ index]
38
+ if char == " . " {
39
+ for child in currentNode. children. values {
40
+ let result = dfs ( child, word: word, index: index + 1 )
41
+ if result {
42
+ return true
43
+ }
44
+ }
45
+ } else {
46
+ if let child = currentNode. children [ char] {
47
+ return dfs ( child, word: word, index: index + 1 )
48
+ } else {
49
+ return false
50
+ }
51
+ }
52
+
53
+ return false
54
+ }
55
+ }
56
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func lengthOfLIS( _ nums: [ Int ] ) -> Int {
3
+ var tails : [ Int ] = [ ]
4
+ for num in nums {
5
+ if let lastValue = tails. last {
6
+ if num > lastValue {
7
+ tails. append ( num)
8
+ } else {
9
+ if let index = tails. firstIndex ( where: { $0 >= num } ) {
10
+ tails [ index] = num
11
+ }
12
+ }
13
+ } else {
14
+ tails. append ( num)
15
+ }
16
+ }
17
+
18
+ return tails. count
19
+ }
20
+ }
21
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func spiralOrder( _ matrix: [ [ Int ] ] ) -> [ Int ] {
3
+ var answer : [ Int ] = [ ]
4
+ var top = 0
5
+ var bottom = matrix. endIndex - 1
6
+ var left = 0
7
+ var right = matrix [ 0 ] . endIndex - 1
8
+ while top <= bottom && left <= right {
9
+ for column in left... right {
10
+ answer. append ( matrix [ top] [ column] )
11
+ }
12
+ top += 1
13
+
14
+ if top <= bottom {
15
+ for row in top... bottom {
16
+ answer. append ( matrix [ row] [ right] )
17
+ }
18
+ }
19
+ right -= 1
20
+
21
+ if top <= bottom {
22
+ for column in stride ( from: right, through: left, by: - 1 ) {
23
+ answer. append ( matrix [ bottom] [ column] )
24
+ }
25
+ }
26
+ bottom -= 1
27
+
28
+ if left <= right {
29
+ for row in stride ( from: bottom, through: top, by: - 1 ) {
30
+ answer. append ( matrix [ row] [ left] )
31
+ }
32
+ }
33
+ left += 1
34
+ }
35
+ return answer
36
+ }
37
+ }
38
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func isValid( _ s: String ) -> Bool {
3
+ let dictionary : [ Character : Character ] = [ " ) " : " ( " , " ] " : " [ " , " } " : " { " ]
4
+ var stack : [ Character ] = [ ]
5
+ for char in s {
6
+ if let openBucket = dictionary [ char] {
7
+ if stack. isEmpty == false , stack. removeLast ( ) == openBucket {
8
+ continue
9
+ } else {
10
+ return false
11
+ }
12
+ }
13
+ stack. append ( char)
14
+ }
15
+
16
+ return stack. isEmpty
17
+ }
18
+ }
19
+
You can’t perform that action at this time.
0 commit comments