English 中文(简体)
一对各行各业增加特定价值的数字
原标题:First pair of numbers adding to a specific value in a stream

一批愤怒分子即将到来。 问题在于找到“first pair of number from the stream that increase to a specific Value (say, k).

在静态阵列下,可以采用以下两种办法:

  • Approach (1): Sort the array, use two pointers to beginning and end of array and compare.
  • Approach (2): Use hashing, i.e. if A[i]+A[j]=k, then A[j]=k-A[i]. Search for A[j] in the hash table.

但是,这两种办法都没有很好地扩大上游。 任何关于有效解决这一问题的想法?

最佳回答

我认为,这样做没有任何办法,至少不使用O(n)记忆,因为那是头奶牛之前出现的数量。 我假定,我们使用的是援助团机器,但不是允许一夫一妻的黑客的机器(换言之,我们可以不做任何包裹。)

证据概述如下。 假设我们不储存在一对一对一对一对一对一对一对一对一对一对一对一的零件。 然后,当我们看到第一点点,即有某种前值的元件可以拿到k时,我们就有机会放弃了以前它所穿戴的那部分内容,从而知道已经达到了千米。 更正式地说,假定对手可以观察我们在研究第一个零-一要素时所储存的是什么价值,并指出我们确实储存了一些要素x。 然后,对手可将下游要素定为k-x,我们将不正确地报告,这笔钱尚未达到,因为我们不会忘记看到x。

鉴于我们需要储存我们所看到的所有内容,而没有更多地了解上游的数字,一个非常好的办法是使用一个包含我们迄今所看到的所有内容的散列表格。 考虑到一个好的洗衣桌,这将需要O(n)记忆和O(n)时间才能完成。

我不敢肯定,如果你对上游人数的类型作出更强有力的假设,那么解决这一问题的战略是否更为模糊,但我相信,在时间和空间方面,这是完美的理想。

希望这一帮助!

问题回答

暂无回答




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

热门标签