Skip to content

485. Max Consecutive Ones.cpp #175

@PralayeshMukherjee

Description

@PralayeshMukherjee

Problem Number

485

Problem Title

Max Consecutive Ones

LeetCode Link

https://leetcode.com/problems/max-consecutive-ones

Contribution Checklist

  • I have written the Approach section.
  • I have written the Intuition section.
  • I have included a working C++ solution.
  • I will raise a PR and ensure the filename follows the convention [Number]. [Problem Title].cpp.

Approach

First I have to init
then iterate and then return

Intuition

have to find out the maximum consecutive once

Solution in C++

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int max = 0;
        int count = 0;
        for(int i=0;i<nums.size();i++){
            if(nums[i]==1){
                count++;
                if(max<count){
                    max = count;
                }
            }else{
                count = 0;
            }
        }
        return max;
    }
};

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions