HomeLeetcode628. Maximum Product of Three Numbers - Leetcode Solutions

628. Maximum Product of Three Numbers – Leetcode Solutions

Description:

Given an integer array numsfind three numbers whose product is maximum and return the maximum product.

Examples:

Example 1:

Input: nums = [1,2,3]
Output: 6

Example 2:

Input: nums = [1,2,3,4]
Output: 24

Example 3:

Input: nums = [-1,-2,-3]
Output: -6

Solution in Python:

Steps to Solve the Problem:

  1. Sort the Array:
    • By sorting the array, we can easily access the largest and smallest numbers.
  2. Consider Two Cases:
    • The maximum product can be achieved in two ways:
      1. The product of the three largest numbers.
      2. The product of the two smallest numbers (which can be negative, resulting in a positive product when multiplied) and the largest number.
  3. Return the Maximum of the Two Cases:
    • Compute both potential products and return the maximum of the two.
Python
from typing import List

class Solution:
    def maximumProduct(self, nums: List[int]) -> int:
        # Step 1: Sort the array
        nums.sort()
        
        # Step 2: Compute the product of the three largest numbers
        max1 = nums[-1] * nums[-2] * nums[-3]
        
        # Step 3: Compute the product of the two smallest numbers and the largest number
        max2 = nums[0] * nums[1] * nums[-1]
        
        # Step 4: Return the maximum of the two computed products
        return max(max1, max2)

Solution in Javascript:

JavaScript
/**
 * @param {number[]} nums
 * @return {number}
 */
var maximumProduct = function(nums) {
    // Step 1: Sort the array
    nums.sort((a, b) => a - b);
    
    // Step 2: Compute the product of the three largest numbers
    const max1 = nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3];
    
    // Step 3: Compute the product of the two smallest numbers and the largest number
    const max2 = nums[0] * nums[1] * nums[nums.length - 1];
    
    // Step 4: Return the maximum of the two computed products
    return Math.max(max1, max2);
};

Solution in Java:

Java
import java.util.Arrays;

class Solution {
    public int maximumProduct(int[] nums) {
        // Step 1: Sort the array
        Arrays.sort(nums);
        
        // Step 2: Compute the product of the three largest numbers
        int max1 = nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3];
        
        // Step 3: Compute the product of the two smallest numbers and the largest number
        int max2 = nums[0] * nums[1] * nums[nums.length - 1];
        
        // Step 4: Return the maximum of the two computed products
        return Math.max(max1, max2);
    }
}

Solution in C#:

C#
public class Solution {
    public int MaximumProduct(int[] nums) {
        // Step 1: Sort the array
        Array.Sort(nums);
        
        // Step 2: Compute the product of the three largest numbers
        int max1 = nums[nums.Length - 1] * nums[nums.Length - 2] * nums[nums.Length - 3];
        
        // Step 3: Compute the product of the two smallest numbers and the largest number
        int max2 = nums[0] * nums[1] * nums[nums.Length - 1];
        
        // Step 4: Return the maximum of the two computed products
        return Math.Max(max1, max2);
    }
}

Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments

Popular