Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit36765a3

Browse files
committed
Add solution #2488
1 parente4ffe17 commit36765a3

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-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,978 LeetCode solutions in JavaScript
1+
#1,979 LeetCode solutions in JavaScript
22

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

@@ -1875,6 +1875,7 @@
18751875
2485|[Find the Pivot Integer](./solutions/2485-find-the-pivot-integer.js)|Easy|
18761876
2486|[Append Characters to String to Make Subsequence](./solutions/2486-append-characters-to-string-to-make-subsequence.js)|Medium|
18771877
2487|[Remove Nodes From Linked List](./solutions/2487-remove-nodes-from-linked-list.js)|Medium|
1878+
2488|[Count Subarrays With Median K](./solutions/2488-count-subarrays-with-median-k.js)|Hard|
18781879
2490|[Circular Sentence](./solutions/2490-circular-sentence.js)|Easy|
18791880
2493|[Divide Nodes Into the Maximum Number of Groups](./solutions/2493-divide-nodes-into-the-maximum-number-of-groups.js)|Hard|
18801881
2503|[Maximum Number of Points From Grid Queries](./solutions/2503-maximum-number-of-points-from-grid-queries.js)|Hard|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 2488. Count Subarrays With Median K
3+
* https://leetcode.com/problems/count-subarrays-with-median-k/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array nums of size n consisting of distinct integers from 1 to n and
7+
* a positive integer k.
8+
*
9+
* Return the number of non-empty subarrays in nums that have a median equal to k.
10+
*
11+
* Note:
12+
* - The median of an array is the middle element after sorting the array in ascending order.
13+
* If the array is of even length, the median is the left middle element.
14+
* - For example, the median of [2,3,1,4] is 2, and the median of [8,4,3,5,1] is 4.
15+
* - A subarray is a contiguous part of an array.
16+
*/
17+
18+
/**
19+
*@param {number[]} nums
20+
*@param {number} k
21+
*@return {number}
22+
*/
23+
varcountSubarrays=function(nums,k){
24+
constn=nums.length;
25+
constkIndex=nums.indexOf(k);
26+
constbalanceMap=newMap([[0,1]]);
27+
letbalance=0;
28+
letresult=0;
29+
30+
for(leti=kIndex+1;i<n;i++){
31+
balance+=nums[i]>k ?1 :-1;
32+
balanceMap.set(balance,(balanceMap.get(balance)||0)+1);
33+
}
34+
35+
balance=0;
36+
for(leti=kIndex;i>=0;i--){
37+
balance+=nums[i]>k ?-1 :nums[i]<k ?1 :0;
38+
result+=balanceMap.get(balance)||0;
39+
result+=balanceMap.get(balance+1)||0;
40+
}
41+
42+
returnresult;
43+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp