Загрузка...

485. Max Consecutive Ones | Best Linear Solution Explained in C Plus

Title: 485. Max Consecutive Ones | Best Linear Solution Explained in C Plus Plus | LeetCode Solution

Description:

In this video, I solve LeetCode 485 Max Consecutive Ones using an efficient linear traversal approach in C Plus Plus.

Problem Explanation

Optimized Approach

Dry Run

Time and Space Complexity

Clean STL Based Code

Approach:

We traverse the array and count consecutive ones.

Whenever we encounter zero, we reset the count.

At every step, we maintain the maximum consecutive count found so far.

Algorithm:

1. Initialize count equals 0 and maxi equals 0

2. Traverse the array

3. If current element equals 1 increment count

4. Update maximum count using max function

5. If current element equals 0 reset count to 0

Time Complexity O of N

Space Complexity O of 1

GitHub
https://github.com/atulXdev

LeetCode Profile
https://leetcode.com/u/atul_singh_cg/

LinkedIn
https://www.linkedin.com/in/atul-singh-987b0b394/

leetcode dsa cpp maxconsecutiveones arrays codinginterview striverdsa leetcode485 datastructures algorithms

Видео 485. Max Consecutive Ones | Best Linear Solution Explained in C Plus канала Atul Singh
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять