You are given a string representation of an integer n
(which may be negative), and a digit x
(0 ≤ x ≤ 9). Your task is to insert x
anywhere in the string n
such that the resulting integer (after insertion) is maximized. The function should return the resulting string.
n
is a string, which can be positive or negative (may start with a minus sign).x
at any position (including at the beginning or end).Constraints:
n.length
≤ 105n
consists of digits, and may start with a minus sign for negative numbers.x
≤ 9
At first glance, you might consider trying every possible position to insert x
and then comparing the resulting numbers to find the maximum. However, with large input sizes, this brute-force approach would be inefficient.
Instead, we can observe that to maximize the value:
n
is positive, we want x
to appear as early as possible, but only before a digit that is less than x
.n
is negative, the number becomes less negative (and thus larger) if we insert x
before the first digit that is greater than x
.n
once and decide the optimal insertion point without generating all possibilities.
Let's break down the solution step by step:
n
is negative:
n
starts with '-', treat it as a negative number and process accordingly.x
before the first digit that is less than x
.
x
before the first digit that is greater than x
.
x
at the end of the string.x
at the identified position.This approach ensures only one pass through the string, making it efficient for large inputs.
Example 1:
n = "99"
, x = 9
x
.x
, so insert x
at the end."999"
Example 2:
n = "-13"
, x = 2
x
(2), so keep scanning.x
(2).x
before 3: "-123"
Example 3:
n = "235"
, x = 4
"4235"
Brute-force approach:
This makes the optimized approach efficient and scalable.
To maximize the value after inserting a digit into a number string, we scan the string once to find the optimal insertion point based on whether the number is positive or negative. This avoids unnecessary computation and leverages the properties of number ordering. The solution is elegant, efficient, and handles all edge cases, making it suitable for large inputs.
def maxValue(n: str, x: int) -> str:
x = str(x)
if n[0] == '-':
# Negative number: insert x before first digit > x
for i in range(1, len(n)):
if n[i] > x:
return n[:i] + x + n[i:]
return n + x
else:
# Positive number: insert x before first digit < x
for i in range(len(n)):
if n[i] < x:
return n[:i] + x + n[i:]
return n + x
class Solution {
public:
string maxValue(string n, int x) {
char c = x + '0';
if (n[0] == '-') {
// Negative number: insert x before first digit > x
for (int i = 1; i < n.size(); ++i) {
if (n[i] > c) {
return n.substr(0, i) + c + n.substr(i);
}
}
return n + c;
} else {
// Positive number: insert x before first digit < x
for (int i = 0; i < n.size(); ++i) {
if (n[i] < c) {
return n.substr(0, i) + c + n.substr(i);
}
}
return n + c;
}
}
};
class Solution {
public String maxValue(String n, int x) {
char c = (char) (x + '0');
if (n.charAt(0) == '-') {
// Negative number: insert x before first digit > x
for (int i = 1; i < n.length(); ++i) {
if (n.charAt(i) > c) {
return n.substring(0, i) + c + n.substring(i);
}
}
return n + c;
} else {
// Positive number: insert x before first digit < x
for (int i = 0; i < n.length(); ++i) {
if (n.charAt(i) < c) {
return n.substring(0, i) + c + n.substring(i);
}
}
return n + c;
}
}
}
var maxValue = function(n, x) {
x = x.toString();
if (n[0] === '-') {
// Negative number: insert x before first digit > x
for (let i = 1; i < n.length; ++i) {
if (n[i] > x) {
return n.slice(0, i) + x + n.slice(i);
}
}
return n + x;
} else {
// Positive number: insert x before first digit < x
for (let i = 0; i < n.length; ++i) {
if (n[i] < x) {
return n.slice(0, i) + x + n.slice(i);
}
}
return n + x;
}
};