Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 我的代码有多复杂?_Ruby_Performance_Algorithm_Complexity Theory_Computer Science - Fatal编程技术网

Ruby 我的代码有多复杂?

Ruby 我的代码有多复杂?,ruby,performance,algorithm,complexity-theory,computer-science,Ruby,Performance,Algorithm,Complexity Theory,Computer Science,给定一个整数数组,编写一个方法,返回所有加起来等于100的唯一对 示例数据: sample_data = [0, 1, 100, 99, 0, 10, 90, 30, 55, 33, 55, 75, 50, 51, 49, 50, 51, 49, 51] sample_output = [[1,99], [0,100], [10,90], [51,49], [50,50]] 这个周末我正在解决这个问题,虽然我的解决方案看起来是可伸缩的和高效的,但我想确定我的解决方案最糟糕的时间复杂度是多少 以

给定一个整数数组,编写一个方法,返回所有加起来等于100的唯一对

示例数据:

sample_data = [0, 1, 100, 99, 0, 10, 90, 30, 55, 33, 55, 75, 50, 51, 49, 50, 51, 49, 51]
sample_output = [[1,99], [0,100], [10,90], [51,49], [50,50]]
这个周末我正在解决这个问题,虽然我的解决方案看起来是可伸缩的和高效的,但我想确定我的解决方案最糟糕的时间复杂度是多少

以下是我的解决方案:

def solution(arr)
  res = []
  h = Hash.new

  # this seems to be O(N)
  arr.each do |elem|
    h[elem] = true
  end

  # how do I determine what Time complexity of this could be?
  arr.each do |elem|
    if h[100-elem]
      h[100-elem] = false
      h[elem] = false
      res << [elem, 100-elem]
    end
  end
  res 
end
def溶液(arr)
res=[]
h=散列。新
#这似乎是O(N)
每一道菜|
h[elem]=真
结束
#我如何确定这个问题的时间复杂度?
每一道菜|
如果h[100 elem]
h[100元素]=假
h[elem]=假

res你是对的。如果考虑哈希搜索/插入的摊销运行时间,该代码的Big-O将是<代码> O(n)< /代码>。 如果您采用哈希搜索/插入的最坏情况(
O(n)
),那么它将是
O(n^2)


问题可能是询问哈希的时间复杂性,但对于特定问题,哈希最好实现为一个由输入0..sum(本例中为100)索引的布尔数组。这将有最佳、最差和平均情况下的恒定时间


这种方法计算O(N)的复杂性更简单。

这是正确的。我认为你的假设基本上是正确的。这也是假设元素可以是负的(超过100),这才是有意义的-否则,只有消除初始输入的重复才会有任何缩放成本,并且在填充完所有键0..100后,所有其他内容都可以被视为固定成本。从技术上讲,
h[elem]=true
不是
O(1)
(很多人似乎都这么认为),而是
O(log(N))
,所以你的总体复杂度可能是
O(Nlog(N))
最坏的情况——你只能看到,如果你在数组中输入数百万个整数though@NeilSlater你错了
h
是一个哈希映射,搜索是线性时间@Screenmut:我在基准测试中看不到这一点。e、 g.
array=(0..10000000).map{x | SecureRandom.random_number(20000000000)-100000000};Benchmark.bm{124; bm | h=Hash.new;bm.report(:five{100000.times{i | h[array[i]]]=true}};h=Hash.new;bm.report(:six{1000000.times{i | h[array[i]]]=true}}}
-事实上,我看到了我对O(Nlog N))
-解释一下?@slater你能解释一下为什么
h[elem]=true
可能是
O(log(N))
?我的假设和理解是,这是你提到的
O(1)
。是的,这是实现解决方案的另一种方式。我也考虑过这种方法,我想在实践中你会发现它更快(更确切地计算复杂性)。稀疏阵列用一点空间(在低N时几乎没有)来换取速度。