Minimum Number of Operations to Move All Balls to Each Box - Leetcode 1769 - Python

  Рет қаралды 9,280

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 50
@FifaHades
@FifaHades Күн бұрын
O(n) is the biggest enemy of the interview, great video!
@aadil4236
@aadil4236 2 күн бұрын
I solved it in O(n) by myself without looking at the solution or watching this video. Without you, I wouldn't have gotten here. I used to struggle with even easy problems in the beginning, but you improved me. Thank you so much for your kindness!
@staywithmeforever
@staywithmeforever Күн бұрын
88% acceptance rate
@alexgabriel5877
@alexgabriel5877 Күн бұрын
@@staywithmeforever because bruteforce passes
@staywithmeforever
@staywithmeforever Күн бұрын
@@alexgabriel5877 oh really, i didn't knew that, O(n) is kinda obvious though. Some problems don't even give me the thought of the solution.
@anantakesharipanda4085
@anantakesharipanda4085 Күн бұрын
How long did it take for you to overcome your struggle phase?
@staywithmeforever
@staywithmeforever Күн бұрын
@@anantakesharipanda4085 just remember the patterns like prefix sum, reversing the question, sorting, monotonic stack etc that will help solving questions quickly.
@soupayt
@soupayt 2 күн бұрын
Nice video! I didn't think of it as product of array except self, but you're correct that this is basically the same problem. I calculated the left side and right side prefix & suffix on the fly instead when building the solution array, but still the same! class Solution: def minOperations(self, boxes: str) -> List[int]: # You can simplify this problem to keeping track of the distance between the current box and the boxes on the left and right with a non-zero # amount of balls. We can use a running prefix sum to pull this off. We keep track of the number of 1's on the left and right sides and can adjust the left and right sums accordingly with that after each iteration. boxes = [int(b) for b in boxes] left = 0 right = 0 left_ones = 0 right_ones = 0 res = [] # precalculate right for i, box in enumerate(boxes): if box == 1: right_ones += 1 right += i for i, box in enumerate(boxes): res.append(left + right) if box == 1: left_ones += 1 right_ones -= 1 left += left_ones right -= right_ones return res
@Kaviarasu_NS
@Kaviarasu_NS 2 күн бұрын
I rewrote NeetCode's approch and I personally I find it more easy to understand def minOperations(self, boxes: str) -> List[int]: res = [0] * len(boxes) balls, moves = 0, 0 for i in range(len(boxes)): moves += balls res[i] += moves balls += int(boxes[i]) balls, moves = 0, 0 for i in range(len(boxes) - 1, -1, -1): moves += balls res[i] += moves balls += int(boxes[i]) return res
@indian_geocacher
@indian_geocacher 2 күн бұрын
I have one question @Neetcode Can you please explain how did you know that the problem will be this one as you have published the solution at 00:00 as soon as the problem appeared, is it prerecorded for all the problems and you just post it?
@NeetCodeIO
@NeetCodeIO 2 күн бұрын
Yes I've actually recorded a video for every leetcode problem just to be safe
@vinayakbajpeyi931
@vinayakbajpeyi931 2 күн бұрын
@@NeetCodeIO 😂
@joeAnon796
@joeAnon796 2 күн бұрын
Also he said it's NYE... it's actually the 5th. Something fishy going on here! I think he gets the questions in advance 🤔
@freecourseplatformenglish2829
@freecourseplatformenglish2829 2 күн бұрын
@@joeAnon796 Actually you can see the upcoming question in advance. Navdeep mentioned it in one video. I couldn't found the source but it is possible.
@leo881010able
@leo881010able 2 күн бұрын
If we follow these dailies everyday we would know, he explained this awhile ago. It was by some Editorial updating logic, editorial often updates roughly a week before a question becomes a daily problem
@staywithmeforever
@staywithmeforever 2 күн бұрын
2 balls still left
@eznx616
@eznx616 2 күн бұрын
lmao
@AjayKumar-gs9sg
@AjayKumar-gs9sg 2 күн бұрын
Navdeep can you please explain solutions for yesterday's contest problems ? @neetcode
@rode_atharva
@rode_atharva 23 сағат бұрын
yesterday solved by myself using brute force. after listening that "you might be genius if you haven't seen the pattern and come up with the solution by yourself !". but i actually know the pattern of dynamic programming (so can't say am i genious or not 😅).. you are genoius. beacause of "you" and "codestory-with-mik" i am able to come at this level ! .... here is my solution class Solution: def minOperations(self, boxes: str) -> List[int]: count = 0 ans_left = [0] * len(boxes) ans_right = [0] * len(boxes) for i in range(len(boxes)): if i != 0: ans_left[i] = ans_left[i-1] + count if boxes[i] == '1': count += 1 count = 0 for i in range(len(boxes)-1, -1, -1): if i != len(boxes)-1: ans_right[i] = ans_right[i+1] + count if boxes[i] == '1': count += 1 for i in range(len(ans_left)): ans_left[i] += ans_right[i] return ans_left
@STACYEMMA-y2e
@STACYEMMA-y2e 2 күн бұрын
Did it in O(n^2) , came here for O(n) solution
@ramez3038
@ramez3038 2 күн бұрын
Solved it in o(n^2) after that optimized it to o(n) and came here to see that i thought about it the same way neet did seems hardwork and consistency pays off thanks alot neet 🔥🔥
@dixittilaji6147
@dixittilaji6147 Күн бұрын
came here after solving a problem with a n^2 approach, I knew it there has to be optimized way haha, love your videos man!
@luciell4782
@luciell4782 2 күн бұрын
Thanks! Amazing solution!
@sri_harsha_dv
@sri_harsha_dv Күн бұрын
Explanation felt little confusing while building the right part of the array and in solution where moves should be updated before balls. I was able to solve this problem surprisingly. Moves variable is not necessary atleast in the first array of your solution. My solution is one pass by building these left and right arrays. The intuition is that the right array is built using complete opposite steps of the left array. Only tricky part is understanding the indexing for the right array. Solution: def minOperations(self, boxes: str) -> List[int]: length = len(boxes) left, right = [0]*length, [0]*length left_count = right_count = 0 for i in range(length): if i: left[i] = left[i-1]+left_count right[length-i-1] = right[length-i]+right_count if boxes[i] == "1": left_count += 1 if boxes[length-i-1] == "1": right_count += 1 return [l+r for l,r in zip(left,right)]
@eznx616
@eznx616 2 күн бұрын
damn playing with balls is hard
@TheSmashten
@TheSmashten Күн бұрын
Hey Neetcode can you please do LC #37 (Sudoku Solver)? I can't even submit the editorial solutions for Python
@sauravsingh4497
@sauravsingh4497 Күн бұрын
Did somebody count how many times he said balls during the video
@MP-ny3ep
@MP-ny3ep 2 күн бұрын
Thank you for the daily
@fdasfsadfsdfa
@fdasfsadfsdfa 14 сағат бұрын
Does it count as O(n) if, in this video, the problem is actually solved in O(n + n)?
@fancypants6062
@fancypants6062 3 сағат бұрын
Yes, it is standard to reduce to O(n) for situations like this.
@user-my6yf1st8z
@user-my6yf1st8z 2 күн бұрын
thanks boss, but that's a fuckin magic you did, most of the vid i thought what is he talking about, but in the end it kinda all made sense
@Kaviarasu_NS
@Kaviarasu_NS 2 күн бұрын
I think this problems solution approch is similar to Leetcode 135. Candy
@shivamvats2108
@shivamvats2108 Күн бұрын
Thanks!
@yhbarve
@yhbarve Күн бұрын
Goated!
@andybnhquang
@andybnhquang Күн бұрын
How many days until that leetcode tshirt lol
@studymotivation-c8t
@studymotivation-c8t Күн бұрын
2:08 and i solved it myself lol
@business_central
@business_central 2 күн бұрын
wait it's New year's eve ??
@eznx616
@eznx616 2 күн бұрын
real
@karthi7102
@karthi7102 2 күн бұрын
Goat
@adilansari-hq9ge
@adilansari-hq9ge 2 күн бұрын
Recording on the eve of christmas, Big hug to you Bro, Thank you for all hthe ard work , effort , dedication and motivation.
@freecourseplatformenglish2829
@freecourseplatformenglish2829 2 күн бұрын
After solving yeserday's question this was a cakewalk.
@user-my6yf1st8z
@user-my6yf1st8z 2 күн бұрын
you posted a solution to a different problem, bruv
@freecourseplatformenglish2829
@freecourseplatformenglish2829 2 күн бұрын
@@user-my6yf1st8z Thanks man, Solved the diffrent problem 😅😅🤣
@ritikpatra4757
@ritikpatra4757 Күн бұрын
damn
@leechaolan6227
@leechaolan6227 2 күн бұрын
like if you are here after finishing it with Brute force
@ThaoRatke
@ThaoRatke Күн бұрын
hi
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 54 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 55 МЛН
Load Balancers are not Magic - Dissecting Atlassian Outage
13:07
Arpit Bhayani
Рет қаралды 27 М.
Please Master This MAGIC Python Feature... 🪄
25:10
Tech With Tim
Рет қаралды 103 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 191 М.
Getting a Dev Job in 2025
21:42
Theo - t3․gg
Рет қаралды 114 М.
How I Approach a New Leetcode Problem (live problem solving)
25:31
String Matching in an Array - Leetcode 1408 - Python
8:01
NeetCodeIO
Рет қаралды 8 М.
I Will Not Write Rust Again
7:19
ThePrimeTime
Рет қаралды 178 М.
Shifting Letters II - Leetcode 2381 - Python
19:07
NeetCodeIO
Рет қаралды 11 М.
The only Cloud services you actually need to know
17:17
NeetCodeIO
Рет қаралды 209 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 234 М.