数据结构问题-逆时间顺序

发布于 2025-01-20 02:56:50 字数 540 浏览 0 评论 0原文

我在一次面试中被问到一个与数据结构相关的问题。

问题:获取数据流,并且应该以相反的时间顺序显示。 没有重复项。

1. Which data structure to use?
2. What would be your solution approach?

Example

 Data    Output
  
(first set of data)
 A B  ->  A B   
 
(new streamed data i.e. D E arrives)
 D E  ->  D E A B 

(new streamed data i.e. A F arrives)
 A F  ->  A F D E B 

有人可以分享一些知识吗?

我的做法:

数据结构:数组

算法

  1. 创建一个空数组
  2. 将传入的数据插入列表顶部
  3. 当新数据到达时,进行线性搜索。
  4. 如果数据已存在,则将其删除并将新数据插入到列表顶部。
  5. 重复

I was asked a question in an interview related to the data structure.

Problem: Getting stream of data and it should be shown in the reverse chronological order.
No duplicates.

1. Which data structure to use?
2. What would be your solution approach?

Example

 Data    Output
  
(first set of data)
 A B  ->  A B   
 
(new streamed data i.e. D E arrives)
 D E  ->  D E A B 

(new streamed data i.e. A F arrives)
 A F  ->  A F D E B 

Can someone please share some knowledge?

My approach:

Data structure: Array

Algorithm

  1. Create an empty array
  2. Insert the incoming data into the top of the list
  3. When new data arrives, do a linear search.
  4. If data is already present then remove it and insert the new data on top of the list.
  5. Repeat

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

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

发布评论

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

评论(5

一场春暖 2025-01-27 02:56:50

我认为最好的解决方案是指向链表节点的指针的哈希映射,我将解释:

哈希映射用于在 O(1) 时间内搜索,

在哈希映射中,键将是data,values 将是指向链表中带有 key 值的节点的指针。

使用哈希映射,可以查找当前链表中是否存在某个元素(在 O(1) 时间内),并在 O(1) 时间内更改元素的位置(使用指向该元素的指针的优点)链表中的节点)

总的来说,新数据的更新将是 O(1) 时间,而数据结构是 O(n) 空间。

如果答案中的某些内容不满意,请务必发表评论:)

I think the best soluttion would be an Hash-map of pointer to linked-list Nodes, I'll explain:

The hash-map is used for searching in O(1) time,

In the hash-map, the keys will be the data, and the values will be the pointer to the node in the linked list with the value of the key.

With the hash-map, it's possible to find if an element is present in the current linked list (in O(1) time), and changing the position of an element in O(1) time (the positive of using a pointer to a node in a linked list)

Over all, the Update of the new data will be O(1) time and the data structures are O(n) space.

If something in the answer is unsatisfying, be sure to leave a comment :)

对你而言 2025-01-27 02:56:50

对于订单逆转,经典数据结构为a stack 最后)。
将新条目推到堆栈的顶部。最后,堆栈条目以相反的顺序弹出。

为了防止重复项,可以使用 hashset 。每当新的条目到达时,都会计算其哈希签名。如果哈希集合中存在哈希签名,则该条目将被封锁为重复。否则,将哈希注册为集合的新成员,并将条目推到堆栈。

与线性搜索相比,标签更快。但这需要额外的记忆。

For order reversal, the classical data structure is a stack (FILO = first in, last out).
New entries are pushed on top of the stack. In the end, the stack entries are popped off in reverse order.

To prevent duplicates, one could use a hashset. Whenever a new entry arrives, its hash signature is computed. If the hash signature is present in the hash set, the entry is blocked as duplicate. Otherwise, the hash is registered as new member of the set and the entry is pushed to the stack.

Compared to linear search, the hashset is faster. But it requires extra memory.

笙痞 2025-01-27 02:56:50

我认为一个简单的解决方案是使用hashmap使用双重链接列表。

  1. o(1)如果已经存在元素,请检查列表中的存在
  2. o(1)从旧位置删除由于您有nextprev在双重链接列表节点中的指针)和o(1)
  3. 如果元素不在呈现,o(1)在列表中插入
  4. o(n)遍历以使所有元素按顺序获取

I think an easy solution for this would be to use a doubly linked list with a hashmap.

  1. O(1) check for an element's presence in the list
  2. If the element is already present, O(1) removal from the old position (no need to traverse the entire list since you have next and prev pointers in doubly linked list nodes) and O(1) insertion at the list head
  3. If the element isn't presented, O(1) insertion at the list head
  4. O(N) traversal to get all elements in order
深居我梦 2025-01-27 02:56:50

当显示没有重复的输出时,我将使用数组的数组来存储数据流和哈希集结构。

1. Initialize empty array `x`.
2. While new stream `a` arrives, x.push(a)

# To show data with no duplicate,
3. Initialize an empty set `shown`.
4. Repeat while `x` is not empty:
   a = x.pop()
   For each element in a:
      if element not in shown:
         display(element)
         shown.add(element)

通过推送和弹出来维持逆时间顺序。并且重复项会被集合内检查跳过。考虑到哈希集包含测试的复杂度为 O(1),复杂度与传入元素的数量成线性关系。

这是我的想法,但也许这太天真了。欢迎评论。

I would use an array of arrays to store the data stream, and hashset structure when showing the output with no duplicate.

1. Initialize empty array `x`.
2. While new stream `a` arrives, x.push(a)

# To show data with no duplicate,
3. Initialize an empty set `shown`.
4. Repeat while `x` is not empty:
   a = x.pop()
   For each element in a:
      if element not in shown:
         display(element)
         shown.add(element)

Reverse-chronological order is maintained by push and pop. And duplicates are skipped by the in-set check. Given that hashset inclusion test is O(1), the complexity is linear in the number of incoming elements.

This is what I thought, but maybe this is too naive. Comments are welcome.

愛上了 2025-01-27 02:56:50

这可以通过哈希在 O(n) 空间和 O(1) 更新时间内解决,其中键是数据,值是表示数据的元组之前的数据和之后的数据;以及存储第一个数据的变量。

This can be solved in O(n) space and O(1) update-time with a hash where the key is the datum, and the value is a tuple representing the datum before and the datum after; and a variable that stores the first datum.

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