10 Largest number in K swaps Code and Time Complexity
HTML-код
- Опубликовано: 18 сен 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.
please make a playlist on trees and graphs
correction the max element we need to find is
char max = *max_element(str.begin()+start,str.end())🙃
It's a humble request to bhaiya
Please make playlist on trees,array and strings.....
I found at your channel in sliding window playlist it was amazing by seeing the problem im able to solve...
Thank you so much 👍❤
Your teaching was perfect 😊
Sir waiting for trees and graphs!! sb achi companies graph puch rhi hai
Yes sir he is correct
Not comparing with anyone, Aditya bhai ka andaz hi alg hai
Vote for graphs playlist 👆 . Ab to hamara dard sunle 😢
Tree and graphs plz ❤
@TheAdityaVerma bhai you are awesome man!
Thanks a lot for the DP playlist ❣️
Thanks a lot for sharing your coding experience learning so well. I really appreciate your effort. Your way of tackling questions are awesome. Very nice!!.
Please try to complete all backtracking questions as soon as possible (its a kind request to you).
One more request: If possible please choose Tree and Graph also. (Tree first)
There is virat in cricket
And there is Aditya Verma sir for DSA👑
thank you.
Great lecture.
Please complete the remaining dp playlist variations of LIS , kadanes ,grid nd rest others . waiting for it so much long time
Back in Business 😤🔥
Finally!
Sir, please add TC in the next videos as well
month mai ek video daalre ho ,kuch to socho bhiaiya hmare baare mai
Wo Basics of Backtracking wali video ke Outro mai "subscribe if you want girlfriend" tha, Subscribe toh kar diya, wo girlfriend bhi mil jati to acha rehta, graph ki playlist 4-5 saal delay bhi ho jaye to chalega 😶
He will be with us ❤ only
amazing video
thanks a lot
Graph please
🙌
sir stack ki playlist please complete kar dijiye
Who all think he was dead......
I searched in google today is he dead than saw he uploaded latest video 2 months ago....
This is the correct time he has arrived ...
Lagtha hai bhagwaan ne ek farishtha beja hai, verma bhai muje appke sath kam karna hai.... I am coming flipkart next year placement hai.....
Sir aak din live Instagram Or RUclips
Graph and tree please
C++ code:
class Solution
{
public:
void solve(string &str, int k, int start, string &ans){
if(k==0 || start==str.size()-1){
return;
}
char maxs= *max_element(str.begin()+start,str.end());
for(int i=start+1;i
Max_element main start+1 hoga right?
Can we have the base condition as k==0 or all the numbers in the result string to be in decreasing order??
I'm not able to intuitively understand the need for the start variable to go all the way to the end of the input string.
Any help/comment on the same is greatly appreciated.
Thanks in advance!!!
Consider the example 7512, and let's say k=2. In this case we only need one swap, swap 1 and 2 and we get the largest number 7521, we did not even need 2 swaps, so in cases like these k will never reduce to zero and the recursion would never stop, that is why the other base condition is added.
I think k swap using inplace insertion sort would be better for this
how to make recursion strong i am facing issues in recursion
Aditya verma sir live ajao ek baar ❤
what will be the equivalent function for max_element() in JAVA? Can anyone help me?
max()
char maxs = str.substring(start).chars()
.mapToObj(c -> (char) c)
.max(Character::compareTo)
.orElse(Character.MIN_VALUE);
Subscribed in the name of Bengali Girls
Is it only me who feels that the volume is low?