LeetCode #525

Contiguous Array

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        int N = (int)nums.size();
        vector<int> idx(2 * N + 1, -2);
        idx[N] = -1;
        int ans = 0, S = 0;
        for(int i = 0; i < N; ++i) {
            S += nums[i] ? 1 : -1;
            if(idx[S + N] == -2) idx[S + N] = i;
            else ans = max(ans, i - idx[S + N]);
        }
        return ans;
    }
};
auto _=[]() {ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.