@@ -11,4 +11,129 @@ import Foundation
1111
1212
1313//*************** START PRACTICE *************************
14+ func findPeakElement ( _ nums : [ Int ] ) -> Int {
15+ var left = 0 , right = nums . count - 1
16+ while left >= 0 , right < nums . count , left <= right {
17+ let mid = ( left + right ) / 2
18+ if isPeak ( idx : mid ) {
19+ return mid
20+ } else if mid > 0 , nums [ mid ] < nums [ mid - 1 ] {
21+ right = mid - 1
22+ } else {
23+ left = mid + 1
24+ }
25+ }
26+ return 0
1427
28+ func isPeak ( idx : Int ) -> Bool {
29+ let isGreaterThanLeftNeighbour = idx == 0 ? true : nums [ idx - 1 ] < nums [ idx ]
30+ let isGreaterThanRightNeighbour = idx == nums . count - 1 ? true : nums [ idx + 1 ] < nums [ idx ]
31+ return isGreaterThanLeftNeighbour && isGreaterThanRightNeighbour
32+ }
33+ }
34+
35+
36+ print ( findPeakElement ( [ 1 , 2 ] ) )
37+
38+
39+ func shortestDistance ( _ wordsDict : [ String ] , _ word1 : String , _ word2 : String ) -> Int {
40+ var i = - 1 , j = - 1 , distance = wordsDict . count
41+ for ( idx , word ) in wordsDict . enumerated ( ) {
42+ if word == word1 {
43+ i = idx
44+ }
45+ if word == word2 {
46+ j = idx
47+ }
48+ if i > - 1 , j > - 1 {
49+ distance = min ( distance , abs ( i - j ) )
50+ }
51+
52+ }
53+ return distance
54+ }
55+
56+ let words = [ " practice " , " makes " , " perfect " , " coding " , " makes " ]
57+ let word1 = " makes " , word2 = " coding "
58+ //print(shortestDistance(words, word1, word2))
59+
60+
61+ func confusingNumber ( _ n : Int ) -> Bool {
62+ let invalidSet : Set < Int > = [ 2 , 3 , 4 , 5 , 7 ]
63+ let rotatedMap = [
64+ 0 : 0 ,
65+ 1 : 1 ,
66+ 6 : 9 ,
67+ 8 : 8 ,
68+ 9 : 6
69+ ]
70+ var num = n
71+ var currentNum = 0
72+ while num > 0 {
73+ let last = num % 10
74+ if invalidSet . contains ( last ) {
75+ return false
76+ }
77+ currentNum = currentNum * 10 + rotatedMap [ last ] !
78+ num /= 10
79+ }
80+ return currentNum != n
81+ }
82+
83+
84+ print ( confusingNumber ( 89 ) )
85+
86+ // [1, 1] [2, 2]
87+
88+ func fairCandySwap ( _ aliceSizes : [ Int ] , _ bobSizes : [ Int ] ) -> [ Int ] {
89+ let totalAliceCandies = aliceSizes . reduce ( 0 , + )
90+ let totalBobCandies = bobSizes . reduce ( 0 , + )
91+ let bobCandyBoxes = Set ( bobSizes )
92+ for aliceCandyBox in aliceSizes {
93+ let aliceExpects = aliceCandyBox + ( totalBobCandies - totalAliceCandies ) / 2
94+ if bobCandyBoxes . contains ( aliceExpects ) {
95+ return [ aliceCandyBox , aliceExpects ]
96+ }
97+ }
98+ return [ ]
99+ }
100+
101+ print ( fairCandySwap ( [ 2 ] , [ 1 , 3 ] ) )
102+
103+
104+ func isAlienSorted ( _ words : [ String ] , _ order : String ) -> Bool {
105+ // create custom order map
106+ var alienOrder = [ Character : Int ] ( )
107+ for ( idx , char ) in order . enumerated ( ) {
108+ alienOrder [ char ] = idx
109+ }
110+
111+ // compare adjacent words
112+ for i in 0 ..< words . count - 1 {
113+ let firstWord = Array ( words [ i ] )
114+ let secondWord = Array ( words [ i + 1 ] )
115+ if ! isOrdered ( firstWord , secondWord ) {
116+ return false
117+ }
118+ }
119+ return true
120+
121+ func isOrdered ( _ first : [ Character ] , _ second : [ Character ] ) -> Bool {
122+ let minLength = min ( first . count , second . count )
123+ for i in 0 ..< minLength {
124+ let w1 = first [ i ]
125+ let w2 = second [ i ]
126+
127+ if w1 != w2 {
128+ return alienOrder [ w1 ] ! < alienOrder [ w2 ] !
129+ }
130+ }
131+
132+ return first . count <= second . count
133+ }
134+ }
135+
136+
137+ let words1 = [ " hello " , " leetcode " ]
138+ let order = " hlabcdefgijkmnopqrstuvwxyz "
139+ print ( isAlienSorted ( words1 , order ) )
0 commit comments