-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAlgorithmic skills. ArrayInversionCount.swift
170 lines (128 loc) · 5.29 KB
/
Algorithmic skills. ArrayInversionCount.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import Foundation
import Glibc
// Solution @ Sergey Leschev, Belarusian State University
// Algorithmic skills. ArrayInversionCount.
// An array A consisting of N integers is given. An inversion is a pair of indexes (P, Q) such that P < Q and A[Q] < A[P].
// Write a function:
// class Solution { public int solution(int[] A); }
// that computes the number of inversions in A, or returns −1 if it exceeds 1,000,000,000.
// For example, in the following array:
// A[0] = -1 A[1] = 6 A[2] = 3
// A[3] = 4 A[4] = 7 A[5] = 4
// there are four inversions:
// (1,2) (1,3) (1,5) (4,5)
// so the function should return 4.
// Write an efficient algorithm for the following assumptions:
// N is an integer within the range [0..100,000];
// each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
public func solution(_ A: inout [Int]) -> Int {
let count = A.count
if count <= 1 { return 0 }
let tree = AVLTree(A[0])
for i in 1..<count {
tree.insert(A[i])
if tree.numberOfInversions > 1_000_000_000 { return -1 }
}
return tree.numberOfInversions
}
class AVLTree {
var root: Node
var numberOfInversions = 0
init(_ newKey: Int) { root = Node(newKey) }
func insert(_ newKey: Int) { root = insert(root, newKey) }
private func insert(_ node: Node, _ newKey: Int) -> Node {
guard node.key != newKey else {
node.count += 1
numberOfInversions += node.rightCount
return node
}
if node.key > newKey {
node.leftCount += 1
if let l = node.left {
node.left = insert(l, newKey)
let rightHeight = node.right?.height ?? 0
node.height = 1 + max(l.height, rightHeight)
node.balance = l.height - rightHeight
numberOfInversions += node.count + node.rightCount
return balance(node)
} else {
node.left = Node(newKey)
let rightHeight = node.right?.height ?? 0
node.height = 1 + max(1, rightHeight)
node.balance = 1 - rightHeight
numberOfInversions += node.count + node.rightCount
return node
}
} else {
node.rightCount += 1
if let r = node.right {
node.right = insert(r, newKey)
let leftHeight = node.left?.height ?? 0
node.height = 1 + max(leftHeight, r.height)
node.balance = leftHeight - r.height
return balance(node)
} else {
node.right = Node(newKey)
let leftHeight = node.left?.height ?? 0
node.height = 1 + max(leftHeight, 1)
node.balance = leftHeight - 1
return node
}
}
}
func rotateRight(_ node: Node) -> Node {
let newTop = node.left!
node.left = newTop.right
node.leftCount = newTop.rightCount
newTop.right = node
newTop.rightCount = node.count + node.leftCount + node.rightCount
updateNodeHeightAndBalance(node)
updateNodeHeightAndBalance(newTop)
return newTop
}
func rotateLeft(_ node: Node) -> Node {
let newTop = node.right!
node.right = newTop.left
node.rightCount = newTop.leftCount
newTop.left = node
newTop.leftCount = node.count + node.leftCount + node.rightCount
updateNodeHeightAndBalance(node)
updateNodeHeightAndBalance(newTop)
return newTop
}
func updateNodeHeightAndBalance(_ node: Node) {
let leftHeight = node.left?.height ?? 0
let rightHeight = node.right?.height ?? 0
node.height = 1 + max(leftHeight, rightHeight)
node.balance = leftHeight - rightHeight
}
func balance(_ node: Node) -> Node {
if let leftBalance = node.left?.balance, node.balance > 1 {
if leftBalance < 0 {
node.left = rotateLeft(node.left!)
return rotateRight(node)
} else {
return rotateRight(node)
}
} else if let rightBalance = node.right?.balance, node.balance < -1 {
if rightBalance < 0 {
return rotateLeft(node)
} else {
node.right = rotateRight(node.right!)
return rotateLeft(node)
}
}
return node
}
}
class Node {
var key: Int
var count = 1
var height = 1
var balance = 0
var left: Node?
var leftCount = 0
var right: Node?
var rightCount = 0
init(_ key: Int) { self.key = key }
}