Leetcode Problem 628. Maximum Product of Three Numbers

628. Maximum Product of Three Numbers

Leetcode Solutions

Approach: Single Scan

  1. Initialize three variables to store the largest numbers (max1, max2, max3) and two variables to store the smallest numbers (min1, min2).
  2. Iterate through each number in the array.
  3. Update max1, max2, max3 if the current number is larger than any of them.
  4. Similarly, update min1 and min2 if the current number is smaller than any of them.
  5. After the iteration, calculate the product of max1, max2, and max3.
  6. Calculate the product of min1, min2, and max1.
  7. Return the maximum of the two products calculated in steps 5 and 6.
UML Thumbnail

Approach: Using Sorting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...