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

Commit aa29d7e

Browse files
committed
Add solution #2962
1 parent ae644af commit aa29d7e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-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,515 LeetCode solutions in JavaScript
1+
# 1,516 LeetCode solutions in JavaScript
22

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

@@ -1493,6 +1493,7 @@
14931493
2873|[Maximum Value of an Ordered Triplet I](./solutions/2873-maximum-value-of-an-ordered-triplet-i.js)|Easy|
14941494
2874|[Maximum Value of an Ordered Triplet II](./solutions/2874-maximum-value-of-an-ordered-triplet-ii.js)|Medium|
14951495
2948|[Make Lexicographically Smallest Array by Swapping Elements](./solutions/2948-make-lexicographically-smallest-array-by-swapping-elements.js)|Medium|
1496+
2962|[Count Subarrays Where Max Element Appears at Least K Times](./solutions/2962-count-subarrays-where-max-element-appears-at-least-k-times.js)|Medium|
14961497
2965|[Find Missing and Repeated Values](./solutions/2965-find-missing-and-repeated-values.js)|Easy|
14971498
2999|[Count the Number of Powerful Integers](./solutions/2999-count-the-number-of-powerful-integers.js)|Hard|
14981499
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 2962. Count Subarrays Where Max Element Appears at Least K Times
3+
* https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums and a positive integer k.
7+
*
8+
* Return the number of subarrays where the maximum element of nums appears at least k times in
9+
* that subarray.
10+
*
11+
* A subarray is a contiguous sequence of elements within an array.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var countSubarrays = function(nums, k) {
20+
const maxNum = Math.max(...nums);
21+
let maxCount = 0;
22+
let left = 0;
23+
let result = 0;
24+
25+
for (let right = 0; right < nums.length; right++) {
26+
if (nums[right] === maxNum) maxCount++;
27+
while (maxCount >= k) {
28+
result += nums.length - right;
29+
if (nums[left] === maxNum) maxCount--;
30+
left++;
31+
}
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)