Load Balancer

https://www.lintcode.com/problem/load-balancer/description

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LoadBalancer {
    private List list = new ArrayList<>();
    private Random random = new Random();

    public LoadBalancer() {
        // do intialization if necessary
    }

    /*
     * @param server_id: add a new server to the cluster
     * @return: nothing
     */
    public void add(int server_id) {
        // write your code here
        list.add(server_id);
    }

    /*
     * @param server_id: server_id remove a bad server from the cluster
     * @return: nothing
     */
    public void remove(int server_id) {
        // write your code here
        list.remove(list.indexOf(server_id));
    }

    /*
     * @return: pick a server in the cluster randomly with equal probability
     */
    public int pick() {
        // write your code here
        return list.get(random.nextInt(list.size()));
    }
}

你可能感兴趣的:(Load Balancer)