Maximum Valued Number After Swap
Maximum valued number achieved by swapping digits at most once: swap 2 and 7 in 2736 to get 7236.
670. Maximum Swap Difficulty: Medium Topics: Math, Greedy You are given an integer num. You can swap two digits at most once to get the maximum valued number. Return the maximum valued number you can get. Example 1: Input: num = 2736 Output: 7236 Explanation: Swap the number 2 and the number 7. Example 2: Input: num = 9973 Output: 9973 Explanation: No swap. Constraints: 0 <= num <= 108 Solution: We can follow a greedy approach. Here's a step-by-step explanation and the solution: Approach: Convert the Number to an Array: Since digits need to be swapped, converting the number in...