1881. Maximum Value after Insertion Medium

@problem@discussion
#String#Greedy



1/**
2 * [1881] Maximum Value after Insertion
3 *
4 * 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.
5 * 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.
6 * 
7 * 	For example, if n = 73 and x = 6, it would be best to insert it between 7 and 3, making n = 763.
8 * 	If n = -55 and x = 2, it would be best to insert it before the first 5, making n = -255.
9 * 
10 * Return a string representing the maximum value of n​​​​​​ after the insertion.
11 *  
12 * Example 1:
13 * 
14 * Input: n = "99", x = 9
15 * Output: "999"
16 * Explanation: The result is the same regardless of where you insert 9.
17 * 
18 * Example 2:
19 * 
20 * Input: n = "-13", x = 2
21 * Output: "-123"
22 * Explanation: You can make n one of {-213, -123, -132}, and the largest of those three is -123.
23 * 
24 *  
25 * Constraints:
26 * 
27 * 	1 <= n.length <= 10^5
28 * 	1 <= x <= 9
29 * 	The digits in n​​​ are in the range [1, 9].
30 * 	n is a valid representation of an integer.
31 * 	In the case of a negative n,​​​​​​ it will begin with '-'.
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/maximum-value-after-insertion/
37// discuss: https://leetcode.com/problems/maximum-value-after-insertion/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn max_value(n: String, x: i32) -> String {
43        String::new()
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_1881() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.