"Leetcode 347: Heap + Hashtable
Follow up question: create heap with the length of K instead of N (more time complexity but less space )"
Chen J. - "Leetcode 347: Heap + Hashtable
Follow up question: create heap with the length of K instead of N (more time complexity but less space )"See full answer
"I try to solve this initially using quick select where will take a pivot element and position the remaining elements and check if the current index is answer or not and continue the same but it requires o(n*n), but interviewee is expecting the best from me, so at the end i tried solving using heaps where will check the difference between k and n-k to use min or max heap after that we will heap the array, and will keep popping the element k-1 and return the peek one which leads to answer."
Mourya C. - "I try to solve this initially using quick select where will take a pivot element and position the remaining elements and check if the current index is answer or not and continue the same but it requires o(n*n), but interviewee is expecting the best from me, so at the end i tried solving using heaps where will check the difference between k and n-k to use min or max heap after that we will heap the array, and will keep popping the element k-1 and return the peek one which leads to answer."See full answer
Software Engineer
Coding
+2 more
🧠Want an expert answer to a question? Saving questions lets us know what content to make next.
"
import pandas as pd
from datetime import datetime
def findfastestlike(log: pd.DataFrame) -> pd.DataFrame:
log=log.sortvalues(['userid','timestamp'])
#get the prev event, time by user
log['prevevent'] = log.groupby('userid')['event'].shift(1)
log['prevtimestamp'] = log.groupby('userid')['timestamp'].shift(1)
True only on rows where the previous event was a login
and the current event is a like
log['loginlike'] = (log['prevevent'] == 'log"
Sean L. - "
import pandas as pd
from datetime import datetime
def findfastestlike(log: pd.DataFrame) -> pd.DataFrame:
log=log.sortvalues(['userid','timestamp'])
#get the prev event, time by user
log['prevevent'] = log.groupby('userid')['event'].shift(1)
log['prevtimestamp'] = log.groupby('userid')['timestamp'].shift(1)
True only on rows where the previous event was a login
and the current event is a like
log['loginlike'] = (log['prevevent'] == 'log"See full answer
"filter function usually exists in some high level programming that adopt FP paradigm.
It taks a sequence of items and a predicate function, and returns (conceptually) a subset of the items that satisfy the predicate.
Adopt this kind of operation (filter, map, reduce, take, pairwise ...) can help writting
cleaner code, and reduce the usage of mutable part in the program, lower the
possibility of making human mistake.
Take Python for example (although const-ness is not exists in Python),
assu"
Weida H. - "filter function usually exists in some high level programming that adopt FP paradigm.
It taks a sequence of items and a predicate function, and returns (conceptually) a subset of the items that satisfy the predicate.
Adopt this kind of operation (filter, map, reduce, take, pairwise ...) can help writting
cleaner code, and reduce the usage of mutable part in the program, lower the
possibility of making human mistake.
Take Python for example (although const-ness is not exists in Python),
assu"See full answer
"def countuniqueoutfits(totalpants: int, uniquepants: int,
totalshirts: int, uniqueshirts: int,
totalhats: int, uniquehats: int) -> int:
"""
Number of unique outfits can simply be defined by
(uniquepantschoose1uniqueshirtschoose1uniquehatschoose_1)
(uniquepantschoose1*uniqueshirtschoose1) # Not wearing a hat
nchoosek is n
"""
res = (uniquepants*uniqueshirtsuniquehats) + (uniquepantsunique_shirts)
return res
print(countuniqueoutfits(2, 1, 1, 1, 3, 2))"
Sai R. - "def countuniqueoutfits(totalpants: int, uniquepants: int,
totalshirts: int, uniqueshirts: int,
totalhats: int, uniquehats: int) -> int:
"""
Number of unique outfits can simply be defined by
(uniquepantschoose1uniqueshirtschoose1uniquehatschoose_1)
(uniquepantschoose1*uniqueshirtschoose1) # Not wearing a hat
nchoosek is n
"""
res = (uniquepants*uniqueshirtsuniquehats) + (uniquepantsunique_shirts)
return res
print(countuniqueoutfits(2, 1, 1, 1, 3, 2))"See full answer
"Count how many 1 and 2. calculate how many remaining items with '0'. Override existing data with knowledge of how many '0','1','2' in that order; TC=O(n), SC=O(1)"
Konstantin P. - "Count how many 1 and 2. calculate how many remaining items with '0'. Override existing data with knowledge of how many '0','1','2' in that order; TC=O(n), SC=O(1)"See full answer