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
Like target is 150. Please do like if you have understood the explanation as well as the code ❤
You deserve more ❤
similar to product of array except itself , wonderful explanation sir 💜
@@Rahul_Mongia 100%
Bhiaya leetcode weekly contest pr bhi video banao please ......it would be really helpfull for us
bnda job krta hai , full time youtiube nhi
bruiteforce done
There is no use for the prevRight variable.
Bro how will you evn think like this .great
Bhaiya optimal idea nahi aaraha he brute hohi ja raha
@@SAI-q1v product of array except itself solve kra tha pehle? Same intuition h
@shashwat_tiwari_st nahi bhaiya karunga❤️
public int[] minOperations(String boxes) {
int n=boxes.length();
int res[]=new int[n];
for(int i=0;i
It was easy brute force
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);🎉❤
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
}