蛮力对数

时间:2018-04-14 13:02:07

标签: javascript data-structures time-complexity

(node:20730) UnhandledPromiseRejectionWarning: Error: 7 PERMISSION_DENIED: Cloud Video Intelligence API has not been used in project firebase-cli before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/videointelligence.googleapis.com/overview?project=firebase-cli then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
at new createStatusError (/home/warcram/node_modules/grpc/src/client.js:64:15)
at /home/warcram/node_modules/grpc/src/client.js:583:15
(node:20730) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:20730) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.e

我试图找到数组中的对(x,y)的数量,使得x ^ y> y ^ x。它给出了正确的结果3但是这里的时间复杂度是O(mn)。有没有办法优化它

1 个答案:

答案 0 :(得分:2)

O(nLogn + mLogn)时间内存在解决方案。

以下是算法:

  • 排序数组Y []。
  • 对于X []中的每个x,找到最小数字的索引idx 使用二分法搜索,在Y []中使用x(也称为ceil of x)。
  • idx之后的所有数字都满足关系,所以只需添加(n-idx)即可 伯爵。

这些将是您的计划的基本案例:

  • 如果x = 0,则此x的对数为0。
  • 如果x = 1,则此x的对数等于0的计数 在Y []。

以下情况必须单独处理,因为它们不遵循x小于y意味着x ^ y大于y ^ x的一般规则。

  • x = 2,y = 3或4
  • x = 3,y = 2

我不知道Javascript,所以这里是C ++中的代码:

#include<iostream>
#include<algorithm>
using namespace std;

// This function return count of pairs with x as one element
// of the pair. It mainly looks for all values in Y[] where
// x ^ Y[i] > Y[i] ^ x
int count(int x, int Y[], int n, int NoOfY[])
{
    // If x is 0, then there cannot be any value in Y such that
    // x^Y[i] > Y[i]^x
    if (x == 0) return 0;

    // If x is 1, then the number of pais is equal to number of
    // zeroes in Y[]
    if (x == 1) return NoOfY[0];

    // Find number of elements in Y[] with values greater than x
    // upper_bound() gets address of first greater element in Y[0..n-1]
    int* idx = upper_bound(Y, Y + n, x);
    int ans = (Y + n) - idx;

    // If we have reached here, then x must be greater than 1,
    // increase number of pairs for y=0 and y=1
    ans += (NoOfY[0] + NoOfY[1]);

    // Decrease number of pairs for x=2 and (y=4 or y=3)
    if (x == 2)  ans -= (NoOfY[3] + NoOfY[4]);

    // Increase number of pairs for x=3 and y=2
    if (x == 3)  ans += NoOfY[2];

    return ans;
}

// The main function that returns count of pairs (x, y) such that
// x belongs to X[], y belongs to Y[] and x^y > y^x
int countPairs(int X[], int Y[], int m, int n)
{
    // To store counts of 0, 1, 2, 3 and 4 in array Y
    int NoOfY[5] = {0};
    for (int i = 0; i < n; i++)
        if (Y[i] < 5)
            NoOfY[Y[i]]++;

    // Sort Y[] so that we can do binary search in it
    sort(Y, Y + n);

    int total_pairs = 0; // Initialize result

    // Take every element of X and count pairs with it
    for (int i=0; i<m; i++)
        total_pairs += count(X[i], Y, n, NoOfY);

    return total_pairs;
}

// Driver program to test above functions
int main()
{
    int X[] = {2, 1, 6};
    int Y[] = {1, 5};

    int m = sizeof(X)/sizeof(X[0]);
    int n = sizeof(Y)/sizeof(Y[0]);

    cout << "Total pairs = " << countPairs(X, Y, m, n);

    return 0;
}

Source