设计一个在 O(logn) 时间内运行的数据结构

发布于 2025-01-07 10:07:21 字数 256 浏览 0 评论 0原文

我正在审核这个算法课程的工作,并尝试做一些课堂上给出的练习题。这个问题把我难住了,我就是无法理解它。我的解决方案都没有在 O(logn) 时间内出现。谁能帮我解决这个问题吗?

问题: 假设我们以任意顺序给出一个由 n 个值 x1, x2, ... , xn 组成的序列,并且 寻求快速回答以下形式的重复查询:给定任意对 i 和 j 1≤i<1 j ≤ n,找出 x1, ... , xj 中的最小值。设计一个使用 O(n) 空间并在 O(log n) 时间内回答每个查询的数据结构。

I'm auditing this algorithms class for work and I'm trying to do some practice problems given in class. This problem has me stumped and I just can't wrap my head around it. None of my solutions come out in O(logn) time. Can anyone help me with this problem??

Question:
Suppose that we are given a sequence of n values x1, x2, ... , xn in an arbitrary order and
seek to quickly answer repeated queries of the form: given an arbitrary pair i and j with
1 ≤ i < j ≤ n, find the smallest value in x1, ... , xj . Design a data structure that uses O(n) space and answers each query in O(log n) time.

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

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

发布评论

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

评论(5

ぃ弥猫深巷。 2025-01-14 10:07:21

对于输入 a1,a2,a3,...an ,构造一个包含最小值 (a1,..,ak) 和最小值 (ak+1,..,an) 的节点,其中 k = n/2。

递归构造树的其余部分。

现在,如果您想找到 ai 和 aj 之间的最小值:

  1. 确定 i,j 的最低共同祖先。让它成为 k
  2. 从 i 开始并继续移动直到达到 k。每次迭代时检查子节点是否为左节点。如果是,则比较右子树的最小值并相应地更新当前最小值。
  3. 类似地,对于 j,检查它是否是正确的节点......
  4. 在节点 k 比较每个子树返回的值并返回最小值

For input of a1,a2,a3,...an , construct a node that contains minimum of (a1,..,ak) and minimum of (ak+1,..,an) where k = n/2.

Recursively construct the rest of the tree.

Now, if you want to find the minimum between ai and aj:

  1. Identify the lowest common ancestor of i,j. Let it be k
  2. Start with i and keep moving until you hit k. AT every iteration check if the child node was left node. If yes, then compare the right subtree's min and update current min accordingly.
  3. Similarly, for j, check if it is right node....
  4. At node k compare values returned by each subtree and return the min
随梦而飞# 2025-01-14 10:07:21

人们想太多了。假设您从列表开始:

47, 13, 55, 29, 56, 9, 17, 48, 69, 15

制作以下列表列表:

47, 13, 55, 29, 56, 9, 17, 48, 69, 15
13,     29,     9,     17,     15
13,             9,             15
9,                             15
9

我将这些列表的构造、正确用法以及它们为原始问题提供答案的证明作为练习留给读者。 (这可能不是你的家庭作业,但很可能是某人的家庭作业,而且我不喜欢给出家庭作业问题的完整答案。)

People are overthinking this. Suppose that you start with the list:

47, 13, 55, 29, 56, 9, 17, 48, 69, 15

Make the following list of lists:

47, 13, 55, 29, 56, 9, 17, 48, 69, 15
13,     29,     9,     17,     15
13,             9,             15
9,                             15
9

I leave the construction of these lists, correct usage, and proof that they provide an answer to the original question as exercises for the reader. (It might not be homework for you, but it could easily be for someone, and I don't like giving complete answers to homework questions.)

等风来 2025-01-14 10:07:21

我认为关键的一步是您需要事先对数据进行排序。然后您可以将数据存储在数组/列表中。然后,您可以在 O(logn) 中运行快速二分搜索,选出满足条件的第一个值(我假设您的意思是在 xi 和 xj 之间,而不是 x1 和 xj 之间)。

编辑:再想一想,确保该值满足条件可能并不像我想象的那么微不足道

I think the crucial step is that you'll need to sort the data before hand. Then you can store the data in an array/list. Then you can run through a quick binary search in O(logn), picking out the first value that satisfies the condition (I'm assuming you meant between xi and xj, not x1 and xj).

edit: on second thought, ensuring that the value satisfies the condition may not be as trivial as I thought

清风挽心 2025-01-14 10:07:21

之前以稍微不同的方式提出了这个问题: 对于范围最小查询,我应该使用什么使用 O(n) 存储和 O(log n) 查询时间的数据结构?

尽管如此,为了快速回答,您面临的问题这是一个经过充分研究的——范围最小查询。线段树是一种可以解决 O(N) 空间和 O(logN) 时间要求的问题的数据结构。您可以在此处查看更多详细信息,其中对结构和所涉及的复杂性进行了解释。

The question was asked before in a slightly different way: What data structure using O(n) storage with O(log n) query time should I use for Range Minimum Queries?

Nevertheless, to quickly answer, the problem you're facing it's a well studied one - Range Minimum Query. A Segment Tree is a Data Structure that can solve the problem with O(N) space and O(logN) time requirements. You can see more details in here, where there's an explanation of the structure and the complexities involved.

十六岁半 2025-01-14 10:07:21

尝试解释建议的数据结构:

对于每一对数字,计算并保留较小数字的值。
对于每四个连续数字,计算并保留四个中最小的值。通过选择两对值中较小的一个可以快速完成此操作。
对于每八个连续数字,计算并保留八个中最小的值。
等等。

假设我们想要 x19 到 x65 之间的最小值。

我们查看以下存储值:
x32 到 x63 中最小的一个。
x24 到 x31 中最小的一个。
x20 到 x23 中最小的一个。
x19。
x64 到 x65 中最小的一个。

然后我们选择其中最小的一个。

Trying to explain the suggested data structure:

For every pair of numbers, calculate and keep the value of the smaller one.
For every four consecutive numbers, calculate and keep the value of the smallest of the four. This is done quickly by picking the smaller of the two pair values.
For every eight consecutive numbers, calculate and keep the value of the smallest of the eight.
And so on.

Let's say we want the smallest value of x19 to x65.

We look at the following stored values:
Smallest of x32 to x63.
Smallest of x24 to x31.
Smallest of x20 to x23.
x19.
Smallest of x64 to x65.

Then we pick the smallest of these.

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