Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
2.0 kB
3
Indexable
class Solution {
public:
    int twoSum2(vector<int>& nums, int target, int m, int n) {
        unordered_map<int, set<int>> indexMap;

        int count = 0;

        vector<bool> visited(nums.size(), false);

        for (int i=0; i<nums.size(); i++) {
            indexMap[nums[i]].insert(i);
        }

        for (int i=0; i<nums.size(); i++) {
            int diff = target - nums[i];

            if ((indexMap.find(diff) != indexMap.end())) {
                if (i<m && !visited[i]) {
                    int countB = 0;
                    for (auto x: indexMap[diff]) {
                        if (x>=m && x!=i)
                            countB++;
                    }
                    
                    if (countB>0) {
                        visited[i] = true;
                        count+=countB;
                    }
                    
                }        
            }
        }

        return count;
    }

    vector<int> solve(vector<int>& a, vector<int>& b, vector<vector<int>>& queries) {

        int m = a.size();
        int n = b.size();

        vector<int> combined;

        vector<int> result;
        for(auto x:a)
            combined.push_back(x);

        for(auto x:b)
            combined.push_back(x);

        for (auto query: queries) {
            if (query[0] == 0) {
                combined[m+query[1]]+=query[2];
            }
            else if (query[0] == 1) { 
                int temp = twoSum2(combined,query[1],m,n);
                result.push_back(temp);
            }
        }

        return result;

    }

    vector<int> twoSum(vector<int> &nums, int target) {
        vector<int> a = {83,2,32,99,80,15};
        vector<int> b = {2,81,14,12,81,21,17,45};
        vector<vector<int>> queries = {{0,0,46},{0,7,57},{1,113},{1,23},{0,2,49},{1,104},{0,4,91}};

        vector<int> ans = solve(a,b,queries);
        for (auto x:ans)
            cout<<x<<" ";

        cout<<endl;

        return {};
    }

    

};
Leave a Comment