Leetcode SQL

626. Exchange Seats

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

  1. : Inserts an item val to the set if not already present.
  2. : Removes an item val from the set if present.
  3. : Returns a random element from current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.

Example:

Logic: One thing to notice: Aside from create a new variable cnt, we also create a temp table t for cnt. Additionally, Use = instead of == as it is not boolean statement.

Solution:

Link

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store