Campbell Ritchie wrote:Welcome to the Ranch
Why are you not using the ready‑made functions? The max function has been used millions of times and that tests its semantics against its contract as in its documentation.
It is probably better to copy'n'paste your code into the code tags, but you appear to have used the tags correctly. That technique will preserve the formatting you have in the original.
Please explain the algorithm you are using: is this for finding consecutive 1s in an array? It is not obvious what the algorithm is from the code, but I can see at least one major error without actually looking.
Thank you for welcoming me! I intend to be active on this forum along with other Java forums to immerse myself world of programming!
Problem statement:
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input: [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.
Note:
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
What the algorithm does is it iterates for the array, increments the counter until it hits an element which does not equal "1". It proceeds to check current max 'count' value against previously 'max count' value. If it's grater, swap and set counter back to zero and resume.
Another way to implement Math.max was to use the following code:
But I still doesn't understand the way I wrote it initially worked.