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

preview_player
Показать описание
Problem Link:

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:

Dynamic Programming:

Graph Playlist:

Java Plus DSA Placement Course Playlist:

Java Plus DSA Sheet:

Notes:

Telegram Link:

Ultimate Recursion Series Playlist:

Samsung Interview Experience:

Company Tags:
Facebook | Amazon | Microsoft | Netflix | Google | LinkedIn | Pega Systems | VMware | Adobe | Samsung

Timestamp:
0:00 - Introduction
Рекомендации по теме
Комментарии
Автор

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

shashwat_tiwari_st
Автор

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

Rahul_Mongia
Автор

public int[] minOperations(String boxes) {
int n=boxes.length();
int res[]=new int[n];
for(int i=0;i<boxes.length();i++)
{
int temp=0;
for(int j=0;j<boxes.length();j++)
{
if(boxes.charAt(j)=='1')
{
temp+=Math.abs(j-i);
}
}
res[i]=temp;
}
return res;


}

srishanth-rn
Автор

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

GautamParmar-ugld
Автор

There is no use for the prevRight variable.

shivendrasinghthakur
Автор

public int[]minOperations(String boxes){
int n=boxes.length();
int[]ans=new int[n];
for(int i=0, ops=0, cnt=0;i<n;i++){
ans[i]+=ops;
cnt+=boxes.charAt(i)-'0';
ops+=cnt;
}
for(int i=n-1, 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);🎉❤

dayashankarlakhotia
Автор

Bro how will you evn think like this .great

nishith
Автор

Bhaiya optimal idea nahi aaraha he brute hohi ja raha

SAI-qv
Автор

More simple solution (golang)

func minOperations(boxes string) []int {
n := len(boxes)

result:=make([]int, n)

balls:=0
moves:=0

for i:=0;i<n;i++{
result[i]+= moves

if boxes[i]=='1'{
balls++
}

moves+=balls
}

moves = 0
balls = 0

for i:=n-1;i>=0;i--{
result[i]+=moves

if boxes[i]=='1'{
balls++
}

moves+=balls
}

return result

}

Ajay-cvzs
visit shbcf.ru