leetcode

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

0525.cpp (468B)


      1 class Solution {
      2   public:
      3     int findMaxLength(const vector<int> &nums) const {
      4         unordered_map<int, int> um;
      5         int res = 0, sum = 0;
      6 
      7         um.emplace(0, -1);
      8         for (int i = 0; i < size(nums); i++) {
      9             sum += nums[i] ? 1 : -1;
     10             auto it = um.find(sum);
     11             if (it != end(um))
     12                 res = max(res, i - it->second);
     13             else
     14                 um.emplace(sum, i);
     15         }
     16 
     17         return res;
     18     }
     19 };