A concise JAVA solution.

  • 0
    public class Solution {
        public int maxProduct(int[] nums) {
            int Max = 1;
            int Min = 1;
            int ans = Integer.MIN_VALUE;
            for (int k=0; k<nums.length; k++){
                int newMax = Math.max(Math.max(nums[k]*Max, nums[k]*Min),nums[k]);
                int newMin = Math.min(Math.min(nums[k]*Max, nums[k]*Min),nums[k]);
                ans = Math.max(ans, newMax);
                Max = newMax;
                Min = newMin;
            return ans;

    Why this solution is not as efficient as other solutions shared here? I think my solution also takes O(N) time. Any suggestions to improve it?

Log in to reply

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