1881. Maximum Value after Insertion
Description
You are given a very large integer n
, represented as a string, and an integer digit x
. The digits in n
and the digit x
are in the inclusive range [1, 9]
, and n
may represent a negative number.
You want to maximize n
's numerical value by inserting x
anywhere in the decimal representation of n
. You cannot insert x
to the left of the negative sign.
- For example, if
n = 73
andx = 6
, it would be best to insert it between7
and3
, makingn = 763
. - If
n = -55
andx = 2
, it would be best to insert it before the first5
, makingn = -255
.
Return a string representing the maximum value of n
after the insertion.
Example 1:
Input: n = "99", x = 9 Output: "999" Explanation: The result is the same regardless of where you insert 9.
Example 2:
Input: n = "-13", x = 2 Output: "-123" Explanation: You can make n one of {-213, -123, -132}, and the largest of those three is -123.
Constraints:
1 <= n.length <= 105
1 <= x <= 9
- The digits in
n
are in the range[1, 9]
. n
is a valid representation of an integer.- In the case of a negative
n
, it will begin with'-'
.
Solutions
Solution 1: Greedy
If $n$ is negative, we need to find the first position greater than $x$ and insert $x$ at that position. If $n$ is positive, we need to find the first position less than $x$ and insert $x$ at that position.
The time complexity is $O(m)$, where $m$ is the length of $n$. The space complexity is $O(1)$.
Python3
class Solution:
def maxValue(self, n: str, x: int) -> str:
i = 0
if n[0] == "-":
i += 1
while i < len(n) and int(n[i]) <= x:
i += 1
else:
while i < len(n) and int(n[i]) >= x:
i += 1
return n[:i] + str(x) + n[i:]
Java
class Solution {
public String maxValue(String n, int x) {
int i = 0;
if (n.charAt(0) == '-') {
++i;
while (i < n.length() && n.charAt(i) - '0' <= x) {
++i;
}
} else {
while (i < n.length() && n.charAt(i) - '0' >= x) {
++i;
}
}
return n.substring(0, i) + x + n.substring(i);
}
}
C++
class Solution {
public:
string maxValue(string n, int x) {
int i = 0;
if (n[0] == '-') {
++i;
while (i < n.size() && n[i] - '0' <= x) {
++i;
}
} else {
while (i < n.size() && n[i] - '0' >= x) {
++i;
}
}
n.insert(i, 1, x + '0');
return n;
}
};
Go
func maxValue(n string, x int) string {
i := 0
y := byte('0' + x)
if n[0] == '-' {
i++
for i < len(n) && n[i] <= y {
i++
}
} else {
for i < len(n) && n[i] >= y {
i++
}
}
return n[:i] + string(y) + n[i:]
}
TypeScript
function maxValue(n: string, x: number): string {
let i = 0;
if (n[0] === '-') {
i++;
while (i < n.length && +n[i] <= x) {
i++;
}
} else {
while (i < n.length && +n[i] >= x) {
i++;
}
}
return n.slice(0, i) + x + n.slice(i);
}
Rust
impl Solution {
pub fn max_value(n: String, x: i32) -> String {
let s = n.as_bytes();
let mut i = 0;
if n.starts_with('-') {
i += 1;
while i < s.len() && (s[i] - b'0') as i32 <= x {
i += 1;
}
} else {
while i < s.len() && (s[i] - b'0') as i32 >= x {
i += 1;
}
}
let mut ans = String::new();
ans.push_str(&n[0..i]);
ans.push_str(&x.to_string());
ans.push_str(&n[i..]);
ans
}
}
JavaScript
/**
* @param {string} n
* @param {number} x
* @return {string}
*/
var maxValue = function (n, x) {
let i = 0;
if (n[0] === '-') {
i++;
while (i < n.length && +n[i] <= x) {
i++;
}
} else {
while (i < n.length && +n[i] >= x) {
i++;
}
}
return n.slice(0, i) + x + n.slice(i);
};