Accepted Java solution


  • 30
    public int maxProduct(int[] a) {
      if (a == null || a.length == 0)
        return 0;
    
      int ans = a[0], min = ans, max = ans;
      
      for (int i = 1; i < a.length; i++) {
        if (a[i] >= 0) {
          max = Math.max(a[i], max * a[i]);
          min = Math.min(a[i], min * a[i]);
        } else {
          int tmp = max;
          max = Math.max(a[i], min * a[i]);
          min = Math.min(a[i], tmp * a[i]);
        }
        
        ans = Math.max(ans, max);
      }
      
      return ans;
    }

Log in to reply
 

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