Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- int pickCoupons(int n, vector<int> &coupons) // 2 Pointers Solution
- {
- int l = 0;
- unordered_map<int, int> mp;
- int ans = INT_MAX;
- for (int r = 0; r < n; r++) { // using map for sliding window
- mp[coupons[r]]++;
- while (mp[coupons[r]] > 1) {
- /*
- once the occurrence of coupons[r] > 1 this means there are more than 1 of the same coupon
- then we will check the answer
- and remove the left side till the occurrence return to 1 : mp[coupons[r]] == 1
- */
- ans = min(ans, r - l + 1);
- mp[coupons[l]]--;
- l++;
- }
- }
- return (ans == INT_MAX ? -1 : ans);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement