C++ O(N) space O(N) time


  • 1
    J
    class Solution {
    public:
        int findMaxLength(vector<int>& nums) {
            unordered_map<int, int> mp;
            int sum = 0, res = 0;
            mp[0] = -1;
            for (int i = 0; i < nums.size(); i++) {
                sum += nums[i] ? 1 : -1;
                if (mp.find(sum) == mp.end()) {
                    mp[sum] = i;
                } else {
                    res = max(res, i - mp[sum]);
                }
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.