| 14 | class Solution { |
| 15 | public: |
| 16 | int findMaxLength(vector<int>& nums) { |
| 17 | if (nums.size() == 0) return 0; |
| 18 | vector<int> height (nums.size() + 1, 0); |
| 19 | int sum = 0; |
| 20 | unordered_map<int, pair<int, int>> map; |
| 21 | map[0].first = 0; |
| 22 | map[0].second = 0; |
| 23 | for (int i = 0; i < nums.size(); i ++ ) { |
| 24 | sum += nums[i] == 1 ? 1 : -1; |
| 25 | if (map.count(sum) == 0) { |
| 26 | map[sum].first = i + 1; |
| 27 | map[sum].second = 0; |
| 28 | } |
| 29 | else map[sum].second = i + 1; |
| 30 | } |
| 31 | int res = 0; |
| 32 | for (auto &&[x, y] : map) { |
| 33 | if (y.second != 0 and y.second - y.first > res) |
| 34 | res = y.second - y.first; |
| 35 | } |
| 36 | return res; |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | int main() { |