Most Beautiful Item for Each Query - Leetcode 2070 - Python

  Рет қаралды 7,678

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 34
@treelibrarian7618
@treelibrarian7618 14 күн бұрын
Just a comment on why to not bother with the leetcode runtimes: they do not provide valid comparison to other coders submissions, for 2 reasons: 1: the runtimes are inconsistent with the exact same code: submit 10 times and see what I mean. variation of 30% to 50% is common, I presume because of a noisy runtime environment. 2: it is possible for users to add testcases to the workload *after* having got their runtime acknowledged. This puts everyone who comes after them at a disadvantage. I have several times tried the code of the "fastest" example (that you can get by clicking on the leftmost bar of the graph), copy paste it into my code window and try it, and it is not only slower than mine but consistently only an average result. I'm up for competition as much as the next man, but the playing field needs to be level.
@_theone_99
@_theone_99 15 күн бұрын
I only sorted the items array by price (not the queries), and then ran a binary search for each query. Before that, one additional step was, I looped over items and set items[i][1] = max(items[i][1], items[i-1][1]), because if my binary search landed on any index, the answer to that query will be the maximum beauty of all items on or before that, so I just set the max beauty upto index i as the beauty of item[i]. That amounts to nlogn + n + mlogn, beat 100% in time and space, because I reused the queries array as the answer array
@hamirmahal
@hamirmahal 15 күн бұрын
What language? I had a similar approach in JavaScript that only beat 50%.
@_theone_99
@_theone_99 15 күн бұрын
@@hamirmahal I am using go
@_theone_99
@_theone_99 15 күн бұрын
@@hamirmahal i used go
@DeathSugar
@DeathSugar 15 күн бұрын
Leetcode runtimes are good way to understand how to optimize your code, so if you want to write performant code than you can try optimize your runtimes. Doesn't really work much for python and other scripting languages, but for compile-time is a bliss - when your solution comes from 2 seconds to 0 ms is a beautiful feeling
@business_central
@business_central 15 күн бұрын
This problem when I was solving it reminded me of one about intervals and queries, it's in the NC 150 - that helped a lot in solving it! Thank you!
@adityarao4157
@adityarao4157 15 күн бұрын
It was a nice problem to learn how exactly lower_bound and upper_bound works (for C++ users ifk)
@moralized
@moralized 15 күн бұрын
Ouch.. I found the brute force solution but didn’t come up with that optimization. Great video as always!
@BIBIN63
@BIBIN63 15 күн бұрын
My intuition was to use hashmaps. I haven’t coded up this yet. 1. Create a hashmap for the items array. Only update the value of a key if the beauty is larger. 2. Create a new array for result, Iterate through the queries and use each as query as a key for the hashmap and insert the value into result array.
@Tommy-sm4ed
@Tommy-sm4ed 15 күн бұрын
class Solution: def maximumBeauty(self, items: List[List[int]], queries: List[int]) -> List[int]: res = [] beauty_map ={} largest = 0 max_index = 0 items.sort() for p, b in items: largest = max(largest, b) max_index = max(max_index, p) beauty_map[p] = largest for q in queries: temp = min(max_index, q) while temp not in beauty_map and temp > 0: temp -= 1 if temp in beauty_map: res.append(beauty_map[temp]) else: res.append(0) return res
@lucacorsetti2131
@lucacorsetti2131 15 күн бұрын
yes same!
@tp_pm
@tp_pm 15 күн бұрын
I did the same and got TLE :/
@nirmalgurjar8181
@nirmalgurjar8181 15 күн бұрын
instead of hashmap using treemap would be optimal and checking if value less than equal to key exists.
@waleeddaud3497
@waleeddaud3497 15 күн бұрын
Applied upper bound on items array for each query
@monismomin6759
@monismomin6759 15 күн бұрын
such a good intuitive solution
@yang5843
@yang5843 15 күн бұрын
My constant time solution seems to be slower than my binary search solution, which is weird
@Reck1025
@Reck1025 15 күн бұрын
I did not think to sort the queries array at all 🤯 Is this faster than sorting the the items array and then using binary search?
@vayunjain9289
@vayunjain9289 15 күн бұрын
only while queries length is less than items length
@tp_pm
@tp_pm 15 күн бұрын
sorting the the items array and then using binary search - I did this and got TLE
@Lamport14
@Lamport14 15 күн бұрын
Thanks
@challengemania4447
@challengemania4447 15 күн бұрын
Java run time is 78 ms and its faster than only 7.25% of java online submission.. But here it is 178 ms and faster than 59%.. Why this ?
@rahulsiddhardh3701
@rahulsiddhardh3701 15 күн бұрын
It is specific to the language you are using. If you CPP you'll get much lesser times
@devmahad
@devmahad 15 күн бұрын
thanks :)
@ajitpalsingh606
@ajitpalsingh606 15 күн бұрын
I only forgot indexes part in queries and rest i was able to do...
@ZeroTrunks
@ZeroTrunks 14 күн бұрын
I know what amoritiz..... something is...
@bhanunani1307
@bhanunani1307 15 күн бұрын
how you come up with these solutions
@business_central
@business_central 15 күн бұрын
First! I've never been here so early lol
@mosestrosin
@mosestrosin 15 күн бұрын
olny items sorted, beats 99% of solutions def maximumBeauty(self, items: List[List[int]], queries: List[int]) -> List[int]: items.sort() prices = [] max_beauties = [] current_max = 0 for price, beauty in items: if beauty > current_max: current_max = beauty prices.append(price) max_beauties.append(current_max) results = [] for q in queries: idx = bisect_right(prices, q) - 1 if idx >= 0: results.append(max_beauties[idx]) else: results.append(0) return results
Count the Number of Fair Pairs - Leetcode 2563 - Python
18:52
NeetCodeIO
Рет қаралды 10 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 8 МЛН
Minimum Array End - Leetcode 3133 - Python
23:22
NeetCodeIO
Рет қаралды 10 М.
10 Crazy Python Operators That I Rarely Use
11:37
Indently
Рет қаралды 43 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 286 М.
TMUX in 100 seconds | Prime Reacts
11:43
ThePrimeTime
Рет қаралды 160 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 303 М.
IPC: To Share Memory Or To Send Messages
14:15
Core Dumped
Рет қаралды 77 М.