Skip to content

Commit 732a776

Browse files
committed
+ problem 1881
1 parent 67bcee2 commit 732a776

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 1881. Maximum Value after Insertion
2+
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]`, a`nd` n may represent a **negative** number.
3+
4+
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.
5+
6+
* For example, if `n = 73` and `x = 6`, it would be best to insert it between `7` and `3`, making `n = 763`.
7+
* If `n = -55` and `x = 2`, it would be best to insert it before the first `5`, making `n = -255`.
8+
9+
Return *a string representing the **maximum** value of* `n` *after the insertion*.
10+
11+
#### Example 1:
12+
<pre>
13+
<strong>Input:</strong> n = "99", x = 9
14+
<strong>Output:</strong> "999"
15+
<strong>Explanation:</strong> The result is the same regardless of where you insert 9.
16+
</pre>
17+
18+
#### Example 2:
19+
<pre>
20+
<strong>Input:</strong> n = "-13", x = 2
21+
<strong>Output:</strong> "-123"
22+
<strong>Explanation:</strong> You can make n one of {-213, -123, -132}, and the largest of those three is -123.
23+
</pre>
24+
25+
#### Constraints:
26+
* <code>1 <= n.length <= 10<sup>5</sup></code>
27+
* `1 <= x <= 9`
28+
* The digits in `n` are in the range `[1, 9]`.
29+
* `n` is a valid representation of an integer.
30+
* In the case of a negative `n`, it will begin with `'-'`.
31+
32+
## Solutions (Rust)
33+
34+
### 1. Solution
35+
```Rust
36+
impl Solution {
37+
pub fn max_value(n: String, x: i32) -> String {
38+
let x = x as u8 + b'0';
39+
let mut n = n.into_bytes();
40+
41+
if n[0] != b'-' {
42+
for i in 0..=n.len() {
43+
if i == n.len() || x > n[i] {
44+
n.insert(i, x);
45+
break;
46+
}
47+
}
48+
} else {
49+
for i in 1..=n.len() {
50+
if i == n.len() || x < n[i] {
51+
n.insert(i, x);
52+
break;
53+
}
54+
}
55+
}
56+
57+
String::from_utf8(n).unwrap()
58+
}
59+
}
60+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 1881. 插入后的最大值
2+
给你一个非常大的整数 `n` 和一个整数数字 `x` ,大整数 `n` 用一个字符串表示。`n` 中每一位数字和数字 `x` 都处于闭区间 `[1, 9]` 中,且 `n` 可能表示一个 **负数**
3+
4+
你打算通过在 `n` 的十进制表示的任意位置插入 `x`**最大化** `n`**数值** 。但 **不能** 在负号的左边插入 `x`
5+
6+
* 例如,如果 `n = 73``x = 6` ,那么最佳方案是将 `6` 插入 `7``3` 之间,使 `n = 763`
7+
* 如果 `n = -55``x = 2` ,那么最佳方案是将 `2` 插在第一个 `5` 之前,使 `n = -255`
8+
9+
返回插入操作后,用字符串表示的 `n` 的最大值。
10+
11+
#### 示例 1:
12+
<pre>
13+
<strong>输入:</strong> n = "99", x = 9
14+
<strong>输出:</strong> "999"
15+
<strong>解释:</strong> 不管在哪里插入 9 ,结果都是相同的。
16+
</pre>
17+
18+
#### 示例 2:
19+
<pre>
20+
<strong>输入:</strong> n = "-13", x = 2
21+
<strong>输出:</strong> "-123"
22+
<strong>解释:</strong> 向 n 中插入 x 可以得到 -213、-123 或者 -132 ,三者中最大的是 -123 。
23+
</pre>
24+
25+
#### 提示:
26+
* <code>1 <= n.length <= 10<sup>5</sup></code>
27+
* `1 <= x <= 9`
28+
* `n` 中每一位的数字都在闭区间 `[1, 9]` 中。
29+
* `n` 代表一个有效的整数。
30+
*`n` 表示负数时,将会以字符 `'-'` 开始。
31+
32+
## 题解 (Rust)
33+
34+
### 1. 题解
35+
```Rust
36+
impl Solution {
37+
pub fn max_value(n: String, x: i32) -> String {
38+
let x = x as u8 + b'0';
39+
let mut n = n.into_bytes();
40+
41+
if n[0] != b'-' {
42+
for i in 0..=n.len() {
43+
if i == n.len() || x > n[i] {
44+
n.insert(i, x);
45+
break;
46+
}
47+
}
48+
} else {
49+
for i in 1..=n.len() {
50+
if i == n.len() || x < n[i] {
51+
n.insert(i, x);
52+
break;
53+
}
54+
}
55+
}
56+
57+
String::from_utf8(n).unwrap()
58+
}
59+
}
60+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn max_value(n: String, x: i32) -> String {
3+
let x = x as u8 + b'0';
4+
let mut n = n.into_bytes();
5+
6+
if n[0] != b'-' {
7+
for i in 0..=n.len() {
8+
if i == n.len() || x > n[i] {
9+
n.insert(i, x);
10+
break;
11+
}
12+
}
13+
} else {
14+
for i in 1..=n.len() {
15+
if i == n.len() || x < n[i] {
16+
n.insert(i, x);
17+
break;
18+
}
19+
}
20+
}
21+
22+
String::from_utf8(n).unwrap()
23+
}
24+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@
11081108
[1877][1877l]|[Minimize Maximum Pair Sum in Array][1877] |![rs]
11091109
[1878][1878l]|[Get Biggest Three Rhombus Sums in a Grid][1878] |![rs]
11101110
[1880][1880l]|[Check if Word Equals Summation of Two Words][1880] |![rs]
1111+
[1881][1881l]|[Maximum Value after Insertion][1881] |![rs]
11111112
[1882][1882l]|[Process Tasks Using Servers][1882] |![rs]
11121113
[1884][1884l]|[Egg Drop With 2 Eggs and N Floors][1884] |![rs]
11131114
[1886][1886l]|[Determine Whether Matrix Can Be Obtained By Rotation][1886] |![rs]
@@ -2571,6 +2572,7 @@
25712572
[1877]:Problemset/1877-Minimize%20Maximum%20Pair%20Sum%20in%20Array/README.md#1877-minimize-maximum-pair-sum-in-array
25722573
[1878]:Problemset/1878-Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README.md#1878-get-biggest-three-rhombus-sums-in-a-grid
25732574
[1880]:Problemset/1880-Check%20if%20Word%20Equals%20Summation%20of%20Two%20Words/README.md#1880-check-if-word-equals-summation-of-two-words
2575+
[1881]:Problemset/1881-Maximum%20Value%20after%20Insertion/README.md#1881-maximum-value-after-insertion
25742576
[1882]:Problemset/1882-Process%20Tasks%20Using%20Servers/README.md#1882-process-tasks-using-servers
25752577
[1884]:Problemset/1884-Egg%20Drop%20With%202%20Eggs%20and%20N%20Floors/README.md#1884-egg-drop-with-2-eggs-and-n-floors
25762578
[1886]:Problemset/1886-Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/README.md#1886-determine-whether-matrix-can-be-obtained-by-rotation
@@ -4033,6 +4035,7 @@
40334035
[1877l]:https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/
40344036
[1878l]:https://leetcode.com/problems/get-biggest-three-rhombus-sums-in-a-grid/
40354037
[1880l]:https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/
4038+
[1881l]:https://leetcode.com/problems/maximum-value-after-insertion/
40364039
[1882l]:https://leetcode.com/problems/process-tasks-using-servers/
40374040
[1884l]:https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors/
40384041
[1886l]:https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@
11081108
[1877][1877l]|[数组中最大数对和的最小值][1877] |![rs]
11091109
[1878][1878l]|[矩阵中最大的三个菱形和][1878] |![rs]
11101110
[1880][1880l]|[检查某单词是否等于两单词之和][1880] |![rs]
1111+
[1881][1881l]|[插入后的最大值][1881] |![rs]
11111112
[1882][1882l]|[使用服务器处理任务][1882] |![rs]
11121113
[1884][1884l]|[鸡蛋掉落-两枚鸡蛋][1884] |![rs]
11131114
[1886][1886l]|[判断矩阵经轮转后是否一致][1886] |![rs]
@@ -2571,6 +2572,7 @@
25712572
[1877]:Problemset/1877-Minimize%20Maximum%20Pair%20Sum%20in%20Array/README_CN.md#1877-数组中最大数对和的最小值
25722573
[1878]:Problemset/1878-Get%20Biggest%20Three%20Rhombus%20Sums%20in%20a%20Grid/README_CN.md#1878-矩阵中最大的三个菱形和
25732574
[1880]:Problemset/1880-Check%20if%20Word%20Equals%20Summation%20of%20Two%20Words/README_CN.md#1880-检查某单词是否等于两单词之和
2575+
[1881]:Problemset/1881-Maximum%20Value%20after%20Insertion/README_CN.md#1881-插入后的最大值
25742576
[1882]:Problemset/1882-Process%20Tasks%20Using%20Servers/README_CN.md#1882-使用服务器处理任务
25752577
[1884]:Problemset/1884-Egg%20Drop%20With%202%20Eggs%20and%20N%20Floors/README_CN.md#1884-鸡蛋掉落-两枚鸡蛋
25762578
[1886]:Problemset/1886-Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/README_CN.md#1886-判断矩阵经轮转后是否一致
@@ -4033,6 +4035,7 @@
40334035
[1877l]:https://leetcode.cn/problems/minimize-maximum-pair-sum-in-array/
40344036
[1878l]:https://leetcode.cn/problems/get-biggest-three-rhombus-sums-in-a-grid/
40354037
[1880l]:https://leetcode.cn/problems/check-if-word-equals-summation-of-two-words/
4038+
[1881l]:https://leetcode.cn/problems/maximum-value-after-insertion/
40364039
[1882l]:https://leetcode.cn/problems/process-tasks-using-servers/
40374040
[1884l]:https://leetcode.cn/problems/egg-drop-with-2-eggs-and-n-floors/
40384041
[1886l]:https://leetcode.cn/problems/determine-whether-matrix-can-be-obtained-by-rotation/

0 commit comments

Comments
 (0)