Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不排序求解三个数组元素的最大乘积_Java_Algorithm_Sorting - Fatal编程技术网

Java 不排序求解三个数组元素的最大乘积

Java 不排序求解三个数组元素的最大乘积,java,algorithm,sorting,Java,Algorithm,Sorting,关于三个任务的MaxProductOfThree有多种答案,其中大多数涉及排序算法 问题是: 给出了一个由N个整数组成的非空零索引数组 问题是从给定数组中找出3个元素的最大乘积 阵列的长度介于3到100000之间 数组A的每个元素都是范围内的整数[−1000..1000] expected worst-case time complexity is O(N*log(N)); expected worst-case space complexity is O(1), 超出输入

关于三个任务的MaxProductOfThree有多种答案,其中大多数涉及排序算法

问题是:

给出了一个由N个整数组成的非空零索引数组

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

阵列的长度介于3到100000之间

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

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

    expected worst-case space complexity is O(1), 
超出输入存储(不计算输入参数所需的存储)。 例如:

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

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

/*获得3的最大乘积的方法基本上是在数组上只需1次迭代,就可以从数组中找到最大的3个数字,从数组中找到最小的2个数字。以下是java代码:*/
/*    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;

    } 
int解决方案(int[]a){ /*使用max int初始化最小值,以避免数组中出现极端最大值和返回错误最小值0的情况*/ int min1=整数的最大值; int min2=整数的最大值; /*最大初始化的逻辑相同,但为了避免数组中的极端最小值和错误的0最大值,当然要反转*/ int max1=整数.MIN_值; int max2=整数.MIN_值; int max3=整数.MIN_值; //数组上的迭代 for(int i=0;imax1){ //如果a[i]大于最大值,则开始链式反应, //max3将是max2,max2将是实际max1,max1将是[i] max3=max2; max2=max1; max1=a[i]; /*若电流a[i]不大于max1,我们测试它,看它是否大于秒 然后,上述相同逻辑适用于max2 amd max3*/ }else如果(a[i]>max2){ max3=max2; max2=a[i]; /*最后,若当前数组值不大于max1,且max2可能大于max3*/ }else如果(a[i]>max3){ max3=a[i]; } /*上面的逻辑与最大值在这里与最小值一起应用,但当然与之相反 从当前阵列中发现2个最小值*/ if(a[i]prod2?prod1:prod2; }
这里有一个O(n logn)解决方案。 首先我们对数组进行排序, 然后知道两个负数的值越大,正数越大,我们需要计算数组左侧的最大值,以及数组右侧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.array; 请参阅该文件的此链接。

私有静态int[]rev(int[]validData){
对于(int i=0;i

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

这是我对问题的解决方案,得到了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;
        }
    }
}
import java.util.*;
//您可以出于调试目的写入stdout,例如。
//System.out.println(“这是一条调试消息”);
类解决方案{
公共int解决方案(int[]A){
数组。排序(A);
int length=A.length;
int max=0;
如果(A[0]>0 | | A[length-1](A[length-2]*A[length-3])){
max=A[0]*A[1]*last乘数;
}否则{
max=A[length-2]*A[length-3]*last乘数;
}
返回最大值;
}
}
}

在编码问题中使用排序是可以的。这里有一个解决方案,让你100%。如果不进行分类,它会变得混乱,但肯定是可能的

#包括
#包括
#包括
#包括
使用名称空间std;
整数解(向量&A){
int abs1=数值限制::min();
int abs2=数值限制::min();
排序(A.begin(),A.end());
无符号整数大小=A.size()-1;
内部温度;
对于(无符号整数i=0;i 0)继续;
如果(abs(A[i])>=abs1){
温度=abs1;
abs1=abs(A[i]);
abs2=温度;
}else如果(abs(A[i])>=abs2){
abs2=abs(A[i]);
}
}
返回最大值(A[size]*A[size-1]*A[size-2],abs1*abs2*A[size]);
}
int main(){
向量检验={4,-6,3,4,5};
给你
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);
}
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;
        }
    }
}
#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;
}
#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;
}
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));
}
// 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]);
    }
}
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];
}
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);
}
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 }
}
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)
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)
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]);
}

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])
}
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;
}
    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;
} 
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);
}
    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)
    }