English 中文(简体)
分配算法以计算母体的平衡
原标题:Distributed algorithm to compute the balance of the parentheses

这是 rel=“noreferer”>interview: “如何建立分配算法,以计算母体的平衡?”

通常,他权衡算法会扫描一种留待处理的形式,并用一种记分法确保开放的母体数目总是和大体;=近亲的体数,最后是开放的母体数量=近亲数。

如何分配?

最佳回答

如果你可以阅读并向其他机器同时寄送,你可以将座椅破碎,单独处理。 各位需要两倍。

  1. 最小的宽度与阵列的开始相对照。

  2. 纵深地带宽度的总损益。

有了这些价值观,你就可以将许多领域划归如下:

minNest = 0
totGain = 0
for p in chunkResults
  minNest = min(minNest, totGain + p.minNest)
  totGain += p.totGain
return new ChunkResult(minNest, totGain)

如果<代码>totGain和minNest的最后数值为零,则对括号内的数值加以匹配。

问题回答

I would apply the map-reduce algorithm in which the map function would compute a part of the string return either an empty string if parentheses are balanced or a string with the last parenthesis remaining.

然后,削减职能将按地图功能计算两个回归阵列的结果,并再次计算结果与地图相同。 在计算结果结束时,你要么获得空洞的扼杀,要么获得含有不平衡的母体。

我试图更详细地解释@jonderry的回答。 第一,在Schala

def parBalance(chars: Array[Char], chunkSize: Int): Boolean = {
    require(chunkSize > 0, "chunkSize must be greater than 0")

    def traverse(from: Int, until: Int): (Int, Int) = {
      var count = 0
      var stack = 0
      var nest = 0
      for (n <- from until until) {
        val cur = chars(c)
        if (cur ==  ( ) {
          count += 1
          stack += 1
        }
        else if (cur ==  ) ) {
          count -= 1
          if (stack > 0) stack -= 1
          else nest -= 1
        }
      }
      (nest, count)
    }

    def reduce(from: Int, until: Int): (Int, Int) = {
      val m = (until + from) / 2
      if (until - from <= chunkSize) {
        traverse(from, until)
      } else {
        parallel(reduce(from, m), reduce(m, until)) match {
          case ((minNestL, totGainL), (minNestR, totGainR)) => {
            ((minNestL min (minNestR + totGainL)), (totGainL + totGainR))
          }
        }
      }
    }

    reduce(0, chars.length) == (0,0)
}

如果我们去掉平衡的括号,那么剩下的将以<代码>的形式()(,就<>码/代码>号<>和对<代码>的编号 (, 然后是m'/strong>;=0, n'lt;=0(便于计算)。 http://strong>n。 为了做到真正的平衡,我们需要<代码>m+n == 0 && n = 0。

在一项平行行动中,我们如何从中汲取教训和正确吗? 仅仅为了给Gain添加。 在计算“n代号”时,如果N(right)不捐款或(以较小者为准)。





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

热门标签