1. 程式人生 > >[Swift]LeetCode628. 三個數的最大乘積 | Maximum Product of Three Numbers

[Swift]LeetCode628. 三個數的最大乘積 | Maximum Product of Three Numbers

pan fun UNC count nbsp fin 給定 所有 row

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example 1:

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

Example 2:

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

Note:

  1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
  2. Multiplication of any three numbers in the input won‘t exceed the range of 32-bit signed integer.

給定一個整型數組,在數組中找出由三個數組成的最大乘積,並輸出這個乘積。

示例 1:

輸入: [1,2,3]
輸出: 6

示例 2:

輸入: [1,2,3,4]
輸出: 24

註意:

  1. 給定的整型數組長度範圍是[3,104],數組中所有的元素範圍是[-1000, 1000]。
  2. 輸入的數組中任意三個數的乘積不會超出32位有符號整數的範圍。

136ms
 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         if(nums.count == 3){ return
nums[0] * nums[1] * nums[2] } 4 let sorted = nums.sorted(by: >) 5 if(sorted[sorted.count - 1] >= 0 || sorted[0] < 0){ 6 return sorted[0] * sorted[1] * sorted[2] 7 } 8 //只有一個負數的情況,取三個正數 9 if(sorted[sorted.count - 1] < 0 && sorted[sorted.count - 2
] > 0){ 10 return sorted[0] * sorted[1] * sorted[2] 11 } 12 //有多個負數情況,比較 13 let neg = sorted[sorted.count - 2] * sorted[sorted.count - 1] 14 let pos = sorted[1] * sorted[2] 15 return sorted[0] * max(neg, pos) 16 } 17 }

180ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         var nums = nums
 4         nums.sort { $0 > $1 }
 5         
 6         let count = nums.count
 7         return max(nums[0] * nums[1] * nums[2],
 8                    nums[0] * nums[count - 1] * nums[count - 2])
 9     }
10 }

252ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3     if nums.count < 3 {
 4         return 0
 5     }
 6     var numsTemp : [Int] = nums.sorted()
 7     if numsTemp[1] > 0 {
 8         return numsTemp.last! * numsTemp[numsTemp.count - 2] * numsTemp[numsTemp.count - 3]
 9     } else {
10         if numsTemp.last! <= 0 {
11             return numsTemp.last! * numsTemp[numsTemp.count - 2] * numsTemp[numsTemp.count - 3]
12         } else {
13             return numsTemp.last! * numsTemp[numsTemp.count - 2] * numsTemp[numsTemp.count - 3] > numsTemp.first! * numsTemp[1] * numsTemp.last! ? numsTemp.last! * numsTemp[numsTemp.count - 2] * numsTemp[numsTemp.count - 3] : numsTemp.first! * numsTemp[1] * numsTemp.last!
14         }
15      }
16    }
17 }

Runtime: 272 ms

Memory Usage: 19 MB
 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         var mx1:Int = Int.min
 4         var mx2:Int = Int.min
 5         var mx3:Int = Int.min
 6         var mn1:Int = Int.max
 7         var mn2:Int = Int.max
 8         for num in nums
 9         {
10             if num > mx1
11             {
12                 mx3 = mx2
13                 mx2 = mx1
14                 mx1 = num
15             }
16             else if num > mx2
17             {
18                 mx3 = mx2
19                 mx2 = num
20             }
21             else if num > mx3
22             {
23                 mx3 = num
24             }
25             if num < mn1
26             {
27                 mn2 = mn1
28                 mn1 = num
29             }
30             else if num < mn2
31             {
32                 mn2 = num
33             }            
34         }
35         return max(mx1 * mx2 * mx3, mx1 * mn1 * mn2)
36     }
37 }

320ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         if nums.count < 3 { return Int.min }
 4         var max1 = Int.min
 5         var max2 = max1
 6         var max3 = max1
 7         var min1 = Int.max
 8         var min2 = Int.max
 9         
10         nums.forEach {
11             if $0 >= max1 {
12                 max3 = max2
13                 max2 = max1
14                 max1 = $0
15             } else if $0 >= max2 {
16                 max3 = max2
17                 max2 = $0
18             } else if $0 > max3 {
19                 max3 = $0
20             }
21             
22             if $0 <= min1 {
23                 min2 = min1
24                 min1 = $0
25             } else if $0 <= min2 {
26                 min2 = $0
27             }
28         }
29             let val1 = max1 * max2 * max3
30             let val2 = min1 * min2 * max1
31         
32             return max(val1, val2)
33     }   
34 }

388ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         
 4         let sorted = nums.sorted()
 5         
 6         if sorted[sorted.count - 1] == 0 {
 7             return 0
 8         } else if sorted[sorted.count - 1] < 0 {
 9             return sorted[0] * sorted[1] * sorted[sorted.count - 1]
10         } else if sorted[0] >= 0 {
11             return sorted[sorted.count - 1] * sorted[sorted.count - 2] * sorted[sorted.count - 3]
12         } else {
13             return max(sorted[sorted.count - 1] * sorted[sorted.count - 2] * sorted[sorted.count - 3],
14                       sorted[0] * sorted[1] * sorted[sorted.count - 1])
15         }
16     }
17 }

392ms

1 class Solution {
2     func maximumProduct(_ nums: [Int]) -> Int {
3         let sorted = nums.sorted()
4         return max(sorted[nums.count - 1] * sorted[nums.count - 2] * sorted[nums.count - 3], sorted[nums.count - 1] * sorted[0] * sorted[1])
5     }
6 }

404ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         if nums.count == 3 {
 4             return nums.reduce(1, *)
 5         }
 6         var sort = nums.sorted(by: >)
 7         let one = sort[0] * sort[1] * sort[2]
 8         let two = sort[0] * sort[nums.count - 1] * sort[nums.count - 2]
 9         
10         return max(one, two)
11     }
12 }

440ms

 1 class Solution {
 2     func maximumProduct(_ nums: [Int]) -> Int {
 3         let sortGreatestToLeast: [Int] = nums.sorted(by: >)
 4         let greatestThreeNums: [Int] = Array(sortGreatestToLeast.prefix(3))
 5         let leastTwoNums: [Int] = Array(sortGreatestToLeast.suffix(2))
 6         let productFromGreatest: Int = greatestThreeNums.reduce(1, { x, y in x * y })
 7         let productFromBoth: Int = leastTwoNums.reduce(1, { x, y in x * y }) * greatestThreeNums.first!
 8         return productFromGreatest > productFromBoth ? productFromGreatest : productFromBoth
 9     }
10 }

[Swift]LeetCode628. 三個數的最大乘積 | Maximum Product of Three Numbers