English 中文(简体)
B. 数据设计问题:在O(1)中发现插入删除和摘录
原标题:Data design Issue to find insertion deletion and getMin in O(1)

As said in the title i need to define a datastructure that takes only O(1) time for insertion deletion and getMIn time.... NO SPACE CONSTRAINTS.....

我已经对SO进行了同样的搜索,所有发现都是在O(1)时间插入和删除。 i 看到前一个员额充 st了他们所说的洗衣......

with my analysis for getMIn in O(1) time we can use heap datastructure
for insertion and deletion in O(1) time we have stack...

so inorder to achieve my goal i think i need to tweak around heapdatastructure and stack... How will i add hashing technique to this situation ...

如果说用 has,那么我的过去职能是什么样子? 那么,我们会想如何使局势在洗 the方面发生转变。

最佳回答

如果你同意你最初的假设,即插入和删除是奥(1)的复杂性(如果你只想插入顶头,删除/从顶头删除,那么打工就会被罚款)。 Min将最低价值归还给你需要储存矿.的固定时间。 如果你只是一个成员变量能够跟踪清白,那么如果从中删除,就会发生什么情况? 你们需要下一个最低点,或与 st中留下的东西相比的最低点。 为此,你可以把你认为是最起码的东西放在一栏中。 标记在编码中以一个链接清单表示,因此,相关清单中点子的位置就看着:

struct Node
{
  int value;
  int min;
  Node *next;
}

如果看一例:7->3->1->5->2。 让我们看看一下如何做到这一点。 首先,你把价值2推向一个空洞,这是个亮点,因为它是头一个数字,能够跟踪它,并在你建造时添加它:{2,2}。 然后,你将这5条推向四舍五入;2 因此,这颗min子是同样的推力{5,2},现在你们有1{5,2}->{2,2}。 然后,你将1号推到1<2 因此,新的污染物为1号,推船{1,1},现为1,1}->{5,2}->{2,2}等。 到底:

{7,1}->{3,1}->{1,1}->{5,2}->{2,2}

在执行过程中,如果你跑掉了7人,3人,1人将成为2人。 你们的所有行动都持续了一段时间,因为你们只是对 no子添加了比较和另一种价值。 (你可以使用C++ s peek()等一些东西,或只是使用名单头顶点,看一看那里min和min鱼的顶部,这样就能够给你固定的时间。

在执行过程中的权衡是,你在你的节点上有额外感受,如果你在一份非常大的名单中只有一个或两个明星,那就是一种记忆浪费。 如果是这样的话,那么你就可以把min子放在一个单独的栏目中,而只是比较你把 no子重新删除到该名单上的顶点的价值,并在这两个名单上删除。 更需要跟踪了解情况。

DISCLAIMER:这是我在这个论坛上的第一个职位,这样,我会感到担心,如果这个职位有点混淆或说。 我也不要说这是“一个真正的答案”,但我认为是最简单的,符合问题的要求。 总是存在权衡,视形势而定,需要采取不同的办法。

问题回答

这是一个设计问题,这意味着他们希望看到你能够迅速扩大现有的数据结构。

首先是你们知道的:

  • O(1) update, i.e. insertion/deletion, is screaming hashtable
  • O(1) getMin is screaming hashtable too, but this time ordered.

在此,我提出这样做的一种方式。 你可能会发现你喜欢的东西。

  • create a HashMap, call it main, where to store all the elements
  • create a LinkedHashMap (java has one), call it mins where to track the minimum values.
  • the first time you insert an element into main, add it to mins as well.
  • for every subsequent insert, if the new value is less than what s at the head of your mins map, add it to the map with something equivalent to addToHead.
  • when you remove an element from main, also remove it from mins. 2*O(1) = O(1)
  • Notice that getMin is simply peeking without deleting. So just peek at the head of mins.

EDIT:

。 摊销算法:

( ( maz To

We all know that the cost of insertion into a hashtable is O(1). But in fact, if you have ever built a hash table you know that you must keep doubling the size of the table to avoid overflow. Each time you double the size of a table with n elements, you must re-insert the elements and then add the new element. By this analysis it would seem that worst-case cost of adding an element to a hashtable is O(n). So why do we say it s O(1)? because not all the elements take worst-case! Indeed, only the elements where doubling occurs takes worst-case. Therefore, inserting n elements takes n+summation(2^i where i=0 to lg(n-1)) which gives n+2n = O(n) so that O(n)/n = O(1) !!!

为什么没有将同一原则适用于相关的HashMap? 你们必须重载所有内容! 因此,每当你将<代码>main增加一倍时,就将<编码>main中的所有内容放在斜体内,并将其列入<代码>mins。 随后,所有其他案件均按上文(简易步骤)处理。

在O(1)中添加和删除一个字句,因为你在一栏中可能会有漏洞。 但是,你也可以在O(1)中提一下,因为订购你的部件可能比O(n*log(n))更快。 (t is a theorem,意思是O(log(n)对每个要素。

You can keep a pointer to the min to have getMin in O(1). This pointer can be updated easily for an insertion but not for the deletion of the min. But depending on how often you use deletion it can be a good idea.

您可使用trie。 如果L是你重新寻找的(或不管怎么做)体的长度,那么在插入、删除和沾染上皮条(L)的复杂性。 在N(要素数目)方面,它一直很复杂。

It requires a huge amount of memory, though. As they emphasized "no space constraints", they were probably thinking of a trie. :D

严格地说,可以说你的问题是不可能的,但认为:

鉴于T类对此种类型的所有可能要素进行了列举,这些要素的数值不低于tj iff T(i) < T(j)。 (按顺序排列的T类所有可能的数值)

Create an array of that size.

2. 编制阵列的要素:

struct PT
{
   T t;
   PT* next_higher;
   PT* prev_lower;
}

在保留双重链接清单(按指数顺序排列)的阵列中加入和删除有关内容

This will give you constant getMin and delete.

添加如下内容:你需要随时找到阵列中的下一个要素,因此我将使用一种照相机。

If the size of the array is 2^x then maintain x "skip" arrays where element j of array i points to the nearest element of the main array to index (j << i).

因此,总是需要固定的X号调查,以更新和搜索,从而持续地安置时间。

This uses exponential space, but this is allowed by the requirements of the question.

in your problem statement " insertion and deletion in O(1) time we have stack..." so I am assuming deletion = pop() in that case, use another stack to track min

algo: Stack 1 -- normal stack; Stack 2 -- min stack

Insertion

push to stack 1. if stack 2 is empty or new item < stack2.peek(), push to stack 2 as well

目标:在任何时间段,Ppeek()都应给你以O(1)号令。

Deletion

pop() from stack 1. if popped element equals stack2.peek(), pop() from stack 2 as well





相关问题
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 "...

热门标签