-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
36 lines (34 loc) · 1005 Bytes
/
Solution.cs
File metadata and controls
36 lines (34 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Solution
{
public int CountPartitions(int[] nums, int k)
{
int mod = (int)1e9 + 7;
int n = nums.Length;
long[] dp = new long[n + 1];
long[] prefix = new long[n + 1];
dp[0] = 1;
prefix[0] = 1;
SortedSet<int> cnt = [];
Dictionary<int, int> freq = [];
for (int i = 0, j = 0; i < n; i++)
{
int num = nums[i];
cnt.Add(num);
freq[num] = freq.GetValueOrDefault(num, 0) + 1;
while (j < i && cnt.Max - cnt.Min > k)
{
int key = nums[j];
freq[key]--;
if (freq[key] == 0)
{
freq.Remove(key);
cnt.Remove(key);
}
j++;
}
dp[i + 1] = j > 0 ? (prefix[i] - prefix[j - 1] + mod) % mod : prefix[i];
prefix[i + 1] = (prefix[i] + dp[i + 1]) % mod;
}
return (int)dp[n];
}
}