我发现了一种贪婪的算法问题,我们必须找到最小的算法。让人们坐在一起需要的跳跃。
这是链接与文章供参考。
文章中的问题描述:
贪婪的解决方案说,将元素转移到中位数。但是找不到证据。是否存在使用这种方法的数学证明?
I found one greedy algorithm problem, where we have to find min no. of jumps required to make persons sit together.
Here is a link to the article for reference.
Question Description from article:
-
Title:
Minimum jumps required to make a group of persons sit together.
-
Description:
Given a string S of length N consisting of ‘x’ and ‘.’. The given string represents a row of seats where ‘x’ and ‘.’ represent occupied and unoccupied seats respectively. The task is to minimize the total number of hops or jumps to make all the occupants sit together i.e., next to each other, without having any vacant seat between them.
-
Examples:
-
Input: S = “. . . . x . . x x . . . x . .”
Output: 5
Explanation: Make the person at 5th seat jump 2 places to the 7th seat. Make the person at 13th seat jump 3 places to the 10th seat.Therefore, total number of jumps required = 2 + 3 = 5.
-
Input: S = “x x x . . . . . . . . x x x x x x”
Output: 24
Explanation: Move the occupants from 1st, 2nd and 3rd position to the 9th, 10th, 11th positions respectively. Therefore, the total number of jumps required = (11 – 3) + (10 – 2) + (9 – 3) = 24.
-
Approach:
The idea is to use a Greedy Approach to solve this problem. Observe that it is always optimal to shift the elements towards the median element among the persons or the center person among all the persons present. The number of jumps will always be minimum when we shift points to the median.
The greedy solution says to shift the elements towards the median. But no proof of this has been found. Does there exist some mathematical proof of using this approach?
发布评论
评论(1)
令
a [i]
为人的初始位置i
(基于0的索引),其中人的顺序设置为增加位置的顺序,> n
是人的数量。假设所有人连续坐着的位置的最终间隔从位置t
开始。由于人们的相对顺序没有改变,因此我们看到该人i
从a [i]
移动到t + i
。何时以及如何确切地发生这一举动对我们来说并不重要,但是一个人无需朝着远离目标位置的方向跳下来,因此它贡献了| a [i] - (t + i)| == | t - (a [i] - i)| == | t -b [i] |
跳跃,其中b
,由b [i] = a [i] - i
定义em> non -decreasing 数组,因为a
是严格增加(因此b [i + 1] -b [i] == a [i [i] + 1] - (a [i] + 1)> = 0
)。因此,从位置开始 t 的增加, ,
t
IStotal [t] = sum(i,| t -b [i] |)
的最小值是t
是t
。现在,d_total [t] = total [t + 1] - 总计[t] (i,t> = b [i]?1:-1)== count(i,t> = b [i]) - count(i,t< b [i])== 2*count (i,t> = b [i]) - n
是total
的'离散导数',很容易看到随着d_total [t]
是非折扣的,在某些i
的点上增加了t == b [i]
2 *计数(i,t == b [i])。的最小值
发生在最小的t
中,d_total [t]
是正(意味着count> count(i,t) > = b [i])> n/2
)。特别是,对于奇数
n == 2*k + 1
这样的t
正是b
的中值,而所得间隔的“中心” ISt + k == b [k] + k == a [k]
,即a
的中位数,如解决方案中所假设。对于n == 2*k
我们在t
b [k -1]
和b [k]
其中total [t]
是最小的(d_total [t] == 0
fortє[b [k [k [k] -1]; b [k] -1]
t == b [k] 是最小的d_total [t]>这意味着所产生的间隔的“左中心”是
t + k -1є[a [k -1]; a [k] -1]
(或等效地,'右中心't + kє[a [k -1] + 1; a [k]]
),所以我们在这种情况下,如果a [k]> A [K -1]
。在任何情况下,正如我们看到的,最佳策略是向中位数跳跃(或
n
案例中的“中间间隔”,但随后将中位数定义为此间隔的末端的平均值,我们,我们仍然可以说“朝向中间人”的职位。最后,也许这很明显,但是对于完整性:请注意,获得的全局最小
t
不会产生超出输入字符串的间隔,因为在两个奇偶校验中,很容易证明,a [0]< = t< = t + n -1< = a [n -1]
和a
元素是该字符串的位置。因此,这种全球最低限度始终适合我们的目的。Let
a[i]
be initial position of personi
(0-based indexing), where order of people is set to be in order of increasing positions, andn
be the count of people. Suppose that final interval of positions, where all people sit consecutively, starts at positiont
. Since relative order of people doesn't change, we see that personi
moves froma[i]
tot + i
. When and how exactly this move happens is unimportant to us, but there's never need for a person to jump in the direction away from its target position, so it contributes|a[i] - (t + i)| == |t - (a[i] - i)| == |t - b[i]|
jumps, whereb
, defined byb[i] = a[i] - i
, is a non-decreasing array sincea
is strictly increasing (thusb[i + 1] - b[i] == a[i + 1] - (a[i] + 1) >= 0
).So, minimal total number of jumps for resulting interval starting at position
t
istotal[t] = sum(i, |t - b[i]|)
. Now,d_total[t] = total[t + 1] - total[t] == sum(i, |t - b[i] + 1| - |t - b[i]|) == sum(i, t >= b[i] ? 1 : -1) == count(i, t >= b[i]) - count(i, t < b[i]) == 2*count(i, t >= b[i]) - n
is the 'discrete derivative' oftotal
, and it's easy to see that ast
increases,d_total[t]
is non-decreasing, increasing at points wheret == b[i]
for somei
by2*count(i, t == b[i])
. The smallest value oftotal
happens at the smallestt
for whichd_total[t]
is positive (which meanscount(i, t >= b[i]) > n/2
).In particular, for odd
n == 2*k + 1
sucht
is exactly the median ofb
, and 'center' of resulting interval ist + k == b[k] + k == a[k]
, i.e. median ofa
, as is assumed in the solution. For evenn == 2*k
we have inclusive interval oft
betweenb[k - 1]
andb[k]
wheretotal[t]
is minimal (d_total[t] == 0
fort є [b[k - 1]; b[k] - 1]
andt == b[k]
is the smallest whered_total[t] > 0
), which means that resulting interval's 'left center' ist + k - 1 є [a[k - 1]; a[k] - 1]
(or, equivalently, 'right center't + k є [a[k - 1] + 1; a[k]]
), so we have several optimal solutions in this case ifa[k] > a[k - 1]
.In any case, as we see, optimal strategy is to jump towards the median (or 'median interval' in even
n
case, but since median is then defined as average of the ends of this interval, we still can say 'towards the median') of the starting people positions.Finally, maybe this is obvious, but for completeness: note that the obtained global minimum
t
doesn't produce interval that goes beyond the input string because, as is easy to prove in both parity cases,a[0] <= t <= t + n - 1 <= a[n - 1]
, anda
elements are positions withing this string. So, this global minimum is always suitable for our purposes.