Skip to content

Commit 1b562d2

Browse files
authored
Create 2012-sum-of-beauty-in-the-array.js
1 parent 1e67a7b commit 1b562d2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

2012-sum-of-beauty-in-the-array.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const sumOfBeauties = function(nums) {
6+
const n = nums.length
7+
const maxArr = Array(n).fill(0), minArr = Array(n).fill(0)
8+
let max = -Infinity, min = Infinity
9+
for(let i = 0; i < n; i++) {
10+
const tmp = Math.max(max, nums[i])
11+
if(tmp > max) max = tmp
12+
maxArr[i] = max
13+
}
14+
15+
for(let i = n - 1; i >= 0; i--) {
16+
const tmp = Math.min(min, nums[i])
17+
if(tmp < min) min = tmp
18+
minArr[i] = min
19+
}
20+
let res = 0
21+
22+
for(let i = 1; i < n - 1; i++) {
23+
if(nums[i] > maxArr[i - 1] && nums[i] < minArr[i + 1]) res += 2
24+
else if(nums[i] > nums[i - 1] && nums[i] < nums[i + 1]) res += 1
25+
}
26+
27+
return res
28+
};

0 commit comments

Comments
 (0)