Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
import random


class RandomizedSet:

def __init__(self):
self.data_map = {} # dictionary, aka map, aka hashtable, aka hashmap
self.data = [] # list aka array

def insert(self, val: int) -> bool:

# the problem indicates we need to return False if the item
# is already in the RandomizedSet---checking if it's in the
# dictionary is on average O(1) where as
# checking the array is on average O(n)
if val in self.data_map:
return False

# add the element to the dictionary. Setting the value as the
# length of the list will accurately point to the index of the
# new element. (len(some_list) is equal to the index of the last item +1)
self.data_map[val] = len(self.data)

# add to the list
self.data.append(val)

return True

def remove(self, val: int) -> bool:

# again, if the item is not in the data_map, return False.
# we check the dictionary instead of the list due to lookup complexity
if not val in self.data_map:
return False

# essentially, we're going to move the last element in the list
# into the location of the element we want to remove.
# this is a significantly more efficient operation than the obvious
# solution of removing the item and shifting the values of every item
# in the dicitionary to match their new position in the list
last_elem_in_list = self.data[-1]
index_of_elem_to_remove = self.data_map[val]

self.data_map[last_elem_in_list] = index_of_elem_to_remove
self.data[index_of_elem_to_remove] = last_elem_in_list

# change the last element in the list to now be the value of the element
# we want to remove
self.data[-1] = val

# remove the last element in the list
self.data.pop()

# remove the element to be removed from the dictionary
self.data_map.pop(val)
return True

def getRandom(self) -> int:
# if running outside of leetcode, you need to `import random`.
# random.choice will randomly select an element from the list of data.
return random.choice(self.data)



# Your RandomizedSet object will be instantiated and called as such:
# obj = RandomizedSet()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class RandomizedSet {
private dataMap: Map<number, number>; // dictionary, aka map, aka hashtable, aka hashmap
private data: number[]; // list aka array

constructor() {
this.dataMap = new Map();
this.data = [];
}

insert(val: number): boolean {
// the problem indicates we need to return False if the item
// is already in the RandomizedSet---checking if it's in the
// dictionary is on average O(1) where as
// checking the array is on average O(n)
if (this.dataMap.has(val)) {
return false;
}

// add the element to the dictionary. Setting the value as the
// length of the list will accurately point to the index of the
// new element. (len(some_list) is equal to the index of the last item +1)
this.dataMap.set(val, this.data.length);

// add to the list
this.data.push(val);

return true;
}

remove(val: number): boolean {
// again, if the item is not in the dataMap, return false.
// we check the dictionary instead of the list due to lookup complexity
if (!this.dataMap.has(val)) {
return false;
}

// essentially, we're going to move the last element in the list
// into the location of the element we want to remove.
// this is a significantly more efficient operation than the obvious
// solution of removing the item and shifting the values of every item
// in the dictionary to match their new position in the list
const lastElemInList = this.data[this.data.length - 1];
const indexOfElemToRemove = this.dataMap.get(val)!;

this.dataMap.set(lastElemInList, indexOfElemToRemove);
this.data[indexOfElemToRemove] = lastElemInList;

// change the last element in the list to now be the value of the element
// we want to remove
this.data[this.data.length - 1] = val;

// remove the last element in the list
this.data.pop();

// remove the element to be removed from the dictionary
this.dataMap.delete(val);
return true;
}

getRandom(): number {
// random.choice will randomly select an element from the list of data.
return this.data[Math.floor(Math.random() * this.data.length)];
}
}

/**
* Your RandomizedSet object will be instantiated and called as such:
* var obj = new RandomizedSet()
* var param_1 = obj.insert(val)
* var param_2 = obj.remove(val)
* var param_3 = obj.getRandom()
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_12_insert_delete_get_random import RandomizedSet

class InsertAndDeleteGetRandomTestCase(unittest.TestCase):

def test_first_case(self):
randomized_set = RandomizedSet()
self.assertTrue(randomized_set.insert(1)) # Inserts 1, returns true
self.assertFalse(randomized_set.remove(2)) # 2 not present, returns false
self.assertTrue(randomized_set.insert(2)) # Inserts 2, returns true
self.assertIn(randomized_set.getRandom(), [1, 2]) # getRandom returns 1 or 2
self.assertTrue(randomized_set.remove(1)) # Removes 1, returns true
self.assertFalse(randomized_set.insert(2)) # 2 already present, returns false
self.assertEqual(randomized_set.getRandom(), 2) # Only 2 in set

def test_insert_duplicate(self):
randomized_set = RandomizedSet()
self.assertTrue(randomized_set.insert(5))
self.assertFalse(randomized_set.insert(5)) # duplicate

def test_remove_nonexistent(self):
randomized_set = RandomizedSet()
self.assertFalse(randomized_set.remove(99))

def test_remove_existing(self):
randomized_set = RandomizedSet()
randomized_set.insert(10)
self.assertTrue(randomized_set.remove(10))
self.assertFalse(randomized_set.remove(10)) # already removed

def test_get_random_single_element(self):
randomized_set = RandomizedSet()
randomized_set.insert(42)
self.assertEqual(randomized_set.getRandom(), 42)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest
from src.my_project.interviews.top_150_questions_round_23\
.ex_12_insert_delete_get_random import RandomizedSet

class InsertAndDeleteGetRandomTestCase(unittest.TestCase):

def test_first_case(self):
randomized_set = RandomizedSet()
self.assertTrue(randomized_set.insert(1)) # Inserts 1, returns true
self.assertFalse(randomized_set.remove(2)) # 2 not present, returns false
self.assertTrue(randomized_set.insert(2)) # Inserts 2, returns true
self.assertIn(randomized_set.getRandom(), [1, 2]) # getRandom returns 1 or 2
self.assertTrue(randomized_set.remove(1)) # Removes 1, returns true
self.assertFalse(randomized_set.insert(2)) # 2 already present, returns false
self.assertEqual(randomized_set.getRandom(), 2) # Only 2 in set

def test_insert_duplicate(self):
randomized_set = RandomizedSet()
self.assertTrue(randomized_set.insert(5))
self.assertFalse(randomized_set.insert(5)) # duplicate

def test_remove_nonexistent(self):
randomized_set = RandomizedSet()
self.assertFalse(randomized_set.remove(99))

def test_remove_existing(self):
randomized_set = RandomizedSet()
randomized_set.insert(10)
self.assertTrue(randomized_set.remove(10))
self.assertFalse(randomized_set.remove(10)) # already removed

def test_get_random_single_element(self):
randomized_set = RandomizedSet()
randomized_set.insert(42)
self.assertEqual(randomized_set.getRandom(), 42)
Loading