题解 | 01序列

01序列

https://www.nowcoder.com/practice/b0c948dbe577485598b982a430d65c39

m = int(input())
list1 = list(map(int, input().split()))
n = int(input())
count = 0
i = 0

while i < m:
    if list1[i] == 0:
        j = i + 1
        while j < m and list1[j] == 0:
            j += 1

        # 判断这段 0 是不是在边界
        left_empty = (i == 0)
        right_empty = (j == m)

        length = j - i
        if left_empty or right_empty:
            # 在边上:(length + 1) // 2
            cnt = (length + 1) // 2
        else:
            # 在中间被1夹住:(length - 1) // 2
            cnt = (length - 1) // 2

        count += cnt
        i = j + 1
    else:
        i += 1

print('true' if count >= n else 'false')

全部评论

相关推荐

不愿透露姓名的神秘牛友
03-09 19:13
求你们别卷了的大学生...:你不骂他,我就要骂你了
今天你投了哪些公司?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务