Maximum Product Difference Between Two - Leetcode 1913 - Python

  Рет қаралды 5,286

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 23
@RyanStalloneable
@RyanStalloneable 9 ай бұрын
Figured this one out on my own but came to see your solution. Nearly identical solution. Either I have watched so much neetcode that i am getting neetcode brain, or I am finally actually getting the hang of this. Either one works with me
@servantofthelord8147
@servantofthelord8147 3 ай бұрын
Neetcode brain is hilarious lol - I hope to be there soon.
@foodoodle4415
@foodoodle4415 9 ай бұрын
The BF that I came up with: class Solution: def maxProductDifference(self, nums: List[int]) -> int: nums.sort() result = (nums[-1]*nums[-2])-(nums[0]*nums[1]) return result
@KeyAndLock77
@KeyAndLock77 9 ай бұрын
Sort is O(n * log n) which is slower than O(n) solution just finding two largest and two smallest elements. Also why do you call it BF?
@foodoodle4415
@foodoodle4415 9 ай бұрын
@@KeyAndLock77what else do I call it if I come up with the quickest possible solution?
@PhantomfireWasHere
@PhantomfireWasHere 9 ай бұрын
@@foodoodle4415 They just explained that your solution is not the quickest.
@phatboislym
@phatboislym 9 ай бұрын
this is what I did and it runs about as fast as Neetcode's solution
@PhantomfireWasHere
@PhantomfireWasHere 9 ай бұрын
@@phatboislym pretty big difference between nlogn and n.
@CS_n00b
@CS_n00b 9 ай бұрын
why is heap o(n)? i thought putting each element in a heap is o(log(n)) and there are n elements?
@victor_ls
@victor_ls 9 ай бұрын
That's true when the heap has already n elements, but if you are adding one element to an empty heap it will only take log(0), the second element log(1)... you will need at most (I think) 2n operations to build a heap of size n
@dorio5535
@dorio5535 9 ай бұрын
Hey Neetcode, would you be able to do a video on Meeting Rooms III? It's the most frequent question on the Google Leetcode tag and I've actually gotten this question in a Google interview 2 years ago.
@jamesisaacson6414
@jamesisaacson6414 9 ай бұрын
heyy @dorio5535, thanks for letting us know you encountered this problem in your Google interview I am no @Neetcode but there is my solution. Just solved after seeing your comment class Solution: def mostBooked(self, n: int, meetings: List[List[int]]) -> int: # NOTE: use 2 min-heaps: one for currently running meetings and # one for rooms (we need to use the available room with least room number) # min-heap used for running meetings as it allows us to get the meeting # that ends next. # NOTE: in the running_meetings heap, we push (end_time, assigned_room) # assigned_room is used to free the room # in some test cases, the meetings are NOT in ascending order by start time meetings.sort() running_meetings = [] rooms = [i for i in range(n)] heapq.heapify(rooms) counter = {i:0 for i in range(n)} # counts the number of meetings held at each room for start, end in meetings: # pop the meetings that end before this meeting starts # this is needed to handle the cases where a meeting starts waaay later than other meetings while running_meetings and running_meetings[0][0] most_meetings: most_meetings = meeting_count result_room = room_num return result_room
@KeyAndLock77
@KeyAndLock77 9 ай бұрын
Twist in problem: What if the array contains mix of positive and negative? You probably want to choose one largest negatvie and another largest positive so the subtration is added unto instead subtracted? Similarly for two largest numbers, we can also choose two smallest -ve numbers
@kolhesatish
@kolhesatish 9 ай бұрын
No bro you can also tackle that type of problem also
@gie4830
@gie4830 9 ай бұрын
Initialize min variables to -inf
@ROHITKUMAR-xp2xe
@ROHITKUMAR-xp2xe 9 ай бұрын
why you only make videos on easy daily challenge and skips most of the time hard daily challenge
@NeetCodeIO
@NeetCodeIO 9 ай бұрын
I don't skip problems based on difficulty, have done plenty of Hards this year
@dingus2332
@dingus2332 9 ай бұрын
I think he is skipping easies ,if any. Check your facts
@shreehari2589
@shreehari2589 9 ай бұрын
Nibba you don’t owe him anything, you are acting like you paid him, he can make whatever videos he wants to
@ROHITKUMAR-xp2xe
@ROHITKUMAR-xp2xe 9 ай бұрын
@@shreehari2589 I know he can make whatever he wants and Im not ordering him I just noticed it that's why I am asking because many times I don't see any videos on medium-hard and hard problems and that's what most people wants.
Image Smoother - Leetcode 661 - Python
16:02
NeetCodeIO
Рет қаралды 15 М.
Time Needed to Buy Tickets - Leetcode 2073 - Python
12:45
NeetCodeIO
Рет қаралды 14 М.
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 23 МЛН
Поветкин заставил себя уважать!
01:00
МИНУС БАЛЛ
Рет қаралды 6 МЛН
哈莉奎因怎么变骷髅了#小丑 #shorts
00:19
好人小丑
Рет қаралды 54 МЛН
Is Computer Science still worth it?
20:08
NeetCodeIO
Рет қаралды 345 М.
Minimum Difficulty of a Job Schedule - Leetcode 1335 - Python
13:41
Design a Food Rating System - Leetcode 2353 - Python
15:37
NeetCodeIO
Рет қаралды 14 М.
Set Mismatch - Leetcode 645 - Python
19:43
NeetCodeIO
Рет қаралды 10 М.
I used to hate QR codes. But they're actually genius
35:13
Veritasium
Рет қаралды 1,1 МЛН
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 87 М.
why are switch statements so HECKIN fast?
11:03
Low Level
Рет қаралды 410 М.
Number of Dice Rolls with Target Sum - Leetcode 1155 - Python
20:20
Backspace String Compare - Leetcode 844 - Python
14:10
NeetCodeIO
Рет қаралды 14 М.
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 23 МЛН