Editorial for Longest One Chain


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Penguin60

C++


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    int localMax = 0;
    int globalMax = 0;

    string res; cin >> res;

    for (int i = 0; i < res.size(); i++) {
        if (res[i] == '0') {
            localMax = 0;
        } else {
            localMax++;
            globalMax = max(globalMax, localMax);
        }
    }

    cout << globalMax << "\n";
}

Java


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        sc.nextLine();
        String res = sc.nextLine();

        int localMax = 0;
        int globalMax = 0;

        for (int i = 0; i < res.length(); i++) {
            if (res.charAt(i) == '0') {
                localMax = 0;
            } else {
                localMax++;
                globalMax = Math.max(globalMax, localMax);
            }
        }

        System.out.println(globalMax);
        sc.close();
    }
}

Python


n = int(input())
res = input()

local_max = 0
global_max = 0

for char in res:
    if char == '0':
        local_max = 0
    else:
        local_max += 1
        global_max = max(global_max, local_max)

print(global_max)

Comments

There are no comments at the moment.