Time Based Key-Value Store - Leetcode 981 - Python

Time Based Key-Value Store - Leetcode 981 - Python

NeetCode

2 года назад

102,963 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@masternobody1896
@masternobody1896 - 09.10.2021 21:14

I dont get it

Ответить
@trungthanhbp
@trungthanhbp - 10.10.2021 11:03

nice bro

Ответить
@ameynaik2743
@ameynaik2743 - 11.10.2021 18:12

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

Ответить
@dianav357
@dianav357 - 12.10.2021 01:03

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

Ответить
@Jason-be2cy
@Jason-be2cy - 22.10.2021 08:48

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

Ответить
@enterprisecloudnative8757
@enterprisecloudnative8757 - 23.01.2022 06:42

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

Ответить
@baolingchen2891
@baolingchen2891 - 07.02.2022 23:22

Very concise solution and easy to understand. Thank you!

Ответить
@halahmilksheikh
@halahmilksheikh - 18.04.2022 05:02

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.

Ответить
@dheerajchidambaranathan
@dheerajchidambaranathan - 29.04.2022 19:43

Why did you choose to save the zeroth element of the value dictionary and not the 1st which satisfied the condition of time stamp being less than or equal to queried time stamp?

Ответить
@adeniyiadeboye3300
@adeniyiadeboye3300 - 30.04.2022 04:46

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

Ответить
@symbol767
@symbol767 - 10.05.2022 10:33

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

Ответить
@naimeimran3247
@naimeimran3247 - 19.06.2022 15:22

Thanks

Ответить
@ece116pranaykumar4
@ece116pranaykumar4 - 21.06.2022 13:58

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;
if(it[mid].second<=timestamp)
{
ans=it[mid].first;
low=mid+1;
}
else
high=mid-1;
}
return ans;

} getting tle in cpp

Ответить
@arkamukherjee457
@arkamukherjee457 - 23.06.2022 01:29

"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.

Ответить
@nnamdiadom8256
@nnamdiadom8256 - 27.07.2022 03:06

Time Limit Exceeded with same solution

Ответить
@gurazeez
@gurazeez - 06.10.2022 09:45

if I write values =self.store[key] instead of self.store.get(keys,[]), the run time difference is huge, why is get() so much faster ??

Ответить
@krateskim4169
@krateskim4169 - 06.10.2022 11:36

Awesome explanation

Ответить
@TheQuancy
@TheQuancy - 10.11.2022 09:00

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

Ответить
@gugolinyo
@gugolinyo - 26.11.2022 19:41

At first I thought of a TreeMap. Then I saw your video and thought otherwise. Then I realized I discarded my initial idea just because I thought that to be something like a brute force solution. Taking advantage of the fact that TreeMap implements NavigableMap I used floorEntry() to spare myself all that binary search clutter.

Ответить
@goodguy6589
@goodguy6589 - 02.12.2022 14:48

What about java code...?

Ответить
@jonnatanortiz7159
@jonnatanortiz7159 - 29.12.2022 01:14

Great explanation, thank you!

Ответить
@Princebharti9971
@Princebharti9971 - 26.02.2023 11:01

I am getting TLE for same solution in Swift, anyone can help ? I have written down the solution below.

class TimeMap {

private var dictionary = [String: [(Int,String)]]()

init(){}

func set(_ key: String, _ value: String, _ timestamp: Int) {
var list = dictionary[key] ?? []
list.append((timestamp, value))
dictionary[key] = list
}

func get(_ key: String, _ timestamp: Int) -> String {
var result = ""
let array = dictionary[key, default: []]

var left = 0, right = array.count-1
while left <= right {
let mid = (left+right)/2
if array[mid].0 <= timestamp {
result = array[mid].1
left = mid+1
} else {
right = mid-1
}
}
return result
}
}

Ответить
@gregorvm7443
@gregorvm7443 - 24.03.2023 23:08

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.

Ответить
@Zynqify
@Zynqify - 26.04.2023 07:42

