Minimum Difference Between Largest and Smallest Value in Three Moves - Leetcode 1509 - Python

  Рет қаралды 10,672

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
🐦 Twitter: / neetcode1
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
Problem Link: leetcode.com/problems/minimum...
0:00 - Read the problem
0:30 - Drawing Explanation 1
5:39 - Coding Explanation 1
8:11 - Drawing Explanation 2
12:26 - Coding Explanation 2
leetcode 1509
#neetcode #leetcode #python

Пікірлер: 55
@utkarshdewan8736
@utkarshdewan8736 Ай бұрын
That was so bloody clever. I just learnt heap a week ago and did not even think that we can use a heap here
@aashishbathe
@aashishbathe Ай бұрын
Awesome man. Learnt something new today. Nsmallest and nlargest. Thank you!
@MP-ny3ep
@MP-ny3ep Ай бұрын
Great explanation as always. Thank you
@puneettripathi8418
@puneettripathi8418 Ай бұрын
doesn't heapifying an array of size n, itself is a process of O(n*log(n)), you perform sink and swim operations that both take O(log(n)) for n elements?
@kareni7572
@kareni7572 Ай бұрын
Thanks for showing us previous attempts! Good explanation & 2nd solution was interesting...
@chien-yuyeh9386
@chien-yuyeh9386 Ай бұрын
Nice🎉🎉
@woodylucas
@woodylucas Ай бұрын
Python is such a cheatcode it is ridiculous, and it isn't even fair 🤕
@chrischika7026
@chrischika7026 Ай бұрын
skill issue.
@notcountdankula
@notcountdankula 26 күн бұрын
Cheat code in which way? Easy syntax?
@chrischika7026
@chrischika7026 26 күн бұрын
@@notcountdankula yup and it just looks clean
@HuyLe-zx8ko
@HuyLe-zx8ko Ай бұрын
brilliant
@kennycarneal3577
@kennycarneal3577 29 күн бұрын
Just wanted to say I love you bro you're awesome
@NeetCodeIO
@NeetCodeIO 29 күн бұрын
Love you too ❤️
@adityabhatt4177
@adityabhatt4177 Ай бұрын
the video and the explaination is awsome..but could you try to code in java or c++
@LasTCursE69
@LasTCursE69 Ай бұрын
"Solution to the most hardest part of this problem is simple, just use a built-in python library" Amazing explanation 👏 -.-
@MykolaPavluchynskyi
@MykolaPavluchynskyi Ай бұрын
Interesting that if you try to do in java with heaps - it will be more than twice slower. Because with sorting it can be done on int[], without using a collections with all their overhead
@MykolaPavluchynskyi
@MykolaPavluchynskyi Ай бұрын
Sorry, it's not actually true, it was because of not optimal updates in heaps, but always adding and removing extra. With extra checks for avoiding non efficient poll/offer - it is actually faster
@business_central
@business_central Ай бұрын
can someone clarify further why the sorted in the heaps solutions? doesn't adding the sorting will end us at nlogn ?
@mayursmahajan
@mayursmahajan Ай бұрын
It would have been nlogn if we sorted nums. We are not sorting nums but sorting a sub arr of size 4 that we got from the heap. Thus their sorting will be considered as Constant time
@business_central
@business_central Ай бұрын
@@mayursmahajan Thank you very much! Now I get it! Thanks!
@notcountdankula
@notcountdankula 26 күн бұрын
​@@mayursmahajanbut we are heapifying the entire array. So it will take nlogn anyways
@RuslanZinovyev
@RuslanZinovyev Ай бұрын
Logically, the solution was obvious but it would take me a lot of time to come up with this: int right = nums.length - 4 + left; btw is it really necessary to sort Heaps? It is supposed to be in the right order out of the box.
@tirasjeffrey2002
@tirasjeffrey2002 Ай бұрын
by remove, he means setting it to the min value right? coz you cant actually remove, you can only change it to any val
@HuyLe-zx8ko
@HuyLe-zx8ko Ай бұрын
he means we don't need to care about those
@dss963
@dss963 Ай бұрын
When the lower one incremented to next high or higher element to next lower , there is no point of considering it as we care about diff b/w high and low
@Anthony-oz1jc
@Anthony-oz1jc Ай бұрын
similar idea : class Solution { public: int minDifference(vector& nums) { int n=nums.size(); if(n
@johnniewalkerjohnniewalker2459
@johnniewalkerjohnniewalker2459 Ай бұрын
same code i developed in java!
@CuriousAnonDev
@CuriousAnonDev Ай бұрын
na bro i am so done i cant fucking solve these questions no matter how much i try shit
@tirasjeffrey2002
@tirasjeffrey2002 Ай бұрын
one day at a time man, no shame in accepting u cant come up with the solution, how else do we increase our knowledge
@joaopedrocastro4486
@joaopedrocastro4486 Ай бұрын
its okay bro, just have pacience and try to solve it, if you cant just look a solution and try to solve it another day!
@pastori2672
@pastori2672 Ай бұрын
multiple choice problem detected brute force selected 🗿
@antondonohue8943
@antondonohue8943 Ай бұрын
oh first haha. I appreciate you Neet
@tarekradwan8661
@tarekradwan8661 27 күн бұрын
The question is not removing could you pleaSe fix that. I know it is still the same but just confuses
@roderickli6505
@roderickli6505 Ай бұрын
is heap actually a N operation? i feel like its not and when i view the time complexity on leetcode its n lgn
@chrischika7026
@chrischika7026 Ай бұрын
its only 4 elements so its O(N)
@shnorlaxzz395
@shnorlaxzz395 Ай бұрын
I think the question should be more clear and provide more details
@MrSpeedFrk
@MrSpeedFrk Ай бұрын
I guess I'm the only one that found this explanation confusing sigh
@samridhshubham8109
@samridhshubham8109 Ай бұрын
Wer u able to understand? Or need help?
@bullyversal5313
@bullyversal5313 Ай бұрын
Nah I think it's pretty much understandable
@chrischika7026
@chrischika7026 Ай бұрын
nah its lightwork
@edmonddantes587
@edmonddantes587 Ай бұрын
difficult to come up with the intuition here.
@olegleonov1310
@olegleonov1310 Ай бұрын
Why not just? fun minDifference(nums: IntArray): Int { if(nums.size
@tirasjeffrey2002
@tirasjeffrey2002 Ай бұрын
if you think neet's way of using hardcoded numbers is hard to wrap your mind around, try this: n = len(nums) i = 0 j = 3 if len(nums) i will go from 0 to 3, j will go from 3 to 0 -> i will be the left pointer, ie number of values cut off from the left -> j will be the right pointer, ie number of values cut off from the right -> thru this, we can get 4 combinations of removals: => 0 values from left, 3 values from right => 1 value from left, 2 from right => 2 from left, 1 from right => 3 from left, 0 from right return the min among these calculations hope this helps those who couldnt grasp neet's explanation peace, x edit: i had no clue heap could be used here im dumb asf
@pranithtirumalsetti1453
@pranithtirumalsetti1453 Ай бұрын
I am waiting for you from 1 hour
@MWKING
@MWKING Ай бұрын
The video is speeded-up by default
@bundiderp5109
@bundiderp5109 Ай бұрын
It's a bit unfortunate that you jumped right in with the intention to REMOVE elements. The description says "change to any value". The observation that this is effectively the same as removing was not made in the video, but beforehand.
@shwethaks7994
@shwethaks7994 Ай бұрын
@NeetCodeIO can you make a video on giving tips regarding job search and some websites where we can apply. I think this would be very helpful to many people like me in the middle of a job hunt.
@dheereshagrwal
@dheereshagrwal Ай бұрын
*no need to sort bud* min_four = nsmallest(4, nums) max_four = nlargest(4, nums) res = float("inf") for i in range(4): res = min(res, max_four[4 - i - 1] - min_four[i]) return res
@akialter
@akialter Ай бұрын
Man coding in a language that doesn't have min or max heap is tricky, probably just sort the array and call it a day Edit: can implement quickselect which average O(n), and a good segway to LC 215 kth largest element in array
@bhavyajainnd
@bhavyajainnd Ай бұрын
What's the Java equivalent fo heapq.nsmallest and largest?
@akshatsingh6036
@akshatsingh6036 Ай бұрын
implement it yourself LOL
@f840810
@f840810 Ай бұрын
You really need to use priority queue to implement that equivalent function. Yet, you can use two arrays and some element comparisons to determine the biggest and smallest 4 elements
@RuslanZinovyev
@RuslanZinovyev Ай бұрын
For minHeap it should be something like this: Queue minHeap = new PriorityQueue(Comparator.naturalOrder()); For maxHeap: Queue maxHeap = new PriorityQueue(Comparator.reverseOrder());
132  Activity TryFrom TryInto Zero To Mastery Academy 1920x1080 558K
10:16
Дмитрий Машарыгин
Рет қаралды 6
لقد سرقت حلوى القطن بشكل خفي لأصنع مصاصة🤫😎
00:33
Cool Tool SHORTS Arabic
Рет қаралды 21 МЛН
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 255 М.
Merge Nodes in Between Zeros - Leetcode 2181 - Python
11:24
NeetCodeIO
Рет қаралды 6 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 392 М.
Subarray Sums Divisible by K - Leetcode 974 - Python
16:41
NeetCodeIO
Рет қаралды 13 М.
Water powered timers hidden in public restrooms
13:12
Steve Mould
Рет қаралды 660 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 152 М.
Running "Hello World!" in 10 FORBIDDEN Programming Languages
18:07
The World Depends on 60-Year-Old Code No One Knows Anymore
9:30
Coding with Dee
Рет қаралды 687 М.
Why is everyone LYING?
7:56
NeetCodeIO
Рет қаралды 182 М.
Частая ошибка геймеров? 😐 Dareu A710X
1:00
Вэйми
Рет қаралды 4,3 МЛН
iPhone 15 Pro в реальной жизни
24:07
HUDAKOV
Рет қаралды 482 М.
Look, this is the 97th generation of the phone?
0:13
Edcers
Рет қаралды 7 МЛН
Что делать если в телефон попала вода?
0:17
Лена Тропоцел
Рет қаралды 3,2 МЛН