-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
160 lines (144 loc) · 4.14 KB
/
solution.cpp
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
/**
* 20 / 20 test cases passed.
* Runtime: 60 ms
* Memory Usage: 33.2 MB
*/
class Skiplist {
public:
Skiplist(): head(INT_MIN, MAX_LEVEL) {}
bool search(int target) {
Node *prev = previousLevel(target)[0];
return prev->level[0] && prev->level[0]->val == target;
}
void add(int num) {
auto prevs = previousLevel(num);
int level = randomLevel();
if (level > curr_max_level) {
for (int i = curr_max_level; i < level; i++) {
prevs[i] = &head;
}
curr_max_level = level;
}
Node *curr = new Node(num, level);
for (int i = level - 1; i >= 0; i--) {
curr->level[i] = prevs[i]->level[i];
prevs[i]->level[i] = curr;
}
}
bool erase(int num) {
auto prevs = previousLevel(num);
if (!prevs[0]->level[0] || prevs[0]->level[0]->val != num) {
return false;
}
Node *del = prevs[0]->level[0];
for (int i = 0; i < curr_max_level; i++) {
if (prevs[i]->level[i] == del) {
prevs[i]->level[i] = del->level[i];
}
}
delete del;
while (curr_max_level > 1 && !head.level[curr_max_level - 1]) {
curr_max_level--;
}
return true;
}
private:
struct Node {
int val;
vector<Node*> level;
Node(int _val, int size) : val(_val), level(size) {}
};
Node head;
int curr_max_level = 1;
const double SKIPLIST_P = 0.25;
static const int MAX_LEVEL = 8;
vector<Node*> previousLevel(int key) {
Node *curr = &head;
vector<Node*> prevs(MAX_LEVEL);
for (int i = curr_max_level - 1; i >= 0; i--) {
while (curr->level[i] && curr->level[i]->val < key) {
curr = curr->level[i];
}
prevs[i] = curr;
}
return prevs;
}
int randomLevel() {
int level = 1;
while ((rand() & 0xFFFF) < (SKIPLIST_P * 0xFFFF) && (level < MAX_LEVEL)) level += 1;
return level;
}
};
/**
* 20 / 20 test cases passed.
* Runtime: 100 ms
* Memory Usage: 63.7 MB
*/
class Skiplist2 {
public:
Skiplist() {
head_ = make_shared<Node>();
}
bool search(int target) {
for (auto node = head_; node; node = node->down) {
while (node->next && node->next->val < target) {
node = node->next;
}
if (node->next && node->next->val == target) {
return true;
}
}
return false;
}
void add(int num) {
stack<shared_ptr<Node>> stk;
for (auto node = head_; node; node = node->down) {
while (node->next && node->next->val < num) {
node = node->next;
}
stk.push(node);
}
shared_ptr<Node> down;
bool insert = true;
while (!stk.empty() && insert) {
auto curr = stk.top(); stk.pop();
curr->next = make_shared<Node>(num, curr->next, down);
down = curr->next;
insert = rand() & 1;
}
if (insert) {
head_ = make_shared<Node>(INT_MIN, nullptr, head_);
}
}
bool erase(int num) {
bool found = false;
for (auto node = head_; node; node = node->down) {
while (node->next && node->next->val < num) {
node = node->next;
}
if (node->next && node->next->val == num) {
found = true;
node->next = node->next->next;
}
}
return found;
}
private:
struct Node {
int val;
shared_ptr<Node> next;
shared_ptr<Node> down;
Node(int _val = INT_MIN,
shared_ptr<Node> _next = nullptr,
shared_ptr<Node> _down = nullptr):
val(_val), next(_next), down(_down) {}
};
shared_ptr<Node> head_;
};
/**
* Your Skiplist object will be instantiated and called as such:
* Skiplist* obj = new Skiplist();
* bool param_1 = obj->search(target);
* obj->add(num);
* bool param_3 = obj->erase(num);
*/