Why not just check the number of negative signs?

  • 2

    Since all the elements in the array are integers. If there are even number of negative elements, the maximum product will be the multiplication of all the elements. If there are odd number of negative elements, we can just compare the products of the following two:

    1. multiplication of all the elements until we find the last negative element.
    2. multiplication of all the elements starting after the first negative element.
      Both cases contain even number of negative elements and the result will be positive. Is this a correct analysis?

  • 3

    You're on the right track but make sure you handle the case in which arrays contains zeros.

  • 0

    I think in exactly the same way. But I got caught for a lot of corner cases when zeros are involved. Especially when you have zeros at the beginning and ending of an array. You end up writing more lines of codes than the common way.

  • 0

    Yes... My solution contains about 100 lines of code. Pay too much attentions to zeros and negatives.

Log in to reply

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