Brute force class Solution { public int maximumSwap(int num) { int maxnum=num; String str=Integer.toString(num); StringBuilder sb=new StringBuilder(str); for(int i=0;i
class Solution { public void swap(StringBuilder sb,int i,int j) { char temp = sb.charAt(i); sb.setCharAt(i,sb.charAt(j)); sb.setCharAt(j,temp); } public int maximumSwap(int num) { StringBuilder sb = new StringBuilder(Integer.toString(num)); boolean check = false; for(int i = 0; i < sb.length(); i++) { for(int j = i+1; j < sb.length(); j++) { int ch1 = (int)sb.charAt(i); int ch2 = (int)sb.charAt(j); if(ch2 > ch1) { int k = j+1; int max = j; while(k < sb.length()) { int ch3 = (int)sb.charAt(k); if(ch3 >= ch2) { max = k; ch2 = ch3; } k++; } swap(sb,i,max); check = true; break; } } if(check) { break; } } return Integer.parseInt(sb.toString()); } } this is the brute force approche by which i have solved this quesiton
class Solution{ public: int maximumSwap(int num) { string numStr = to_string(num); int maxNum = num; // original number // Try swapping every pair of digits for (int i = 0; i < numStr.size(); ++i) { for (int j = i + 1; j < numStr.size(); ++j) { // Swap digits swap(numStr[i], numStr[j]); // Convert back to an integer and check if it's the largest seen so far maxNum = max(maxNum, stoi(numStr)); // Swap back to restore the original order swap(numStr[i], numStr[j]); } } return maxNum; } };
Brute Force approach : class Solution { public static void swap(char [] arr , int i , int j){ char temp = arr[i]; arr[i]= arr[j]; arr[j]=temp; } public int maximumSwap(int num) { int maxNum=num;
char [] numArr = Integer.toString(num).toCharArray(); int n = numArr.length;
C++ Code: class Solution { public: int maximumSwap(int num) { string temp = to_string(num); int n = temp.size(); int maxEle = n - 1; int j = -1, k = -1; for (int i = n-2; i >= 0; i--) { if (temp[i] > temp[maxEle]) { maxEle = i; } else if (temp[i] < temp[maxEle]) { j = i; k = maxEle; } } if (j == -1 || k == -1) return num; swap(temp[j], temp[k]); return stoi(temp); } };
O(N^2) code - vector vec; int n = num; while(n){ vec.push_back(n%10); n/=10; } reverse(vec.begin(), vec.end()); int m = vec.size(); bool largerfound = false; int largerindex = -1; for(int i = 0; i
java solution || 0ms || 100% beats || Easy Solution|| public static int maximumSwap(int num) { String str = String.valueOf(num); int[] arr = new int[str.length()]; for (int i = 0; i < arr.length; i++) { arr[i] = str.charAt(i) - '0'; } int swap = 0; for (int i = 0; i < arr.length - 1; i++) { if (swap == 1) { break; } int max = findMax(i + 1, arr); if (arr[i] < arr[max]) { int temp = arr[i]; arr[i] = arr[max]; arr[max] = temp; swap++; } } int result = 0; for (int i = 0; i < arr.length; i++) { result = result * 10 + arr[i]; } return result; } public static int findMax(int start, int[] arr) { int max = start; for (int i = start; i < arr.length; i++) { if (arr[i] >= arr[max]) { max = i; } } return max; }
o(N) class Solution { public: int maximumSwap(int num) { vector vec; int n = num; while (n) { vec.push_back(n % 10); n /= 10; } reverse(vec.begin(), vec.end());
int m = vec.size(); vector rightmax(m, 0); rightmax[m - 1] = m - 1; for (int i = m - 2; i >= 0; i--) { if (vec[i] > vec[rightmax[i + 1]]) { rightmax[i] = i; } else { rightmax[i] = rightmax[i + 1]; } } for (int i = 0; i < m; i++) { if (vec[rightmax[i]] != vec[i]) { swap(vec[i], vec[rightmax[i]]); break; } } int ans = 0; for (int i = 0; i < m; i++) { ans = ans * 10 + vec[i]; } return ans; } };
Like target is 150. Please do like if you have understood the explanation as well as the code
great , I used the same solution
class Solution {
public:
int maximumSwap(int num) {
string arr=to_string(num);
int n=arr.size();
int i=0;
while(ii;j--){
if(arr[j]>maxi){
maxi=arr[j];
maxindex=j;
}
}
if(maxindex!=i){
swap(arr[i],arr[maxindex]);
break;
}
i++;
}
return stoi(arr);
}
};
Thanks a lot. I was confused, but you cleared my doubt, this was the best solution video and was easy to understand. I've subscribed.❤
brute force in java
String temp=Integer.toString(num);
int n=temp.length();
int max=num;
for(int i=0;i
class Solution {
public:
int maximumSwap(int num) {
int ans=num;
string str=to_string(num);
int n=str.size();
for(int i=0; i
"Brute Force Beats 100%"
class Solution {
public int maximumSwap(int num) {
String s=Integer.toString(num);
int n=s.length();
char ch[]=s.toCharArray();
int maxi=-1;
int index=-1;
boolean tag=false;
for(int i=0;i=maxi){
maxi=ch[j];
index=j;
}
}
}
if(tag){
char temp=ch[i];
ch[i]=ch[index];
ch[index]=temp;
num=Integer.parseInt(new String(ch));
break;
}
}
return num;
}
}
Thanks i was able to solve this today by myself
thankyou so much sir :))
Brute force
class Solution {
public int maximumSwap(int num) {
int maxnum=num;
String str=Integer.toString(num);
StringBuilder sb=new StringBuilder(str);
for(int i=0;i
class Solution:
def maximumSwap(self, num: int) -> int:
arr=[]
while num>0:
arr.insert(0, num%10)
num//=10
i=0
while i
Bro can you do dp playlist I think this is left topic out of all your playlist
class Solution {
public void swap(StringBuilder sb,int i,int j)
{
char temp = sb.charAt(i);
sb.setCharAt(i,sb.charAt(j));
sb.setCharAt(j,temp);
}
public int maximumSwap(int num) {
StringBuilder sb = new StringBuilder(Integer.toString(num));
boolean check = false;
for(int i = 0; i < sb.length(); i++)
{
for(int j = i+1; j < sb.length(); j++)
{
int ch1 = (int)sb.charAt(i);
int ch2 = (int)sb.charAt(j);
if(ch2 > ch1)
{
int k = j+1;
int max = j;
while(k < sb.length())
{
int ch3 = (int)sb.charAt(k);
if(ch3 >= ch2)
{
max = k;
ch2 = ch3;
}
k++;
}
swap(sb,i,max);
check = true;
break;
}
}
if(check)
{
break;
}
}
return Integer.parseInt(sb.toString());
}
}
this is the brute force approche by which i have solved this quesiton
class Solution{
public:
int maximumSwap(int num) {
string numStr = to_string(num);
int maxNum = num; // original number
// Try swapping every pair of digits
for (int i = 0; i < numStr.size(); ++i) {
for (int j = i + 1; j < numStr.size(); ++j) {
// Swap digits
swap(numStr[i], numStr[j]);
// Convert back to an integer and check if it's the largest seen so far
maxNum = max(maxNum, stoi(numStr));
// Swap back to restore the original order
swap(numStr[i], numStr[j]);
}
}
return maxNum;
}
};
Brute Force approach :
class Solution {
public static void swap(char [] arr , int i , int j){
char temp = arr[i];
arr[i]= arr[j];
arr[j]=temp;
}
public int maximumSwap(int num) {
int maxNum=num;
char [] numArr = Integer.toString(num).toCharArray();
int n = numArr.length;
for (int i=0 ; i
C++ Code:
class Solution {
public:
int maximumSwap(int num) {
string temp = to_string(num);
int n = temp.size();
int maxEle = n - 1;
int j = -1, k = -1;
for (int i = n-2; i >= 0; i--) {
if (temp[i] > temp[maxEle]) {
maxEle = i;
} else if (temp[i] < temp[maxEle]) {
j = i;
k = maxEle;
}
}
if (j == -1 || k == -1) return num;
swap(temp[j], temp[k]);
return stoi(temp);
}
};
O(N^2) code -
vector vec;
int n = num;
while(n){
vec.push_back(n%10);
n/=10;
}
reverse(vec.begin(), vec.end());
int m = vec.size();
bool largerfound = false;
int largerindex = -1;
for(int i = 0; i
java solution || 0ms || 100% beats || Easy Solution||
public static int maximumSwap(int num) {
String str = String.valueOf(num);
int[] arr = new int[str.length()];
for (int i = 0; i < arr.length; i++) {
arr[i] = str.charAt(i) - '0';
}
int swap = 0;
for (int i = 0; i < arr.length - 1; i++) {
if (swap == 1) {
break;
}
int max = findMax(i + 1, arr);
if (arr[i] < arr[max]) {
int temp = arr[i];
arr[i] = arr[max];
arr[max] = temp;
swap++;
}
}
int result = 0;
for (int i = 0; i < arr.length; i++) {
result = result * 10 + arr[i];
}
return result;
}
public static int findMax(int start, int[] arr) {
int max = start;
for (int i = start; i < arr.length; i++) {
if (arr[i] >= arr[max]) {
max = i;
}
}
return max;
}
o(N)
class Solution {
public:
int maximumSwap(int num) {
vector vec;
int n = num;
while (n) {
vec.push_back(n % 10);
n /= 10;
}
reverse(vec.begin(), vec.end());
int m = vec.size();
vector rightmax(m, 0);
rightmax[m - 1] = m - 1;
for (int i = m - 2; i >= 0; i--) {
if (vec[i] > vec[rightmax[i + 1]]) {
rightmax[i] = i;
} else {
rightmax[i] = rightmax[i + 1];
}
}
for (int i = 0; i < m; i++) {
if (vec[rightmax[i]] != vec[i]) {
swap(vec[i], vec[rightmax[i]]);
break;
}
}
int ans = 0;
for (int i = 0; i < m; i++) {
ans = ans * 10 + vec[i];
}
return ans;
}
};