Skip to content

Commit 08a5fb0

Browse files
committed
Add solutions and key bindings for vs code
1 parent 3d767da commit 08a5fb0

File tree

11 files changed

+585
-358
lines changed

11 files changed

+585
-358
lines changed

.vscode/keybindings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"key": "cmd+r",
3+
"key": "cmd+shift+r",
44
"command": "code-runner.run",
55
"when": "editorTextFocus && editorLangId == 'swift' && !inDebugRepl && workspaceFolderName == 'Algorithm-Solutions-In-Swift'"
66
}

Algorithm Solutions In Swift.xcodeproj/project.pbxproj

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
1F21EF262F689A6000E0274E /* FixedPoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EF252F689A5900E0274E /* FixedPoint.swift */; };
3232
1F21EF292F689A9B00E0274E /* MaximumCount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EF282F689A9600E0274E /* MaximumCount.swift */; };
3333
1F21EF2C2F6A556900E0274E /* MaxDistanceColors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EF2B2F6A555F00E0274E /* MaxDistanceColors.swift */; };
34+
1F21EF342F75B0D100E0274E /* FindPeakElementArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EF332F75B0CA00E0274E /* FindPeakElementArray.swift */; };
35+
1F21EF362F75B0EB00E0274E /* ShortestDistance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EF352F75B0E400E0274E /* ShortestDistance.swift */; };
36+
1F21EF382F75B11000E0274E /* ConfusingNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EF372F75B10B00E0274E /* ConfusingNumber.swift */; };
37+
1F21EF3A2F75B13100E0274E /* CandySwap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EF392F75B12B00E0274E /* CandySwap.swift */; };
38+
1F21EF3C2F75B14400E0274E /* AlienSorted.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EF3B2F75B14100E0274E /* AlienSorted.swift */; };
39+
1F21EF3E2F75B15C00E0274E /* DesignMessageLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EF3D2F75B15500E0274E /* DesignMessageLogger.swift */; };
3440
1F7037522F3E5618002CFFE1 /* Algorithms in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037512F3E5618002CFFE1 /* Algorithms */; };
3541
1F7037552F3E5642002CFFE1 /* BasicContainers in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037542F3E5642002CFFE1 /* BasicContainers */; };
3642
1F7037572F3E5642002CFFE1 /* BitCollections in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037562F3E5642002CFFE1 /* BitCollections */; };
@@ -380,6 +386,12 @@
380386
1F21EF252F689A5900E0274E /* FixedPoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FixedPoint.swift; sourceTree = "<group>"; };
381387
1F21EF282F689A9600E0274E /* MaximumCount.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaximumCount.swift; sourceTree = "<group>"; };
382388
1F21EF2B2F6A555F00E0274E /* MaxDistanceColors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaxDistanceColors.swift; sourceTree = "<group>"; };
389+
1F21EF332F75B0CA00E0274E /* FindPeakElementArray.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindPeakElementArray.swift; sourceTree = "<group>"; };
390+
1F21EF352F75B0E400E0274E /* ShortestDistance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortestDistance.swift; sourceTree = "<group>"; };
391+
1F21EF372F75B10B00E0274E /* ConfusingNumber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfusingNumber.swift; sourceTree = "<group>"; };
392+
1F21EF392F75B12B00E0274E /* CandySwap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CandySwap.swift; sourceTree = "<group>"; };
393+
1F21EF3B2F75B14100E0274E /* AlienSorted.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlienSorted.swift; sourceTree = "<group>"; };
394+
1F21EF3D2F75B15500E0274E /* DesignMessageLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DesignMessageLogger.swift; sourceTree = "<group>"; };
383395
1F85E42D2F500D060097933E /* RecursiveBubbleSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecursiveBubbleSort.swift; sourceTree = "<group>"; };
384396
1F85E4302F500D210097933E /* RecusriveInsertionSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecusriveInsertionSort.swift; sourceTree = "<group>"; };
385397
1F85E4352F5062420097933E /* SortedRotated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortedRotated.swift; sourceTree = "<group>"; };
@@ -838,6 +850,54 @@
838850
path = "Max Distance";
839851
sourceTree = "<group>";
840852
};
853+
1F21EF2D2F75B03E00E0274E /* Find Peak Element */ = {
854+
isa = PBXGroup;
855+
children = (
856+
1F21EF332F75B0CA00E0274E /* FindPeakElementArray.swift */,
857+
);
858+
path = "Find Peak Element";
859+
sourceTree = "<group>";
860+
};
861+
1F21EF2E2F75B05600E0274E /* Shortest Distance */ = {
862+
isa = PBXGroup;
863+
children = (
864+
1F21EF352F75B0E400E0274E /* ShortestDistance.swift */,
865+
);
866+
path = "Shortest Distance";
867+
sourceTree = "<group>";
868+
};
869+
1F21EF2F2F75B07800E0274E /* Confusing Number */ = {
870+
isa = PBXGroup;
871+
children = (
872+
1F21EF372F75B10B00E0274E /* ConfusingNumber.swift */,
873+
);
874+
path = "Confusing Number";
875+
sourceTree = "<group>";
876+
};
877+
1F21EF302F75B09000E0274E /* Candy Swap */ = {
878+
isa = PBXGroup;
879+
children = (
880+
1F21EF392F75B12B00E0274E /* CandySwap.swift */,
881+
);
882+
path = "Candy Swap";
883+
sourceTree = "<group>";
884+
};
885+
1F21EF312F75B0AA00E0274E /* Is Alien Sorted */ = {
886+
isa = PBXGroup;
887+
children = (
888+
1F21EF3B2F75B14100E0274E /* AlienSorted.swift */,
889+
);
890+
path = "Is Alien Sorted";
891+
sourceTree = "<group>";
892+
};
893+
1F21EF322F75B0C200E0274E /* Message Logger */ = {
894+
isa = PBXGroup;
895+
children = (
896+
1F21EF3D2F75B15500E0274E /* DesignMessageLogger.swift */,
897+
);
898+
path = "Message Logger";
899+
sourceTree = "<group>";
900+
};
841901
1F85E42C2F500D010097933E /* Recursive Bubble Sort */ = {
842902
isa = PBXGroup;
843903
children = (
@@ -1614,6 +1674,12 @@
16141674
1F21EF242F689A5400E0274E /* Fixed Point */,
16151675
1F21EF272F689A9200E0274E /* Maximum Count */,
16161676
1F21EF2A2F6A555A00E0274E /* Max Distance */,
1677+
1F21EF2D2F75B03E00E0274E /* Find Peak Element */,
1678+
1F21EF2E2F75B05600E0274E /* Shortest Distance */,
1679+
1F21EF2F2F75B07800E0274E /* Confusing Number */,
1680+
1F21EF302F75B09000E0274E /* Candy Swap */,
1681+
1F21EF312F75B0AA00E0274E /* Is Alien Sorted */,
1682+
1F21EF322F75B0C200E0274E /* Message Logger */,
16171683
);
16181684
path = LeetCode;
16191685
sourceTree = "<group>";
@@ -3070,6 +3136,7 @@
30703136
6ABE653526F70B78005C8DE5 /* TwoStrings.swift in Sources */,
30713137
6AF5790D26A6EECD007B37CA /* BitwiseAND.swift in Sources */,
30723138
6AF578EB26A3E923007B37CA /* MonotonicArray.swift in Sources */,
3139+
1F21EF362F75B0EB00E0274E /* ShortestDistance.swift in Sources */,
30733140
6A88CEFC26C8DA0A00A3746B /* PalindromeString.swift in Sources */,
30743141
6A34F270288500B50076570B /* MaxSumIncreasingSubsequence.swift in Sources */,
30753142
6AA826F728129F0900F79CDF /* LongestSubstringWithoutDuplication.swift in Sources */,
@@ -3105,9 +3172,11 @@
31053172
6A91243027023BF200D50C17 /* ReverseWords.swift in Sources */,
31063173
6A651A5E29078969000731BB /* PalindromeLinkedList.swift in Sources */,
31073174
6A88CEF826C8D61900A3746B /* ReverseString.swift in Sources */,
3175+
1F21EF3C2F75B14400E0274E /* AlienSorted.swift in Sources */,
31083176
6A4364C027FB836200F09D92 /* LongestConsecutiveSequence.swift in Sources */,
31093177
6A88CF0026C8DE2800A3746B /* ConvertToBinary.swift in Sources */,
31103178
6A651A5829078869000731BB /* DeleteNode.swift in Sources */,
3179+
1F21EF342F75B0D100E0274E /* FindPeakElementArray.swift in Sources */,
31113180
6A4364C327FC04B900F09D92 /* EncodeDecodeStrings.swift in Sources */,
31123181
6A9124492704842200D50C17 /* AddStrings.swift in Sources */,
31133182
6A91242E27023BCF00D50C17 /* ReverseString2.swift in Sources */,
@@ -3185,6 +3254,7 @@
31853254
6AAC7BED2803543E00ADB33F /* LargestIntergerByParity.swift in Sources */,
31863255
6A38C4A92A783E730065B0F5 /* RandomizedSet.swift in Sources */,
31873256
1F21EEED2F56CD3000E0274E /* NextPermutation.swift in Sources */,
3257+
1F21EF382F75B11000E0274E /* ConfusingNumber.swift in Sources */,
31883258
6AF578EF26A3EDCC007B37CA /* ArrayOfProducts.swift in Sources */,
31893259
6A38C4AC2A7845340065B0F5 /* MergeStrings.swift in Sources */,
31903260
6AF5788C26A2E6C0007B37CA /* SortedSquaredArray.swift in Sources */,
@@ -3250,6 +3320,7 @@
32503320
6A91240A26FE5BB800D50C17 /* DesignHashMap.swift in Sources */,
32513321
6AF5795E26AC2BA0007B37CA /* LinkedListConstruction.swift in Sources */,
32523322
6A970A2D28D5990E002ECB54 /* RemoveElement.swift in Sources */,
3323+
1F21EF3E2F75B15C00E0274E /* DesignMessageLogger.swift in Sources */,
32533324
6A91243C2704819900D50C17 /* PermutationString.swift in Sources */,
32543325
6ABE6FC826FC636D00C6E524 /* DivideTwoIntegers.swift in Sources */,
32553326
6AF578FB26A4B69F007B37CA /* BreadthFirstSearch.swift in Sources */,
@@ -3294,6 +3365,7 @@
32943365
6A09C87B27EA690200BB9A38 /* BuySellStock.swift in Sources */,
32953366
6AF578F326A4AF2F007B37CA /* InvertBinaryTree.swift in Sources */,
32963367
6A67AE4B2864E79D00DCD992 /* LowestCommonInteger.swift in Sources */,
3368+
1F21EF3A2F75B13100E0274E /* CandySwap.swift in Sources */,
32973369
6AE51D2D26B2996A00102A7C /* FindSuccessor.swift in Sources */,
32983370
1F21EF172F68997B00E0274E /* TwoSumLessThanK.swift in Sources */,
32993371
6AF673F628CFB675006C7D4E /* WaterArea.swift in Sources */,

Algorithm Solutions In Swift/main.swift

Lines changed: 12 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -11,129 +11,18 @@ 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
27-
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))
14+
8515

