Codility-CountNonDivisible

Task description

You are given a non-empty zero-indexed array A consisting of N integers.

For each number A[i] such that 0 ≤ i < N, we want to count the number of elements of the array that are not the divisors of A[i]. We say that these elements are non-divisors.

For example, consider integer N = 5 and array A such that:

    A[0] = 3
    A[1] = 1
    A[2] = 2
    A[3] = 3
    A[4] = 6

For the following elements:

  • A[0] = 3, the non-divisors are: 2, 6,
  • A[1] = 1, the non-divisors are: 3, 2, 3, 6,
  • A[2] = 2, the non-divisors are: 3, 3, 6,
  • A[3] = 3, the non-divisors are: 2, 6,
  • A[6] = 6, there aren't any non-divisors.

Write a function:

vector<int> solution(vector<int> &A);

that, given a non-empty zero-indexed array A consisting of N integers, returns a sequence of integers representing the amount of non-divisors.

The sequence should be returned as:

  • a structure Results (in C), or
  • a vector of integers (in C++), or
  • a record Results (in Pascal), or
  • an array of integers (in any other programming language).

For example, given:

    A[0] = 3
    A[1] = 1
    A[2] = 2
    A[3] = 3
    A[4] = 6

the function should return [2, 4, 3, 2, 0], as explained above.

Assume that:

  • N is an integer within the range [1..50,000];
  • each element of array A is an integer within the range [1..2 * N].

Complexity:

  • expected worst-case time complexity is O(N*log(N));
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.

思路:思路来自于stackoverflow,jaho的回答。

1.首先定义一个vector counts来记录A中每个元素出现的次数

2.对任意一个i,计算A中A[i]的因子数。由于我们只需要计算小于或等于sqrt(A[i])的因子便可得出A[i]的所有因子,故外层循环:i,0~N-1

内层循环:j,1~sqrt(A[i])。若A[i] % j = 0则 j 是A[i]的因子,A[i]的因子数+1,若 j 不等于sqrt(A[i]),则A[i] / j 也是A[i]的因子,A[i]的因子数继续 +1。

3.最后,对每个i,res[i]等于N减去A[i]的因子数。

代码:

// you can use includes, for example:
#include 
   
   
    
    
//#include 
    
    
     
     
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

vector
     
     
      
       solution(vector
      
      
       
        &A) {
    // write your code in C++11
    int N = A.size();
    vector
       
       
         ::iterator max = max_element(A.begin(),A.end()); vector 
        
          counts(*max+1,0); for(int i = 0 ; i < N ; ++i) { counts[A[i]]++; } vector 
         
           res(N,0); for(int i = 0 ; i < N ; ++i) { int divisors = 0; for(int j = 1 ; j*j <= A[i] ; ++j) { if(A[i] % j == 0) { divisors += counts[j]; if(A[i]/j != j) { divisors += counts[A[i]/j]; } } } res[i] = N - divisors; } return res; } 
          
         
       
      
      
     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值