Maximize a Number with One Swap – LeetCode 670 Solution in Java, C++, Python, TypeScript
The article first examines how large tech companies are shifting away from short‑term performance cycles to encourage long‑term strategic work, then presents LeetCode problem 670 “Maximum Swap” with detailed explanations and implementations in Java, C++, Python, and TypeScript, including complexity analysis.
Performance
Performance is an important metric for measuring an employee's contribution. However, overly short performance cycles can lead to short‑termism, where people chase KPI completion rates and over‑package reports, neglecting long‑term value. Therefore, many large companies have cancelled half‑year or quarterly reviews to reduce “performance involution” and encourage focus on strategic, long‑term tasks.
Recently, Alibaba Cloud and ByteDance’s Seed have eliminated quarterly and half‑year assessments, and Ant Group is trialing the removal of half‑year performance scores. This shift transforms performance management from a control tool into a strategic navigation, reducing internal competition and freeing innovation.
What are your thoughts? How often does your company evaluate performance?
Problem Description
Platform: LeetCode
Problem 670: Given a non‑negative integer, you may swap any two digits at most once. Return the maximum value you can obtain.
Example 1: Input 2736 → Output 7236 (swap 2 and 7).
Example 2: Input 9973 → Output 9973 (no swap needed).
Simulation
To maximize the number, place larger digits in higher positions. When multiple equal largest digits exist, choose the one at the lower index. The algorithm extracts each digit into an array, pre‑processes an auxiliary array that records the index of the maximum digit to the right, then scans from the most significant digit to find the first position where a larger digit exists to swap.
Java Solution
class Solution {
public int maximumSwap(int num) {
List<Integer> list = new ArrayList<>();
while (num != 0) {
list.add(num % 10);
num /= 10;
}
int n = list.size(), ans = 0;
int[] idx = new int[n];
for (int i = 0, j = 0; i < n; i++) {
if (list.get(i) > list.get(j)) j = i;
idx[i] = j;
}
for (int i = n - 1; i >= 0; i--) {
if (list.get(idx[i]) != list.get(i)) {
int c = list.get(idx[i]);
list.set(idx[i], list.get(i));
list.set(i, c);
break;
}
}
for (int i = n - 1; i >= 0; i--) ans = ans * 10 + list.get(i);
return ans;
}
}C++ Solution
class Solution {
public:
int maximumSwap(int num) {
vector<int> list;
while (num != 0) {
list.push_back(num % 10);
num /= 10;
}
int n = list.size(), ans = 0;
int* idx = new int[n];
for (int i = 0, j = 0; i < n; i++) {
if (list[i] > list[j]) j = i;
idx[i] = j;
}
for (int i = n - 1; i >= 0; i--) {
if (list[idx[i]] != list[i]) {
int c = list[idx[i]];
list[idx[i]] = list[i];
list[i] = c;
break;
}
}
for (int i = n - 1; i >= 0; i--) ans = ans * 10 + list[i];
return ans;
}
};Python Solution
class Solution:
def maximumSwap(self, num: int) -> int:
nums = []
while num != 0:
nums.append(num % 10)
num //= 10
n, ans = len(nums), 0
idx = [0] * n
i, j = 0, 0
while i < n:
if nums[i] > nums[j]:
j = i
idx[i] = j
i += 1
for i in range(n - 1, -1, -1):
if nums[idx[i]] != nums[i]:
c = nums[idx[i]]
nums[idx[i]] = nums[i]
nums[i] = c
break
for i in range(n - 1, -1, -1):
ans = ans * 10 + nums[i]
return ansTypeScript Solution
function maximumSwap(num: number): number {
const list: number[] = [];
while (num != 0) {
list.push(num % 10);
num = Math.floor(num / 10);
}
const n = list.length;
const idx: number[] = [];
let j = 0;
for (let i = 0; i < n; i++) {
if (list[i] > list[j]) j = i;
idx.push(j);
}
for (let i = n - 1; i >= 0; i--) {
if (list[idx[i]] !== list[i]) {
const c = list[idx[i]];
list[idx[i]] = list[i];
list[i] = c;
break;
}
}
let result = 0;
for (let i = n - 1; i >= 0; i--) result = result * 10 + list[i];
return result;
}Complexity
Time complexity: O(n), where n is the number of digits.
Space complexity: O(n) for the digit list and auxiliary index array.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
