TGViewer
Python | LeetCode Python | LeetCode @easy_python_task · 9.01K subscribers
Post #1599 1.01K
Задача: 485. Max Consecutive Ones
Сложность: easy

Дан бинарный массив nums, верните максимальное количество последовательных единиц в массиве.

Пример:
Input: nums = [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.


👨‍💻 Алгоритм:

1⃣Поддерживайте счетчик для подсчета единиц и увеличивайте его на 1 при встрече единицы.

2⃣Когда встречаете ноль, используйте текущий счетчик единиц для нахождения максимального количества последовательных единиц на данный момент, затем сбросьте счетчик единиц на 0.

3⃣В конце верните максимальное значение.

😎 Решение:
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count = 0
maxCount = 0
for num in nums:
if num == 1:
count += 1
else:
maxCount = max(maxCount, count)
count = 0
return max(maxCount, count)


Ставь 👍 и забирай 📚 Базу знаний
  • 👍 1
More from @easy_python_task
  1. Oct 18, 2026Post #2452
  2. Oct 17, 2026Задача: 409. Longest Palindrome Сложность: easy Если задана строка s, состоящая из строчны…
  3. Oct 17, 2026Задача: 1539. Kth Missing Positive Number Сложность: easy Дан массив arr из положительных…
  4. Oct 16, 2026Задача: 1236. Web Crawler Сложность: medium Учитывая url startUrl и интерфейс HtmlParser,…
  5. Oct 15, 2026Задача: 1043. Partition Array for Maximum Sum Сложность: medium Если задан целочисленный м…
  6. Oct 15, 2026Задача: 1474. Delete N Nodes After M Nodes of a Linked List Сложность: easy Вам дано начал…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →