Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
938 B
5
Indexable
Never
#include <bits/stdc++.h>
#include <vector>
#include <unordered_map>

using namespace std;

int findMinimumPrice(vector<int> &price) {
    int n = price.size();
    int minPrice = INT_MAX;
    bool found = false;
    
    int left = 0, right = 0;
    unordered_map<int, int> freq;
    int currPrice = 0;

    while (right < n) {
        currPrice += price[right];
        freq[price[right]]++;

        // If we find a product with matching price, update the minimum price
        while (freq[price[right]] > 1) {
            found = true;
            minPrice = min(minPrice, currPrice);
            
            freq[price[left]]--;
            currPrice -= price[left];
            left++;
        }

        right++;
    }

    return found ? minPrice : -1;
}

int main() {
    vector<int> price = {1, 2, 1, 2};
    cout << findMinimumPrice(price) << endl; // Expected output: 4
    return 0;
}