Maximum Value after Insertion solution leetcode

Maximum Value after Insertion solution leetcode

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.

Maximum Value after Insertion solution leetcode

Example 1: Maximum Value after Insertion solution leetcode

```Input: n = "99", x = 9
Output: "999"
Explanation: The result is the same regardless of where you insert 9.
```

Example 2: Maximum Value after Insertion solution leetcode

```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.
```

Maximum Value after Insertion solution leetcode

Constraints: Maximum Value after Insertion solution leetcode

• `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 `'-'`.

SOLUTION

Welcome to the 243th LeetCode Weekly Contest

Important Note

1. To provide a better contest and ensure fairness, we listened to LeetCoders’ feedback and put in lots of thoughts behind the updated contest rule. Please check out our new contest rule which covers more scenarios with details explained.
2. The penalty time of 5 minutes will be applied for each wrong submission.
3. To ensure the fairness of the contest, LeetCode will hide some test cases during the contest. When users submit incorrect submissions, LeetCode will not show the hidden test cases to the users.
4. The final rating of this contest will be updated within 5 working days after the contest.

Below actions are deemed contest violations

1. :

• One user submitting with multiple accounts during a contest. LCUS (leetcode.com) account and LCCN (leetcode-cn.com) account are considered to be separate accounts, even if both accounts belong to the same user.
• Multiple accounts submitting similar code for the same problem.
• Creating unwanted disturbances which interrupt other users’ participation in a contest.
• Disclosing contest solutions in public discuss posts before the end of a contest.
1. LeetCode heavily emphasizes on the justice and fairness of our contests. We have absolutely

ZERO TOLERANCE

1.  for violation behaviors (such as plagiarism, cheating, etc). When a user is deemed violating contest rules, we will apply the following penalties on this user:

• First violation: LeetCoin amount resets to zero and a contest and discuss ban for 1 month.
• Second violation: Permanent account deactivation without appeal.
Furthermore, we encourage all participants to contribute to maintaining the justice and fairness of our contest. Users who submit valid violation report(s) will earn additional LeetCoins:

• For each violating participant, the first 10 users who submit the violation report towards this participant will each earn 20 LeetCoins.
• Each user can earn up to 100 LeetCoins for reporting violations in a contest.
• Users will not be rewarded LeetCoins for reports on LCCN users.