avatar
Help for an algorithm, Thanks# Computation - 科学计算
r*e
1
Given an array A with positive numbers, find an efficient algorithm to find
out the Maximum of A[j]-A[i], j>i.
My solution is: Divide the array into two parts, first half and second half.
Then the location of j and i have three possibles. 1) j,i are in the first
half. 2) j, i are in the second half. 3) i is in the first and j in sencond.
For 1) and 2), I can use recursive to find out. For 3) I need to find out the
minimum element in the first half and the maximum element in the second half.
Then
avatar
m*e
2
Here's an O(n) one: (Given array A[0..(n-1)])
int x=A[0],y=A[1]-x;
for (int i=1;iif (x>A[i]) x=A[i];
if (y}
return y;

【在 r****e 的大作中提到】
: Given an array A with positive numbers, find an efficient algorithm to find
: out the Maximum of A[j]-A[i], j>i.
: My solution is: Divide the array into two parts, first half and second half.
: Then the location of j and i have three possibles. 1) j,i are in the first
: half. 2) j, i are in the second half. 3) i is in the first and j in sencond.
: For 1) and 2), I can use recursive to find out. For 3) I need to find out the
: minimum element in the first half and the maximum element in the second half.
: Then

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。