Skip to main content

Algorithms: Max Consecutive Ones

Challenge from LeetCode: count the maximum number of consecutive 1s in a list of 1s and 0s. I got it done pretty quickly, or so I thought. I had not accounted for what would happen upon the last iteration through the for loop. Reworked it and this is what I got: 


def checkMax(maxConsec, count):

        if count > maxConsec:

            return count, 0

        else:

            return maxConsec, 0

        

class Solution:    

    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:

        maxConsec = 0

        count = 0

       

        for index, num in enumerate(nums):

            if num == 1:  

                count += 1

                if index+1 == len(nums):

                    maxConsec, count = checkMax(maxConsec, count)

            else:

                maxConsec, count = checkMax(maxConsec, count)

                    

        return maxConsec



The memory usage of my code

 

Runtime. Oops.


I'm sure there are better ways to do this. I thought of putting all my counts in a list and checking for the largest value in the list once the for loop was complete. I wonder if that would make a difference to the runtime and memory usage?

Comments

Popular posts from this blog

Deviants in a normal world

It's definitely been a bit since I've seen this graphy. Anyone who has learnt about standard deviation knows this graph. Standard Deviation Standard deviation shows us how spread out all the values in a set are from the mean. The higher the standard deviation, the more spread out the values are over a wider range and the flatter this curve. In a normal distribution, most values are within 1 standard deviation from the mean(the green part of the graph). Apparently NumPy can calculate standard deviation too! import numpy numSet = [ *lots of numbers* ] numSetStdDev = numpy.std(numSet) Variance The variance also indicates how spread out the values in a set are. It measures the average degree to which each value differs from the mean. variance = standard deviation ^2 import numpy numSet = [ *lots of numbers * ] numSetVar = numpy.var(numSet) Source:  https://www.w3schools.com/python/python_ml_standard_deviation.asp

So long and thanks for all the fish! Part 1 of 2

I have been with the Tech Academy both as a software developer bootcamp student, as well as an employee. After my bootcamp, I was hired first as the live project instructor, and then as Live Project Director. This, I believe, gives me a unique point of view. I have absolutely no regrets and would join the bootcamp again. But there are a number of things I would do differently. What I have learnt as a former student 1. DO NOT WORK PART TIME.   I worked part-time(20-30hrs) during my bootcamp. I was up at 2.30-3.00am every day to work for several hours. I took a short nap, and then I took a 1hr bus ride down to campus. Studied for 7- 9 hours. Took a 1hr bus ride back home. Lather, rinse, repeat. I also had some family obligations. My weekends and half the summer were taken up caring for my young stepdaughter. I was completely exhausted by the end of the bootcamp and I didn't know if I could do more. Learning to program is HARD. You need to be fully focused. I am fortunate because I di...

Finding your roots

I tried working on the Algorithms I course on Coursera a while back and I had no idea what was going on so I never continued with it. I decided to give it another try now that I've read up a little on algorithms. It's still using a lot of my brain cells but I am slowly making my way through it.  Learning about Quick-unions in Java(from the course) Java seems almost identical to C#. I've forgotten most of what I've learnt about C# but, there's enough in this brain for me to read this. I had such a tough time understanding the root method. I wrote it out in my notebook and worked through it to figure out how it functions and I am amazed! That line is so simple yet complex. And it reminded me of a binary tree LeetCode challenge I was trying to work on with my colleagues some time ago. I had no idea what binary trees even were at that point. The challenge involved finding roots and tre...