File tree 2 files changed +44
-1
lines changed 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,686 LeetCode solutions in JavaScript
1
+ # 1,687 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1543
1543
2008|[ Maximum Earnings From Taxi] ( ./solutions/2008-maximum-earnings-from-taxi.js ) |Medium|
1544
1544
2009|[ Minimum Number of Operations to Make Array Continuous] ( ./solutions/2009-minimum-number-of-operations-to-make-array-continuous.js ) |Hard|
1545
1545
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|
1546
1547
2016|[ Maximum Difference Between Increasing Elements] ( ./solutions/2016-maximum-difference-between-increasing-elements.js ) |Easy|
1547
1548
2017|[ Grid Game] ( ./solutions/2017-grid-game.js ) |Medium|
1548
1549
2027|[ Minimum Moves to Convert String] ( ./solutions/2027-minimum-moves-to-convert-string.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments