这个程序的运行时间复杂度是多少?

时间:2016-03-24 12:12:34

标签: c++ runtime time-complexity

代码是:

class RandomPancakeStack {
  vector<int> *del;
  double seen[256][256];
  public:
    double expectedDeliciousness(vector<int>&);
    double dfs(int size, int index);
};

double RandomPancakeStack::dfs(int pos,  int remain){
  if(pos  < 0) return 0.0;

  if(seen[pos][remain] != -1) { cout << "seen\n"; return seen[pos][remain];}
  double sum = 0.0;
  int i;
  for(i=0; i <=pos; i++){
    cout << " sum += "<< (*del)[i] << " + " << dfs(i-1, remain-1) << endl;
    sum += (*del)[i] + dfs(i-1, remain-1);
  }
  sum /= remain;
  cout << " sum /=remain " << sum << endl;
  seen[pos][remain] = sum;
  return sum;
}

double RandomPancakeStack::expectedDeliciousness(vector<int> &d){
  int N = d.size();
  int i,j,k;
  del = &d;

  for(i=0; i < 256; i++){
    for(j=0; j < 256; j++){
      seen[i][j] = -1;
    }
  }
  return dfs(N-1, N);

是O(N * N)吗?

0 个答案:

没有答案