670. Maximum Swap | Leetcode Daily Challenge | DSA | Java | FAANG

Поделиться
HTML-код
  • Опубликовано: 23 дек 2024

Комментарии •

  • @shashwat_tiwari_st
    @shashwat_tiwari_st  2 месяца назад +11

    Like target is 150. Please do like if you have understood the explanation as well as the code

  • @rakshatpratapsingh6404
    @rakshatpratapsingh6404 2 месяца назад +3

    great , I used the same solution

  • @dr.coffin16
    @dr.coffin16 2 месяца назад +1

    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);
    }
    };

  • @thelocalcruiser
    @thelocalcruiser 2 месяца назад +1

    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.❤

  • @ashishjoshi6450
    @ashishjoshi6450 2 месяца назад

    brute force in java
    String temp=Integer.toString(num);
    int n=temp.length();
    int max=num;
    for(int i=0;i

  • @shakir50
    @shakir50 2 месяца назад

    class Solution {
    public:
    int maximumSwap(int num) {
    int ans=num;
    string str=to_string(num);
    int n=str.size();
    for(int i=0; i

  • @AdityaSharma-ox9dv
    @AdityaSharma-ox9dv 2 месяца назад +1

    "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;
    }
    }

  • @manu-singh
    @manu-singh 2 месяца назад

    Thanks i was able to solve this today by myself

  • @ananyieie
    @ananyieie 2 месяца назад

    thankyou so much sir :))

  • @vedantk
    @vedantk 2 месяца назад +1

    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

  • @you.n.u.s
    @you.n.u.s 2 месяца назад

    class Solution:
    def maximumSwap(self, num: int) -> int:
    arr=[]
    while num>0:
    arr.insert(0, num%10)
    num//=10
    i=0
    while i

  • @democreation3594
    @democreation3594 2 месяца назад

    Bro can you do dp playlist I think this is left topic out of all your playlist

  • @debasishpradhan7375
    @debasishpradhan7375 2 месяца назад +1

    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

  • @rohitvermamnvtechcarvons2793
    @rohitvermamnvtechcarvons2793 2 месяца назад

    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;
    }
    };

  • @Amanbiragi17
    @Amanbiragi17 2 месяца назад +1

    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

  • @amitkumar10101
    @amitkumar10101 2 месяца назад

    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);
    }
    };

  • @vikashkansotiya2139
    @vikashkansotiya2139 2 месяца назад

    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

  • @vninfo.2739
    @vninfo.2739 2 месяца назад

    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;
    }

  • @vikashkansotiya2139
    @vikashkansotiya2139 2 месяца назад

    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;
    }
    };