Skip to content

Commit 07314e8

Browse files
committed
+ problem 1330
1 parent ae95b64 commit 07314e8

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 1330. Reverse Subarray To Maximize Array Value
2+
You are given an integer array `nums`. The *value* of this array is defined as the sum of `|nums[i] - nums[i + 1]|` for all `0 <= i < nums.length - 1`.
3+
4+
You are allowed to select any subarray of the given array and reverse it. You can perform this operation **only once**.
5+
6+
Find maximum possible value of the final array.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> nums = [2,3,1,5,4]
11+
<strong>Output:</strong> 10
12+
<strong>Explanation:</strong> By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
13+
</pre>
14+
15+
#### Example 2:
16+
<pre>
17+
<strong>Input:</strong> nums = [2,4,9,24,2,1,10]
18+
<strong>Output:</strong> 68
19+
</pre>
20+
21+
#### Constraints:
22+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
23+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
24+
25+
## Solutions (Rust)
26+
27+
### 1. Solution
28+
```Rust
29+
impl Solution {
30+
pub fn max_value_after_reverse(nums: Vec<i32>) -> i32 {
31+
let origin = (1..nums.len())
32+
.map(|i| (nums[i] - nums[i - 1]).abs())
33+
.sum::<i32>();
34+
let mut max_num = nums[0].min(nums[1]);
35+
let mut min_num = nums[0].max(nums[1]);
36+
let mut ret = origin;
37+
38+
for i in 1..nums.len() - 1 {
39+
let a = nums[i - 1];
40+
let b = nums[i];
41+
let c = nums[i + 1];
42+
43+
ret = ret.max(origin + (c - nums[0]).abs() - (c - b).abs());
44+
ret = ret.max(origin + (a - nums.last().unwrap()).abs() - (b - a).abs());
45+
46+
let x = b.max(c);
47+
let y = b.min(c);
48+
49+
if x < max_num {
50+
ret = ret.max(origin + 2 * max_num - 2 * x);
51+
}
52+
if y > min_num {
53+
ret = ret.max(origin + 2 * y - 2 * min_num);
54+
}
55+
56+
max_num = max_num.max(y);
57+
min_num = min_num.min(x);
58+
}
59+
60+
ret
61+
}
62+
}
63+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 1330. 翻转子数组得到最大的数组值
2+
给你一个整数数组 `nums` 。「数组值」定义为所有满足 `0 <= i < nums.length-1``|nums[i]-nums[i+1]|` 的和。
3+
4+
你可以选择给定数组的任意子数组,并将该子数组翻转。但你只能执行这个操作 **一次**
5+
6+
请你找到可行的最大 **数组值**
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> nums = [2,3,1,5,4]
11+
<strong>输出:</strong> 10
12+
<strong>解释:</strong> 通过翻转子数组 [3,1,5] ,数组变成 [2,5,1,3,4] ,数组值为 10 。
13+
</pre>
14+
15+
#### 示例 2:
16+
<pre>
17+
<strong>输入:</strong> nums = [2,4,9,24,2,1,10]
18+
<strong>输出:</strong> 68
19+
</pre>
20+
21+
#### 提示:
22+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
23+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
24+
25+
## 题解 (Rust)
26+
27+
### 1. 题解
28+
```Rust
29+
impl Solution {
30+
pub fn max_value_after_reverse(nums: Vec<i32>) -> i32 {
31+
let origin = (1..nums.len())
32+
.map(|i| (nums[i] - nums[i - 1]).abs())
33+
.sum::<i32>();
34+
let mut max_num = nums[0].min(nums[1]);
35+
let mut min_num = nums[0].max(nums[1]);
36+
let mut ret = origin;
37+
38+
for i in 1..nums.len() - 1 {
39+
let a = nums[i - 1];
40+
let b = nums[i];
41+
let c = nums[i + 1];
42+
43+
ret = ret.max(origin + (c - nums[0]).abs() - (c - b).abs());
44+
ret = ret.max(origin + (a - nums.last().unwrap()).abs() - (b - a).abs());
45+
46+
let x = b.max(c);
47+
let y = b.min(c);
48+
49+
if x < max_num {
50+
ret = ret.max(origin + 2 * max_num - 2 * x);
51+
}
52+
if y > min_num {
53+
ret = ret.max(origin + 2 * y - 2 * min_num);
54+
}
55+
56+
max_num = max_num.max(y);
57+
min_num = min_num.min(x);
58+
}
59+
60+
ret
61+
}
62+
}
63+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
impl Solution {
2+
pub fn max_value_after_reverse(nums: Vec<i32>) -> i32 {
3+
let origin = (1..nums.len())
4+
.map(|i| (nums[i] - nums[i - 1]).abs())
5+
.sum::<i32>();
6+
let mut max_num = nums[0].min(nums[1]);
7+
let mut min_num = nums[0].max(nums[1]);
8+
let mut ret = origin;
9+
10+
for i in 1..nums.len() - 1 {
11+
let a = nums[i - 1];
12+
let b = nums[i];
13+
let c = nums[i + 1];
14+
15+
ret = ret.max(origin + (c - nums[0]).abs() - (c - b).abs());
16+
ret = ret.max(origin + (a - nums.last().unwrap()).abs() - (b - a).abs());
17+
18+
let x = b.max(c);
19+
let y = b.min(c);
20+
21+
if x < max_num {
22+
ret = ret.max(origin + 2 * max_num - 2 * x);
23+
}
24+
if y > min_num {
25+
ret = ret.max(origin + 2 * y - 2 * min_num);
26+
}
27+
28+
max_num = max_num.max(y);
29+
min_num = min_num.min(x);
30+
}
31+
32+
ret
33+
}
34+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@
770770
[1325][1325l]|[Delete Leaves With a Given Value][1325] |![py]
771771
[1328][1328l]|[Break a Palindrome][1328] |![rb]&nbsp;&nbsp;![rs]
772772
[1329][1329l]|[Sort the Matrix Diagonally][1329] |![rb]&nbsp;&nbsp;![rs]
773+
[1330][1330l]|[Reverse Subarray To Maximize Array Value][1330] |![rs]
773774
[1331][1331l]|[Rank Transform of an Array][1331] |![py]
774775
[1332][1332l]|[Remove Palindromic Subsequences][1332] |![py]
775776
[1333][1333l]|[Filter Restaurants by Vegan-Friendly, Price and Distance][1333] |![rb]&nbsp;&nbsp;![rs]
@@ -2165,6 +2166,7 @@
21652166
[1325]:Problemset/1325-Delete%20Leaves%20With%20a%20Given%20Value/README.md#1325-delete-leaves-with-a-given-value
21662167
[1328]:Problemset/1328-Break%20a%20Palindrome/README.md#1328-break-a-palindrome
21672168
[1329]:Problemset/1329-Sort%20the%20Matrix%20Diagonally/README.md#1329-sort-the-matrix-diagonally
2169+
[1330]:Problemset/1330-Reverse%20Subarray%20To%20Maximize%20Array%20Value/README.md#1330-reverse-subarray-to-maximize-array-value
21682170
[1331]:Problemset/1331-Rank%20Transform%20of%20an%20Array/README.md#1331-rank-transform-of-an-array
21692171
[1332]:Problemset/1332-Remove%20Palindromic%20Subsequences/README.md#1332-remove-palindromic-subsequences
21702172
[1333]:Problemset/1333-Filter%20Restaurants%20by%20Vegan-Friendly,%20Price%20and%20Distance/README.md#1333-filter-restaurants-by-vegan-friendly-price-and-distance
@@ -3559,6 +3561,7 @@
35593561
[1325l]:https://leetcode.com/problems/delete-leaves-with-a-given-value/
35603562
[1328l]:https://leetcode.com/problems/break-a-palindrome/
35613563
[1329l]:https://leetcode.com/problems/sort-the-matrix-diagonally/
3564+
[1330l]:https://leetcode.com/problems/reverse-subarray-to-maximize-array-value/
35623565
[1331l]:https://leetcode.com/problems/rank-transform-of-an-array/
35633566
[1332l]:https://leetcode.com/problems/remove-palindromic-subsequences/
35643567
[1333l]:https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@
770770
[1325][1325l]|[删除给定值的叶子节点][1325] |![py]
771771
[1328][1328l]|[破坏回文串][1328] |![rb]&nbsp;&nbsp;![rs]
772772
[1329][1329l]|[将矩阵按对角线排序][1329] |![rb]&nbsp;&nbsp;![rs]
773+
[1330][1330l]|[翻转子数组得到最大的数组值][1330] |![rs]
773774
[1331][1331l]|[数组序号转换][1331] |![py]
774775
[1332][1332l]|[删除回文子序列][1332] |![py]
775776
[1333][1333l]|[餐厅过滤器][1333] |![rb]&nbsp;&nbsp;![rs]
@@ -2165,6 +2166,7 @@
21652166
[1325]:Problemset/1325-Delete%20Leaves%20With%20a%20Given%20Value/README_CN.md#1325-删除给定值的叶子节点
21662167
[1328]:Problemset/1328-Break%20a%20Palindrome/README_CN.md#1328-破坏回文串
21672168
[1329]:Problemset/1329-Sort%20the%20Matrix%20Diagonally/README_CN.md#1329-将矩阵按对角线排序
2169+
[1330]:Problemset/1330-Reverse%20Subarray%20To%20Maximize%20Array%20Value/README_CN.md#1330-翻转子数组得到最大的数组值
21682170
[1331]:Problemset/1331-Rank%20Transform%20of%20an%20Array/README_CN.md#1331-数组序号转换
21692171
[1332]:Problemset/1332-Remove%20Palindromic%20Subsequences/README_CN.md#1332-删除回文子序列
21702172
[1333]:Problemset/1333-Filter%20Restaurants%20by%20Vegan-Friendly,%20Price%20and%20Distance/README_CN.md#1333-餐厅过滤器
@@ -3559,6 +3561,7 @@
35593561
[1325l]:https://leetcode.cn/problems/delete-leaves-with-a-given-value/
35603562
[1328l]:https://leetcode.cn/problems/break-a-palindrome/
35613563
[1329l]:https://leetcode.cn/problems/sort-the-matrix-diagonally/
3564+
[1330l]:https://leetcode.cn/problems/reverse-subarray-to-maximize-array-value/
35623565
[1331l]:https://leetcode.cn/problems/rank-transform-of-an-array/
35633566
[1332l]:https://leetcode.cn/problems/remove-palindromic-subsequences/
35643567
[1333l]:https://leetcode.cn/problems/filter-restaurants-by-vegan-friendly-price-and-distance/

0 commit comments

Comments
 (0)