Skip to content

Commit 63fb0a9

Browse files
committed
add reverse pairs
1 parent 2761148 commit 63fb0a9

File tree

5 files changed

+215
-106
lines changed

5 files changed

+215
-106
lines changed

Algorithm Solutions In Swift.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
1F21EEE72F54613100E0274E /* MakeArithmeticProgression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EEE62F54612300E0274E /* MakeArithmeticProgression.swift */; };
11+
1F21EEEB2F56CA9700E0274E /* ReversePairs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F21EEEA2F56CA9300E0274E /* ReversePairs.swift */; };
1112
1F7037522F3E5618002CFFE1 /* Algorithms in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037512F3E5618002CFFE1 /* Algorithms */; };
1213
1F7037552F3E5642002CFFE1 /* BasicContainers in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037542F3E5642002CFFE1 /* BasicContainers */; };
1314
1F7037572F3E5642002CFFE1 /* BitCollections in Frameworks */ = {isa = PBXBuildFile; productRef = 1F7037562F3E5642002CFFE1 /* BitCollections */; };
@@ -334,6 +335,7 @@
334335

335336
/* Begin PBXFileReference section */
336337
1F21EEE62F54612300E0274E /* MakeArithmeticProgression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MakeArithmeticProgression.swift; sourceTree = "<group>"; };
338+
1F21EEEA2F56CA9300E0274E /* ReversePairs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReversePairs.swift; sourceTree = "<group>"; };
337339
1F85E42D2F500D060097933E /* RecursiveBubbleSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecursiveBubbleSort.swift; sourceTree = "<group>"; };
338340
1F85E4302F500D210097933E /* RecusriveInsertionSort.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecusriveInsertionSort.swift; sourceTree = "<group>"; };
339341
1F85E4352F5062420097933E /* SortedRotated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortedRotated.swift; sourceTree = "<group>"; };
@@ -608,6 +610,21 @@
608610
path = "Can Make Arithmetic Progression";
609611
sourceTree = "<group>";
610612
};
613+
1F21EEE82F56CA7100E0274E /* Reverse Pairs */ = {
614+
isa = PBXGroup;
615+
children = (
616+
1F21EEEA2F56CA9300E0274E /* ReversePairs.swift */,
617+
);
618+
path = "Reverse Pairs";
619+
sourceTree = "<group>";
620+
};
621+
1F21EEE92F56CA8D00E0274E /* Next Permutation */ = {
622+
isa = PBXGroup;
623+
children = (
624+
);
625+
path = "Next Permutation";
626+
sourceTree = "<group>";
627+
};
611628
1F85E42C2F500D010097933E /* Recursive Bubble Sort */ = {
612629
isa = PBXGroup;
613630
children = (
@@ -1361,6 +1378,8 @@
13611378
6A88CEE626C056BD00A3746B /* 3 Sum Closest */,
13621379
1F85E42F2F500D1D0097933E /* Recursive Insertion Sort */,
13631380
1F21EEE52F54611E00E0274E /* Can Make Arithmetic Progression */,
1381+
1F21EEE82F56CA7100E0274E /* Reverse Pairs */,
1382+
1F21EEE92F56CA8D00E0274E /* Next Permutation */,
13641383
);
13651384
path = LeetCode;
13661385
sourceTree = "<group>";
@@ -2936,6 +2955,7 @@
29362955
6A91246127064A8900D50C17 /* MaxAreaOfIsland.swift in Sources */,
29372956
6A319EC02908764100801A8A /* SymmetricTree_recursive.swift in Sources */,
29382957
6A651A25290705FB000731BB /* ValidParantheses.swift in Sources */,
2958+
1F21EEEB2F56CA9700E0274E /* ReversePairs.swift in Sources */,
29392959
6AF5789226A2E6F6007B37CA /* NonConstructibleChange.swift in Sources */,
29402960
6A651A46290785A2000731BB /* ValidSuduku.swift in Sources */,
29412961
6A88CEE826C056C900A3746B /* ThreeSumClosest.swift in Sources */,

Algorithm Solutions In Swift/main.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@ import Foundation
22

33

44

5-
var array = [3, 5, 7]
6-
print(canMakeArithmeticProgression(array))
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// ReversePairs.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by littlebanana on 03/03/26.
6+
//
7+
8+
func reversePairs(_ nums: [Int]) -> Int {
9+
var arr = nums
10+
return mergeSort(&arr, low: 0, high: nums.count - 1)
11+
}
12+
13+
func mergeSort(_ nums: inout [Int], low: Int, high: Int) -> Int {
14+
var count = 0
15+
if low >= high { return count }
16+
let mid = (low + high) / 2
17+
count += mergeSort(&nums, low: low, high: mid)
18+
count += mergeSort(&nums, low: mid + 1, high: high)
19+
count += countPairs(nums: nums, low: low, mid: mid, high: high)
20+
merge(nums: &nums, low: low, mid: mid, high: high)
21+
return count
22+
}
23+
24+
func countPairs(nums: [Int], low: Int, mid: Int, high: Int) -> Int {
25+
var right = mid + 1
26+
var count = 0
27+
for i in low...mid {
28+
while right <= high, nums[i] > 2 * nums[right] {
29+
right += 1
30+
}
31+
count += right - (mid + 1)
32+
}
33+
return count
34+
}
35+
36+
func merge(nums: inout [Int], low: Int, mid: Int, high: Int) {
37+
var i = low, j = mid + 1
38+
var temp = [Int]()
39+
while i <= mid, j <= high {
40+
if nums[i] < nums[j] {
41+
temp.append(nums[i])
42+
i += 1
43+
} else {
44+
temp.append(nums[j])
45+
j += 1
46+
}
47+
}
48+
while i <= mid {
49+
temp.append(nums[i])
50+
i += 1
51+
}
52+
while j <= high {
53+
temp.append(nums[j])
54+
j += 1
55+
}
56+
var k = low
57+
for i in 0..<temp.count {
58+
nums[k] = temp[i]
59+
k += 1
60+
}
61+
}
62+
63+
64+
//var arr = [2,4,3,5,1]
65+
//print(reversePairs(arr))

0 commit comments

Comments
 (0)