Комментарии:
I dont get it
Ответитьnice bro
ОтветитьNice solution. Can you please do a video on 68. Text Justification from leetcode?
Ответитьi hated this problem but ur explanation made it less complicated ty
ОтветитьI really love your videos! Could you upload a video for "843. Guess the Word"? (Top google question)
Ответить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
ОтветитьVery concise solution and easy to understand. Thank you!
Ответить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.
Ответить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?
Ответитьyou are a leetcode legend!...i have watched a couple of your leetcode videos solution
ОтветитьDamn I love this problem and your explaination, thank you man!
Liked again and commenting to support!
Thanks
Ответить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
"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.
ОтветитьTime Limit Exceeded with same solution
Ответить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 ??
ОтветитьAwesome explanation
ОтветитьCan anyone explain why we write the binary search that way? I usually have 3 conditions in my binary search
Ответить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.
ОтветитьWhat about java code...?
ОтветитьGreat explanation, thank you!
Ответить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
}
}
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.
Ответить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 ""
I don't wanna abuse Python too much to make everything so easy. lol
Ответить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
understood
ОтветитьThe goat fr
Ответить"I don't want to make it too easy"
ОтветитьMy dumbass didn't read the fineprint and just went with SortedDict from sortedcontainers 😭
ОтветитьI am getting time limit exceeded with this solution or it is just me? thanks
ОтветитьI think that problem should be easy
Ответить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?
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]?
ОтветитьThis problem statement is really badly written
ОтветитьNice. And for binary search, could just use right pointer at end of loop -- as that should be the closest value less than target
Ответитьyeah i was able to solve this myself once i saw that constraint.
ОтветитьI got this problem on an interview, if only I was subscribed to you back then.
ОтветитьThanks neetcode , Presentation is really good
Ответить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.
love this question!
ОтветитьI'm getting a TLE for this solution
ОтветитьThank you very much man
Ответить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.
Ответить