Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 89f9776

Browse files
committed
Add solution #2012
1 parent 48c9a34 commit 89f9776

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,686 LeetCode solutions in JavaScript
1+
# 1,687 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1543,6 +1543,7 @@
15431543
2008|[Maximum Earnings From Taxi](./solutions/2008-maximum-earnings-from-taxi.js)|Medium|
15441544
2009|[Minimum Number of Operations to Make Array Continuous](./solutions/2009-minimum-number-of-operations-to-make-array-continuous.js)|Hard|
15451545
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
1546+
2012|[Sum of Beauty in the Array](./solutions/2012-sum-of-beauty-in-the-array.js)|Medium|
15461547
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
15471548
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
15481549
2027|[Minimum Moves to Convert String](./solutions/2027-minimum-moves-to-convert-string.js)|Easy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 2012. Sum of Beauty in the Array
3+
* https://leetcode.com/problems/sum-of-beauty-in-the-array/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array nums. For each index i (1 <= i <= nums.length - 2)
7+
* the beauty of nums[i] equals:
8+
* - 2, if nums[j] < nums[i] < nums[k], for all 0 <= j < i and for all i < k <= nums.length - 1.
9+
* - 1, if nums[i - 1] < nums[i] < nums[i + 1], and the previous condition is not satisfied.
10+
* - 0, if none of the previous conditions holds.
11+
*
12+
* Return the sum of beauty of all nums[i] where 1 <= i <= nums.length - 2.
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @return {number}
18+
*/
19+
var sumOfBeauties = function(nums) {
20+
const n = nums.length;
21+
let result = 0;
22+
const leftMax = new Array(n).fill(nums[0]);
23+
const rightMin = new Array(n).fill(nums[n - 1]);
24+
25+
for (let i = 1; i < n; i++) {
26+
leftMax[i] = Math.max(leftMax[i - 1], nums[i - 1]);
27+
}
28+
29+
for (let i = n - 2; i >= 0; i--) {
30+
rightMin[i] = Math.min(rightMin[i + 1], nums[i + 1]);
31+
}
32+
33+
for (let i = 1; i < n - 1; i++) {
34+
if (leftMax[i] < nums[i] && nums[i] < rightMin[i]) {
35+
result += 2;
36+
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
37+
result += 1;
38+
}
39+
}
40+
41+
return result;
42+
};

0 commit comments

Comments
 (0)