📅  最后修改于: 2023-12-03 15:12:42.499000             🧑  作者: Mango
GATE-CS-2015(Set 1)-Question 3 is a coding problem that requires you to implement a function that takes in a list of numbers and returns the maximum product of any three numbers in the list.
Given a list of integers, find the maximum product of any three numbers.
Input: [1, 2, 3, 4, 5, -6]
Output: 120
To solve this problem, we need to consider three cases:
The answer to the problem will be the maximum of these three cases.
def maximum_product_of_three_numbers(nums):
nums.sort()
n = len(nums)
case1 = nums[n - 1] * nums[n - 2] * nums[n - 3]
case2 = nums[0] * nums[1] * nums[n - 1]
case3 = nums[0] * nums[1] * nums[2]
return max(case1, case2, case3)
The time complexity of this solution is O(n log n) because of the sorting algorithm used.
The space complexity of this solution is O(1) because we are not using any extra space to store values.