code >>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
class Solution {
public:
int findMaxLength(vector<int>& nums) {
const int numSize = (int)nums.size();
unordered_map<int, vector<int>> hashMap;
int result = 0;
hashMap[0].emplace_back(-1);
int accum = 0;
for (int i = 0; i < numSize; i++)
{
if (nums[i] == 0)
{
accum = accum + -1;
hashMap[accum].push_back(i);
}
else
{
accum = accum + 1;
hashMap[accum].push_back(i);
}
}
for (const auto& ind_hash : hashMap)
{
int diff = ind_hash.second.back() - ind_hash.second.front();
if (diff > result)
result = diff;
}
return result;
}
};
|
'C' 카테고리의 다른 글
c++ leetcode climbing stairs DP solution(Top-down and bottom-up) (0) | 2021.07.24 |
---|---|
c++ leetcode 939 minimum rectangle area o(n^2) solution code (0) | 2021.07.24 |
c++ leetcode 380 Insert Delete GetRandom o(1) solution code (0) | 2021.07.17 |
c++ leetcode top-k frequent elements O(nlogn) solution (0) | 2021.07.16 |
c++ leetcode top k frequent words (sort and custom comparison) solution (0) | 2021.07.16 |