Insert Delete GetRandom O(1) | LeetCode 380 | C++, Java, Python | 🔥🔥

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

June LeetCoding Challenge | Problem 12 | Insert Delete GetRandom O(1) | 12 June,
Facebook Coding Interview question,
google coding interview question,
leetcode,
Insert Delete GetRandom O(1),
Insert Delete GetRandom O(1) c++,
Insert Delete GetRandom O(1) Java,
Insert Delete GetRandom O(1) python,
Insert Delete GetRandom O(1) solution,
380. Insert Delete GetRandom O(1),

#Facebook #CodingInterview #LeetCode #JuneLeetCodingChallenge #Google #Amazon #InsertDeleteGetRandom
Рекомендации по теме
Комментарии
Автор

This is true Machine Learning, Program automatically learns how to be fast.

Amit_Kumar_
Автор

hi...i just want to know where the elements of vector and their indexes are being assigned to the unordered map...or is it prewritten in the code?

hemanthshankar
Автор

In my java solution, Time is taking 132 ms and runtime is beating only 9.5% . Although I am performing all the operations in o(1) time.

Here is my java code : Please suggest Where I went wrong

ArrayList<Integer> list;
HashMap<Integer, Integer> map;
public RandomizedSet() {
list=new ArrayList();
map=new HashMap();
}

/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
if(!list.contains(val)){
list.add(val);
map.put(val, list.size()-1);
return true;
}
else
return false;

}

/** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
if(map.containsKey(val)){
int curIndex =map.get(val);
list.set(curIndex, list.get(list.size()-1));
map.put(list.get(curIndex), curIndex);
list.remove(list.size()-1);
map.remove(val);
return true;
}
else
return false;

}

/** Get a random element from the set. */
public int getRandom() {
int
return list.get(random);

}

praveenj
Автор

Isn't the insert function taking O(n) time??

sushilsekharsatapathy
Автор

Sir pls share java code in discription

himanshuchhikara
welcome to shbcf.ru