forked from qiyuangong/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
628_Maximum_Product_of_Three_Numbers
- Loading branch information
1 parent
a845d79
commit ede4a59
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
public class Solution { | ||
/*public int maximumProduct(int[] nums) { | ||
Arrays.sort(nums); | ||
return Math.max(nums[0] * nums[1] * nums[nums.length - 1], nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3]); | ||
}*/ | ||
|
||
public int maximumProduct(int[] nums) { | ||
int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE; | ||
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE; | ||
for (int n: nums) { | ||
if (n <= min1) { | ||
min2 = min1; | ||
min1 = n; | ||
} else if (n <= min2) { // n lies between min1 and min2 | ||
min2 = n; | ||
} | ||
if (n >= max1) { // n is greater than max1, max2 and max3 | ||
max3 = max2; | ||
max2 = max1; | ||
max1 = n; | ||
} else if (n >= max2) { // n lies betweeen max1 and max2 | ||
max3 = max2; | ||
max2 = n; | ||
} else if (n >= max3) { // n lies betwen max2 and max3 | ||
max3 = n; | ||
} | ||
} | ||
return Math.max(min1 * min2 * max1, max1 * max2 * max3); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution(object): | ||
# def maximumProduct(self, nums): | ||
# """ | ||
# :type nums: List[int] | ||
# :rtype: int | ||
# """ | ||
# nums.sort() | ||
# # Check min1*min2*max1 and max1*max2*max3 | ||
# return max(reduce(lambda x, y: x * y, nums[:2]) * nums[-1], | ||
# reduce(lambda x, y: x * y, nums[-3:])) | ||
|
||
def maximumProduct(self, nums): | ||
min1 = min2 = float('inf') | ||
max1 = max2 = max3 = float('-inf') | ||
for num in nums: | ||
if num <= min1: | ||
min2 = min1 | ||
min1 = num | ||
elif num <= min2: | ||
min2 = num | ||
if num >= max1: | ||
max3 = max2 | ||
max2 = max1 | ||
max1 = num | ||
elif num >= max2: | ||
max3 = max2 | ||
max2 = num | ||
elif num >= max3: | ||
max3 = num | ||
return max(min1 * min2 * max1, max1 * max2 * max3) |