【问题标题】:Brute Force Number of pairs蛮力对数
【发布时间】:2018-04-14 13:02:07
【问题描述】:
function noOfPairs(X,Y) {
   var ans = {};
   var arr = [];
   var count = 0;
   for(var i = 0;i< X.length; i++) {       
        for(var j=0;j < Y.length; j++) {
             if(Math.pow(X[i],Y[j]) > Math.pow(Y[j],X[i])) {
                count++;
             }
        }
    }
   console.log("Total number of pairs is   "+count);
}

var X= [2, 1, 6];
var Y =[1,5];
noOfPairs(X,Y);

我正在尝试在数组中查找对 (x, y) 的数量,使得 x^y > y^x。它给了我正确的结果 3,但这里的时间复杂度是 O(mn)。有什么办法可以优化一下

【问题讨论】:

  • 您可能想在Code Review 上提问,但请阅读他们的主题问题指南。
  • 尝试将 y^x 重新排列为 z^y 形式,以便将 x 与 z 进行比较。
  • 答案可能取决于数字是否总是正整数
  • 这是作业吗?提示:存在O(nlogn + mlogn) 解,假设只有非负整数。

标签: javascript data-structures time-complexity


【解决方案1】:

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

算法如下:

  • 对数组 Y[] 排序。
  • 对于 X[] 中的每一个 x,找到大于的最小数字的索引 idx 比 Y[] 中的 x(也称为 x 的 ceil)使用二分查找。
  • 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    相关资源
    最近更新 更多