File tree Expand file tree Collapse file tree 5 files changed +146
-1
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 +146
-1
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ func maxArea( _ height: [ Int ] ) -> Int {
3+ var heights = height
4+ var start = 0
5+ var end = heights. count - 1
6+ var maxAmount = 0
7+
8+ while start < end {
9+ let startHeight = heights [ start]
10+ let endHeight = heights [ end]
11+ let amount = min ( startHeight, endHeight) * ( end - start)
12+ maxAmount = max ( amount, maxAmount)
13+
14+ if startHeight < endHeight {
15+ start += 1
16+ } else {
17+ end -= 1
18+ }
19+ }
20+
21+ return maxAmount
22+
23+ //시간 O(n)
24+ //공간 O(1)
25+ }
26+ }
27+
Original file line number Diff line number Diff line change 1+ class WordDictionary {
2+ class TrieNode {
3+ var children : [ Character : TrieNode ] = [ : ]
4+ var isEndOfWord = false
5+ }
6+
7+ private var root : TrieNode
8+
9+ init ( ) {
10+ root = TrieNode ( )
11+ }
12+
13+ // O(n) time / O(n) space
14+ func addWord( _ word: String ) {
15+ var node = root
16+
17+ for character in word {
18+ if node. children [ character] == nil {
19+ node. children [ character] = TrieNode ( )
20+ }
21+
22+ node = node. children [ character] !
23+ }
24+
25+ node. isEndOfWord = true
26+ }
27+
28+ // O(m) ~ O(26^m) time / O(m) space
29+ func search( _ word: String ) -> Bool {
30+ return dfs ( word: Array ( word) , index: 0 , node: root)
31+ }
32+
33+ private func dfs( word: [ Character ] , index: Int , node: TrieNode ) -> Bool {
34+ if index == word. count {
35+ return node. isEndOfWord
36+ }
37+
38+ let character = word [ index]
39+
40+ if character == " . " {
41+ for child in node. children. values {
42+ if dfs ( word: word, index: index + 1 , node: child) {
43+ return true
44+ }
45+ }
46+ return false
47+ } else {
48+ guard let child = node. children [ character] else {
49+ return false
50+ }
51+ return dfs ( word: word, index: index + 1 , node: child)
52+ }
53+ }
54+ }
55+
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ class Solution {
22 func groupAnagrams( _ strs: [ String ] ) -> [ [ String ] ] {
33 var stringsByCount = [ [ Int] : [ String] ] ( )
44
5- strs. map { str in
5+ strs. forEach { str in
66 var countsByAlphabet = Array ( repeating: 0 , count: 26 )
77 for char in str. unicodeScalars {
88 countsByAlphabet [ Int ( char. value) - 97 ] += 1
Original file line number Diff line number Diff line change 1+ class Solution {
2+ // O(nlogn) time / O(n) space
3+ func lengthOfLIS( _ nums: [ Int ] ) -> Int {
4+ var piles : [ Int ] = [ ]
5+ // Patience(1인 카드놀이) sort 이용
6+ for num in nums {
7+ let leftInsertion = binarySearch ( piles: piles, target: num)
8+
9+ // piles[k] >= x인 pile이 없을 때만 append(길이 증가)
10+ if leftInsertion == piles. count {
11+ piles. append ( num)
12+ } else {
13+ //가장 왼쪽의 piles[k] >= x 위치에 교체
14+ piles [ leftInsertion] = num
15+ }
16+
17+ }
18+
19+ // piles[k] = 길이 (k+1) 증가수열이 가질 수 있는 '끝값의 최소치'
20+ // sort 로직상 piles.count가 최대한 길게 만들었기 때문에 piles.count는 가장 긴 길이가 됨
21+ return piles. count
22+ }
23+
24+ func binarySearch( piles: [ Int ] , target: Int ) -> Int {
25+ var left = 0
26+
27+ //target의 삽입 위치고 맨 끝에 덧붙일수도 있어서 nums.count - 1 이 아님
28+ var right = piles. count
29+
30+ while left < right {
31+ let mid = left + ( right - left) / 2
32+ if target > piles [ mid] {
33+ left = mid + 1
34+ } else {
35+ //맨 왼쪽 거를 찾아야 함으로 다시 쪼개가면서 맨 왼쪽 거를 찾는다. 맨 왼쪽 거에 넣어야 길이(k) 증가수열의 끝값이 가장 작게 된다. 이래야 최대한 뒤에 계속 더 이어붙여서 최대한 길게 piles를 만들 수 있음.
36+ right = mid
37+ }
38+ }
39+ return left
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func isValid ( _ s: String ) -> Bool {
3+ var brackets : [ Character : Character ] = [ " ( " : " ) " , " [ " : " ] " , " { " : " } " ]
4+ var closers = [ Character] ( )
5+
6+ for character in s {
7+ if let closer = brackets [ character] {
8+ closers. append ( closer)
9+ } else if character == closers. last {
10+ closers. removeLast ( )
11+ } else {
12+ return false
13+ }
14+ }
15+
16+ return closers. isEmpty
17+
18+ //시간 O(n) (string의 길이)
19+ //공간 O(n)
20+ }
21+ }
22+
You can’t perform that action at this time.
0 commit comments