# "Leetcode" Array-2 (628)-the maximum number of three in the array is multiplied

Source: Internet
Author: User

The topic is not difficult:

Idea one (sort takes both ends)

Sort first, and the last three numbers multiply. (Soon I thought of it, but didn't want to be comprehensive [??])

Defect: Without considering the case of negative numbers, when there are at least two negative numbers, it is necessary to determine the maximum number multiplied by two minimum negative numbers and three maximum number to return the large one.

The code is as follows:

``` Public class Solution {    publicint maximumproduct (int[] nums) {        Arrays.sort (nums );         return Math.max (nums[nums.length-1] * nums[nums.length-2] * nums[nums.length-3], nums[0] * nums[1] * nums[nums.length-1

]);}    }
```

Complexity analysis is mainly a sort of waste of comparison

Complexity of Time: O (N*LOGN)

Space complexity: O (N*LOGN)

Idea two (not sorted, only traversed once, save only three largest, and two smallest)

This topic get the representation of the maximum and minimum number of new skills. Integer.max_value Integer.min_value

Pay attention to the update can not only judge one, the other to judge, or lose the solution. The following procedure explains.

(Error code demonstration)

`1  Public classSolution {2      Public intMaximumproduct (int[] nums) {3         if(Nums = =NULL|| Nums.length < 1) {4             return-1;5         }6         intMin1 =Integer.max_value;7         intMin2 =Integer.max_value;8         intMax1 =Integer.min_value;9         intMAX2 =Integer.min_value;Ten         intMax3 =Integer.min_value; One          A          for(inti:nums) { -             if(I <min1) { -Min2 =min1; theMin1 =i; -             }  -             if(I >max1) { -Max3 =Max2; +MAX2 =max1; -Max1 =i; +             } A         } at         returnMath.max (MAX1 * max2 * max3, min1 * min2 *max3); -     } -}`

(the correct code) Also note that the equal sign in the inequality cannot be omitted, because there may be equal conditions.

`1  Public classSolution {2      Public intMaximumproduct (int[] nums) {3         if(Nums = =NULL|| Nums.length < 1) {4             return-1;5         }6         intMin1 =Integer.max_value;7         intMin2 =Integer.max_value;8         intMax1 =Integer.min_value;9         intMAX2 =Integer.min_value;Ten         intMax3 =Integer.min_value; One          A          for(inti:nums) { -             if(I<=min1) { -Min2 =min1; theMin1 =i; -}Else if(I >= min1 && i <=min2) { -Min2 = i;//don't forget to update min2 Oh, the following same ~ -             } +             if(I >=max1) { -Max3 =Max2; +MAX2 =max1; AMax1 =i; at}Else if(I <= max1 && i >=max2) { -Max3 =Max2; -MAX2 =i; -}Else if(I <= max2 && i >=max3) { -Max3 =i; -             } in         } -         returnMath.max (MAX1 * max2 * max3, min1 * min2 *max1); to     } +}`

Analysis of complexity

Time complexity: O (N)

Space complexity: O (1)

"Leetcode" Array-2 (628)-the maximum number of three in the array is multiplied

Related Keywords:

### Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.