<p>You are given an integer <code>num</code>. You can swap two digits at most once to get the maximum valued number.</p>

<p>Return <em>the maximum valued number you can get</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = 2736
<strong>Output:</strong> 7236
<strong>Explanation:</strong> Swap the number 2 and the number 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = 9973
<strong>Output:</strong> 9973
<strong>Explanation:</strong> No swap.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= num &lt;= 10<sup>8</sup></code></li>
</ul>

class Solution {
public:
    int maximumSwap(int num) {
        string s = to_string(num);
        int n = s.size();
        char maxi = '0';
        int maxi_idx= -1 ;
        vector<int>maxi_after(n , -1);
        for(int i = n-1 ; i >= 0 ; i--)
        {
            if(s[i] > maxi)
            {
                maxi = s[i];
                maxi_after[i] = -1;
                maxi_idx = i;
            }
            else if(s[i] == maxi)
            {
                maxi_after[i] = -1;
            }
            else
            {
                maxi_after[i] = maxi_idx;
            }
        }
        for(int i = 0 ; i < n ; i++)
        {
            cout<<maxi_after[i]<<" ";
        }
        cout<<endl;
        for(int i = 0 ; i < n ; i++)
        {
            if(maxi_after[i] != -1)
            {
                cout<<"i = "<<i<<endl;
                cout<<"maxi_idx = "<<maxi_after[i]<<endl;
                char temp = s[i];
                s[i] = s[maxi_after[i]];
                s[maxi_after[i]]= temp;
                break;
            }
        }
        return stoi(s);
    }
};
7600