【问题标题】:solve maximum product of three array elements without sorting不排序求解三个数组元素的最大乘积
【发布时间】:2021-09-07 16:27:50
【问题描述】:

codility.com 上的 MaxProductOfThree 任务有多种答案,其中大部分都涉及排序算法。

问题是:

给定一个由 N 个整数组成的非空零索引数组 A。

问题是从给定数组中找到 3 个元素的最大乘积。

数组的长度在 3 到 100,000 之间

数组 A 的每个元素都是 [−1,000..1,000] 范围内的整数

    expected worst-case time complexity is O(N*log(N));

    expected worst-case space complexity is O(1), 

超出输入存储(不包括输入参数所需的存储)。 示例:

  a[0] = -3;
  a[1] = 7;
  a[2] = 2;
  a[3] = 1;
  a[4] = 5;
  a[5] = 7;

最大乘积为 a[1]*a[4]*a[5] = 245

除了涉及排序的 O(n log n) 方法之外,还有线性时间解决方案吗?

【问题讨论】:

  • 请更好地描述问题,输入是什么,预期输出是什么。例子会很棒。
  • 如果你只需要找到数组中最大的 3 个元素,是的,这在 O(n) 中是可能的:en.wikipedia.org/wiki/Selection_algorithm
  • 我也有答案,但我需要等待 8 小时才能发布,因为我是新成员。获得 3 的最大乘积的方法基本上是在数组的 1 次迭代中从数组中找到最大的 3 个数字和从数组中找到最小的 2 个数字。当然,我会在正确评论 8 小时后发布代码答案
  • @DanutClapa 如果您已经有了答案,为什么还要在 SO 上发布问题?
  • @DanutClapa 如果我没记错的话,问题表单中有一个复选框,上面写着“自己回答这个问题”,它允许您同时发布问题和答案。顺便说一句,您的问题不会因为您打算自己回答而被降低标准。您必须遵守 SO 准则,而您的问题缺乏质量。例如,至少您应该包含或解释问题陈述。

标签: java algorithm sorting


【解决方案1】:
/*    The method get the max product of 3 consists in basically find the biggest 3 numbers from the array and the smallest 2 numbers from the array in just 1 iteration over the array. Here is the java code:*/

    int solution(int[] a) {
/* the minimums initialized with max int to avoid cases with extreme max in array and false minims 0 minimums returned */

        int min1 = Integer.MAX_VALUE;
        int min2 = Integer.MAX_VALUE;
/* the same logic for maximum initializations but of course inverted to avoid extreme minimum values in array and false 0 maximums */

        int max1 = Integer.MIN_VALUE;
        int max2 = Integer.MIN_VALUE;
        int max3 = Integer.MIN_VALUE;

//the iteration over the array

        for (int i = 0; i < a.length; i++) {

//test if max1 is smaller than current array value

            if (a[i] > max1) {
            //if a[i] is greater than the biggest max then a chain reaction is started,
            // max3 will be max2, max2 will be actual max1 and max1 will be a[i]    
                max3=max2;
                max2=max1;
                max1=a[i];
/* in case if current a[i] isn't bigger than max1 we test it to see maybe is bigger than second
 max. Then the same logic from above is applied for the max2 amd max3 */

            }else if(a[i]>max2){
                max3 = max2;
                max2 = a[i];
/* finally if current array value isn't bigger than max1 and max2 maybe is greater than max3 */

            }else if(a[i]>max3){
                max3 = a[i];
            }

/* The logic from above with maximums is is applied here with minimums but of course inverted to
discover the 2 minimums from current array. */

            if (a[i] < min1) {
                min2 =min1;
                min1=a[i];
            } else if (a[i] < min2) {
                min2 = a[i];
            }
        }
/* after we discovered the 3 greatest maximums and the 2 smallest minimums from the array
we do the 2 products of 3 from the greatest maximum and the 2 minimums . This is necessary
because mathematically the product of 2 negative values is a positive value, and because of
this the product of min1 * min2 * max1 can be greater than max1 * max2 * max3
 and the product built from the the 3 maximums. */

        int prod1 = min1 * min2 * max1;
        int prod2 = max1 * max2 * max3;

//here we just return the biggest product

        return prod1 > prod2 ? prod1 : prod2;

    } 

