Maximum Pairwise Product

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maximum Pairwise Product相关的知识,希望对你有一定的参考价值。

问题描述

Given a sequence of non-negative integers a0,,an1, find the maximum pairwise product, that is, the largest integer that can be obtained by multiplying two different elements from the sequence (or, more formally, max0ijn1aiaj). Different elements here mean ai and ajwith ij (it can be the case that ai=aj).

输入格式

The first line of the input contains an integer n. The next line contains nnon-negative integers a0,,an1 (separated by spaces).

限制条件

2n21050a0,,an1105.

输出格式

Output a single number — the maximum pairwise product.

 

样例 1

输入:

3

1 2 3

输出:

6

 

样例 2

输入:

10

7 5 14 2 8 8 10 1 2 3

输出:

140

 

样例 3 

输入:

5

4 6 2 6 1

输出:

36

 

 

n = int(input())
a = [int(x) for x in input().split()]
assert(len(a) == n)

curMax=sndMax=0
for idx in range(0, n):
    if curMax < a[idx]:
        sndMax = curMax
        curMax = a[idx]
    elif sndMax < a[idx]:
        sndMax=a[idx]
print(curMax*sndMax)

以上是关于Maximum Pairwise Product的主要内容,如果未能解决你的问题,请参考以下文章

318. Maximum Product of Word Lengths

Maximum Product of Word

318. Maximum Product of Word Lengths

318. Maximum Product of Word Lengths

leetcode 152. Maximum Product Subarray

Maximum Product Subarray