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

Commit 9dd2734

Browse files
30-03-2024
1 parent b20980b commit 9dd2734

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

‎2024/03 - March/29-03-2024.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
Author : Manas Rawat
4+
Date : 29/03/2024
5+
Problem : Count Subarrays Where Max Element Appears at Least K Times
6+
Difficulty : Medium
7+
Problem Link : https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/description
8+
Video Solution : NA
9+
10+
*/
11+
12+
13+
class Solution {
14+
public:
15+
long long countSubarrays(vector<int>& nums, int k) {
16+
int maxElement = *max_element(nums.begin(), nums.end());
17+
vector<int> indexesOfMaxElements;
18+
long long ans = 0;
19+
20+
for (int index = 0; index < nums.size(); index++) {
21+
if (nums[index] == maxElement) {
22+
indexesOfMaxElements.push_back(index);
23+
}
24+
25+
int freq = indexesOfMaxElements.size();
26+
if (freq >= k) {
27+
ans += indexesOfMaxElements[freq - k] + 1;
28+
}
29+
}
30+
31+
return ans;
32+
}
33+
};

‎2024/03 - March/30-03-2024.cpp‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
3+
Author : Manas Rawat
4+
Date : 30/03/2024
5+
Problem : Subarrays with K Different Integers
6+
Difficulty : Hard
7+
Problem Link : https://leetcode.com/problems/subarrays-with-k-different-integers/description/
8+
Video Solution : NA
9+
10+
*/
11+
12+
13+
class Solution {
14+
public:
15+
int subarraysWithKDistinct(vector<int>& nums, int k) {
16+
vector<int> f(nums.size() + 1, 0);
17+
18+
int ans = 0;
19+
int l = 0;
20+
int r = 0;
21+
int cur = 0;
22+
23+
while (r < nums.size()) {
24+
if (++f[nums[r++]] == 1) {
25+
k--;
26+
}
27+
28+
if (k < 0) {
29+
--f[nums[l++]];
30+
k++;
31+
cur = 0;
32+
}
33+
34+
if (k == 0) {
35+
while (f[nums[l]] > 1) {
36+
--f[nums[l++]];
37+
cur++;
38+
}
39+
ans += (cur + 1);
40+
}
41+
}
42+
return ans;
43+
}
44+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /