1769. Minimum Number of Operations to Move All Balls to Each Box | leetcode daily challenge | dsa

Поделиться
HTML-код
  • Опубликовано: 21 янв 2025
  • Problem Link:
    leetcode.com/p...
    Problem Statement:
    You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if the ith box is empty, and '1' if it contains one ball.
    In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to box j if abs(i - j) == 1. Note that after doing so, there may be more than one ball in some boxes.
    Return an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box.
    Each answer[i] is calculated considering the initial state of the boxes.
    Solution Link:
    github.com/Tiw...
    Dynamic Programming:
    • Dynamic Programming in...
    Graph Playlist:
    • Graph Data Structure S...
    Java Plus DSA Placement Course Playlist:
    • Java and DSA Course Pl...
    Java Plus DSA Sheet:
    docs.google.co...
    Notes:
    github.com/Tiw...
    Telegram Link:
    shashwattiwari...
    Ultimate Recursion Series Playlist:
    • Recursion and Backtrac...
    Instagram Handle: (@shashwat_tiwari_st)
    shashwattiwari...
    Samsung Interview Experience:
    • I cracked Samsung | SR...
    Company Tags:
    Facebook | Amazon | Microsoft | Netflix | Google | LinkedIn | Pega Systems | VMware | Adobe | Samsung
    Timestamp:
    0:00 - Introduction

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

  • @shashwat_tiwari_st
    @shashwat_tiwari_st  16 дней назад +4

    Like target is 150. Please do like if you have understood the explanation as well as the code ❤

  • @Rahul_Mongia
    @Rahul_Mongia 16 дней назад +3

    similar to product of array except itself , wonderful explanation sir 💜

  • @GautamParmar-ug9ld
    @GautamParmar-ug9ld 16 дней назад +4

    Bhiaya leetcode weekly contest pr bhi video banao please ......it would be really helpfull for us

    • @OnstreamGaming
      @OnstreamGaming 16 дней назад +1

      bnda job krta hai , full time youtiube nhi

  • @OnstreamGaming
    @OnstreamGaming 16 дней назад

    bruiteforce done

  • @shivendrasinghthakur4476
    @shivendrasinghthakur4476 16 дней назад

    There is no use for the prevRight variable.

  • @nishith5380
    @nishith5380 16 дней назад

    Bro how will you evn think like this .great

  • @SAI-q1v
    @SAI-q1v 16 дней назад +1

    Bhaiya optimal idea nahi aaraha he brute hohi ja raha

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  16 дней назад +1

      @@SAI-q1v product of array except itself solve kra tha pehle? Same intuition h

    • @SAI-q1v
      @SAI-q1v 16 дней назад +1

      @shashwat_tiwari_st nahi bhaiya karunga❤️

  • @srishanth-r7n
    @srishanth-r7n 16 дней назад

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

  • @asad_iliyas
    @asad_iliyas 16 дней назад +2

    It was easy brute force

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 16 дней назад +1

    public int[]minOperations(String boxes){
    int n=boxes.length();
    int[]ans=new int[n];
    for(int i=0,ops=0,cnt=0;i=0;i--){
    ans[i]+=ops;
    cnt+=boxes.charAt(i)-'0';
    ops+=cnt;
    }
    return ans;
    tc=0(n);
    sc=0(n);🎉❤

  • @Ajay-cv1zs
    @Ajay-cv1zs 16 дней назад

    More simple solution (golang)
    func minOperations(boxes string) []int {
    n := len(boxes)
    result:=make([]int,n)
    balls:=0
    moves:=0
    for i:=0;i=0;i--{
    result[i]+=moves
    if boxes[i]=='1'{
    balls++
    }
    moves+=balls
    }
    return result
    }