#include <iostream> #include <vector> #include <unordered_map> typedef long long ll; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<ll> arr(n); for (int i = 0; i < n; i++) { cin >> arr[i]; } unordered_map<ll, int> freq; int left = 0, right = 0; ll count = 0; while (right < n) { int curr = arr[right]; freq[curr]++; int distinct = freq.size(); while (distinct > k) { freq[arr[left]]--; if (freq[arr[left]] == 0) { distinct--; freq.erase(arr[left]); } left++; } count += right - left + 1; right++; } cout << count << endl; return 0; } Frontend
Source:https://dev.to/guhan29/distinct-values-subarrays-ii-cses-algoritmit-ongelmanratkaisussa-4a01
Distinct Values Subarrays II | CSES - Algoritmit ongelmanratkaisussa
dev.to0 views
PreviousA Practical Checklist for Mobile Browser Games Built With HTML5NextTemp mail got my signups blocked, so I switched to a real inbox
Related posts
Personal vs Organization Developer Account on Google Play, and How to Get Useful User Feedback Once Your Test Starts
0dev.to
A Practical Checklist for Mobile Browser Games Built With HTML5
0dev.to
One React Component, Two Layouts: A Pattern for Better UI Architecture
0HackerNoon
How One RGB Formula Transformed My Entire Portfolio
0HackerNoon
Dynatrace Managed SLO: configuration, limits and API (tutorial)
0dev.to
Make the agent use the existing feature-flag hook, not a hardcoded boolean
0dev.to