Problem statement

/https://binarysearch.com/problems/Consecutive-Ones

Solution

Find place of the first and the last one and total number of ones.

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, nums):
        i1 = nums.index(1)
        i2 = nums[::-1].index(1)
        return i1 + i2 + nums.count(1) == len(nums)