Skip to content

Commit 421b4e3

Browse files
committed
add digits
1 parent 63fb0a9 commit 421b4e3

File tree

7 files changed

+437
-216
lines changed

7 files changed

+437
-216
lines changed

Algorithm Solutions In Swift.xcodeproj/project.pbxproj

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
/* Begin PBXBuildFile section */
1010
1F21EEE72F54613100E0274E /* MakeArithmeticProgression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EEE62F54612300E0274E /* MakeArithmeticProgression.swift */; };
1111
1F21EEEB2F56CA9700E0274E /* ReversePairs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EEEA2F56CA9300E0274E /* ReversePairs.swift */; };
12+
1F21EEED2F56CD3000E0274E /* NextPermutation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EEEC2F56CD2A00E0274E /* NextPermutation.swift */; };
13+
1F21EEF12F56F53D00E0274E /* MergeInPlace.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EEF02F56F53800E0274E /* MergeInPlace.swift */; };
14+
1F21EEF32F56F54800E0274E /* MajorityElement2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EEF22F56F54200E0274E /* MajorityElement2.swift */; };
15+
1F21EEF62F5F45A200E0274E /* AddDigits.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EEF52F5F459F00E0274E /* AddDigits.swift */; };
1216
1F7037522F3E5618002CFFE1 /* Algorithms in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037512F3E5618002CFFE1 /* Algorithms */; };
1317
1F7037552F3E5642002CFFE1 /* BasicContainers in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037542F3E5642002CFFE1 /* BasicContainers */; };
1418
1F7037572F3E5642002CFFE1 /* BitCollections in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037562F3E5642002CFFE1 /* BitCollections */; };
@@ -336,6 +340,10 @@
336340
/* Begin PBXFileReference section */
337341
1F21EEE62F54612300E0274E /* MakeArithmeticProgression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MakeArithmeticProgression.swift; sourceTree = "<group>"; };
338342
1F21EEEA2F56CA9300E0274E /* ReversePairs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReversePairs.swift; sourceTree = "<group>"; };
343+
1F21EEEC2F56CD2A00E0274E /* NextPermutation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextPermutation.swift; sourceTree = "<group>"; };
344+
1F21EEF02F56F53800E0274E /* MergeInPlace.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MergeInPlace.swift; sourceTree = "<group>"; };
345+
1F21EEF22F56F54200E0274E /* MajorityElement2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MajorityElement2.swift; sourceTree = "<group>"; };
346+
1F21EEF52F5F459F00E0274E /* AddDigits.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddDigits.swift; sourceTree = "<group>"; };
339347
1F85E42D2F500D060097933E /* RecursiveBubbleSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecursiveBubbleSort.swift; sourceTree = "<group>"; };
340348
1F85E4302F500D210097933E /* RecusriveInsertionSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecusriveInsertionSort.swift; sourceTree = "<group>"; };
341349
1F85E4352F5062420097933E /* SortedRotated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortedRotated.swift; sourceTree = "<group>"; };
@@ -621,10 +629,35 @@
621629
1F21EEE92F56CA8D00E0274E /* Next Permutation */ = {
622630
isa = PBXGroup;
623631
children = (
632+
1F21EEEC2F56CD2A00E0274E /* NextPermutation.swift */,
624633
);
625634
path = "Next Permutation";
626635
sourceTree = "<group>";
627636
};
637+
1F21EEEE2F56F51C00E0274E /* Merge In Place */ = {
638+
isa = PBXGroup;
639+
children = (
640+
1F21EEF02F56F53800E0274E /* MergeInPlace.swift */,
641+
);
642+
path = "Merge In Place";
643+
sourceTree = "<group>";
644+
};
645+
1F21EEEF2F56F53200E0274E /* Majority Element 2 */ = {
646+
isa = PBXGroup;
647+
children = (
648+
1F21EEF22F56F54200E0274E /* MajorityElement2.swift */,
649+
);
650+
path = "Majority Element 2";
651+
sourceTree = "<group>";
652+
};
653+
1F21EEF42F5F459A00E0274E /* Add Digits */ = {
654+
isa = PBXGroup;
655+
children = (
656+
1F21EEF52F5F459F00E0274E /* AddDigits.swift */,
657+
);
658+
path = "Add Digits";
659+
sourceTree = "<group>";
660+
};
628661
1F85E42C2F500D010097933E /* Recursive Bubble Sort */ = {
629662
isa = PBXGroup;
630663
children = (
@@ -1272,6 +1305,7 @@
12721305
6A88CEE526C056A400A3746B /* LeetCode */ = {
12731306
isa = PBXGroup;
12741307
children = (
1308+
1F21EEF42F5F459A00E0274E /* Add Digits */,
12751309
1F85E4372F52E2EF0097933E /* Set Zeros Matrix */,
12761310
1F85E4342F50623E0097933E /* Sorted and Rotated Array */,
12771311
1F85E42C2F500D010097933E /* Recursive Bubble Sort */,
@@ -1380,6 +1414,8 @@
13801414
1F21EEE52F54611E00E0274E /* Can Make Arithmetic Progression */,
13811415
1F21EEE82F56CA7100E0274E /* Reverse Pairs */,
13821416
1F21EEE92F56CA8D00E0274E /* Next Permutation */,
1417+
1F21EEEE2F56F51C00E0274E /* Merge In Place */,
1418+
1F21EEEF2F56F53200E0274E /* Majority Element 2 */,
13831419
);
13841420
path = LeetCode;
13851421
sourceTree = "<group>";
@@ -2894,6 +2930,7 @@
28942930
6A651A222907054E000731BB /* ReverseLinkedLists.swift in Sources */,
28952931
6AF578A126A2E7A7007B37CA /* MinimumWaitingTime.swift in Sources */,
28962932
6AF5793126A9E0D3007B37CA /* LinkedList.swift in Sources */,
2933+
1F21EEF32F56F54800E0274E /* MajorityElement2.swift in Sources */,
28972934
6AF5789E26A2E74D007B37CA /* DepthFirstSearch.swift in Sources */,
28982935
6A651A552907881B000731BB /* LongestCommonPrefix.swift in Sources */,
28992936
6A4364C927FC655400F09D92 /* TopKFrequentElements.swift in Sources */,
@@ -2939,6 +2976,7 @@
29392976
6A651A49290785FF000731BB /* ReverseInteger.swift in Sources */,
29402977
6AAC7BED2803543E00ADB33F /* LargestIntergerByParity.swift in Sources */,
29412978
6A38C4A92A783E730065B0F5 /* RandomizedSet.swift in Sources */,
2979+
1F21EEED2F56CD3000E0274E /* NextPermutation.swift in Sources */,
29422980
6AF578EF26A3EDCC007B37CA /* ArrayOfProducts.swift in Sources */,
29432981
6A38C4AC2A7845340065B0F5 /* MergeStrings.swift in Sources */,
29442982
6AF5788C26A2E6C0007B37CA /* SortedSquaredArray.swift in Sources */,
@@ -2977,6 +3015,7 @@
29773015
6A4364B127F6F6EC00F09D92 /* SpiralMatrix.swift in Sources */,
29783016
6A34F27928891CD10076570B /* MinNumberOfJumps.swift in Sources */,
29793017
6AF5788926A2E6A4007B37CA /* ValidateSubsequence.swift in Sources */,
3018+
1F21EEF12F56F53D00E0274E /* MergeInPlace.swift in Sources */,
29803019
6A4364BD27FA242600F09D92 /* FindPlayers.swift in Sources */,
29813020
6A912457270646FB00D50C17 /* WordPattern.swift in Sources */,
29823021
6AC63CA42B88F2CF0050F3E8 /* KthLargestElement.swift in Sources */,
@@ -2989,6 +3028,7 @@
29893028
6AF5788F26A2E6D7007B37CA /* TournamentWinner.swift in Sources */,
29903029
6A88CEDF26BFD73500A3746B /* SearchInSortedMatrix.swift in Sources */,
29913030
6AF5792126A860E8007B37CA /* FirstDuplicateValue.swift in Sources */,
3031+
1F21EEF62F5F45A200E0274E /* AddDigits.swift in Sources */,
29923032
6A651A3A290710AC000731BB /* ArrayRotation.swift in Sources */,
29933033
6AF5793A26A9E332007B37CA /* TypeAliases.swift in Sources */,
29943034
6AC63CAD2B8A64E80050F3E8 /* FindAllPeopleWithSecrets.swift in Sources */,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// AddDigits.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by littlebanana on 09/03/26.
6+
//
7+
8+
func addDigits(_ num: Int) -> Int {
9+
// digital root of a number (in base 10) is the remainder of the number when divided by 9
10+
// there are three cases -
11+
// for num == 0, return 0
12+
// if the num is divisible by 9, then the result will be always 9
13+
// otherwise the result is num%9
14+
15+
if num == 0 { return 0 }
16+
if num % 9 == 0 { return 9 }
17+
return num % 9
18+
19+
}
20+
21+
func addDigitsRecursive(_ num: Int) -> Int {
22+
func helper(_ num: Int) -> Int {
23+
if "\(num)".count == 1 {
24+
return num
25+
}
26+
var n = num, sum = 0
27+
while n > 0 {
28+
sum += n%10
29+
n /= 10
30+
}
31+
return helper(sum)
32+
}
33+
return helper(num)
34+
}
35+
36+
//print(addDigits(38))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// MajorityElement2.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by littlebanana on 03/03/26.
6+
//
7+
8+
func majorityElement2(_ nums: [Int]) -> [Int] {
9+
// there can be max 2 elements appear n/3 times
10+
var fm = (0, 0), sm = (0, 0)
11+
for num in nums {
12+
if num == fm.0 {
13+
fm.1 += 1
14+
} else if num == sm.0 {
15+
sm.1 += 1
16+
} else if fm.1 == 0 {
17+
fm.1 += 1
18+
fm.0 = num
19+
} else if sm.1 == 0 {
20+
sm.1 += 1
21+
sm.0 = num
22+
} else {
23+
fm.1 -= 1
24+
sm.1 -= 1
25+
}
26+
}
27+
var final = [Int]()
28+
for candidate in [fm, sm] {
29+
let count = nums.count{ $0 == candidate.0 && candidate.1 > 1 }
30+
if count > nums.count/3 {
31+
final.append(candidate.0)
32+
}
33+
}
34+
return final
35+
}
36+
37+
//var arr = [0, 0, 0]
38+
//print(majorityElement2(arr))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// MergeInPlace.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by littlebanana on 03/03/26.
6+
//
7+
8+
func merge(_ nums1: inout [Int], _ m: Int, _ nums2: [Int], _ n: Int) {
9+
var i = m - 1, j = n - 1
10+
var k = nums1.count - 1
11+
while i >= 0, j >= 0 {
12+
if nums1[i] > nums2[j] {
13+
nums1[k] = nums1[i]
14+
i -= 1
15+
} else {
16+
nums1[k] = nums2[j]
17+
j -= 1
18+
}
19+
k -= 1
20+
}
21+
while i >= 0 {
22+
nums1[k] = nums1[i]
23+
i -= 1
24+
k -= 1
25+
}
26+
while j >= 0 {
27+
nums1[k] = nums2[j]
28+
j -= 1
29+
k -= 1
30+
}
31+
}
32+
33+
//var nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
34+
//merge(&nums1, m, nums2, n)
35+
//print(nums1)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// NextPermutation.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by littlebanana on 03/03/26.
6+
//
7+
8+
func nextPermutation(_ nums: inout [Int]) {
9+
var idx = -1
10+
let size = nums.count
11+
// traverse from n-2 to 0, to find where the break point where nums[i] < nums[i + 1]
12+
for i in stride(from: size - 2, through: 0, by: -1) {
13+
if nums[i] < nums[i + 1] {
14+
idx = i
15+
break
16+
}
17+
}
18+
// if there is no such index means next permutation is not possible, nums is already
19+
if idx == -1 {
20+
nums.reverse()
21+
return
22+
}
23+
for i in stride(from: size - 1, through: 0, by: -1) {
24+
if nums[i] > nums[idx] {
25+
nums.swapAt(i, idx)
26+
break
27+
}
28+
}
29+
reverse(&nums, from: idx + 1, to: size - 1)
30+
}
31+
32+
//var arr = [2, 1, 5, 4, 3, 0, 0]
33+
//nextPermutation(&arr)

0 commit comments

Comments
 (0)