用给定数组中的两个数字替换两个数字?

时间:2016-06-06 11:06:04

标签: c++ recursion dynamic-programming

问题是我们有一个长度为N的数组A和一个随机数X.

从数组A中选择任意两个数字,例如a,b,并用一个数字替换两个数字,如Y,使得< = y< = b。

在N-1操作之后,阵列A中只剩下一个数字。检查我们是否可以得到这个数字?

我认为它的递归问题但我无法接近? 请告诉我们如何接近。

1 个答案:

答案 0 :(得分:0)

假设数组是a1,a2,..,按排序顺序排列。所以最小值是a1,max是a;

you may choose a1,a2 and replace with a1. 
Then after choose a1, a3(as a2 is gone now) and replace by a1.
.
Similarly you may choose a1, an-1 {a2 to an-2 is gone now} and replace by a1.

Now you are left with 2 number a1 and an. So a1<=x<=an, ans is yes otherwise two. 

这是贪婪的方法,因此需要证明。我要离开你。

只是一个旁注:回归和迭代具有相同的数学能力。只是他们有不同的可视化。