对于数组A的每个元素A[i],找到最接近的j使得A[j]>人工智能]

发布于 2024-08-27 06:50:38 字数 587 浏览 5 评论 0原文


给定:实数数组A[1..n]

该数组

D[i] = min{ distance(i,j) : A[j] > A[i] }

目标:一个数组D[1..n] ,当没有更高值的元素时, 或某个默认值(如0)。我真的很想在这里使用欧几里得距离。

示例

A = [-1.35, 3.03, 0.73, -0.06, 0.71, -0.21, -0.12, 1.49, 1.41, 1.42]
D = [1, 0, 1, 1, 2, 1, 1, 6, 1, 2]

有什么方法可以击败明显的 O(n^2) 解决方案吗?到目前为止,我取得的唯一进展是,每当 A[i] 不是局部最大值时,D[i] = 1 。我想了很多却什么也没想出来。我希望最终将其扩展到二维(因此 AD 是矩阵)。


Given : An array A[1..n] of real numbers.

Goal : An array D[1..n] such that

D[i] = min{ distance(i,j) : A[j] > A[i] }

or some default value (like 0) when there is no higher-valued element. I would really like to use Euclidean distance here.

Example :

A = [-1.35, 3.03, 0.73, -0.06, 0.71, -0.21, -0.12, 1.49, 1.41, 1.42]
D = [1, 0, 1, 1, 2, 1, 1, 6, 1, 2]

Is there any way to beat the obvious O(n^2) solution? The only progress I've made so far is that D[i] = 1 whenever A[i] is not a local maxima. I've been thinking a lot and have come up with NOTHING. I hope to eventually extend this to 2D (so A and D are matrices).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生不灭 2024-09-03 06:50:38

所以我对此有点困惑,但我还没有想出任何更好的方法。一些想法:

  • 使用可以在 O(n) 时间或更短的时间内获得的额外信息来扩充数组。例如,添加索引、邻居之间的差异等。
  • 排序 (O(n(log n)) 有任何帮助吗?
  • 看起来像 动态编程在这里可能会有所帮助,如果您可以根据其邻居的解决方案找出解决每个元素的方法(使用j 对于每个 A[i] 而不仅仅是距离)。

So I've puzzled on this a bit but I haven't come up with anything better that works. A few ideas:

  • Augment the array with extra information that can be gained in O(n) time or better. e.g., add indices, difference between neighbors, etc.
  • Would sorting (O(n(log n)) help in any way?
  • Seems like dynamic programming could be helpful here, if you can figure out a way to solve for each element based on the solution for its neighbors (augmenting the answers with information like the j for each A[i] instead of just the distance maybe).
世俗缘 2024-09-03 06:50:38

将数组从最高元素到最低元素排序。如果我正确理解你的问题,这会立即给你答案,因为与原始列表中任何元素最接近的较大元素是它之前的元素。这样,您甚至不需要创建 D[] 数组,因为可以专门使用数组 A[] 来计算其内容。排序后的 A[] 数组中的第一个元素没有更大的朋友,因此它的答案将是您的默认值(也许是 0?)。扩展矩阵算法可能很容易(取决于您如何“看待”矩阵) - 只需使用映射函数即可将矩阵转换为一维数组。

Sort the array from highest to lowest element. If I understand your problem correctly, this gives you the answer immediately, since the closest bigger element to any element in the original list is the one before it. This way you don't even need to create the D[] array, since computation of its contents can be done using the array A[] exclusively. The first element in the sorted A[] array does not have a bigger friend so the answer for it would be your default valye ( 0 perhaps?). Extending the algorithm for matrices might be easy (depends on how you "look" at the matrix) - just use a mapping function which sort of transofrms the matrix into a 1D array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文