• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Math.max To Be Or Not To Be

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

I'm been programming Java for over eight and striving to become a good programmer. People have told me I get stuck at 'stupid stuff' and really kills my tempo. I just like to know the inny gritty of things. Anyways, the problem:

Currently, I'm doing a LeetCode and I don't understand why version of code is giving me the wrong answer. The only difference is I don't use:


and use this instead to do the same job


Yet, for a very large input, I get different answer and I don't know what's going on. It's so simple yet I have no clue where it's breaking apart. The implementation:




PS: Sorry for the bad formatting, trying to get a hang of the forum.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Abdu samed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is not the same as

 
reply
    Bookmark Topic Watch Topic
  • New Topic