diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_11_h_index.py b/src/my_project/interviews/top_150_questions_round_22/ex_11_h_index.py new file mode 100644 index 00000000..2bc31b7a --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_11_h_index.py @@ -0,0 +1,16 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def hIndex(self, citations: List[int]) -> int: + + citations.sort(reverse=True) + len_cit:int = len(citations) + answer:int = 0 + + for i in range(len_cit): + + if citations[i] >= i + 1: + answer = i + 1 + + return answer diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_12_insert_delete_get_random.py b/src/my_project/interviews/top_150_questions_round_22/ex_12_insert_delete_get_random.py new file mode 100644 index 00000000..08f299ea --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_12_insert_delete_get_random.py @@ -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() \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_11_h_index_round_22.py b/tests/test_150_questions_round_22/test_11_h_index_round_22.py new file mode 100644 index 00000000..38fb4431 --- /dev/null +++ b/tests/test_150_questions_round_22/test_11_h_index_round_22.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_11_h_index import Solution + +class hIndexTestCase(unittest.TestCase): + + def test_h_index_test_1(self): + solution = Solution() + output = solution.hIndex(citations = [3,0,6,1,5]) + target = 3 + self.assertEqual(output, target) + + def test_h_index_test_2(self): + solution = Solution() + output = solution.hIndex(citations = [1,3,1]) + target = 1 + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_12_insert_delete_get_random_round_22.py b/tests/test_150_questions_round_22/test_12_insert_delete_get_random_round_22.py new file mode 100644 index 00000000..e69de29b