-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDesignSkipList.kt
306 lines (283 loc) · 8 KB
/
DesignSkipList.kt
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package questions
import _utils.UseCommentAsDocumentation
import questions.common.ClassInvoker
import utils.shouldBe
import kotlin.random.Random
/**
* <img src="https://assets.leetcode.com/uploads/2019/09/27/1506_skiplist.gif" height="200" width="800"/>
*
* [Source](https://leetcode.com/problems/design-skiplist/) – [Gist](https://www.youtube.com/watch?v=UGaOXaXAM5M)
*/
@UseCommentAsDocumentation
class SkipList {
private val _headNode = Array<Node>(3) { level ->
Node(Int.MIN_VALUE).also {
it.level = level
}
}
private val random = Random(58)
private var array: Array<Node>
init {
for (i in 1.._headNode.lastIndex) {
_headNode[i].downRef = _headNode[i - 1]
}
array = Array(_headNode.size) {
_headNode[it]
}
}
fun search(target: Int): Boolean {
return search(array.last(), target)
}
private fun search(node: Node?, target: Int): Boolean {
if (node == null) {
return false
}
val parent = findParentInCurrentLevel(node, target)
if (parent.value == target) {
return true
}
if (parent.next != null) {
if (parent.next!!.value == target) {
return true
}
}
return search(parent.downRef, target)
}
/**
* Parent is the node with value less than [target]
* and the [Node.next] pointer points to a [Node] whose value is higher than [target]
*/
private fun findParentInCurrentLevel(node: Node, target: Int): Node {
var current: Node? = node
while (current != null) {
if (current.next == null) {
return current
} else if (current.value < target && current.next!!.value >= target) {
return current
}
current = current.next
}
return node
}
fun add(num: Int) {
val node = Node(num)
val level0Parent = findParentInCurrentLevel(array[0], num)
val next = level0Parent.next
// level 0 must contain the node
level0Parent.next = node
level0Parent.level = 0
node.next = next
var tempNode = node
for (i in 1..array.lastIndex) {
if (shouldAddUp()) {
val cloneNode = Node(num)
cloneNode.downRef = tempNode
cloneNode.level = i
val parent = findParentInCurrentLevel(array[i], num)
val next = parent.next
parent.next = cloneNode
cloneNode.next = next
tempNode = cloneNode
}
}
}
private fun shouldAddUp(): Boolean {
return random.nextBoolean()
}
fun erase(num: Int): Boolean {
val eraseList = mutableSetOf<Int>()
for (i in array.lastIndex downTo 0) {
if (i !in eraseList) {
erase(array[i], num, eraseList)
}
}
return eraseList.isNotEmpty()
}
private fun erase(node: Node?, target: Int, erasedList: MutableSet<Int>) {
if (node == null) {
return
}
val parent = findParentInCurrentLevel(node, target)
if (parent.next == null) {
// Not found in current level, so start from scratch in one level below
// erase(array.getOrNull(parent.level - 1), target, erasedList)
} else if (parent.next!!.value != target) {
// erase(array.getOrNull(parent.level - 1), target, erasedList)
} else if (parent.next!!.value == target) {
val nodeToBeRemoved = parent.next!!
erasedList.add(nodeToBeRemoved.level) // collect the levels at which the erasure happened
parent.next = parent.next?.next
}
}
inner class Node(val value: Int) {
var next: Node? = null
var downRef: Node? = null
var level: Int = 0
override fun toString(): String {
return "Node($value, lvl=${level}, next=${next?.value})"
}
}
}
fun main() {
SkipList().apply {
add(2)
add(3)
add(5)
add(9)
add(15)
add(0)
add(7)
search(15) shouldBe true
search(9) shouldBe true
search(2) shouldBe true
search(5) shouldBe true
search(0) shouldBe true
search(7) shouldBe true
search(11) shouldBe false
search(22) shouldBe false
search(33) shouldBe false
search(90) shouldBe false
add(40)
search(40) shouldBe true
erase(40) shouldBe true
search(40) shouldBe false
erase(7) shouldBe true
search(7) shouldBe false
}
SkipList().apply {
for (i in 0..100) {
add(i)
add(i)
add(i)
}
for (i in 1..50) {
search(i) shouldBe true
erase(i) shouldBe true
erase(i) shouldBe true
erase(i) shouldBe true
erase(i) shouldBe false
}
}
run {
val invoker = ClassInvoker<IntArray, Int>(
listOf(
"SkipList",
"add",
"add",
"add",
"add",
"add",
"erase",
"erase",
"add",
"search",
"search",
"add",
"erase",
"search",
"add",
"add",
"add",
"erase",
"search",
"erase",
"search",
"search",
"search",
"erase",
"erase",
"search",
"erase",
"add",
"add",
"erase",
"add",
"search",
"search",
"search",
"search",
"search"
)
) {
it.first()
}
invoker.invoke(
listOf(
intArrayOf(),
intArrayOf(9),
intArrayOf(4),
intArrayOf(5),
intArrayOf(6),
intArrayOf(9),
intArrayOf(2),
intArrayOf(1),
intArrayOf(2),
intArrayOf(7),
intArrayOf(4),
intArrayOf(5),
intArrayOf(6),
intArrayOf(5),
intArrayOf(6),
intArrayOf(7),
intArrayOf(4),
intArrayOf(3),
intArrayOf(6),
intArrayOf(3),
intArrayOf(4),
intArrayOf(3),
intArrayOf(8),
intArrayOf(7),
intArrayOf(6),
intArrayOf(7),
intArrayOf(4),
intArrayOf(1),
intArrayOf(6),
intArrayOf(3),
intArrayOf(4),
intArrayOf(7),
intArrayOf(6),
intArrayOf(1),
intArrayOf(0),
intArrayOf(3)
),
expectedAnswers = listOf(
null,
null,
null,
null,
null,
null,
false,
false,
null,
false,
true,
null,
true,
true,
null,
null,
null,
false,
true,
false,
true,
false,
false,
true,
true,
false,
true,
null,
null,
false,
null,
false,
true,
true,
false,
false
)
)
}
}