381.Insert Delete GetRandom O(1) - Duplicates allowed

Tags: [list], [set], [map], [trick], [data_structure], [random], [iterator]

Com: {t}

Hard: [####]

Link: https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/\#/description

Design a data structure that supports all following operations in average O(1) time.

Note: Duplicate elements are allowed.

  1. insert(val): Inserts an item val to the collection.
  2. remove(val): Removes an item val from the collection if present.
  3. getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.

Example:

// Init an empty collection.
RandomizedCollection collection = new RandomizedCollection();

// Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1);

// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].
collection.insert(1);

// Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
collection.insert(2);

// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
collection.getRandom();

// Removes 1 from the collection, returns true. Collection now contains [1,2].
collection.remove(1);

// getRandom should return 1 and 2 both equally likely.
collection.getRandom();

Better Solution: Set, Map, Iterator

class RandomizedCollection(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.val_to_indices = collections.defaultdict(set)
        self.index_to_val = {}

    def insert(self, val):
        """
        Inserts a value to the collection. Returns true if the collection did not already contain the specified element.
        :type val: int
        :rtype: bool
        """
        has_val = val in self.val_to_indices

        # update the val_to_indices.
        self.val_to_indices[val].add(len(self.index_to_val))

        # update the index_to_val.
        self.index_to_val[len(self.index_to_val)] = val

        return not has_val


    def remove(self, val):
        """
        Removes a value from the collection. Returns true if the collection contained the specified element.
        :type val: int
        :rtype: bool
        """
        if val not in self.val_to_indices:
            return False

        last_index = len(self.index_to_val) - 1
        last_val = self.index_to_val[last_index]

        indices = self.val_to_indices[val]
        if last_index in indices:
            # the current val can be the last val.
            indices.remove(last_index)
            if not indices:
                del self.val_to_indices[val]

            del self.index_to_val[last_index]

        else:
            # the current val cannot be the last val.
            index = indices.pop()
            # instead of using set.pop(), here we can also use the followings:
            # index = iter(indices).next()
            # indices.remove(index)
            if not indices:
                del self.val_to_indices[val]

            del self.index_to_val[index]

            self.val_to_indices[last_val].add(index)
            self.val_to_indices[last_val].remove(last_index)
            self.index_to_val[index] = last_val
            del self.index_to_val[last_index]

        return True


    def getRandom(self):
        """
        Get a random element from the collection.
        :rtype: int
        """
        random_index = random.randint(0, len(self.index_to_val) - 1)
        return self.index_to_val[random_index]


# Your RandomizedCollection object will be instantiated and called as such:
# obj = RandomizedCollection()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()

Revelation:

  • In Python, choose and remove one item from set, we can use the followings:
s = set([1, 2, 3])
s.pop()

--------------------
s = set([1, 2, 3])
item = iter(s).next()
s.remove(item)
  • In Java, choose and remove one item from set, we can use the followings:
Set<Integer> s = new HashSet<>();
int item = s.iterator().next();
s.remove(item);

Note:

  • Time complexity for all operations = O(1).

Solution: List, Set, Map

class RandomizedCollection(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.vals = []
        self.indices = collections.defaultdict(set)


    def insert(self, val):
        """
        Inserts a value to the collection. Returns true if the collection did not already contain the specified element.
        :type val: int
        :rtype: bool
        """
        has_val = val in self.indices

        self.vals.append(val)
        self.indices[val].add(len(self.vals) - 1)

        return not has_val


    def remove(self, val):
        """
        Removes a value from the collection. Returns true if the collection contained the specified element.
        :type val: int
        :rtype: bool
        """
        if val not in self.indices:
            return False

        last_index = len(self.vals) - 1
        last_val = self.vals[-1]

        indices = self.indices[val]
        if last_index in indices:
            indices.remove(last_index)
            if not indices:
                del self.indices[val]

            self.vals.pop()
        else:
            index = indices.pop()
            if not indices:
                del self.indices[val]

            self.vals[index] = last_val
            self.indices[last_val].add(index)
            self.indices[last_val].remove(last_index)
            self.vals.pop()

        return True


    def getRandom(self):
        """
        Get a random element from the collection.
        :rtype: int
        """
        random_index = random.randint(0, len(self.vals) - 1)
        return self.vals[random_index]


# Your RandomizedCollection object will be instantiated and called as such:
# obj = RandomizedCollection()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()

results matching ""

    No results matching ""