要求您在A [关闭]中找到第二个最大数字

问题描述 投票:-1回答:1

给出n => 2个正整数的数组A,要求您找到第二个A中的最大数字。

  1. 设计分治算法以解决该问题。
  2. 证明您的算法正确。
  3. 找出算法的时间复杂度。(为简单起见,您可以假设n为2的幂。)
arrays algorithm divide-and-conquer code-complexity discrete
1个回答
-2
投票
int biggest = -99999999
int second  = -99999999

for i = 0 ... n-1
|   if A[i] >= biggest
|   |   second = biggest
|   |   biggest = A[i]
|   else if A[i] >= second
|   |   second = A[i]
return second

由于我们只查找数组的每个元素一次,所以复杂度为O(n)。

© www.soinside.com 2019 - 2024. All rights reserved.