Time Based Key-Value Store - Leetcode 981 - Python

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


0:00 - Read the problem
0:35 - Drawing Explanation
10:30 - Coding Explanation

leetcode 981

#coding #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

That's so interesting. I didn't know you could find the closest elements this way if the value being searched for doesn't exist in a binary search. Never would have thought of that.

halahmilksheikh
Автор

i hated this problem but ur explanation made it less complicated ty

dianav
Автор

Nice. And for binary search, could just use right pointer at end of loop -- as that should be the closest value less than target

Dhruvbala
Автор

you are a leetcode legend!...i have watched a couple of your leetcode videos solution

adeniyiadeboye
Автор

Damn I love this problem and your explaination, thank you man!
Liked again and commenting to support!

symbol
Автор

I was racking my brain with this, didn't read or realize that timestamp was in increasing order, I even implement a sort method in the set so that the array was sorted and the get could be done in log n time, but it wasn't enough some test failed because the time keep running out, then I saw that part of the video expecting some fancy algorithm and was like ._. oh? it's already sorted.

gregorvm
Автор

"Questions to ask in real interviews" - I'd love if you share thoughts on this topic on other problems too! In my experience, it makes a big difference, especially if you can't solve a problem during the interview.

arkamukherjee
Автор

I got this problem on an interview, if only I was subscribed to you back then.

nishiketsingh
Автор

This video help me how to find optimal solution.
What is my first solution:
Implement binary search and if value is not finded just go from the end of array, becouse last element is with highest timestamp and check if we can find value < then timestamp.
So now you can see this is time compl of O(n) + O(log n) but O(n) is dominant then time complexity in worst case will be O(n), but in general they will be better but we are looking for worst case.

dusvn
Автор

Very concise solution and easy to understand. Thank you!

baolingchen
Автор

Thanks neetcode, Presentation is really good

iamnoob
Автор

map<string, vector<pair<string, int>>>mp;

void set(string key, string value, int timestamp) {
mp[key].push_back({value, timestamp});
}

string get(string key, int timestamp) {
auto it=mp[key];
int high=it.size()-1;
int low=0;
string ans="";
while(low<=high)
{
int mid=low+(high-low)/2;

{
ans=it[mid].first;
low=mid+1;
}
else
high=mid-1;
}
return ans;

} getting tle in cpp

ecepranaykumar
Автор

i don't understand why the nested part can't be another hashmap like a nested dictionary. in the nested dictionary, timestamp is a string

enterprisecloudnative
Автор

I really love your videos! Could you upload a video for "843. Guess the Word"? (Top google question)

Jason-becy
Автор

yeah i was able to solve this myself once i saw that constraint.

qtlmyfj
Автор

I don't wanna abuse Python too much to make everything so easy. lol

sidazhong
Автор

Nice solution. Can you please do a video on 68. Text Justification from leetcode?

ameynaik
Автор

So we assume the input timestamp(s) are always ascending for each key/value? For example they cannot give you ["foo", "bar", 4] then ["foo", "bar", 1]?

zenu
Автор

I'm getting a TLE for this solution

GenevieveKochel
Автор

Can anyone explain why we write the binary search that way? I usually have 3 conditions in my binary search

TheQuancy