Why does this attempt fail at test case 20?


  • 0
    2

    why does it fail test case 20?

    Output: 3323
    Expected: 2840

    public class Solution {
        public int findMaxConsecutiveOnes(int[] nums) {
            int counter = 0;
            int maxConsecutiveOnes = 0;
            
            for(int i = 0; i < nums.length; i++){
                if(nums[i] == 1){
                    counter+=1;
                }else{
                    // element == 0
                    
                    if(counter != 0 && counter > maxConsecutiveOnes){
                        maxConsecutiveOnes = counter;
                        counter = 0;
                    }
                }
            }
            
            if(counter > maxConsecutiveOnes){
                maxConsecutiveOnes = counter;
            }
            
            return maxConsecutiveOnes;
        }
    }
    

  • 0
    D

    The right code should be like this :
    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 1){
    counter+=1;
    }else{
    // element == 0

                **if(counter != 0 && counter > maxConsecutiveOnes){
                    maxConsecutiveOnes = counter;  
                }
                counter = 0;**
            }
        }
    

    You can use this example to think about it.
    int[] nums = {1,1,1,0,1,1,0,1,1};
    The right answer is 3, but your result is 4.


  • 0
    D
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.