Skip to content

Commit 75b6fa2

Browse files
committed
+ problem 2012
1 parent d1b7d18 commit 75b6fa2

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 2012. Sum of Beauty in the Array
2+
You are given a **0-indexed** integer array `nums`. For each index `i` (`1 <= i <= nums.length - 2`) the **beauty** of `nums[i]` equals:
3+
4+
* `2`, if `nums[j] < nums[i] < nums[k]`, for **all** `0 <= j < i` and for **all** `i < k <= nums.length - 1`.
5+
* `1`, if `nums[i - 1] < nums[i] < nums[i + 1]`, and the previous condition is not satisfied.
6+
* `0`, if none of the previous conditions holds.
7+
8+
Return *the **sum of beauty** of all* `nums[i]` *where* `1 <= i <= nums.length - 2`.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> nums = [1,2,3]
13+
<strong>Output:</strong> 2
14+
<strong>Explanation:</strong> For each index i in the range 1 <= i <= 1:
15+
- The beauty of nums[1] equals 2.
16+
</pre>
17+
18+
#### Example 2:
19+
<pre>
20+
<strong>Input:</strong> nums = [2,4,6,4]
21+
<strong>Output:</strong> 1
22+
<strong>Explanation:</strong> For each index i in the range 1 <= i <= 2:
23+
- The beauty of nums[1] equals 1.
24+
- The beauty of nums[2] equals 0.
25+
</pre>
26+
27+
#### Example 3:
28+
<pre>
29+
<strong>Input:</strong> nums = [3,2,1]
30+
<strong>Output:</strong> 0
31+
<strong>Explanation:</strong> For each index i in the range 1 <= i <= 1:
32+
- The beauty of nums[1] equals 0.
33+
</pre>
34+
35+
#### Constraints:
36+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
37+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
38+
39+
## Solutions (Rust)
40+
41+
### 1. Solution
42+
```Rust
43+
impl Solution {
44+
pub fn sum_of_beauties(nums: Vec<i32>) -> i32 {
45+
let n = nums.len();
46+
let mut max = vec![i32::MIN; nums.len()];
47+
let mut min = vec![i32::MAX; nums.len()];
48+
let mut ret = 0;
49+
50+
for i in 1..nums.len() {
51+
max[i] = max[i - 1].max(nums[i - 1]);
52+
min[n - 1 - i] = min[n - i].min(nums[n - i]);
53+
}
54+
55+
for i in 1..nums.len() - 1 {
56+
if max[i] < nums[i] && nums[i] < min[i] {
57+
ret += 2;
58+
} else if nums[i - 1] < nums[i] && nums[i] < nums[i + 1] {
59+
ret += 1;
60+
}
61+
}
62+
63+
ret
64+
}
65+
}
66+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 2012. 数组美丽值求和
2+
给你一个下标从 **0** 开始的整数数组 `nums` 。对于每个下标 `i``1 <= i <= nums.length - 2`),`nums[i]`**美丽值** 等于:
3+
4+
* `2`,对于所有 `0 <= j < i``i < k <= nums.length - 1` ,满足 `nums[j] < nums[i] < nums[k]`
5+
* `1`,如果满足 `nums[i - 1] < nums[i] < nums[i + 1]` ,且不满足前面的条件
6+
* `0`,如果上述条件全部不满足
7+
8+
返回符合 `1 <= i <= nums.length - 2` 的所有 `nums[i]`**美丽值的总和**
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> nums = [1,2,3]
13+
<strong>输出:</strong> 2
14+
<strong>解释:</strong> 对于每个符合范围 1 <= i <= 1 的下标 i :
15+
- nums[1] 的美丽值等于 2
16+
</pre>
17+
18+
#### 示例 2:
19+
<pre>
20+
<strong>输入:</strong> nums = [2,4,6,4]
21+
<strong>输出:</strong> 1
22+
<strong>解释:</strong> 对于每个符合范围 1 <= i <= 2 的下标 i :
23+
- nums[1] 的美丽值等于 1
24+
- nums[2] 的美丽值等于 0
25+
</pre>
26+
27+
#### 示例 3:
28+
<pre>
29+
<strong>输入:</strong> nums = [3,2,1]
30+
<strong>输出:</strong> 0
31+
<strong>解释:</strong> 对于每个符合范围 1 <= i <= 1 的下标 i :
32+
- nums[1] 的美丽值等于 0
33+
</pre>
34+
35+
#### 提示:
36+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
37+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
38+
39+
## 题解 (Rust)
40+
41+
### 1. 题解
42+
```Rust
43+
impl Solution {
44+
pub fn sum_of_beauties(nums: Vec<i32>) -> i32 {
45+
let n = nums.len();
46+
let mut max = vec![i32::MIN; nums.len()];
47+
let mut min = vec![i32::MAX; nums.len()];
48+
let mut ret = 0;
49+
50+
for i in 1..nums.len() {
51+
max[i] = max[i - 1].max(nums[i - 1]);
52+
min[n - 1 - i] = min[n - i].min(nums[n - i]);
53+
}
54+
55+
for i in 1..nums.len() - 1 {
56+
if max[i] < nums[i] && nums[i] < min[i] {
57+
ret += 2;
58+
} else if nums[i - 1] < nums[i] && nums[i] < nums[i + 1] {
59+
ret += 1;
60+
}
61+
}
62+
63+
ret
64+
}
65+
}
66+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn sum_of_beauties(nums: Vec<i32>) -> i32 {
3+
let n = nums.len();
4+
let mut max = vec![i32::MIN; nums.len()];
5+
let mut min = vec![i32::MAX; nums.len()];
6+
let mut ret = 0;
7+
8+
for i in 1..nums.len() {
9+
max[i] = max[i - 1].max(nums[i - 1]);
10+
min[n - 1 - i] = min[n - i].min(nums[n - i]);
11+
}
12+
13+
for i in 1..nums.len() - 1 {
14+
if max[i] < nums[i] && nums[i] < min[i] {
15+
ret += 2;
16+
} else if nums[i - 1] < nums[i] && nums[i] < nums[i + 1] {
17+
ret += 1;
18+
}
19+
}
20+
21+
ret
22+
}
23+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@
11951195
[2007][2007l]|[Find Original Array From Doubled Array][2007] |![rs]
11961196
[2008][2008l]|[Maximum Earnings From Taxi][2008] |![rs]
11971197
[2011][2011l]|[Final Value of Variable After Performing Operations][2011] |![py]&nbsp;&nbsp;![rs]
1198+
[2012][2012l]|[Sum of Beauty in the Array][2012] |![rs]
11981199
[2013][2013l]|[Detect Squares][2013] |![rs]
11991200
[2016][2016l]|[Maximum Difference Between Increasing Elements][2016] |![rs]
12001201
[2017][2017l]|[Grid Game][2017] |![rs]
@@ -2699,6 +2700,7 @@
26992700
[2007]:Problemset/2007-Find%20Original%20Array%20From%20Doubled%20Array/README.md#2007-find-original-array-from-doubled-array
27002701
[2008]:Problemset/2008-Maximum%20Earnings%20From%20Taxi/README.md#2008-maximum-earnings-from-taxi
27012702
[2011]:Problemset/2011-Final%20Value%20of%20Variable%20After%20Performing%20Operations/README.md#2011-final-value-of-variable-after-performing-operations
2703+
[2012]:Problemset/2012-Sum%20of%20Beauty%20in%20the%20Array/README.md#2012-sum-of-beauty-in-the-array
27022704
[2013]:Problemset/2013-Detect%20Squares/README.md#2013-detect-squares
27032705
[2016]:Problemset/2016-Maximum%20Difference%20Between%20Increasing%20Elements/README.md#2016-maximum-difference-between-increasing-elements
27042706
[2017]:Problemset/2017-Grid%20Game/README.md#2017-grid-game
@@ -4202,6 +4204,7 @@
42024204
[2007l]:https://leetcode.com/problems/find-original-array-from-doubled-array/
42034205
[2008l]:https://leetcode.com/problems/maximum-earnings-from-taxi/
42044206
[2011l]:https://leetcode.com/problems/final-value-of-variable-after-performing-operations/
4207+
[2012l]:https://leetcode.com/problems/sum-of-beauty-in-the-array/
42054208
[2013l]:https://leetcode.com/problems/detect-squares/
42064209
[2016l]:https://leetcode.com/problems/maximum-difference-between-increasing-elements/
42074210
[2017l]:https://leetcode.com/problems/grid-game/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@
11951195
[2007][2007l]|[从双倍数组中还原原数组][2007] |![rs]
11961196
[2008][2008l]|[出租车的最大盈利][2008] |![rs]
11971197
[2011][2011l]|[执行操作后的变量值][2011] |![py]&nbsp;&nbsp;![rs]
1198+
[2012][2012l]|[数组美丽值求和][2012] |![rs]
11981199
[2013][2013l]|[检测正方形][2013] |![rs]
11991200
[2016][2016l]|[增量元素之间的最大差值][2016] |![rs]
12001201
[2017][2017l]|[网格游戏][2017] |![rs]
@@ -2699,6 +2700,7 @@
26992700
[2007]:Problemset/2007-Find%20Original%20Array%20From%20Doubled%20Array/README_CN.md#2007-从双倍数组中还原原数组
27002701
[2008]:Problemset/2008-Maximum%20Earnings%20From%20Taxi/README_CN.md#2008-出租车的最大盈利
27012702
[2011]:Problemset/2011-Final%20Value%20of%20Variable%20After%20Performing%20Operations/README_CN.md#2011-执行操作后的变量值
2703+
[2012]:Problemset/2012-Sum%20of%20Beauty%20in%20the%20Array/README_CN.md#2012-数组美丽值求和
27022704
[2013]:Problemset/2013-Detect%20Squares/README_CN.md#2013-检测正方形
27032705
[2016]:Problemset/2016-Maximum%20Difference%20Between%20Increasing%20Elements/README_CN.md#2016-增量元素之间的最大差值
27042706
[2017]:Problemset/2017-Grid%20Game/README_CN.md#2017-网格游戏
@@ -4202,6 +4204,7 @@
42024204
[2007l]:https://leetcode.cn/problems/find-original-array-from-doubled-array/
42034205
[2008l]:https://leetcode.cn/problems/maximum-earnings-from-taxi/
42044206
[2011l]:https://leetcode.cn/problems/final-value-of-variable-after-performing-operations/
4207+
[2012l]:https://leetcode.cn/problems/sum-of-beauty-in-the-array/
42054208
[2013l]:https://leetcode.cn/problems/detect-squares/
42064209
[2016l]:https://leetcode.cn/problems/maximum-difference-between-increasing-elements/
42074210
[2017l]:https://leetcode.cn/problems/grid-game/

0 commit comments

Comments
 (0)