Java Easy AC...


  • 19
        public int maximumProduct(int[] nums) {
            
             Arrays.sort(nums);
             //One of the Three Numbers is the maximum value in the array.
    
             int a = nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3];
             int b = nums[0] * nums[1] * nums[nums.length - 1];
             return a > b ? a : b;
        }
    

    python3

     def maximumProduct(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            nums.sort()
            a = nums[-1] * nums[-2] * nums[-3]
            b = nums[0] * nums[1] * nums[-1]
            return max(a,b)
    

  • 1
    A

    I tried...

    int[] sorted = Arrays.sort(nums);
    

    No wonder I couldn't get it through, that'll teach me.


  • 1

    @allanitis em......


  • 3
    P
    public class Solution {
        public int maximumProduct(int[] nums) {
            Arrays.sort(nums);
            int n = nums.length;
            return Math.max(nums[n-1] * nums[n-2] * nums[n-3], nums[0] * nums[1] * nums[n-1]);
        }
    }
    

  • 2

    Good job LeiYu!


  • 4

    @jaqenhgar Niubility :P


  • 0
    L
    This post is deleted!

  • 0
    M

    similar idea

    public class Solution {
        public int maximumProduct(int[] nums) {
            if (nums.length < 3) return 0;
            Arrays.sort(nums);
            int len = nums.length - 1;
            return Math.max(nums[0] * nums[1] * nums[len], Math.max(nums[0] * nums[1] * nums[2], nums[len] * nums[len - 1] * nums[len - 2]));
        }
    }
    

  • 0

    @MutouMan
    you can delete if condition because

    The length of the given array will be in range [3,104] 
    

    :)


  • 0
    N
    This post is deleted!

  • 1
    J

    The time complexity is O(nlgn) if u need Arrays.sort(). You can just pass through the whole array and find the positive max, second max, third max, and negative min, negative second min. That should be enough.


  • 0
    M

    @LeiYu said in Java Easy AC...:

    nums[0] * nums[1] * nums[nums.length - 1]

    why are we taking this into account?


  • 0
    Y

    @motianimohit720 If given [-4, -3, -2, -1, 5], then you can see~~~


  • 0
    L

    Same idea.

    public class Solution {
        public int maximumProduct(int[] nums) {        
            Arrays.sort(nums);
            int n = nums.length;        
            return Math.max(nums[n-1] * nums[n-2] * nums[n-3], nums[n-1] * nums[0] * nums[1]);              
        }
    }
    

  • 0

    your python code will TLE because of the sort() for a very long list ,the most quick is O(nlogn),we just want some max and min number ,so don't have to sort, we can use min and max ,which just cost O (n)

    class Solution(object):
        def maximumProduct(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            nums_new=nums[:]
            max_1=nums.pop(nums.index(max(nums)))
            max_2=nums.pop(nums.index(max(nums)))
            max_3=nums.pop(nums.index(max(nums)))
            min_1=nums_new.pop(nums_new.index(min(nums_new)))
            min_2=nums_new.pop(nums_new.index(min(nums_new)))
            a=max_1*max_2*max_3
            b=min_1*min_2*max_1
            return  max(a,b)
    

Log in to reply
 

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