【讨论】:

  • 我以类似的方式解决了它,但是您需要考虑仅输入负数,例如[-1,-2,-3,-4,-5]。有了这个解决方案,复杂度甚至是 O(N)
  • 我只考虑带有负数的数组。因此,如果您看一下该数组中的 3 个最大数字是 -1、-2、-3,则在 3 个最大最大值的情况下它们的乘积是负数,但仍然是最大乘积。
【解决方案2】:

排序数组中的最大乘积只有两种可能的选项。

1) 最大(最后)三个元素

2) 两个最小和最大元素的组合(如果是负元素,两个负数的乘积是正数,乘以数组的最大元素,如果是正数,可以产生最大的乘积)

所以解决方案是两者的最大值,仅此而已。以下在 Codility 上获得了 100/100。

// you can also use imports, for example:
// import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Arrays;

class Solution {

    public int solution(int[] A) {

        int N = A.length;
        Arrays.sort(A);

        /**
         * When we sort an array there are two possible options for the largest product
         * 1) The largest (the last) three elements
         * 2) Combination of two smallest and the largest elements
         * Logic of (1): Obvious
         * Logic of (2): A pair of negatives multiplied returns a positive, which in combination with 
         * the largest positive element of the array can give the max outcome.
         * Therefore we return the max of options (1) and (2)
         */
        return Math.max(A[0] * A[1] * A[N - 1], A[N - 1] * A[N - 2] * A[N - 3]);
    }
}

干杯

【讨论】:

  • 问题的标题和正文询问without sorting
  • 问题指定不排序是真的。然而,这个 Codility 练习在他们关于排序的章节中。伙计,如果在三行代码中有一个 100% 的解决方案,我完全赞成!太棒了!
  • 和你有同样的想法!拿着我的啤酒=))
【解决方案3】:

没有排序的 Kotlin。

不知道为什么这里有这么多回答忽略了问题标题“没有排序”!

    fun solution(A: IntArray): Int {
        // write your code in Kotlin
        if (A.size < 3) return -1

        var max1: Int = Int.MIN_VALUE
        var max2: Int = Int.MIN_VALUE
        var max3: Int = Int.MIN_VALUE
        var min1: Int = Int.MAX_VALUE
        var min2: Int = Int.MAX_VALUE

        A.forEach {
            when {
                it > max1 -> {
                    max3 = max2
                    max2 = max1
                    max1 = it
                }
                it > max2 -> {
                    max3 = max2
                    max2 = it
                }
                it > max3 -> {
                    max3 = it
                }
            }

            when {
                it < min1 -> {
                    min2 = min1
                    min1 = it
                }
                it < min2 -> {
                    min2 = it
                }
            }
        }

        return (min1 * min2 * max1).coerceAtLeast(max1 * max2 * max3)
    }

