简体   繁体   English

什么是我的递归函数的Big-O分析?

[英]What is the Big-O analysis of my recursive function?

Preparing for technical interviews, I solved a practice problem with a recursive solution. 准备技术面试,我用递归解决方案解决了练习题。

What is the runtime complexity of a recursive function such as this? 这样的递归函数的运行时复杂性是多少? I am more concerned with the explanation rather than the answer. 我更关心的是解释而不是答案。

From my analysis- the number of operations is going to be half of n. 根据我的分析,操作次数将是n的一半。 That is, a string of 10 characters is going to take 5 function calls in the worst case scenario. 也就是说,在最坏的情况下,一个10个字符的字符串将进行5次函数调用。 But I have never seen an O(n/2) runtime. 但我从未见过O(n / 2)运行时。 Also, my analysis excludes the call to the helper function counterpartOf . 此外,我的分析排除了对辅助函数counterpartOf的调用。 Could someone please show me a proper analysis? 有人可以给我一个正确的分析吗?

Write a function that accepts a string consisting of brackets ({}) and returns whether it is balanced. 编写一个接受由括号({})组成的字符串的函数,并返回它是否是平衡的。

function checkBraces(input){
  // start at the center and work outwards, recursively
  var c = input.length / 2;

  if (input.charAt(c) !== counterpartOf(input.charAt(c-1))) {
    var match = false;
    return match;
  } else {
    // if only 2 characters are left, all braces matched
    if (input.length === 2){
      var match = true;
      return match;
    } else {
      input = input.substring(0,c-1) + input.substring(c+1,input.length);
      return checkBraces(input);
    }
  }
  return match;
}

function counterpartOf(brace){
  closing = ['}', ')', ']'];
  opening = ['{', '(', '['];
  var i = opening.indexOf(brace);
  var counterpart = closing[i];
  return counterpart;
}

Constants are irrelevant, this is why you won't see /2, *2 or anything similar. 常数是无关紧要的,这就是为什么你不会看到/ 2,* 2或类似的东西。

Details: 细节:

http://en.wikipedia.org/wiki/Big_O_notation#Multiplication_by_a_constant http://en.wikipedia.org/wiki/Big_O_notation#Multiplication_by_a_constant

O(k*g) = O(g) if k is non zero. 如果k不为零,则O(k * g)= O(g)。

Otherwise as Yevgeniy.Chernobrivets mentioned your algorithm is not accurate. 否则,因为Yevgeniy.Chernobrivets提到你的算法不准确。 But apart from his comment I think there are other problems as well. 但除了他的评论,我认为还有其他问题。

Usually for similar tasks, they use push down automata's. 通常对于类似的任务,他们使用下推自动机。 There is some theoretical background about the issue: http://people.cs.clemson.edu/~goddard/texts/theoryOfComputation/7.pdf 关于这个问题有一些理论背景: http//people.cs.clemson.edu/~goddard/texts/theoryOfComputation/7.pdf

Complexity of your function will be O(n) only in case if javascript substring function takes constant time. 只有在javascript substring函数占用恒定时间的情况下,函数的复杂度才为O(n) If complexity of substring function is O(k) where k is length of substring then complexity of your function will be O(n^2) . 如果substring函数的复杂度为O(k) ,其中k是子串的长度,则函数的复杂度将为O(n^2) You need to check implementation of javascript substring function to be sure. 你需要检查javascript substring函数的实现。

Kind of off topic, and not sure if you were required to use recursion, but I think there's a far more efficient way to get the result: 有点偏离主题,不确定你是否需要使用递归,但我认为有一种更有效的方法来获得结果:

function checkBraces( input )
{
   // if the string is an odd number of characters, return immediately.
   if( input.length % 2 !== 0 ) return false; 

   // split the string in half
   var c = input.length / 2;
   var r = input.substr( c );
   var l = input.substr( 0, c );

   // take the left side, reverse it, and swap each left bracket character with it's counterpart
   l = l.split('').reverse().join('').replace(/\{|\(|\[/g, counterpartOf );

   // strings should match
   return r == l;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM