【问题标题】:replacing two numbers by a single number which is in-between two in an given array?用给定数组中两个介于两个数字之间的单个数字替换两个数字?
【发布时间】:2016-06-06 11:06:04
【问题描述】:

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

从数组 A 中选择任意两个数字 a,b,并将它们都替换为单个数字 Y,这样 a

经过N-1次操作后,数组A中应该只剩下一个数了。检查我们是否能得到这个数?

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

【问题讨论】:

  • 是否允许发布LIVE在线比赛问题?
  • 也许您还需要数组中 A 在 B 之前的约束?
  • 如果我正确理解了这个问题,那么只要 X 介于数组的最小值和最大值之间(每次只需选择 y=x),执行起来就很简单。也许你应该举个例子。
  • X 是干什么用的?
  • x 是需要检查是否可以实现的数字。

标签: c++ recursion dynamic-programming


【解决方案1】:

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

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. 

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-27
    • 2015-07-07
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多