of course for lists that are much much bigger in size the binary search option is still better with a time complexity of O(log n), but i feel like there's a much simpler approach that is O(n) at worst. since the timestamps are always in increasing order, we can iterate through the timestamps backwards and return the value whose timestamp is less than or equal to the input timestamp, otherwise return an empty string.

class TimeMap:
def __init__(self):
self.pairs = {}

def set(self, key: str, value: str, timestamp: int) -> None:
if key in self.pairs:
self.pairs[key].append((value, timestamp))
else:
self.pairs[key] = [(value, timestamp)]

def get(self, key: str, timestamp: int) -> str:
if key not in self.pairs:
return ""
else:
for values in reversed(self.pairs[key]):
if values[1] <= timestamp:
return values[0]
return ""

Ответить
@sidazhong2019
@sidazhong2019 - 30.04.2023 06:03

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

Ответить
@JoseAntonio-sn6sf
@JoseAntonio-sn6sf - 15.06.2023 02:32

I think there is no need to use binary search because according to the condition, the timestamps are strictly set in increasing order, so the right most index has the max timestamp which you can compare it with the current timestamp, so you could condense the get function like:
def get(self, key, timestamp):
res = " "
values = self.store.get(key, [])

if values[-1][1] <= timestamp:
res = values[-1][0]

return res

Ответить
@indhumathi5846
@indhumathi5846 - 30.06.2023 17:54

understood

Ответить
@LavanVivekanandasarma
@LavanVivekanandasarma - 02.07.2023 07:16

The goat fr

Ответить
@kuoyulu6714
@kuoyulu6714 - 10.08.2023 08:29

"I don't want to make it too easy"

Ответить
@lowerkinded
@lowerkinded - 26.08.2023 13:42

My dumbass didn't read the fineprint and just went with SortedDict from sortedcontainers 😭

Ответить
@grhaonan
@grhaonan - 19.10.2023 08:35

I am getting time limit exceeded with this solution or it is just me? thanks

Ответить
@AndreiSokolov-k7j
@AndreiSokolov-k7j - 14.03.2024 04:37

I think that problem should be easy

Ответить
@boli7171
@boli7171 - 20.03.2024 06:47

The answer will be wrong if I gave

while (l <= r) {
mid = (l + r) / 2
if (values[m][1] == timestamp):
return values[m][0];
else if (values[m][1] < timestamp):
l = m+1
else:
r = m-1;
}

can you explain why this is wrong?

Ответить
@zenulee
@zenulee - 03.04.2024 20:03

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]?

Ответить
@sheikhmkrifat7749
@sheikhmkrifat7749 - 18.04.2024 08:13

This problem statement is really badly written

Ответить
@Dhruvbala
@Dhruvbala - 26.04.2024 21:17

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

Ответить
@NeilSharma-u5n
@NeilSharma-u5n - 17.06.2024 01:40

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

Ответить
@nishiketsingh5283
@nishiketsingh5283 - 28.06.2024 00:45

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

Ответить
@iamnoob7593
@iamnoob7593 - 10.07.2024 14:30

Thanks neetcode , Presentation is really good

Ответить
@dusvn1484
@dusvn1484 - 13.08.2024 12:35

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.

Ответить
@shreyaschaudhary-r6d
@shreyaschaudhary-r6d - 14.08.2024 05:45

love this question!

Ответить
@GenevieveKochel
@GenevieveKochel - 01.09.2024 00:46

I'm getting a TLE for this solution

Ответить
@Emorinken
@Emorinken - 12.09.2024 18:03

Thank you very much man

Ответить
@zishiwu7757
@zishiwu7757 - 18.09.2024 05:49

I recently took a CodeSignal online assessment that contained this problem but with additional method compare_and_set which checks if the current value stored at key equals expected_value, and if that condition is true only then do we update the key to store new_value. Man I wish I had seen this video a week before. Oh well, you live and you learn.

Ответить