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."
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.
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
Brute Force Approach: class Solution { public: vector minOperations(string boxes) { int n = boxes.size(); vectoranswer(n,0); unordered_setidx; for(int i = 0;i
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!!!
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. ❤
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
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
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?
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
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;
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..🫠..
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 💪
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
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.
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.
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! ❤️
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...
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.
@@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
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; } }
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."
Thanks for the quote
i did this question by my own, now i am watching your video so that i can learn something new.... thankyou brother
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.
sir aaj khu se optimal solution likha... all thanks to you :)
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
Brute Force Approach:
class Solution {
public:
vector minOperations(string boxes) {
int n = boxes.size();
vectoranswer(n,0);
unordered_setidx;
for(int i = 0;i
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!!!
Great Explaination !
Legit explanation. Thanks a lot MIK ❤
Today is the youngest you will ever be
//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;
}
};
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. ❤
Did By My OWN in O(N^2)❤
me too
+1
Solving this question in O(N) is of medium difficulty...
We can do this using extra space
@@unknown47896 Not much they are same just to use prefix and suffix ❤️
solve this optimally within 5 minute , its all because of you mik .
Bro I saw you leetcode profile it is same as mine.would you like to to peer programming with me
@sahebraojadhav9727 yus sure bro why not
vector minOperations(string boxes) {
int n=boxes.length();
vectorans;
unordered_setst;
for(int i=0;i
vector minOperations(string boxes) {
vectorans;
// Brute Force solution
// for(int i=0;i
Great & Thanksss
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
Loved it 👌
class Solution {
public:
vector minOperations(string boxes) {
unordered_set st;
int n = boxes.length();
for(int i = 0; i
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
Less
Go
brute force
vectorresult;
unordered_setst;
int n=boxes.length();
for(int i=0;i
my solution:-
class Solution {
public:
vector minOperations(string boxes) {
vectorindex;
vectorans;
for(int i=0;i
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?
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;
}
};
First
vectoroneidx;
for(int i=0;i
brother BRUTE FORCE :----
class Solution {
public:
int fun(string boxes,int j){
int sum=0;
for(int i=0;i
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
sir please make explaination vdo for leetcode 3186
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 .
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
I also wrote a similar one after understanding optimal approach
@@gui-codes nice bro..😊😊
Clean code ❤️👌
@@codestorywithMIK thank you sir...🫠🫠
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
//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
sir pls solve weekly and biweekly questions , if not posiible then try to solve only 3 and 4, please sir
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;
}
}
somewhat Q like product except self not exactly as we take cumsum of prefixarray and suffixarray
BRUTE FORCE APPROACH!
vector ans;
for(int i=0;i
Intution can be built if you would have done product of array except self
hello bhaiyya kal ka leetcode weekly contest 431 ka 3rd question per ek video banao pleaseeee thanky you...
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..🫠..
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 💪
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
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
class Solution {
public int[] minOperations(String boxes) {
int n=boxes.length();
int pre[]= new int [n];
for(int i=0;i
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 😅
Nice 👌🏻
please explain leetcode 2035....
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.
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.
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! ❤️
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...
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.
@@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
BF passed but got stuck at optimal
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;
}
}
🫡
after seeing 500 videos of yours this was surely very easy question even O(n) . thank you 🫡