# LeetCode C++ 1881. Maximum Value after Insertion

Posted by curioadmin on Sat, 05 Feb 2022 11:34:33 +0100

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 and x = 6, it would be best to insert it between 7 and 3, making n = 763.
• If n = -55 and x = 2, it would be best to insert it before the first 5, making n = -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 '-'.

Give you a very large integer n and an integer number X. the large integer n ， is represented by a string. Each digit in N and the number x are in the closed interval [1,9], and N may represent a negative number.

You intend to maximize the value of N by inserting x anywhere in the decimal representation of n. But you cannot insert an X to the left of the minus sign. Returns the maximum value of N expressed as a string after the insert operation.

### Solution greed

Greedy insert string x into n. Specifically, when n is a positive number, to maximize the result, you must traverse from high to low (from left to right). When a character is less than x, insert x into that position; If the character is equal to x, it is uncertain whether there is a character less than X after it (if any, it should be inserted at that position, which may make the whole number smaller), so it is not inserted; If there is no such character at the end, it is inserted at the end of the string.

When n is a negative number, to maximize the result, you must traverse from high to low (from left to right, starting from 1 subscript). When a character is greater than x, insert x into the position; If the character is equal to x, it is uncertain whether there is a character greater than X after it (if any, it should be inserted at that position, which may make the whole number smaller), so it is not inserted; If there is no such character at the end, it is inserted at the end of the string.

However, the above practices are limited to the case where x has only one digit. When x is also an integer with more than one digit, the code should be written as follows:

```class Solution {
public:
string maxValue(string n, int x) {
string sx = to_string(x);
bool flag = n[0] == '-';
int xl = sx.size(), i = flag;
for (int nl = n.size(); i < nl; ++i) {
string s = n.substr(i, xl);
if ((flag && s > sx) || (!flag && s < sx))
return n.substr(0, i) + sx + n.substr(i); //negative
}
return n + sx;
}
};
```

The operating efficiency is as follows:

```Execution time: 84 ms, At all C++ Defeated 26 in submission.33% User
Memory consumption: 33.9 MB, At all C++ Defeated 51 in submission.03% User
```

Since X has only one digit, compare n[i] with x[0]:

```class Solution {
public:
string maxValue(string n, int x) {
bool flag = n[0] == '-';
for (int i = flag, nl = n.size(); i < nl; ++i)
if ((flag && n[i] > x + '0') || (!flag && n[i] < x + '0'))
return n.substr(0, i) + to_string(x) + n.substr(i); //negative
return n + to_string(x);
}
};
```

The operating efficiency is as follows:

```Execution time: 72 ms, At all C++ Defeated 68 in submission.40% User
Memory consumption: 33.4 MB, At all C++ Defeated 60 in submission.88% User
```