小编use*_*907的帖子

找到产品大于总和的对

作为输入,浮标的排序后的数组,我需要找到对的总数(i,j)A[i]*A[j]>=A[i]+A[j]对于每个i < j.我已经知道了天真的解决方案,在其他循环中使用循环,这将给我O(n ^ 2)算法,但我想知道是否有更优化的解决方案.

algorithm optimization

10
推荐指数
2
解决办法
2940
查看次数

这个解决方案有什么问题?(Perm-Missing-Elem codility test)

我已经开始玩鳕鱼并遇到了这个问题:

给出了由N个不同整数组成的零索引数组A. 该数组包含[1 ..(N + 1)]范围内的整数,这意味着只缺少一个元素.

你的目标是找到缺少的元素.

写一个函数:

int solution(int A[], int N); 
Run Code Online (Sandbox Code Playgroud)

在给定零索引数组A的情况下,返回缺少元素的值.

例如,给定数组A,使得:

A [0] = 2 A [1] = 3 A [2] = 1 A [3] = 5

函数应该返回4,因为它是缺少的元素.

假使,假设:

    N is an integer within the range [0..100,000];
    the elements of A are all distinct;
    each element of array A is an integer within the range [1..(N + 1)].
Run Code Online (Sandbox Code Playgroud)

复杂:

    expected worst-case time complexity is O(N);
    expected worst-case space complexity is O(1), beyond input storage (not counting …
Run Code Online (Sandbox Code Playgroud)

php arrays

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

algorithm ×1

arrays ×1

optimization ×1

php ×1