I think those who are still not aware of this legend's channel are missing a very important treasure. I hope all freshers in college get to know about this Goat. Legend MIK
Great explanation, bro! I couldn't come up with the optimal approach on my own, but after seeing your intuition part, I was able to code it myself. Thanks, Mik!
that's why i love this channel and this teacher just because he solve every problem building from scratch don't even let others think that he already made that solution thank you so much mik please be like this only chahe 1 ghante ka video ho jaaye chahe do content yahi best hai
@@Ben10291 I think the brute force solution helps us to identify the final and so we can observe that elements are sorted but in groups so we can optimal the approach, I done it by using 2D vector for different groups then store index(s) in map but it gives you TLE because the Grouping part is not optimal.. You can see the below mentioned approach but TLE comes so go for the map instead for grouping... C++ Code: (TLE) class Solution { public: vector lexicographicallySmallestArray(vector& nums, int limit) { int n=nums.size(); vectorcopy=nums; sort(copy.begin(),copy.end()); // Grouping in 2-D Vector vectornumbers; vectortemp; for(int i=1;i
@ youre right that brute force and optimal were two different solutions. The thing is, the brute force lays the foundation for the optimal solution. When we were brute forcing the solution, you must have noticed that the one thing that was hindering us from an optimized solution was that we had to repetitively check if for an element (say 10, the first element) if we still had a smaller number to the right of it under the limit. ultimately you could notice that a chain was formed 10->9->8->7->6. This would lead u to notice that these groups will always come in ascending order because they ARE SWAPPABLE indirectly. Noticing this was the crux of the problem. I understand that it could be hard to notice this intuition, especially if youre a beginner so dont beat yourself up on that.
umm, mik we can sort the array in reverse and then can store it in an unordered_map. Now I guess we can perform pop_back() and .back() operations efficiently.
I am following your videos for POTD since i found out your channel and i do watch everyday your video of POTD despite being able to solve it by my own or not, i really like and try to learn that how you approach the solution from brute to optimal and typically like a story and today i am really happy that i followed exactly same path as you described in your video from brute force to optimal one , I just didn't know about O(1) operation of pop.front() using list that's why i have done it using min-heap to get minimum element from each group , Thank you for such precious content! ❤
/*********************** JAVA ************************************/ //Approach-1 (Brute Force) //T.C : O(n^3) //S.C : O(1) class Solution { public int[] lexicographicallySmallestArray(int[] nums, int limit) { int n = nums.length; for (int i = 0; i < n; i++) { while (true) { int smallValue = nums[i]; int idx = -1; for (int j = i + 1; j < n; j++) { if (Math.abs(nums[i] - nums[j]) limit) { groupNum++; } numToGroup.put(vec[i], groupNum); groupToList.putIfAbsent(groupNum, new LinkedList()); groupToList.get(groupNum).add(vec[i]); } int[] result = new int[n]; for (int i = 0; i < n; i++) { int num = nums[i]; int group = numToGroup.get(num); result[i] = groupToList.get(group).pollFirst(); // Use and remove the smallest element } return result; } }
Bro I learned from you... But just I want to suggest more optimized code.. class Solution { public: vector lexicographicallySmallestArray(vector& nums, int limit) { vectorvec; int n = nums.size(); for(int i = 0 ; i < n ; i++){ vec.push_back({nums[i],i}); } sort(begin(vec),end(vec)); int l = 0 , r = 0; vectorindex; while(r < n){ if(index.empty()) index.push_back(vec[r++].second); else{ if(vec[r].first - vec[r-1].first
I attempted this question and initially tried to write a brute-force solution. After two unsuccessful attempts, I was finally able to write a working brute-force solution on my third attempt. class Solution { public int[] lexicographicallySmallestArray(int[] nums, int limit) { int n = nums.length; // for(int i=0; i
@@codestorywithMIK sir how to build thought process ? i am struggling😥 when i see new question I feel so weak that sometimes i cant think brute force also
Seeing the hints confused me more. This was a much better and simpler approach. Here is the Python implementation for both, brute and optimal approaches: Brute: class Solution: def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]: for i in range(len(nums)): j = i+1 while j limit: curGroup += 1 groups[curGroup].append(arr[i]) whichGroup[arr[i]] = curGroup for i in reversed(range(len(nums))): arr[i] = groups[whichGroup[nums[i]]].pop() return arr
I came to this question and when bhaiya said brute force se khud se code karo , I did this -> class Solution { public: vector lexicographicallySmallestArray(vector& nums, int limit) {
while inserting we can also take an index i = 0 and when its inserted we can get the next element in vector since in sorted order it will give correct answer
MIK bhaiya aise ques OA's me ajate hai, toh solve kaise kare? pehli baar me toh i dont think so soch pana mushkil hai. Pehle bruteforce karo then sochne me time lagega & kabhi kabhi click nhi karega plus OA's me 3-4 ques hote hai...
solved brute force but now stuck in TLE public int[] lexicographicallySmallestArray(int[] nums, int limit) { int n = nums.length, idx = 0; parentLoop: while (idx < n){ boolean flag = false; for (int i = idx; i < n; i++){ if (nums[i] < nums[idx] && Math.abs(nums[i] - nums[idx])
Mazhar sir if I don't know any concept like DP, Greedy, sliding window, and leetcode questions comes on that topics then what should we do? I solved anyway by looking at your solution but I don't find it benificial to my learning. I am finding difficulty.
Hey, I think this will not be beneficial. Always work on fundamentals first then move to qns. When you're unfamiliar with concepts like DP, Greedy, or Sliding Window, tackling problems on those topics can feel overwhelming. Here's what I suggest: Focus on the Basics: Start by learning the foundational concepts of each topic. For example, with DP, understand how recursion works, and for Sliding Window, practice problems involving subarrays. Solve Easy Problems First: Don’t jump directly to LeetCode's medium or hard problems. Platforms like LeetCode, GFG, or even TH-cam tutorials have beginner-friendly problems to get you started. Brute Force First: Always try to solve the problem using a brute force approach. It may not be efficient, but it will help you understand the problem deeply. Once you’re comfortable, you can explore optimizations. Learn Through Solutions: When you look at solutions, don’t just copy them. Take time to understand why each step is taken and how it relates to the problem. Remember, learning DSA is a gradual process. The more you practice and focus on understanding, the easier it becomes over time.
@@codestorywithMIK Okay bhaiya I'll do this, I'll focus mostly on base only. I have good understanding in few DSA topics like binary search, linked list(favorite), hashmaps but then I stopped and started doing contests, questions that I didn't knew topic of, etc. I'll focus on one thing that is to string my base. I was too overwheelmed with so many topics and problems but it's better to step back, slowly process each DS. Thanks for the reply.
U said write brute force , i have tried myself and see i have used only two for loops but it is also passed only 504 test casses 😁 vector lexicographicallySmallestArray(vector& nums, int limit) { for(int i = 0 ; i
I was able to find Brute Force Solution of this problem but i know the BT will fail.. So i didn't code But that was my mistake i should code that myself. then jump to optimal solution.
This code is giving me wrong answer on few TC's can anyone suggest what's wrong? vector lexicographicallySmallestArray(vector& nums, int limit) { int n = nums.size();
why are you not explaining this code to everyone , a 0ms code?????????????????????????????????????????????????????????????????????????????????????????????????//
Bhaiya, lagatar 3 se 4 din se POTD ke questions nhi ho rahe. Kabhi approach nahi mil raha to kabhi approach milta to code me solution fit nahi horha. 🥲
Company tags phone pe OA
thanks bhai. online assessment k hisaab se to tough hai problem. kitne minutes me solve karna tha ?
thank you for sharing
@@codestorywithMIK atlassian also
I think those who are still not aware of this legend's channel are missing a very important treasure. I hope all freshers in college get to know about this Goat. Legend MIK
official solution me bhi brute force nahi mentioned hai.
this clearly shows you are here for teaching and not only for views. thank you sir
Perfect question with great intuition.... Literally I learned many things from this lecture and question. Thanks MIK
Thank you for the brute force MIK, i can only expect from you to cover so many details. you are the real teacher
Glad to know 😇
Great explanation, bro! I couldn't come up with the optimal approach on my own, but after seeing your intuition part, I was able to code it myself. Thanks, Mik!
that's why i love this channel and this teacher just because he solve every problem building from scratch don't even let others think that he already made that solution thank you so much mik please be like this only chahe 1 ghante ka video ho jaaye chahe do content yahi best hai
true bro
Means a lot to me ❤️❤️🙏🙏
i could only think about brute force and coded it up thanks mik for optimal answer
Awesome explaination as always. Your channel is my goto when I need explaination at the end of the day.
Thank you so much for your detailed explaination🙏
After I saw the brute force solution, I was able to optimize it myself. Thank you so much for the first approach.
That's awesome! Keep exploring and learning! ❤️
how?brute and optimal were totally different ,
right?
@@Ben10291 I think the brute force solution helps us to identify the final and so we can observe that elements are sorted but in groups so we can optimal the approach, I done it by using 2D vector for different groups then store index(s) in map but it gives you TLE because the Grouping part is not optimal..
You can see the below mentioned approach but TLE comes so go for the map instead for grouping...
C++ Code: (TLE)
class Solution {
public:
vector lexicographicallySmallestArray(vector& nums, int limit) {
int n=nums.size();
vectorcopy=nums;
sort(copy.begin(),copy.end());
// Grouping in 2-D Vector
vectornumbers;
vectortemp;
for(int i=1;i
@ youre right that brute force and optimal were two different solutions. The thing is, the brute force lays the foundation for the optimal solution. When we were brute forcing the solution, you must have noticed that the one thing that was hindering us from an optimized solution was that we had to repetitively check if for an element (say 10, the first element) if we still had a smaller number to the right of it under the limit. ultimately you could notice that a chain was formed 10->9->8->7->6. This would lead u to notice that these groups will always come in ascending order because they ARE SWAPPABLE indirectly. Noticing this was the crux of the problem. I understand that it could be hard to notice this intuition, especially if youre a beginner so dont beat yourself up on that.
@@soumyamakkar4677 Yes Exactly, but I don't know where my comment gone may be deleted by someone.
❤❤❤❤apka solution and explenation is wow. Brute force bhi karke dikhaya. Wo helpful tha....
umm, mik we can sort the array in reverse and then can store it in an unordered_map. Now I guess we can perform pop_back() and .back() operations efficiently.
Beautiful idea and thought 💭 ❤️
@@codestorywithMIK same can be easily done by using unordered_map for o(1)
Mind blowing explanation
broooo brooo damn amazing way of explaining solution man.
2nd solution is mind blowing
This was so helpful ! When you used list from STL, i was like wowwww
I am following your videos for POTD since i found out your channel and i do watch everyday your video of POTD despite being able to solve it by my own or not, i really like and try to learn that how you approach the solution from brute to optimal and typically like a story and today i am really happy that i followed exactly same path as you described in your video from brute force to optimal one , I just didn't know about O(1) operation of pop.front() using list that's why i have done it using min-heap to get minimum element from each group , Thank you for such precious content! ❤
Thank you for your kind words. Means a lot to me ❤️🙏
We can use a deque for optimal push and pop operations on both the front and back sides.
Thank you so much bhai.. BHot acha question tha...
Was waiting for you video only. thank you so much MIK
I have been waiting for your video since morning 🌄🌅🌄🌅🌅😊
Means a lot. Thank for watching and I hope this video helps.
Is there any video for hamming distance question?
public int[]lexicographicallySmallestArray(int[]nums,int limit){
int n=nums.length;
Listsorted =new ArrayList();
int[]groups=new int[n];
ListgroupIdx=new ArrayList();
int[]ans=new int[n];
for(int i=0;ia[0]-b[0]);
int group=0;
groupIdx.add(0);
for(int i=1;ilimit){
group++;
groupIdx.add(i);
}
groups[sorted.get(i)[1])=group;
}
for(int i=0;i
very good Q and not that hard solution but very well made and thought
Thank you for the solution MIK!! This question should be marked as hard. It is difficult to come up with the second approach.
/*********************** JAVA ************************************/
//Approach-1 (Brute Force)
//T.C : O(n^3)
//S.C : O(1)
class Solution {
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length;
for (int i = 0; i < n; i++) {
while (true) {
int smallValue = nums[i];
int idx = -1;
for (int j = i + 1; j < n; j++) {
if (Math.abs(nums[i] - nums[j]) limit) {
groupNum++;
}
numToGroup.put(vec[i], groupNum);
groupToList.putIfAbsent(groupNum, new LinkedList());
groupToList.get(groupNum).add(vec[i]);
}
int[] result = new int[n];
for (int i = 0; i < n; i++) {
int num = nums[i];
int group = numToGroup.get(num);
result[i] = groupToList.get(group).pollFirst(); // Use and remove the smallest element
}
return result;
}
}
Great Video Mik ❤❤, Here is the Motivation for the next video:
"The Magic you are looking for is hidden in the work that you're avoiding"
Bro I learned from you...
But just I want to suggest more optimized code..
class Solution {
public:
vector lexicographicallySmallestArray(vector& nums, int limit) {
vectorvec;
int n = nums.size();
for(int i = 0 ; i < n ; i++){
vec.push_back({nums[i],i});
}
sort(begin(vec),end(vec));
int l = 0 , r = 0;
vectorindex;
while(r < n){
if(index.empty()) index.push_back(vec[r++].second);
else{
if(vec[r].first - vec[r-1].first
Thank you bhaiya ❤
Low battery pop up are back again, missed it for a while😂
Its hard not medium 😊
"kaam kar lai nahi toh teri jagah koi aur kar lega"
-- Motivation quote my Harsh
this is not just motivation this is the reality.
I attempted this question and initially tried to write a brute-force solution. After two unsuccessful attempts, I was finally able to write a working brute-force solution on my third attempt.
class Solution {
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length;
// for(int i=0; i
The more I praise, the less it will be. Thanks a lot ❤❤❤.
Thank you sir ❤❤
brute force :
class Solution {
public:
vector lexicographicallySmallestArray(vector& nums, int limit) {
int n = nums.size();
for(int i=0;i
Bhai accha samjhaya apne
Thank you :)
Hello mik bhaiya kaise hoo😇
thanks for this amazing explanation mik bhaiya..love you LONG_LONG_MAX 💘💘
can we use dsu to solve this ? will be really helpful to have explanation of the dsu solution too
sir your are awesome your explanation is another level. 🌟🔥instead of linked list can't we use deque?
Definitely you can use deque as well ❤️
@@codestorywithMIK sir how to build thought process ? i am struggling😥 when i see new question I feel so weak that sometimes i cant think brute force also
Dsu Union find se karke dikhao na bhaiya yeh wala....plz
Seeing the hints confused me more. This was a much better and simpler approach. Here is the Python implementation for both, brute and optimal approaches:
Brute:
class Solution:
def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]:
for i in range(len(nums)):
j = i+1
while j limit:
curGroup += 1
groups[curGroup].append(arr[i])
whichGroup[arr[i]] = curGroup
for i in reversed(range(len(nums))):
arr[i] = groups[whichGroup[nums[i]]].pop()
return arr
can it be done using priority_queue or multiset?
I came to this question and when bhaiya said brute force se khud se code karo , I did this ->
class Solution {
public:
vector lexicographicallySmallestArray(vector& nums, int limit) {
bool hasSwapped = false;
for(int i = 0;i
while inserting we can also take an index i = 0 and when its inserted we can get the next element in vector since in sorted order it will give correct answer
Indeed ❤️
MIK bhaiya aise ques OA's me ajate hai, toh solve kaise kare? pehli baar me toh i dont think so soch pana mushkil hai. Pehle bruteforce karo then sochne me time lagega & kabhi kabhi click nhi karega plus OA's me 3-4 ques hote hai...
Why watch your video every problem becomes easy and your way of teaching first brute force then optimal is awesome keep it up 😊
thank you
You're welcome ❤️
We can use .front() to get the front value of list instead of deferencing .begin()
MIK bhai please make videos on contest 3rd and 4th question
Could've also used 'queue' instead of 'doubly linked-list' to achieve same functionality in O(1). Correct me if I'm wrong.
Yes deque works well ❤️
I tried brute force but got stuck at [5,100,44,45,16,30,14,65,83,64], I tried more but could'nt get it, so I'm here
solved brute force but now stuck in TLE
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length, idx = 0;
parentLoop: while (idx < n){
boolean flag = false;
for (int i = idx; i < n; i++){
if (nums[i] < nums[idx] && Math.abs(nums[i] - nums[idx])
Sort the array before applying the logic
We can still use vector if we populate the answer from the end. No need to use list in that case.
we all have sad story either make it excuse or your motivation
why this question is medium level 😔deque use instead of list
Hello bhaiya, can you please make a video on leetcode problem "117. Populating Next Right Pointers in Each Node II" using DFS.
class Solution {
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length;
int i = 0;
while(i
What is the purpose of while(true)??
Using it,
We keep finding the smallest value and swapping until we no longer are able to find a small value for index i.
Tomorrow's quote from my side: Don't climb a mountain for world to see you but for you to see the world.👍
Bhai MIK , i am not able to solve the POTD so i watch your video and solve , is this good?
keep practicing bro. I was also at same position and now consistently practicing for months, i am able to solve medium
Sir please make a code-story video for leetcode 638 🙏🙏
Mazhar sir if I don't know any concept like DP, Greedy, sliding window, and leetcode questions comes on that topics then what should we do? I solved anyway by looking at your solution but I don't find it benificial to my learning. I am finding difficulty.
Hey, I think this will not be beneficial. Always work on fundamentals first then move to qns.
When you're unfamiliar with concepts like DP, Greedy, or Sliding Window, tackling problems on those topics can feel overwhelming. Here's what I suggest:
Focus on the Basics: Start by learning the foundational concepts of each topic. For example, with DP, understand how recursion works, and for Sliding Window, practice problems involving subarrays.
Solve Easy Problems First: Don’t jump directly to LeetCode's medium or hard problems. Platforms like LeetCode, GFG, or even TH-cam tutorials have beginner-friendly problems to get you started.
Brute Force First: Always try to solve the problem using a brute force approach. It may not be efficient, but it will help you understand the problem deeply. Once you’re comfortable, you can explore optimizations.
Learn Through Solutions: When you look at solutions, don’t just copy them. Take time to understand why each step is taken and how it relates to the problem.
Remember, learning DSA is a gradual process. The more you practice and focus on understanding, the easier it becomes over time.
@@codestorywithMIK Okay bhaiya I'll do this, I'll focus mostly on base only. I have good understanding in few DSA topics like binary search, linked list(favorite), hashmaps but then I stopped and started doing contests, questions that I didn't knew topic of, etc. I'll focus on one thing that is to string my base. I was too overwheelmed with so many topics and problems but it's better to step back, slowly process each DS. Thanks for the reply.
30. Substring with Concatenation of All Words
bhaiya please iss question ki video bnakrr explain krddoo
Sir , plz explain leetcode 1163
Brute force in java
class Solution {
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length;
int i=0;
while(i < n){
int j = i+1;
while( j < n){
if((Math.abs(nums[i] - nums[j]) nums[j])){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
j = i+1;
}
else{
j++;
}
}
if(j == n){
i++;
}
}
return nums;
}
}
Bhagwaan mai kab soch paunga khud se ye sab !!!
U said write brute force , i have tried myself and see i have used only two for loops but it is also passed only 504 test casses 😁
vector lexicographicallySmallestArray(vector& nums, int limit) {
for(int i = 0 ; i
I was able to find Brute Force Solution of this problem but i know the BT will fail.. So i didn't code But that was my mistake i should code that myself. then jump to optimal solution.
This code is giving me wrong answer on few TC's
can anyone suggest what's wrong?
vector lexicographicallySmallestArray(vector& nums, int limit) {
int n = nums.size();
vector vec = nums;
sort(vec.begin(), vec.end());
unordered_map groupInd;
unordered_map groupList;
int group=0;
groupInd[vec[0]] = group;
groupList[group].push_back(vec[0]);
for(int i=1; i limit)
{
group+=1;
}
groupInd[vec[i]] = group;
groupList[group].push_back(vec[i]);
}
vector res(n);
for(int i=0; i
int expGroupInd = groupInd[vec[i]]; // for result making we are using original array not the sorted one so instead of vec[i] use nums[i]
I think this is a hard question
chellenging tha ye problem thoda
bhaiya just for watch time sake, please don't make the videos unnneccessarry long!!
Bobal video!!!!!!!!
Bobal ka kya matlab hai bro ?
@@EB-ot8uu (outstanding)
nigga wtf does that even mean?
why are you not explaining this code to everyone , a 0ms code?????????????????????????????????????????????????????????????????????????????????????????????????//
Please keep uploading the ipad slides, its better for the revison ❤@codestorywithMIK
Was not able to do this ques 🥲 Thanks for the explanation
Bhaiya, lagatar 3 se 4 din se POTD ke questions nhi ho rahe. Kabhi approach nahi mil raha to kabhi approach milta to code me solution fit nahi horha. 🥲