【讨论】:

    【解决方案4】:

    这是一个 O(n log n ) 的解决方案。 首先我们对数组进行排序, 然后知道两个具有很大价值的负数会产生一个更大的正数,我们需要计算数组左侧的最大值,以及数组右侧的 3 个元素的乘积,然后比较哪个更大。

    这是一个示例代码:

    // [1,2,3,4] = 24
      public int solution(int[] sortedArray) {
           Arrays.sort(sortedArray);
           int length = sortedArray.length;
           int  P, Q, R;
           int maximumLeft = Integer.MIN_VALUE, maximumRight = Integer.MIN_VALUE;
    
           P = sortedArray[length - 3];
           Q = sortedArray[length - 2];
           R = sortedArray[length - 1];
           maximumRight = P * Q * R;
    
           P = sortedArray[0];
           Q = sortedArray[1];
           R = sortedArray[length -1];
    
           maximumLeft = P * Q * R;
    
    
           return maximumRight > maximumLeft ? maximumRight : maximumLeft;
       }
    

    别忘了导入 java.util.Arrays; 请参阅此链接以获取 Java 文件。

    【讨论】:

    • 是的,但是是 O(n log n),这是执行此操作的标准方法。但我们能做得更好吗?答案是肯定的。看看我的解是O(n)。
    • 是的,你的解决方案在时间方面更好:),我喜欢!!
    • 谢谢 :) ,您的解决方案也正确且整洁。但是效率有点低:)。
    • 短版:短版:Arrays.sort(A);返回 Math.max(A[0]*A[1]*A[A.length-1], A[A.length-1]*A[A.length-2]*A[A.length-3] );
    • 一个小问题。 . .你调用一个变量maximumLeft,但实际上不是这样。这是最小的两个加上最大的。需要进行一些研究才能看到您实际上采用的方法与这里其他人使用的方法相同。使用不同的变量名,例如 tryTwoNegatives,会更清楚。
    【解决方案5】:

    你去吧,它不使用排序,仍然得到 100%。

    #include<limits>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int solution(vector<int> &A) {
        //Keep the absolute value for max 2 -ve num . As their mul will be +ve
        int abs1 = numeric_limits<int>::min();
        int abs2 = numeric_limits<int>::min();
    
        //Keep the max three num irrespective of sign
        int max1 = numeric_limits<int>::min();
        int max2 = numeric_limits<int>::min();
        int max3 = numeric_limits<int>::min();
    
        unsigned int size = A.size()-1;
    
        for (unsigned int i = 0; i <= size ; ++i) {
            if(A[i] > 0 ){
    
            } else if(abs(A[i]) >= abs1 ) {
                abs2 = abs1;
                abs1 = abs(A[i]);
            }else if(abs(A[i]) >= abs2 ) {
                abs2 = abs(A[i]);
            }
    
            if(A[i] >= max1 ){
                //Push max1s prev value to max2 and max2's prev val to max3
                max3 = max2;
                max2 = max1;
                max1 = A[i];
            } else if(A[i] >= max2 ) {
                max3 = max2;
                max2 = A[i];
            }else if(A[i] > max3 ) {
                max3 = A[i];
            }
        }
        // Either max 3 multiplication , or Max 2 negative num whose mul is +ve and the regular max
        return max(max1 * max2 * max3, abs1 * abs2 * max1);
    }
    
    
    int main(){
        vector<int> test = {-3, 1, 2, -2, 5, 6};
        cout << solution(test);
        return 0;
    }
    

    【讨论】:

      【解决方案6】:

      有很多很棒的答案,但我认为这个有一些优雅,也给出了100% on codility


      public static int solution(int[] A) {
          Arrays.sort(A);
          int F = 0, L = A.length - 1;
          int s1 = A[F] * A[F + 1] * A[F + 2];
          int s2 = A[F] * A[F + 1] * A[L];
          int s3 = A[F] * A[L - 1] * A[L];
          int s4 = A[L - 2] * A[L - 1] * A[L];
          return Math.max(Math.max(s1, s2), Math.max(s3, s4));
      }
      

      【讨论】:

      • 实际上s1s3 几乎永远不会是最大的。除非A.length = 3; 但在那种情况下你有s1 = s2 = s3 = s4 :) 所以你只需要return Math.max(s2, s4)+;
      【解决方案7】:

      这是我的解决方案,我只排序了所需的一个 https://app.codility.com/demo/results/training68T6KT-NY6/

      public int solution(int[] A) {
          // write your code in Java SE 8
          int result;
          for (int i = 0; i < 3; i++) {
              for (int j = i; j < A.length; j++) {
                  if (A[i] < A[j]) {
                      int temp = A[i];
                      A[i] = A[j];
                      A[j] = temp;
                  }
              }
          }
      
          for (int i = 0; i < 2; i++) {
              for (int j = 0; j < A.length - i; j++) {
                  if (A[A.length - 1 - i] > A[j]) {
                      int temp = A[A.length - 1 - i];
                      A[A.length - 1 - i] = A[j];
                      A[j] = temp;
                  }
              }
      
          }
          if ((A[A.length - 1] < 0) && A[A.length - 1] * A[A.length - 2] > 0) {
      
              result = A[0] * A[A.length - 1] * A[A.length - 2];
              if (result > A[0] * A[1] * A[2])
                  return result;
          }
          return A[0] * A[1] * A[2];
      }
      

      【讨论】:

      • 使用 pre 而不是 post incement/decrement 可以提高性能和内存使用率!
      【解决方案8】:

      这可能有效:

      int solution(vector<int>& A) {
          // write your code in C++14 (g++ 6.2.0)
          int missing = 1;
          vector<int> count(A.size());
      
          for (int n = 0; n < A.size(); n++) {
              if (A[n] > 0 && A[n] < (A.size() + 1)) {
                  count[A[n] - 1]++;
              }
          }
      
          for (int n = 0; n < A.size(); n++) {
              if (count[n] == 0) {
                  break;
              }
      
              missing++;
          }
      
          return missing;
      }
      

      【讨论】:

        【解决方案9】:

        Python 解决方案 - 100%

        def solution(A): 
            A.sort() 
            return max(A[0] * A[1] * A[-1] , A[-3] * A[-2] * A[-1])
        

        【讨论】:

        • 如果您的解决方案涉及 python,它是题外话,因为问题有 java 标签
        • 完全失败without sorting
        【解决方案10】:
        private static int[] rev(int[] validData) {
            for (int i = 0; i < validData.length / 2; i++) {
                int temp = validData[i];
                validData[i] = validData[validData.length - i - 1];
                validData[validData.length - i - 1] = temp;
            }
            return validData;
        }
        
        public static int solution(int[] A) {
            // write your code in Java SE 8
            long p = 0, q = 0, r = 0, max1 = Integer.MAX_VALUE, max2 = Integer.MAX_VALUE, res = 0;
        
            Arrays.sort(A);
            A = rev(A);
            int start = 0, end = A.length;
        
                //upper bound
                p = A[start];
                q = A[start + 1];
                r = A[start + 2];
                max1 = p * q * r;
        
        
                //lower bound
        
                q = A[end - 1];
                r = A[end - 2];
                max2 = p * q * r;
        
        
            return (int) Math.max(max1, max2);
        }
        

        有点晚了,但这种方法效率较低,但仍然很快。它反转数组,那么逻辑是,上限是第一个元素*两个连续,或者第一个元素*最后两个连续,两者中的任何一个都应该产生最大值。

        【讨论】:

          【解决方案11】:

          这是我应用于问题的解决方案得到 100%。

          import java.util.*;
          
          // you can write to stdout for debugging purposes, e.g.
          // System.out.println("this is a debug message");
          
          class Solution {
              public int solution(int[] A) {
                  Arrays.sort(A);
                  int length = A.length;
                  int max =0;
                  if(A[0]>0||A[length-1]<0){
                      max = (A[length-1]*A[length-2]*A[length-3]);
                      return max;
                  }else{
                      int lastMultiPlier = A[length-1];
                      if((A[0]*A[1])>(A[length-2]*A[length-3])){
                          max=A[0]*A[1]*lastMultiPlier;
                      }else{
                          max=A[length-2]*A[length-3]*lastMultiPlier;
                      }
                      return max;
                  }
              }
          }
          

          【讨论】:

            【解决方案12】:

            在 Codility 问题中使用排序是可以的。这是一个为您提供 100% 的解决方案。如果不排序,它会变得混乱,但绝对有可能。

            #include<limits>
            #include <vector>
            #include <iostream>
            #include <algorithm>
            using namespace std;
            int solution(vector<int> &A) {
                int abs1 = numeric_limits<int>::min();
                int abs2 = numeric_limits<int>::min();
                sort(A.begin(), A.end());
                unsigned int size = A.size()-1;
                int temp;
                for (unsigned int i = 0; i <= size ; ++i) {
                    if(A[i] > 0 ) continue;
                    if(abs(A[i]) >= abs1 ) {
                        temp = abs1;
                        abs1 = abs(A[i]);
                        abs2 = temp;
                    }else if(abs(A[i]) >= abs2 ) {
                        abs2 = abs(A[i]);
                    }
                }
                return max(A[size] * A[size-1] * A[size-2], abs1 * abs2 * A[size]);
            }
            
            
            int main(){
                vector<int> test = {-4, -6, 3, 4, 5};
                cout << solution(test);
                return 0;
            }
            

            【讨论】:

              【解决方案13】:

              使用 JavaScript (Node.js 8.9.4):

              function solution(A) {
                   // first we order it
                   A.sort((a, b) => (a - b));
              
                   // we see the first two possibilities and we compare them        
                   let val1 = A[A.length - 1] * A[A.length - 2] * A[A.length - 3]
              
                   let val2 = A[A.length - 1] * A[0] * A[1]
              
                   // we return the higher value
                   if (val1 > val2) { return val1 } else { return val2 }
              }
              

              【讨论】:

              • 请注意,Node.js 8 已超出其维护窗口,不再获得支持。您应该过渡到 Node.js 12 或 13。
              【解决方案14】:

              python 3,谢谢 DanutClapa,我首先做好了积极的工作,但你对最后两个消极的澄清是解决方案。

              def solution(arr):
              
                  if not arr:
                      return 0
              
                  if len(arr) == 3:
                      m   = 1
                      for i in arr:
                          m *= i
                      return m
                  else:
                      max_num       = min(arr)
                      second_max    = min(arr)
                      tercero_max   = min(arr)
              
                      min_num       = max(arr)
                      min_num_2     = max(arr)
              
                      for i in range(0, len(arr)):
              
                          if (arr[i] > max_num):
                              tercero_max = second_max
                              second_max  = max_num
                              max_num     = arr[i]
                          elif arr[i] > second_max:
                              tercero_max = second_max
                              second_max  = arr[i]
                          elif arr[i] > tercero_max:
                              tercero_max = arr[i]
              
                          if arr[i] < min_num:
                              min_num_2 = min_num
                              min_num   = arr[i]
                          elif arr[i] < min_num_2:
                              min_num_2 = arr[i]
              
                      return max( max_num * second_max * tercero_max,     max_num * min_num * min_num_2)
              

              【讨论】:

                【解决方案15】:

                没有排序...... ES6,但老实说:为什么没有排序?

                1. 找到最大的数 {max[0] > max[1] > max[2]}
                2. 找到最小的2个{min[0]
                3. 返回最大值 [max[0] * max[1] * max[0], max[0] * max[1] * max[2]]
                function answer(A) {
                  maxA = [...A]
                  minA = [...A]
                  max = [];
                  min = [];
                
                  max.push(Math.max(...maxA)); maxA.pop()
                  max.push(Math.max(...maxA)); maxA.pop()
                  max.push(Math.max(...maxA));
                  
                  min.push(Math.min(...minA)); minA.pop()
                  min.push(Math.min(...minA))
                
                  return Math.max(max[0] * max[1] * max[0], max[0] * max[1] * max[2])
                }
                

                【讨论】:

                  【解决方案16】:

                  这是我的 C# 解决方案。首先对数组进行排序,以获得所需的最大产品最大整数。 对于负整数,我们需要查看排序数组的前两个元素。 此解决方案获得 100%

                  public int solution(int[] A)
                  { 
                      var sorted = A.ToList(); 
                      sorted.Sort(); 
                  
                      var last = sorted[sorted.Count-1]*sorted[sorted.Count-2]*sorted[sorted.Count-3];
                      var firsttwo = sorted[0]*sorted[1]*sorted[sorted.Count-1];
                  
                      return Math.Max(last,firsttwo);
                  }
                  

                  【讨论】:

                  • 应该是java
                  • 是的,我知道,它与 c# 非常相似。
                  • 所以尝试改写成java
                  • 这里是 java 等价物: public int solution(int[] A) { Arrays.sort(A); var last = A[A.length-1]*A[A.length-2]*A[A.length-3]; var firsttwo = A[0]*A[1]*A[A.length-1];返回 Math.max(last,firsttwo); }
                  【解决方案17】:

                  100% 分数的 Python 解决方案

                  import sys
                  def solution(A):
                      if len(A) < 3:
                           return 0
                      min_value = sys.maxsize*-1
                      max_value = sys.maxsize
                      positive_nus = [min_value]*3
                      negative_nus =[max_value]*2
                  
                  
                      for i in range(0,len(A)):
                              if  A[i]> positive_nus[0]:
                                  positive_nus[2] = positive_nus[1]
                                  positive_nus[1]= positive_nus[0]
                                  positive_nus[0] = A[i]
                              elif  A[i] > positive_nus[1]:
                                  positive_nus[2] = positive_nus[1]
                                  positive_nus[1]= A[i]
                              elif  A[i] > positive_nus[2]:
                                  positive_nus[2] = A[i]
                              if A[i] < negative_nus[0]:
                                  negative_nus[1] = negative_nus[0]
                                  negative_nus[0] = A[i]
                              elif A[i] < negative_nus[1]:
                                  negative_nus[1] = A[i]
                  
                      sol1 = positive_nus[0]*positive_nus[1]*positive_nus[2]
                      sol2 = positive_nus[0]*negative_nus[0]*negative_nus[1]
                      return  max(sol1,sol2)
                  

                  【讨论】:

                    【解决方案18】:

                    @Slobodan Antonijević 回答的 JavaScript 解决方案

                    function solution(A) {
                        let N = A.length;
                        /* some time sort doesn't work as expected try passing your own 
                         sorting function to sort it in ascending order
                        */
                        A = A.sort((a,b) => (a-b));
                        return Math.max(A[0] * A[1] * A[N - 1], A[N - 1] * A[N - 2] * A[N - 3]);
                    }
                    
                    

                    【讨论】:

                    • 问题的标题和正文询问without sorting
                    【解决方案19】:

                    如果有人关心 C. 我之前尝试过快速排序,但由于性能问题得到了 88% 的结果。 所以我最终得到了一个高效(100%)但混乱的代码: https://app.codility.com/demo/results/trainingGT8RQR-FBM/

                        int solution(int A[], int N) {
                    
                    
                        int NEG[3]; NEG[0]=0; NEG[1] = 0; NEG[2]=0;    int p=-1;
                        int POS[3]; POS[0] = 0; POS[1] =0; POS[2] = 0; int n=-1;
                        int MAXIM[3]; MAXIM[0]=-1001; MAXIM[1]=-1001; MAXIM[2]=-1001; int m = -1;
                    
                        int i =0;
                    
                        for(i = 0 ; i < N ; i++)
                        {
                            if(A[i] < 0 && A[i] < NEG[2])
                            {
                                if(A[i] < NEG[0]) { NEG[2] = NEG[1]; NEG[1] = NEG[0];NEG[0] = A[i];}
                                else if(A[i] < NEG[1]) { NEG[2] = NEG[1]; NEG[1] = A[i];}
                                else if(A[i] < NEG[2]) NEG[2] = A[i];
                                if(n < 2) n++;
                            }        
                            else if(A[i] >= 0 && A[i] > POS[2])
                            {
                            
                                if(A[i] > POS[0]) {POS[2] = POS[1]; POS[1] = POS[0]; POS[0]=A[i];}
                                else if(A[i] > POS[1]) {POS[2] = POS[1]; POS[1] = A[i];}
                                else POS[2] = A[i]; 
                                if(p < 2) p++;
                            }
                    
                            if(A[i] <= 0 )
                            {
                            if(A[i] > MAXIM[0]){ MAXIM[2]=MAXIM[1];MAXIM[1]=MAXIM[0]; MAXIM[0]=A[i]; if(m<2)m++;}
                            else if(A[i]>MAXIM[1]){MAXIM[2]=MAXIM[1]; MAXIM[1]=A[i];if(m<2)m++;}
                            else if(A[i]>MAXIM[2]){MAXIM[2]=A[i]; if(m<2)m++;}
                            
                            
                            }
                        }
                    
                        int max =0, val_set =0;;
                        if( n >=1 && p>=0 )
                        {
                            int tmp = NEG[0] * NEG[1] * POS[0];
                            if(val_set == 0)
                                { max = tmp;
                                    val_set =1;
                                }
                            else    
                            if(tmp > max){
                                max = tmp;
                            } 
                        }
                        if( p > 1 )
                        {
                            int tmp = POS[0] * POS[1] * POS[2];
                            if(val_set == 0)
                                { max = tmp;
                                    val_set =1;
                                }
                            else
                            if(tmp > max )
                                {max = tmp;}
                        } 
                        else
                        if( n > 1)
                        {
                            int tmp = NEG[0] * NEG[1] * NEG[2];
                            if(val_set == 0)
                                { max = tmp;
                                    val_set =1;
                                }
                            else
                            if(tmp > max ){
                                max = tmp;}
                        }
                        if(m>1){
                        int temp = MAXIM[0] * MAXIM[1] * MAXIM[2];
                               if(val_set == 0)
                                { max = temp;
                                    val_set =1;
                                }
                            else if(temp > max){
                            max = temp;}
                        }  
                        return max;
                    } 
                    

                    【讨论】:

                      【解决方案20】:
                      • 按升序对数组进行排序
                      • Product1 = 排序后数组的最后 3 个数字的乘积
                      • Product2 = 排序数组的最后一个数字和前 2 个数字的乘积
                      • 返回最多两个以上产品
                      Javascript 中的

                      O(n) 解决方案可能是:

                      function solution(A) {
                          let sorted = A.sort((a, b) => a-b);
                          let max1 = A[A.length - 1] * A[A.length - 2] * A[A.length - 3];
                          let max2 = A[0] * A[1] * A[A.length - 1];
                          
                          return Math.max(max1, max2);
                      }
                      

                      【讨论】:

                        猜你喜欢
                        • 2019-10-24
                        • 2021-05-09
                        • 1970-01-01
                        • 2017-02-02
                        • 2023-03-09
                        • 2023-03-18
                        • 1970-01-01
                        • 2017-03-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多