9 Largest number in K swaps
Вставка
- Опубліковано 1 жов 2024
- Given a number K and string str of digits denoting a positive integer, build the largest number possible by performing swap operations on the digits of str at most K times.
Example 1:
Input:
K = 4
str = "1234567"
Output:
7654321
Explanation:
Three swaps can make the
input 1234567 to 7654321, swapping 1
with 7, 2 with 6 and finally 3 with 5
------------------------------------------------------------------------------------------
Here are some of the gears that I use almost everyday:
🖊️ : My Pen (Used in videos too): amzn.to/38fKSM1
👨🏻💻 : My Apple Macbook pro: amzn.to/3w8iZh6
💻 : My gaming laptop: amzn.to/3yjcn23
📱 : My Ipad: amzn.to/39yEMGS
✏️ : My Apple Pencil: amzn.to/3kMnKYf
🎧 : My Headphones: amzn.to/3kMOzM7
💺 : My Chair: amzn.to/385weqR
🛋 : My Table: amzn.to/3TyU2IU
⏰ : My Clock: amzn.to/3slFUV3
🙋🏻♀️ : My girlfriend: amzn.to/3M6zLDK ¯\_(ツ)_/¯
PS: While having good gears help you perform efficiently, don’t get under the impression that they will make you successful without any hard work.
bhaiya ek humble request hai bus ye btado backtracking ke bddd konsi playlist aeegi taaki vo topic khi or se na pdhu jab tak please bhaiya
graph ya tree konsi aegi bhaiya??????????
tree
The legend(Aditya verma)will always come back for us❤❤
So happy to see you again making videos ❤❤
please upload next videos. Its been more than a month. We've placements this year. Atleast complete this series.
he's a working professional doing this on the side for free for students such as yourself, be grateful and stop demanding.
Free ka mile toh aur mangte hain
@@shreyashj.sharma9746 bhai tu thoda sa bkl hai kya? request hi toh kar rha tha main. teri kyu jal rhi bhai
Bhai Graphs and Trees please🙏
Aditya Bhaiya kar do upload videos bahut din ho gye🥲
Bhaiya 27 video of dp not uploaded plz upload it 🛑🛑
bhaaiii subah se 10 baar check chuka tha for new video as there was no video since last 10 days. finallyy!
Bhot behterren question hai ye Backtracking ka pura hi concept clear kar diya . Bhut sare edgePoints hai isme ab iska code khud se try karta hai . Accept ho jyega to maza aa jyega
yes i finally i am able to do it :-
class Solution
{
public:
//Function to find the largest number after k swaps.
string maxi = "";
void solve(int idx , string &str , int k)
{
if ( idx == str.size()-1 or k==0){
maxi = max(maxi,str);
return;
}
for(int i = idx+1 ; i < str.size();i++)
{
char maxiChar = *max_element(str.begin()+i,str.end());
if(str[i] > str[idx] && str[i]==maxiChar)
{
swap(str[i],str[idx]);
solve(idx+1,str,k-1);
swap(str[i],str[idx]);
}
}
//agar loop se kaam nhi hua
solve(idx+1,str,k);
}
string findMaximumNum(string str, int k)
{
string temp = "";
solve(0,str,k);
return maxi;
}
};
watching this video from mata mandir aditya bhiya know about this place
CEO of voice recording
those who know why it isnt greedy watch from 08:24
Bhaiya sequence pattern matching ka recursive version jo aapne 31 video of dp m kha tha upload krne ko plz upload it 🛑🛑🛑🛑
Did not understood why it's not a greedy choice.
If we have 1 swap and number is 4577
then you will always swap 4 with last 7 then only the largest number comes.
Ex:
4577 => 7574
if we swap with first 7,
4577 => 7547 which is not largest.
So you will always swap with last largest no.
Please correct me if i missed something
he is saying if there are only 2 swaps
4577
-------- with greedy
1st swap (4 with first 7) - 7547
2nd swap( 5 with 7) - 7745
-------- with backtracking(backtracking considers all the combinations)
-1st combination
1st swap (4 with first 7) - 7547
2nd swap( 5 with 7) - 7745
-2nd combination
1st swap (4 with second 7) - 7574
2nd swap( 5 with 7) - 7754 ***
now with backtracking we are able to find the largest
I think this is it , but I don't have much knowledge about greedy so I may also be wrong.
kisi pe problem link ho toh plz dena. thanks in advance :)
Hi when will you complete the dp series?
Aapne bhaiya bhaut ache se explain kiya especially that similarity and dissimilarity ig no can can explain like this in this whole youtube community
Man we need more people like you. For any DSA topic I first search it with your name. Thanks for all your efforts. 🔥🔥🔥🔥
please upload the next video
I like your videos a lot, I have a constructive feedback: same explanation is repeated multiple times which makes the video longer than it should be.
Again amazing video well explained 🔥🔥 or ha biyaah nhi hua haii😂🤣
Jb maine phele question video ke baad yeh video dekha kya gajab ka smjh aya 💯💯
@Aditya Verma please complete the DP series as well 🙏🙏🙏
Can you please make a video in ENGLISH
Sare problem tumne solve ni kiya 😢
Adi bhaiya at this point I doubt kisi aur youtuber ko DSA aata bhbi tha ya nahi 🥲
bhaiya please DP series complete kardo
14:17 was funny "fhaaltu bakwaas hai"😂
bhaiya please DP series complete kardo
Kese ne notes banae hai kya ?
Please share kar do
Can you explain in English or add English subtitles so that non Hindi people like me can understand 😊
Best Line - 22:32 🤣🤣
Bade dharmik vyakti ho @Aditya Verma. Backtracking me bh dharam ki baat.
Please continue to make vedios.. Strings , Queues , Trees Please cover this topic please
videos* :)
bhaiya i have one question, so i have started cp and getting quite good at it(currently at dp). i am studying bsc cs third year I was searching for the internship online and I am see everywhere that they mostly require some kind of framework more than the coding skill I am losing motivation to study cp tbh, also quite confused with this things.
I want to get a job programming job please can you help with this? ik that cp is important and all for efficient and good coding skills but I am confused now.
same with me too
bro maintain both like learn atleast one web framework like React,Angular.
Bhaiya roz ya alternate days video daala karo plss..
Finally legend back 🎉
Hi again😍
Tree Tree Tree Please please please
Plant a seed :)
Biyah kar liya DSA ke sath, Unsubscribe kardu kya?
😅😅
Is this question on leetcode?
geeksforgeeks
class Solution{
private:
void solve(int start , string &s , int k , string &ans){
if(k==0 || start==s.size()){
ans = max(ans , s);
return;
}
char maxVal = s[start];
for(int j=start+1; j
bro why did u add that no swapping statement. Can u explain ?
@@aryanmittal5676
statements Means : Move on to the next index (start+1) without making a swap at the current index.
Explanation :
The following code for exploring the possibilties for swap :
for(int i=start+1; i s[start] && s[i]==maxVal){
swap(s[start] , s[i]);
solve(start+1 , s , k-1 , ans);
swap(s[start] , s[i]);
}
}
lets suppose the if condition inside the forloop is not true for a atleast once then our recursion is not calling for next start+1.
that case is s[start] > s[start+1,....n] then there no need to swap the s[start] with s[start+1 ,...n] so the problem is how can i call the recursion without making swap , so to do so we call the recursion with the line solve(start+1 , s , k , ans); // no swappting.
@@naveedwaddo8817 Thanks man for the answer
Tu jinda he