Minimum Number of Operations to Move All Balls to Each Box | Leetcode 1769 | 3 Approaches | MIK

Поделиться
HTML-код
  • Опубликовано: 7 янв 2025

Комментарии • 78

  • @AbhijeetMuneshwar
    @AbhijeetMuneshwar 2 дня назад +38

    Respected MIK Sir, 🙏🏼
    1 motivational quote from my side :
    "By setting ambitious goals and diligently working towards them with unwavering discipline, individuals can unlock extraordinary levels of success."

    • @mohit6215
      @mohit6215 День назад

      Thanks for the quote

  • @khushichaudhary4859
    @khushichaudhary4859 2 дня назад +26

    i did this question by my own, now i am watching your video so that i can learn something new.... thankyou brother

  • @gui-codes
    @gui-codes 2 дня назад +11

    Optimal Approach Starts from 15:21
    Thank you so much MIK for beautiful explanation as always. After just 5 minutes of Optimal approach, I coded on my own.

  • @adii16089
    @adii16089 День назад +1

    sir aaj khu se optimal solution likha... all thanks to you :)

  • @joydeep-halder
    @joydeep-halder День назад +1

    Thank you bhaiya for easy explanation. Brute force se solve ker liya tha. Optimal approach ke bahut karib bhi tha, lekin nhi bana paya. vo, left and right side me garbar ker diya

  • @rohitvermamnvtechcarvons2793
    @rohitvermamnvtechcarvons2793 День назад +2

    Brute Force Approach:
    class Solution {
    public:
    vector minOperations(string boxes) {
    int n = boxes.size();
    vectoranswer(n,0);
    unordered_setidx;
    for(int i = 0;i

  • @streetfoodlover2317
    @streetfoodlover2317 День назад +2

    class Solution {
    public int[] minOperations(String boxes) {
    int[] ans = new int[boxes.length()];
    int total = 0;
    for (int i = 0; i < boxes.length(); i++) {
    total = 0;
    for (int j = 0; j < boxes.length(); j++) {
    if (i == j)
    continue;
    total += Math.abs(j - i) * Character.getNumericValue(boxes.charAt(j));
    }
    ans[i] = total;
    }
    return ans;
    }
    }
    Love you brother you are great!!!

  • @24deeshankbatra38
    @24deeshankbatra38 День назад +2

    Great Explaination !

  • @aws_handles
    @aws_handles День назад +1

    Legit explanation. Thanks a lot MIK ❤

  • @nikhil_squats
    @nikhil_squats 2 дня назад +13

    Today is the youngest you will ever be

  • @rickdutta942
    @rickdutta942 День назад +2

    //Using unordered set: Brute force
    //TC: O(n^2)
    //SC: O(n)
    class Solution {
    public:
    vector minOperations(string boxes) {
    int n = boxes.size();
    unordered_set idxs;
    vector ans(n);
    for (int i = 0; i < n; i++) {
    if(boxes[i] == '1') idxs.insert(i);
    }
    for (int i = 0; i < n; i++) {
    int move = 0;
    for(int j: idxs) {
    move += abs(j - i);
    }
    ans[i] = move;
    }
    return ans;
    }
    };

  • @GautamKumar-fw4cb
    @GautamKumar-fw4cb 2 дня назад +2

    Here's is the Java code for the Brute force - 1 using set method
    class Solution {
    public int[] minOperations(String boxes) {
    int n = boxes.length();
    int[] ans = new int[n];
    HashSet hs = new HashSet();

    for(int i = 0; i < n; i++) {
    if(boxes.charAt(i) == '1') {
    hs.add(i);
    }
    }
    for(int i = 0; i < n; i++) {
    for(int idx : hs) {
    ans[i] += Math.abs(idx-i);
    }
    }
    return ans;
    }
    }
    Thanks sir for the optimal solution. ❤

  • @crazygamerrohan9899
    @crazygamerrohan9899 2 дня назад +9

    Did By My OWN in O(N^2)❤

  • @Vibhanshushrivastava
    @Vibhanshushrivastava 2 дня назад +4

    solve this optimally within 5 minute , its all because of you mik .

    • @sahebraojadhav9727
      @sahebraojadhav9727 День назад +2

      Bro I saw you leetcode profile it is same as mine.would you like to to peer programming with me

    • @Vibhanshushrivastava
      @Vibhanshushrivastava День назад +2

      @sahebraojadhav9727 yus sure bro why not

  • @DevanshGupta-io7rl
    @DevanshGupta-io7rl День назад

    vector minOperations(string boxes) {
    int n=boxes.length();
    vectorans;
    unordered_setst;
    for(int i=0;i

  • @gyandyan
    @gyandyan День назад +1

    vector minOperations(string boxes) {
    vectorans;
    // Brute Force solution
    // for(int i=0;i

  • @k-CE-OmkarPathak
    @k-CE-OmkarPathak День назад +1

    Great & Thanksss

  • @universalcosmologist3675
    @universalcosmologist3675 День назад

    i did it using some form of dp like dp[i]=dp[i-1]+count for left operations at i index count is number of ones to the left of i index and dp[i] is cost required to move all ones left of i to i index now it is really easy to understand

  • @yogeshraj6816
    @yogeshraj6816 День назад +1

    class Solution {
    public:
    vector minOperations(string boxes) {
    unordered_set st;
    int n = boxes.length();
    for(int i = 0; i

  • @Akashkumar_12
    @Akashkumar_12 День назад +2

    pahle maine brute force using o(n*n) kiya uske 30 min baad class Solution {
    public:
    vector minOperations(string boxes) {
    int n=boxes.size();
    vectorans(n),pre(n);
    int one=0,cnt=0;
    for(int right=n-1;right>=0;right--){
    pre[right]=one;
    if(boxes[right]=='1')cnt++;
    one+=cnt;
    }
    one=0,cnt=0;
    for(int left=0;left

  • @jain5184
    @jain5184 2 дня назад +7

    Less
    Go

  • @harshitgoyal7206
    @harshitgoyal7206 День назад +1

    brute force
    vectorresult;
    unordered_setst;
    int n=boxes.length();
    for(int i=0;i

  • @bhupendrakalal1727
    @bhupendrakalal1727 2 дня назад +2

    my solution:-
    class Solution {
    public:
    vector minOperations(string boxes) {
    vectorindex;
    vectorans;
    for(int i=0;i

  • @riyaahlawat6836
    @riyaahlawat6836 21 час назад

    Hey, I felt the optimal approach was kinda not intuitive enough. Like you explained it in the best way possible but I still felt I wouldn't have reached there on my own. So, it was more about learning a new way of solving problems, right? And now to revise this again and again until it comes intuitively to me?

  • @DineshBhardwaj-v2r
    @DineshBhardwaj-v2r День назад +1

    class Solution {
    public:
    vector minOperations(string boxes) {
    int n = boxes.length();
    vector ltr(n, 0);
    vector rtl(n, 0);
    vector ans(n, 0);
    int count = 0;
    for (int i = 0; i < n; i++) {
    ltr[i] = count;
    count += (boxes[i] - '0');
    if (i > 0) ltr[i] += ltr[i - 1];
    }
    count = 0;
    for (int i = n - 1; i >= 0; i--) {
    rtl[i] = count;
    count += (boxes[i] - '0');
    if (i < n - 1) rtl[i] += rtl[i + 1];
    }
    for (int i = 0; i < n; i++) {
    ans[i] = ltr[i] + rtl[i];
    }
    return ans;
    }
    };

  • @AbhayJoshi-r2i
    @AbhayJoshi-r2i 2 дня назад +6

    First

  • @gyandyan
    @gyandyan День назад +1

    vectoroneidx;
    for(int i=0;i

  • @kpnit3769
    @kpnit3769 2 дня назад +3

    brother BRUTE FORCE :----
    class Solution {
    public:
    int fun(string boxes,int j){
    int sum=0;
    for(int i=0;i

  • @gauravnegi8540
    @gauravnegi8540 2 дня назад +1

    class Solution {
    public int[] minOperations(String boxes) {
    int n = boxes.length();
    int k = 0;
    int[] result = new int[n];
    HashSet set = new HashSet();
    for(int i = 0;i

  • @saurabhnishad9631
    @saurabhnishad9631 День назад +1

    sir please make explaination vdo for leetcode 3186

  • @HarmanSingh-nw6ix
    @HarmanSingh-nw6ix День назад +1

    APPROACH _ 1
    class Solution {
    public:
    vector minOperations(string s) {
    int n = s.length();
    vector result(n);
    unordered_set st;
    for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
    st.insert(i);
    }
    }
    for(int i = 0 ; i< n ;i++){
    for(int num :st){
    result[i] += abs(num - i);
    }
    }
    return result;
    }
    };
    APPROACH_ 2
    class Solution {
    public:
    vector minOperations(string s) {
    int n = s.length();
    vector result(n);
    for(int i =0 ;i< n ;i++){
    if(s[i] == '1'){
    for(int j = 0 ; j< n ;j++){
    result[j] += abs(i - j);
    }
    }
    }
    return result;
    }
    };
    APPROACH_ 3
    class Solution {
    public:
    vector minOperations(string boxes) {
    int n = boxes.size();
    vectorresult(n);
    int cumvalue = 0 ;
    int cumsumvalue = 0;
    // for the left side
    for(int i = 0 ; i< n ;i++){
    result[i] = cumsumvalue;
    cumvalue += boxes[i] == '0' ? 0 : 1;
    cumsumvalue += cumvalue;
    }
    // for right side balls
    cumvalue = 0 ;
    cumsumvalue = 0 ;
    for(int i = n-1 ; i>= 0 ;i--){
    result[i] += cumsumvalue;
    cumvalue += boxes[i] == '0' ? 0 : 1;
    cumsumvalue += cumvalue;
    }
    return result;
    }
    };
    thank you sir .

  • @unknown47896
    @unknown47896 2 дня назад +8

    solved it on my own
    class Solution {
    public:
    vector minOperations(string boxes) {
    int n= boxes.length();
    vector pre(n,0);
    vector suf(n,0);
    int cnt= (boxes[0]=='1') ;
    for(int i=1;i=0;i--)
    {
    suf[i]=suf[i+1]+cnt;

    }
    vector ans(n,0);
    for(int i=0;i

    • @gui-codes
      @gui-codes 2 дня назад +2

      I also wrote a similar one after understanding optimal approach

    • @unknown47896
      @unknown47896 2 дня назад +2

      @@gui-codes nice bro..😊😊

    • @codestorywithMIK
      @codestorywithMIK  2 дня назад +2

      Clean code ❤️👌

    • @unknown47896
      @unknown47896 2 дня назад +2

      @@codestorywithMIK thank you sir...🫠🫠

    • @33rahulkumarjhaxlm36
      @33rahulkumarjhaxlm36 2 дня назад +1

      Did similar not even used prefix and suffix vector just using two variables iterating by i in forward and n-i-1 for reverse dirn in single loop

  • @knight-z1x
    @knight-z1x День назад

    //using vector : Brute force
    T.C.: O(n^2);
    S.C.: O(n);
    class Solution {
    public:
    vector minOperations(string boxes) {
    int n=boxes.size();
    vector indxs;
    vector answer(n,0);
    for(int i=0;i

  • @techmaster9953
    @techmaster9953 День назад

    sir pls solve weekly and biweekly questions , if not posiible then try to solve only 3 and 4, please sir

  • @AyushPratap-yy9zu
    @AyushPratap-yy9zu 2 дня назад +1

    Brute force approach 1 (java)
    class Solution {
    public int[] minOperations(String boxes) {
    ArrayList ar = new ArrayList();
    int n = boxes.length();
    for (int i = 0; i < n; i++) {
    if(boxes.charAt(i) == '1') {
    ar.add(i);
    };
    }
    int[] arr = new int[n];
    for (int i = 0; i < n; i++) {
    int curr = 0;
    for (int j = 0; j < ar.size(); j++) {
    curr += Math.abs(ar.get(j) - i);
    }
    arr[i] = curr;
    }
    return arr;
    }
    }

  • @deveshzope8555
    @deveshzope8555 День назад

    somewhat Q like product except self not exactly as we take cumsum of prefixarray and suffixarray

  • @AyushSingh-fw1pn
    @AyushSingh-fw1pn День назад

    BRUTE FORCE APPROACH!
    vector ans;
    for(int i=0;i

  • @tauheedahmed4073
    @tauheedahmed4073 День назад

    Intution can be built if you would have done product of array except self

  • @srikanthsatya3226
    @srikanthsatya3226 День назад

    hello bhaiyya kal ka leetcode weekly contest 431 ka 3rd question per ek video banao pleaseeee thanky you...

  • @VaishnavI-me8bz
    @VaishnavI-me8bz День назад

    video deekh k smj aa jata h , but me khud yh intuition kbhi build nhi kr skti thi..
    its just like ki video dekhi .rtta lag gya.....ki ese hi krna h yeh ques..🫠..

    • @codestorywithMIK
      @codestorywithMIK  День назад +2

      Always try to focus on how the intuition is being built in the video. And practice more Qns.
      Practicing more and more will unlock different ways of solving a problem. Don’t worry, we all have been there. You got this 💪

  • @knight-z1x
    @knight-z1x День назад

    good evening sir!!
    sir my main problem is that ki mujhe bhot bar leetcode ke questions ka description hi smj me nhi ata hai.. description ko padte padte confuse sa ho jata hu..and sir phir normal brute force approach bhi nhi banti !! please help

  • @m_fi8926
    @m_fi8926 День назад

    done the brute force by myself. came here for the optimal, i got the intution but was not able to come up with the solution

  • @zaffarzeshan1308
    @zaffarzeshan1308 День назад

    class Solution {
    public int[] minOperations(String boxes) {
    int n=boxes.length();
    int pre[]= new int [n];
    for(int i=0;i

  • @AMANRAJ-dt8gu
    @AMANRAJ-dt8gu 2 дня назад +3

    Everyone is jealous of what you got, but no one is jealous of how you got it. Hoping this quote gets featured as the next POTD. quote from MIK 😅

  • @simmikedia3347
    @simmikedia3347 2 дня назад +1

    please explain leetcode 2035....

  • @shivam-notavlogger5042
    @shivam-notavlogger5042 2 дня назад +6

    Hello Mike, I have been following your channel from a very long tym. But despite of doing potd daily and doing hardwork I am still not able to clear any of the OA rounds of any company. I am feeling very demotivated. I have given the test of many companies but not being able to clear any of them. Sach bolu toh abh mera maan bhi nhi karta kuch karne. Aur nah mujhe abh umeed lag rahe h ke mere placement ho payege. Itna tym hogya h mere sbh dosto ke bhi lag gye h bas mai hi reh gya. Boht burra lag raha. Jise bhi baat karo voh bolta h ho jayega try karo. But aur kitna try karu abh toh opportunities bhi nhi bache h. My 4yr of clg get wasted in just one moment. Sbh khtm ese lag raha h jaise khale haath clg aaye tha vesa hi khale haath jaana padega. Kuch fayada nhi hua yaha aake. Also my family conditions are not good. Maine socha tha ke unke help karunga. But mai kise kaam ka nhi. Boht bekar h sbh kuch. Mujhe kuch smj nhi aa raha ke kya karu. Mera maan bhi nhi kar raha rehne ka. Please help. Pls tell ke mai kya karu.

    • @gui-codes
      @gui-codes 2 дня назад +6

      Bhai aise haar nahi maano.
      Trust me, I have been there. Bahot demotivated tha. Kaafi fight karke consistency banai rakhi maine. Finally kuch ache results aane lage and then confidence boost hogaya.
      Remember that you might fail 100 times, but pass 1 time and this 1 time will make you forget the pain of failure of 100 times.
      Routine banao parhne ka, step by step chalo.

    • @codestorywithMIK
      @codestorywithMIK  2 дня назад +10

      Hi Shivam,
      First of all, thank you for trusting me enough to share your feelings. I understand how hard and frustrating this phase can be, but trust me, you are not alone in this journey.
      Failure is not the end; it’s a step toward success. Even though things seem tough right now, remember that every effort you put in is building a stronger version of yourself. Placements and tests are just a part of life, not the definition of your worth or potential.
      Take a step back, breathe, and re-strategize. Focus on your weak areas, practice consistently, and try to approach problems with a fresh mindset. Also, don’t compare your journey with others-everyone has their own timeline.
      You have the courage to keep trying despite challenges, and that itself is commendable. Stay disciplined, stay hopeful, and never give up. Success will come, and when it does, it’ll be worth all the effort.
      I’m rooting for you! You’ve got this! ❤️

    • @unknown47896
      @unknown47896 2 дня назад +6

      bro...do not demotivate urself...I was also there in this phase once....but now with consitent practice and hardwork I begin to develop skills myself......everyone has their own journey....u may get it in near future......Just be consistent daily.......do not give up bro...

    • @Aryan-cy7cu
      @Aryan-cy7cu 2 дня назад +6

      I understand how stressful and overwhelming this can be, but remember, it's just a phase. You've worked hard, and these struggles are part of the journey to something great ..
      Remember we are here , You're not alone.

    • @shivam-notavlogger5042
      @shivam-notavlogger5042 День назад +2

      @@codestorywithMIK thank u mik for all this. Can you pls tell me ke mai aur kya different karu jise ke OA and interviews mai help ho. I mean mujh se problems yeh potd hojate h but kaafi tym lagta h. Aur OA mai ek dum se click nhi karta problem ke baare mai but baadh mai hojate h. Aur interview mai kahe baar esa hota h ke mai panic kar jaata hu aur aate hue cheez bhi nhi bata paata. Please help

  • @tauheedahmed4073
    @tauheedahmed4073 День назад +1

    BF passed but got stuck at optimal

  • @harshugamer7776
    @harshugamer7776 18 часов назад

    SINGLE pass solution without any extra space..
    class Solution {
    public int[] minOperations(String boxes) {
    int n = boxes.length();
    int ans[] = new int[n];
    int left = 0;
    int right = 0;
    int rightone = boxes.charAt(n - 1) == '1' ? 1 : 0;
    int leftone = boxes.charAt(0) == '1' ? 1 : 0;

    for(int i = 1 , j = n - 2 ; i < n ; i++, j--){
    left += leftone;
    right += rightone;
    ans[i] += left;
    ans[j] += right;
    if(boxes.charAt(i) == '1') leftone++;
    if(boxes.charAt(j) == '1') rightone++;
    }
    return ans;
    }
    }

  • @sohaibshaikh7208
    @sohaibshaikh7208 2 дня назад +2

    🫡

  • @YashSinghal
    @YashSinghal День назад +1

    after seeing 500 videos of yours this was surely very easy question even O(n) . thank you 🫡