-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path526.load-balancer.cpp
88 lines (81 loc) · 1.85 KB
/
526.load-balancer.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
// Tag:
// Time: O(1)
// Space: O(N)
// Ref: -
// Note: -
// Implement a load balancer for web servers.
// It provide the following functionality:
//
// 1.
// Add a new server to the cluster => `add(server_id)`.
// 2.
// Remove a bad server from the cluster => `remove(server_id)`.
// 3.
// Pick a server in the cluster randomly with equal probability => `pick()`.
//
// At beginning, the cluster is empty.
// When `pick()` is called you need to randomly return a `server_id` in the cluster.
//
// **Example 1:**
// ```
// Input:
// add(1)
// add(2)
// add(3)
// pick()
// pick()
// pick()
// pick()
// remove(1)
// pick()
// pick()
// pick()
// Output:
// 1
// 2
// 1
// 3
// 2
// 3
// 3
// Explanation: The return value of pick() is random, it can be either 2 3 3 1 3 2 2 or other.
// ```
//
//
class LoadBalancer {
public:
unordered_map<int, int> table;
vector<int> servers;
LoadBalancer() {
// do intialization if necessary
}
/*
* @param server_id: add a new server to the cluster
* @return: nothing
*/
void add(int server_id) {
// write your code here
servers.push_back(server_id);
table[server_id] = servers.size() - 1;
}
/*
* @param server_id: server_id remove a bad server from the cluster
* @return: nothing
*/
void remove(int server_id) {
// write your code here
int index = table[server_id];
swap(servers[index], servers.back());
table.erase(server_id);
table[servers[index]] = index;
servers.pop_back();
}
/*
* @return: pick a server in the cluster randomly with equal probability
*/
int pick() {
// write your code here
int index = rand() % servers.size();
return servers[index];
}
};