We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1e67a7b commit 1b562d2Copy full SHA for 1b562d2
2012-sum-of-beauty-in-the-array.js
@@ -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