MaxProductOfThree

problem

solution

三值相乘最大值,只會發生在最小兩個(負數)乘以最大,或是前三大(正數)相乘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// you can use includes, for example:
// #include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

#include <algorithm>
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
sort(A.begin(), A.end());
int n = A.size();
return max(A[0]*A[1]*A[n-1],A[n-3]*A[n-2]*A[n-1] );
}

analysis

  • time complexity O(nlogn)
  • space complexity O(1)