Skip to content

Commit 4c6879b

Browse files
committed
Add solution #3452
1 parent fea37c8 commit 4c6879b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@
626626
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
627627
3397|[Maximum Number of Distinct Elements After Operations](./3397-maximum-number-of-distinct-elements-after-operations.js)|Medium|
628628
3402|[Minimum Operations to Make Columns Strictly Increasing](./3402-minimum-operations-to-make-columns-strictly-increasing.js)|Easy|
629+
3452|[Sum of Good Numbers](./3452-sum-of-good-numbers.js)|Easy|
629630

630631
## License
631632

solutions/3452-sum-of-good-numbers.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 3452. Sum of Good Numbers
3+
* https://leetcode.com/problems/sum-of-good-numbers/
4+
* Difficulty: Easy
5+
*
6+
* Given an array of integers nums and an integer k, an element nums[i] is considered good if
7+
* it is strictly greater than the elements at indices i - k and i + k (if those indices exist).
8+
* If neither of these indices exists, nums[i] is still considered good.
9+
*
10+
* Return the sum of all the good elements in the array.
11+
*/
12+
13+
/**
14+
* @param {number[]} nums
15+
* @param {number} k
16+
* @return {number}
17+
*/
18+
var sumOfGoodNumbers = function(nums, k) {
19+
return nums.reduce((sum, n, i) => {
20+
return n > (nums[i - k] ?? 0) && n > (nums[i + k] ?? 0) ? sum + n : sum;
21+
}, 0);
22+
};

0 commit comments

Comments
 (0)