Longest Consecutive Ones
Python
Medium
5 views
Problem Description
Read n bits (0/1). Compute longest consecutive 1s length.
Input Format
First line n. Second line n bits.
Output Format
One integer maxLen.
Sample Test Case
Input:
10
1 1 0 1 1 1 0 1 1 0
Official Solution
import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
a=list(map(int,p[1:1+n]))
best=0
cur=0
for v in a:
if v==1:
cur+=1
if cur>best:
best=cur
else:
cur=0
sys.stdout.write(str(best))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!