leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2657.cpp (705B)


      1 // 2657. Find the Prefix Common Array of Two Arrays
      2 class Solution {
      3   public:
      4     vector<int> findThePrefixCommonArray(const vector<int> &A, const vector<int> &B) {
      5         vector<int> res(A.size());
      6         unordered_set<int> setA, setB;
      7         for (int i = 0, count = 0; i < A.size(); i++) {
      8             if (A[i] == B[i])
      9                 count++;
     10             else {
     11                 if (setB.count(A[i]))
     12                     count++;
     13                 else
     14                     setA.insert(A[i]);
     15                 if (setA.count(B[i]))
     16                     count++;
     17                 else
     18                     setB.insert(B[i]);
     19             }
     20             res[i] = count;
     21         }
     22         return res;
     23     }
     24 };