8616
// [1, 1] [2, 2]
8717

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))
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// CandySwap.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by littlebanana on 26/03/26.
6+
//
7+
8+
func fairCandySwap(_ aliceSizes: [Int], _ bobSizes: [Int]) -> [Int] {
9+
let totalAliceCandies = aliceSizes.reduce(0, +)
10+
let totalBobCandies = bobSizes.reduce(0, +)
11+
let bobCandyBoxes = Set(bobSizes)
12+
for aliceCandyBox in aliceSizes {
13+
let aliceExpects = aliceCandyBox + (totalBobCandies - totalAliceCandies) / 2
14+
if bobCandyBoxes.contains(aliceExpects) {
15+
return [aliceCandyBox, aliceExpects]
16+
}
17+
}
18+
return []
19+
}
20+
21+
//print(fairCandySwap([2], [1, 3]))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// ConfusingNumber.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by littlebanana on 26/03/26.
6+
//
7+
8+
func confusingNumber(_ n: Int) -> Bool {
9+
let invalidSet: Set<Int> = [2, 3, 4, 5, 7]
10+
let rotatedMap = [
11+
0 : 0,
12+
1 : 1,
13+
6 : 9,
14+
8 : 8,
15+
9 : 6
16+
]
17+
var num = n
18+
var currentNum = 0
19+
while num > 0 {
20+
let last = num % 10
21+
if invalidSet.contains(last) {
22+
return false
23+
}
24+
currentNum = currentNum * 10 + rotatedMap[last]!
25+
num /= 10
26+
}
27+
return currentNum != n
28+
}
29+
30+
31+
//print(confusingNumber(89))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// FindPeakElementArray.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by littlebanana on 26/03/26.
6+
//
7+
8+
func findPeakElement(_ nums: [Int]) -> Int {
9+
var left = 0, right = nums.count - 1
10+
while left >= 0, right < nums.count, left <= right {
11+
let mid = (left + right) / 2
12+
if isPeak(idx: mid) {
13+
return mid
14+
} else if mid > 0, nums[mid] < nums[mid - 1] {
15+
right = mid - 1
16+
} else {
17+
left = mid + 1
18+
}
19+
}
20+
return 0
21+
22+
func isPeak(idx: Int) -> Bool {
23+
let isGreaterThanLeftNeighbour = idx == 0 ? true : nums[idx - 1] < nums[idx]
24+
let isGreaterThanRightNeighbour = idx == nums.count - 1 ? true : nums[idx + 1] < nums[idx]
25+
return isGreaterThanLeftNeighbour && isGreaterThanRightNeighbour
26+
}
27+
}
28+
29+
30+
//print(findPeakElement([1,2]))

0 commit comments

Comments
 (0)