简体   繁体   English

提高本代码效率的技巧(初学者)

[英]Tips on Improving Efficiency of this Code (Beginner)

I am currently doing a coding exercise and am missing some cases due to the time limit being exceeded.我目前正在进行编码练习,但由于超过了时间限制而遗漏了一些案例。 Can I get some tips on how to improve the efficiency of my code?我可以获得一些关于如何提高代码效率的提示吗? Also if you have any general tips for a beginner I would also appreciate that.此外,如果您对初学者有任何一般提示,我也将不胜感激。 The problem is below and thanks.问题如下,谢谢。

You are given all numbers between 1,2,…,n except one.您将获得 1,2,...,n 之间的所有数字,除了一个。 Your task is to find the missing number.你的任务是找到丢失的号码。

Input输入

The first input line contains an integer n.第一个输入行包含一个 integer n。

The second line contains n−1 numbers.第二行包含 n-1 个数字。 Each number is distinct and between 1 and n (inclusive).每个数字都是不同的,介于 1 和 n(含)之间。

Output Output

Print the missing number.打印缺少的号码。

Constraints 2≤n≤2⋅105 Example约束条件 2≤n≤2⋅105 示例

Input:输入:

5
2 3 1 5

Output: Output:

4

Here is my code:这是我的代码:

#include <bits/stdc++.h>

using namespace std;

int missingNumber(vector<int> available, int N) {
    for (int i=1; i<=N; i++) {
        bool counter = false;
        for (int j=0; j<N-1; j++) {
            if (i == available[j]) {
                counter = true;    
            }
        }
        if (counter == false) {
            return i;    
        }
    }    
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int N;
    cin >> N;
    vector<int> available(N-1);
    int temp = 0;
    for (int i=0; i<N-1; i++) {
        cin >> temp;
        available[i] = temp;
    }
    cout << missingNumber(available, N);
}

A very simple solution with O(N) complexity is based on the observation that if the N-1 numbers are all between 1 and N and distinct from each other, then it suffices to:一个具有 O(N) 复杂度的非常简单的解决方案是基于以下观察:如果 N-1 个数字都在 1 和 N 之间并且彼此不同,那么它就足够了:

  1. compute the sum of all these N-1 numbers, so linear complexity计算所有这些 N-1 数字的总和,因此线性复杂度
  2. subtract the sum computed at step 1 from the sum of the N numbers from 1 to N, which we know is N * (N + 1) / 2, so O(1) complexity.从从 1 到 N 的 N 个数字的总和中减去第 1 步计算的总和,我们知道是 N * (N + 1) / 2,所以复杂度为 O(1)。

here is an answer with two versions to your problem这是您的问题的两个版本的答案

the first version is using Arithmetic progression formula n*(a1 + an) /2 and then subtract your vector sum with the result of the formula.第一个版本使用Arithmetic progression formula n*(a1 + an) /2 ,然后用公式的结果减去向量和。

double missingNumber_ver1(std::vector<int> available, int N) {
    // formula for sum for Arithmetic progression
    double sum = N * (available[0]+available[N-2]) /2;
    double available_sym = std::accumulate(available.begin(), available.end(), 0); // this is to sum the giving numbers
    double missing_num = sum-available_sym;
    return missing_num;
}

the second version is to use XOR operator and when there is a xor value that is not 0 that means this is the missing number.第二个版本是使用XOR运算符,当异或值不是 0 时,这意味着这是缺失的数字。 I'm also using std::iota to build the comparison vector with range values.我还使用std::iota来构建具有范围值的比较向量。

double missingNumber_ver2(std::vector<int> available, int N) {
      std::vector<int>tem_vec(N-1);  
      std::iota(tem_vec.begin(), tem_vec.end(), available[0]);
      auto av_it = available.begin();
      auto tem_vec_it = tem_vec.begin();
      while(!(*av_it ^ *tem_vec_it))
      {
        av_it++;
        tem_vec_it++;
      }
      return *tem_vec_it;
    }

and here is the full code - look that I made few changes also in the main() function这是完整的代码 - 看看我在main() function 中也做了一些更改

 #include <iostream>
 #include <numeric>
 #include <vector>

double missingNumber_ver1(std::vector<int> available, int N) {
    // formula for sum for Arithmetic progression
    double sum = N * (available[0]+available[N-2]) /2;
    double available_sym = std::accumulate(available.begin(), available.end(), 0);
    double missing_num = sum-available_sym;
    return missing_num;
}

double missingNumber_ver2(std::vector<int> available, int N) {
  std::vector<int>tem_vec(4);
  std::iota(tem_vec.begin(), tem_vec.end(), available[0]);
  auto av_it = available.begin();
  auto tem_vec_it = tem_vec.begin();
  while(!(*av_it ^ *tem_vec_it))
  {
    av_it++;
    tem_vec_it++;
  }
  return *tem_vec_it;
}


int main() {
    int N;
    std::cin >> N;
    std::vector<int> available;
    int temp = 0;
    for (int i=0; i<N-1; i++) {
        std::cin >> temp;
        available.push_back(temp);
    }
    std::cout << "missingNumber_ver1 " << missingNumber_ver1(available, N) << "\n";
    std::cout << "missingNumber_ver2 " <<missingNumber_ver2(available, N) << "\n";
}

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

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