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

Commite2f5b8d

Browse files
committed
Add solution #2763
1 parent16f0e41 commite2f5b8d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,7 @@
20232023
2745|[Construct the Longest New String](./solutions/2745-construct-the-longest-new-string.js)|Medium|
20242024
2748|[Number of Beautiful Pairs](./solutions/2748-number-of-beautiful-pairs.js)|Easy|
20252025
2751|[Robot Collisions](./solutions/2751-robot-collisions.js)|Hard|
2026+
2763|[Sum of Imbalance Numbers of All Subarrays](./solutions/2763-sum-of-imbalance-numbers-of-all-subarrays.js)|Hard|
20262027
2780|[Minimum Index of a Valid Split](./solutions/2780-minimum-index-of-a-valid-split.js)|Medium|
20272028
2799|[Count Complete Subarrays in an Array](./solutions/2799-count-complete-subarrays-in-an-array.js)|Medium|
20282029
2818|[Apply Operations to Maximize Score](./solutions/2818-apply-operations-to-maximize-score.js)|Hard|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 2763. Sum of Imbalance Numbers of All Subarrays
3+
* https://leetcode.com/problems/sum-of-imbalance-numbers-of-all-subarrays/
4+
* Difficulty: Hard
5+
*
6+
* The imbalance number of a 0-indexed integer array arr of length n is defined as the number
7+
* of indices in sarr = sorted(arr) such that:
8+
* - 0 <= i < n - 1, and
9+
* - sarr[i+1] - sarr[i] > 1
10+
*
11+
* Here, sorted(arr) is the function that returns the sorted version of arr.
12+
*
13+
* Given a 0-indexed integer array nums, return the sum of imbalance numbers of all its subarrays.
14+
*
15+
* A subarray is a contiguous non-empty sequence of elements within an array.
16+
*/
17+
18+
/**
19+
*@param {number[]} nums
20+
*@return {number}
21+
*/
22+
varsumImbalanceNumbers=function(nums){
23+
constn=nums.length;
24+
letresult=0;
25+
26+
for(leti=0;i<n;i++){
27+
constseen=newSet();
28+
letimbalance=0;
29+
30+
for(letj=i;j<n;j++){
31+
constcurrent=nums[j];
32+
33+
if(!seen.has(current)){
34+
consthasNext=seen.has(current+1);
35+
consthasPrev=seen.has(current-1);
36+
37+
if(hasNext&&hasPrev){
38+
imbalance--;
39+
}elseif(!hasNext&&!hasPrev&&seen.size>0){
40+
imbalance++;
41+
}
42+
43+
seen.add(current);
44+
}
45+
46+
result+=imbalance;
47+
}
48+
}
49+
50+
returnresult;
51+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp