JAVA | 981. Time Based Key-Value Store | HINDI | LeetCode Daily

preview_player
Показать описание
00:00 Intro
00:07 Approach Discussion
06:34 Coding on LeetCode
08:21 new ArrayList mistake
08:51 correction later in video
15:38 Code debug
16:07 Submitting
16:18 Optimization discussion

Hi folks!
Here we are solving 981. Time Based Key-Value Store using Hash Map and Binary Search.

Easy explanation in Hindi.

If you have any query reach out to me at
Рекомендации по теме
Комментарии
Автор

// JAVA :
class TimeMap {

HashMap<String, List<Pair<String, Integer>>> map;
public TimeMap() {
map = new HashMap<>();
}

public void set(String key, String value, int timestamp) {
map.putIfAbsent(key, new ArrayList<>());
map.get(key).add(new Pair(value, timestamp));
}

public String get(String key, int timestamp) {
String res = "";
if(map.containsKey(key)) {
List<Pair<String, Integer>> temp = map.get(key);
// binary search
int left = 0;
int right = temp.size()-1;
while(left<=right) {
int mid = left + (right-left)/2;
if(temp.get(mid).getValue() <= timestamp) {
res = temp.get(mid).getKey();
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return res;
}
}

udyanishere
Автор

Bro Leetcode pe map.putIfAbsent() name ka method kabhi nahi dekha apke yaha first time dekha OP bhai ♥️.
Bro Ye Pair Class Or Custom Class kese banti h samjj nhi ata h.
Heap Me mostly Custom or Pair class banti h or wo samjh nahi ata h.
Java me min heap max heap krna ho to bhot complex example h code ke Leetcode solution or discussion me. Any Idea kese Seekh skte h ye Class Pair or Custom.

anishbishnoixD
Автор

# Python:
class TimeMap:

def __init__(self):
self.store = {}
def set(self, key, value, timestamp):
if key in self.store:
self.store[key].append((timestamp, value))
else:
self.store[key] = [(timestamp, value)]
def get(self, key, timestamp):
if key in self.store:
for i in range(len(self.store[key])-1, -1, -1):
if self.store[key][i][0] <= timestamp:
return self.store[key][i][1]
return ''

udyanishere