English 中文(简体)
面谈期间动态方案拟订算法
原标题:dynamic programming algorithm during an interview [closed]

在一次面谈中向我提出了这一问题,令人难堪地暴露了我在动态方案规划方面的不足。 如果有人能帮助我打破这一鸿沟,我将不胜感激。 同样,如果你能够按照你制定解决办法的方式解释你的思考过程,那么我(和其他人)将非常有帮助,因为我似乎能够理解,在我看到一种利用动态的方案拟订模式但努力与我自己一道解决问题的解决办法。

如果没有再说一遍,这里的问题是问的。

考虑到按实际行文分列的<代码>i和>Xkx2xk、按实际行文选择>/code>、从第X的点中选取<代码>x,以尽量减少从X>/code>到使用动态方案拟订的<代码>i上每一点的距离。

最佳回答

由于大多数的DP问题,我尝试并找到一种减低和平衡的关系。 也就是说,我可以把问题的规模缩小到每一步(如分裂和qu,但通常会把问题分开,而只是消除一小部分)。 在这个问题上(与许多其他人一样),我们可以简单地指出: 第一点在<代码>i各点,或指t。

缩略语: 让我说X ={x1,x2, ......,x/sub>}, 并注明减少的X ={xn, x, ...,xk>/sub>>>>。

So the observation is that either x1 is one of the i points, or it isn t. Let s call our i-set finding function MSD(i,Xk) (minimum sum of distances). We can express that cut-away observation as follows:

MSD(i,Xk) = Either MSD(i-1,Xk-1) U {x1} or MSD(i,Xk-1)

我们可以将“要么要么要么”部分正式化,具体做法是采用简单的方式检查其中两种选择中哪一种:我们通过X套,计算距离,并检查实际规模较小。 我们当时注意到,这一检查时间为ki。 由于我们将通过<条码>k各点和绕过一套大小(<条码>i)各点的最低距离。

We make two simple observations regarding base cases:

MSD(i,Xi) = Xi
MSD(0,Xn) = {}

The first is that when looking for i points in a set of size i we obviously just take the whole set.
The second is that when looking for no points in a set, we return the empty set. This inductively ensures that MSD returns sets of size i (it s true for the case where i=0 and by induction is true according to our definition of MSD above).

That s it. That will find the appropriate set. Runtime complexity is upper bounded by O(ik * step) where step is our O(ik) check from above. This is because MSD will be run on parameters that range from 0-i and X1 - Xk, which is a total of ik possible arguments.

That leaves us with a runtime of O((ik)2).

The following part is based on my understanding of the OP s question. I m not sure if the distance of every point in X from the i-sized subset is the sum of the distances of every point from every other point in the subset, or the sum of the distances of every point in X from the subset itself.
I.e. sigma of x in X of (sum of distances of x from every point in the subset) OR sigma of x in X of (distance of x from the subset which is the minimum distance from x to any point in the subset)

我假定后者。

我们可以通过选择上述<条码>O(ik)检查来缩短时间。 我们注意到,这些要素实际上是分类的(尽管在目前情况下是相反的),因为当我们补充这些内容时,我们总是从正确的方面来这样做。 假设它们重新分类,它们将一度脱离可持续发展司的例行工作。 如果从头开始,它们可以分类,则只计算费用<条码>O(klogk)

Once sorted, checking the distance of each point from a point in the set will be k * logi since for each point we do a binary search. This yields a total running time of O(ik * klogi + klogk)
= O(k2 * ilogi).

Finally, we can express that as O(k3logk). Not the fastest solution, but a solution.

我肯定会有更多的选择,但这符合我的两点。

问题回答

暂无回答




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签