Sort an Array - Leetcode 912 - Python

  Рет қаралды 35,664

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 42
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Ok, I'm still traveling but I had already recorded this one a while back and just edited it.
@BurhanAijaz
@BurhanAijaz Жыл бұрын
you still uploaded
@yang5843
@yang5843 Жыл бұрын
Thanks Neet, enjoy your vacation
@vixguy
@vixguy Жыл бұрын
Thank you
@dennisgatere7821
@dennisgatere7821 Жыл бұрын
A missed opportunity for this question IMO is counting sort, just tried it and it beats 99% when I submitted it but you need shifting for negative values, check it out and hopefully one day you add it to the channel. public int[] sortArray(int[] nums) { if (nums == null || nums.length == 0) return new int[]{}; int shift = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int num : nums) { shift = Math.min(shift, num); max = Math.max(max, num); } max -= shift; int[] counts = new int[max + 1]; for (int num : nums) { counts[num - shift]++; } for (int num = 0, i = 0; num < counts.length; num++) { while (counts[num] > 0) { nums[i++] = num + shift; counts[num]--; } } return nums; }
@tunguyenxuan8296
@tunguyenxuan8296 Жыл бұрын
At 16:00, the code must be arr[i] = left[j]. The solution will actually work if you put it nums[i] = left[j] as you will assign nums to arr, but for the correctness, it must be arr inside merge function
@karanssh
@karanssh Жыл бұрын
great catch. since it's passed by reference it's pass.
@adarshsasidharan254
@adarshsasidharan254 Жыл бұрын
I also noticed that and got confused, thanks for the comment
@snoopdogg_007
@snoopdogg_007 Жыл бұрын
Good catch! I had similar doubt why using nums instead of arr at 16:00
@metarus208
@metarus208 Жыл бұрын
awesome catch!
@HoppyGamer
@HoppyGamer Жыл бұрын
One of the cleanest explainations of Merge Sort on YT
@DmitriyKl
@DmitriyKl 8 ай бұрын
For those struggling to understand this solution, there's actually a slightly more straightforward way that was easier for me to understand. The overall approach is the same, there are 3 distinct steps: 1 - check if base case 2 - sort left and right halves 3 - merge sorted halves the solution becomes: # base case - array already sorted because len is
@rahulsbhatt
@rahulsbhatt 12 күн бұрын
Thanks, had to revise this after seeing today's daily.
@DimaLifeIsGood
@DimaLifeIsGood Жыл бұрын
Nice, explanation of concept, simplicity of code is amazing, thank you for doing it :)
@adrianfrankowski3784
@adrianfrankowski3784 Жыл бұрын
You can precalculate len(left) and len(right) in merge function to make it a little bit faster.
@cosepeter2197
@cosepeter2197 Жыл бұрын
Your explanations are a thing of beauty ❤
@krateskim4169
@krateskim4169 Жыл бұрын
Awesome explanation
@sarvesh6785
@sarvesh6785 Жыл бұрын
I was waiting for your video. Great explanation as always 🔥🔥
@aliramazani2024
@aliramazani2024 6 ай бұрын
Great job. However, the code below is slightly more efficient than your code because it performs all operations on the original list instead of creating new lists 'left' and 'right'. Overall, it was great to learn your approach as well. def mergeSort(arr): if len(arr) > 1: # Finding the mid of the array mid = len(arr)//2 # Dividing the array elements L = arr[:mid] # Into 2 halves R = arr[mid:] # Sorting the first half mergeSort(L) # Sorting the second half mergeSort(R) i = j = k = 0 # Copy data to temp arrays L[] and R[] while i < len(L) and j < len(R): if L[i]
@varshasingh1299
@varshasingh1299 Ай бұрын
Yes, this is even better
@degamer106
@degamer106 Жыл бұрын
I used heapsort. Do you recommend knowing the other algorithms by heart like Mergesort, Quicksort, etc.. for interviews?
@garth2356
@garth2356 Жыл бұрын
I don't know what you mean by "know them by heart" but you should be able to write those algorithms and understand what every statement does so that you can manipulate them according to your need.
@dheerendrapratapsingh9406
@dheerendrapratapsingh9406 Ай бұрын
Best explanation of mergeSort so far
@TheSiddhaartha
@TheSiddhaartha 3 ай бұрын
The two arrays in *merge* method, still overwrite the *nums* array. In python, array slice copies by reference and not value. So better use .copy after slice eg arr[L:M+1].copy(). Otherwise answers are incorrect on computer. Maybe LeetCode accepted it with some sort of inbuilt conversion but Python interpreter gave incorrect results. So I used .copy() which solved the problem.
@u4rr837
@u4rr837 Жыл бұрын
I passed this problem on leetcode using quick sort. just randomly choose pivot and randomly separate elements with same key. It's quite inefficient though.
@mufaddaltatiwala2199
@mufaddaltatiwala2199 Жыл бұрын
You don't need to pass arr as an argument to the merge and mergeSort methods since they are already inside the sortArray, instead you can simply modify the nums array from both these methods and return nums in the end
@khyunwoo1
@khyunwoo1 4 ай бұрын
this question was asked inside the "insertion sort" lesson on neetcode D:
@doc9448
@doc9448 3 ай бұрын
I noticed that too. I skipped it there, marked the lesson complete and came back to it in the merge sort lesson (it also appeared there in the leetcode section)
@ahmadbasyouni9173
@ahmadbasyouni9173 Ай бұрын
@@doc9448 i think its there for you to do insertion sort
@mahendrakoteri9233
@mahendrakoteri9233 12 күн бұрын
I did try Quick Sort but still got TLE.. so here I am
@user-du9sh8ld7p
@user-du9sh8ld7p Жыл бұрын
Why is i initialized with L and not 0. what is the problem if we initialize i = 0?
@YINXUANYIN
@YINXUANYIN Жыл бұрын
Hi, I also have this problem. Have you solved it?
@hasibqureshi6409
@hasibqureshi6409 Жыл бұрын
it is not that official channel. but videos are same. how .? no copy right issue?
@pwnweb5734
@pwnweb5734 Жыл бұрын
need an heapsort solution too :/
@mirshodoripov1035
@mirshodoripov1035 Жыл бұрын
why is not Quicsort working for this problem?
@mirshodoripov1035
@mirshodoripov1035 Жыл бұрын
class Solution: def sortArray(self, nums: List[int]) -> List[int]: import random if len(nums) < 2: return nums # base case else: random.shuffle(nums) pivot = nums.pop() # recursive case less = [nums[i] for i in range(len(nums)) if nums[i] pivot] return self.sortArray(less) + [pivot] + self.sortArray(greater) return nums
@benpurcell591
@benpurcell591 29 күн бұрын
It's because one of the problems forces it into worst case which becomes n*n which causes tle
@valorantfever3990
@valorantfever3990 Жыл бұрын
Heap sort is not better than merge/quick overall , but its memory efficient. You should have gone for heap sort bro :(
@PannyaTrehan
@PannyaTrehan 2 ай бұрын
d
@BurhanAijaz
@BurhanAijaz Жыл бұрын
it was kinda easy problem
@MeanSoybean
@MeanSoybean Жыл бұрын
almost as if array sorting is one of the first things taught in DSA
Construct Quad Tree - Leetcode 427 - Python
12:26
NeetCodeIO
Рет қаралды 21 М.
Python As Fast as Possible - Learn Python in ~75 Minutes
1:19:41
Tech With Tim
Рет қаралды 1,7 МЛН
Box jumping challenge, who stepped on the trap? #FunnyFamily #PartyGames
00:31
Family Games Media
Рет қаралды 18 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
ПРОВЕРИЛ АРБУЗЫ #shorts
00:34
Паша Осадчий
Рет қаралды 7 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 403 М.
SHA: Secure Hashing Algorithm - Computerphile
10:21
Computerphile
Рет қаралды 1,2 МЛН
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 598 М.
Why is everyone LYING?
7:56
NeetCodeIO
Рет қаралды 198 М.
Merge Sorted Array - Leetcode 88 - Python
10:18
NeetCode
Рет қаралды 199 М.
My Brain after 569 Leetcode Problems
7:50
NeetCode
Рет қаралды 2,5 МЛН
Find Peak Element - Leetcode 162 - Python
11:02
NeetCodeIO
Рет қаралды 36 М.
Doing LeetCode Be Like (Coding Interviews Be Like Pt. 2)
4:41
Nicholas T.
Рет қаралды 754 М.
Box jumping challenge, who stepped on the trap? #FunnyFamily #PartyGames
00:31
Family Games Media
Рет қаралды 18 МЛН