File tree Expand file tree Collapse file tree 1 file changed +15
-17
lines changed
SlidingWindow/SlidingWindow/Medium Expand file tree Collapse file tree 1 file changed +15
-17
lines changed Original file line number Diff line number Diff line change 1- using System ;
2- using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Text ;
5- using System . Threading . Tasks ;
6-
7- namespace SlidingWindow . Medium ;
1+ namespace SlidingWindow . Medium ;
82
93public class SubarrayMaxAtLeastKTimes
104{
115 public long CountSubarrays ( int [ ] nums , int k )
126 {
137 var max = nums . Max ( ) ;
14- var maxAppear = 0 ;
15- var ( l , r ) = ( 0 , 0 ) ;
16- while ( r < nums . Length )
8+ var count = 0 ;
9+ var left = 0 ;
10+ long res = 0 ;
11+ foreach ( var t in nums )
1712 {
18- k -= ( nums [ r ++ ] == max ) ? 1 : 0 ;
19-
20- while ( k == 0 )
13+ if ( t == max )
2114 {
22- k += ( nums [ l ++ ] == max ) ? 1 : 0 ;
15+ count ++ ;
2316 }
2417
25- maxAppear += l ;
18+ while ( count == k )
19+ {
20+ if ( nums [ left ] == max )
21+ count -- ;
22+ left ++ ;
23+ }
24+ res += left ;
2625 }
27-
28- return maxAppear ;
26+ return res ;
2927 }
3028}
You can’t perform that action at this time.
0 commit comments