Algorithm to Compute the Largest Triple Products from Array

  • 时间:2020-10-07 14:34:56
  • 分类:网络文摘
  • 阅读:155 次

You’re given a list of n integers arr[0..(n-1)]. You must compute a list output[0..(n-1)] such that, for each index i (between 0 and n-1, inclusive), output[i] is equal to the product of the three largest elements out of arr[0..i] (or equal to -1 if i < 2, as arr[0..i] then includes fewer than three elements). Note that the three largest elements used to form any product may have the same values as one another, but they must be at different indices in arr.

1
int[] findMaxProduct(int[] arr)
int[] findMaxProduct(int[] arr)

Input
n is in the range [1, 100,000].
Each value arr[i] is in the range [1, 1,000].

Output
Return a list of n integers output[0..(n-1)], as described above.

Example 1
n = 5
arr = [1, 2, 3, 4, 5]
output = [-1, -1, 6, 24, 60]
The 3rd element of output is 3*2*1 = 6, the 4th is 4*3*2 = 24, and the 5th is 5*4*3 = 60.

Example 2
n = 5
arr = [2, 1, 2, 1, 2]
output = [-1, -1, 4, 4, 8]
The 3rd element of output is 2*2*1 = 4, the 4th is 2*2*1 = 4, and the 5th is 2*2*2 = 8.

Largest Triple Products by Sorting 3 Elements

We can keep an array of maximum 3 numbers. For each iteration, we push the current number and sort the array, then pop the smallest number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector<int> findMaxProduct(const vector<int> &arr) {
  vector<int> ans(arr.size(), -1);
  vector<int> max3 = {arr[0], arr[1]};
  for (int i = 2; i < arr.size(); ++ i) {
    max3.push_back(arr[i]);
    if (max3.size() > 3) {
      sort(begin(max3), end(max3), [](auto &a, auto &b) {
        return b < a;
      });
      max3.pop_back();
    }
    ans[i] = max3[0] * max3[1] * max3[2];
  }
  return ans;
}
vector<int> findMaxProduct(const vector<int> &arr) {
  vector<int> ans(arr.size(), -1);
  vector<int> max3 = {arr[0], arr[1]};
  for (int i = 2; i < arr.size(); ++ i) {
    max3.push_back(arr[i]);
    if (max3.size() > 3) {
      sort(begin(max3), end(max3), [](auto &a, auto &b) {
        return b < a;
      });
      max3.pop_back();
    }
    ans[i] = max3[0] * max3[1] * max3[2];
  }
  return ans;
}

The time complexity is O(N) although we are using sort process in the loop – but the complexity for sorting the 4 numbers is constant.

We can remove the condition check in the loop – the first triple products can be computed directly without sorting:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector<int> findMaxProduct(const vector<int> &arr) {
  vector<int> ans(arr.size(), -1);
  vector<int> max3 = {arr[0], arr[1], arr[2]};
  ans[2] = arr[0] * arr[1] * arr[2];
  for (int i = 3; i < arr.size(); ++ i) {
    max3.push_back(arr[i]);
    sort(begin(max3), end(max3), [](auto &a, auto &b) {
      return b < a;
    });
    max3.pop_back();
    ans[i] = max3[0] * max3[1] * max3[2];
  }
  return ans;
}
vector<int> findMaxProduct(const vector<int> &arr) {
  vector<int> ans(arr.size(), -1);
  vector<int> max3 = {arr[0], arr[1], arr[2]};
  ans[2] = arr[0] * arr[1] * arr[2];
  for (int i = 3; i < arr.size(); ++ i) {
    max3.push_back(arr[i]);
    sort(begin(max3), end(max3), [](auto &a, auto &b) {
      return b < a;
    });
    max3.pop_back();
    ans[i] = max3[0] * max3[1] * max3[2];
  }
  return ans;
}

This algorithm is correct because the large elements stay once they come in (they are not discarded) – therefore we just need to keep the maximum 3 numbers at any time.

You can also use Priority queue to pop 3 numbers.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
枸杞保健食疗功效多补肾益精养肝明目  如何从感官上辨别枸杞的真假和质量优劣  红酒对人体有保健作用但并非人人适宜  食品安全:盘点有毒的11种常见食物  鸡蛋的蛋黄和蛋清到底哪个更有营养?  揭秘减肥食品菇类营养价值抗癌降血脂  冬季感冒多喝姜茶可治疗外感风寒感冒  消除身体疲劳可多吃七种带“香”的食物  饮食与健康:维生素B2的补充无需刻意  女性急躁易发怒可能与维生素缺乏有关系 
评论列表
添加评论