NC106. 三个数的最大乘积
描述
给定一个长度为 的无序数组 ,包含正数、负数和 0 ,请从中找出 3 个数,使得乘积最大,返回这个乘积。示例1
输入:
[3,4,1,2]
输出:
24
C++ 解法, 执行用时: 3ms, 内存消耗: 556KB, 提交时间: 2021-11-14
class Solution { public: /** * 最大乘积 * @param A int整型一维数组 * @param ALen int A数组长度 * @return long长整型 */ long long solve(int* A, int ALen) { // write code here if(ALen < 3) return 0; long long min_num[2] = {INT_MAX, INT_MAX}; long long max_num[3] = {INT_MIN, INT_MIN, INT_MIN}; for(int i=0; i<ALen; ++i){ if(A[i] < min_num[1]){ if(A[i] < min_num[0]){ min_num[1] = min_num[0]; min_num[0] = A[i]; } else min_num[1] = A[i]; } if(A[i] > max_num[2]){ if(A[i] > max_num[0]) { max_num[2] = max_num[1]; max_num[1] = max_num[0]; max_num[0] = A[i]; } else if(A[i] > max_num[1]){ max_num[2] = max_num[1]; max_num[1] = A[i]; } else max_num[2] = A[i]; } } return max(min_num[0]*min_num[1]*max_num[0], max_num[0]*max_num[1]*max_num[2]); } };
C++ 解法, 执行用时: 3ms, 内存消耗: 576KB, 提交时间: 2021-07-25
class Solution { public: /** * 最大乘积 * @param A int整型一维数组 * @param ALen int A数组长度 * @return long长整型 */ long long solve(int* A, int ALen) { long long max = LLONG_MIN, max2 = LLONG_MIN, max3 = LLONG_MIN; long long min = LLONG_MAX, min2 = LLONG_MAX; for (int i = 0; i < ALen; i++) { if (A[i] > max) { max3 = max2; max2 = max; max = A[i]; } else if (A[i] > max2) { max3 = max2; max2 = A[i]; } else if (A[i] > max3) max3 = A[i]; if (A[i] < min) { min2 = min; min = A[i]; } else if (A[i] < min2) min2 = A[i]; } long long MAX = max*max2*max3; long long MAX2 = max*min*min2; if (MAX > MAX2) return MAX; else return MAX2; // write code here } };
C++ 解法, 执行用时: 3ms, 内存消耗: 632KB, 提交时间: 2021-11-28
class Solution { public: /** * 最大乘积 * @param A int整型一维数组 * @param ALen int A数组长度 * @return long长整型 */ long long solve(int* A, int ALen) { // write code here long long max1 =INT_MIN, max2 = INT_MIN, max3 = INT_MIN; long long min1 = INT_MAX,min2 = INT_MAX; for(int i = 0;i<ALen;++i) { if(A[i]>max1) { 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]; } if(A[i]<min1) { min2 = min1; min1 = A[i]; } else if(A[i]<min2) { min2 = A[i]; } } long long ret = max(max1*max2*max3,min1*min2*max1); return ret; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 632KB, 提交时间: 2021-07-25
class Solution { public: /** * 最大乘积 * @param A int整型一维数组 * @param ALen int A数组长度 * @return long长整型 */ long long solve(int* A, int ALen) { if (ALen == 3) return A[0] * A[1] * A[2]; // write code here long long max1 = LLONG_MIN; long long max2 = max1, max3 = max1; long long min1 = LLONG_MAX; long long min2 = min1; for (int i = 0; i<ALen; i++) { long long item = A[i]; if (item>max1) { max3 = max2; max2 = max1; max1 = item; } else if (item>max2) { max3 = max2; max2 = item; } else if (item>max3) max3 = item; if (item<min1) { min2 = min1; min1 = item; } else if (item<min2) min2 = item; } cout << max3 << endl; cout << max2 << endl; cout << max1 << endl; cout << min1 << endl; cout << min2 << endl; long long result1 = max1*max2*max3; long long result2 = min1*min2*max1; cout << result1<< endl; cout << result2 << endl; return result1>result2?result1:result2; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 640KB, 提交时间: 2021-11-01
class Solution { public: /** * 最大乘积 * @param A int整型一维数组 * @param ALen int A数组长度 * @return long长整型 */ long long solve(int* A, int ALen) { sort(A,A+ALen); long long ans=1; ans=(long long)A[0]*A[1]; ans=ans*A[ALen-1]; long long ans1; ans1=(long long )A[ALen-1]*A[ALen-2]; ans1=ans1*A[ALen-3]; return max(ans,ans1); } };