📌  相关文章
📜  用索引乘积填充的给定维度矩阵的第 K 个最小元素

📅  最后修改于: 2021-09-07 04:05:28             🧑  作者: Mango

给定一个整数K和一个大小为N x M 的矩阵,其中每个矩阵元素等于其索引 ( i * j ) 的乘积,任务是找到给定矩阵中的K最小元素
例子:

朴素的方法:最简单的方法是将矩阵的所有元素存储在一个数组中,然后通过对数组进行排序来找到第K最小的元素。
时间复杂度: O(N×M×log(N×M))
辅助空间: O(N×M)
有效的方法:
为了优化朴素的方法,想法是使用二进制搜索算法。请按照以下步骤解决问题:

  1. 初始化1N×M,因为第K最小元素位于1N×M 之间。
  2. 找到中点 元素之间的 元素
  3. 如果小于mid的元素个数大于等于 K,然后将high更新为mid-1,因为第K 个最小元素位于lowmid之间
  4. 如果小于mid的元素个数小于 K,然后将low更新为mid+1,因为第K 个最小元素位于midhigh之间
  5. 如在第i行中的元素i的倍数,元素小于第i中间可由分钟(中/ I,M)容易地计算出的数量因此,找到小于mid的元素数量的时间复杂度只能在O(N) 中完成
  6. 执行二分查找,直到low小于或等于high,并返回high+1作为矩阵N×M第K个最小元素

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
#define LL long long
 
// Function that returns true if the
// count of elements is less than mid
bool countLessThanMid(LL mid, LL N,
                    LL M, LL K)
{
    // To store count of elements
    // less than mid
    LL count = 0;
 
    // Loop through each row
    for (int i = 1;
        i <= min((LL)N, mid); ++i) {
 
        // Count elements less than
        // mid in the ith row
        count = count + min(mid / i, M);
    }
 
    if (count >= K)
        return false;
    else
        return true;
}
 
// Function that returns the Kth
// smallest element in the NxM
// Matrix after sorting in an array
LL findKthElement(LL N, LL M, LL K)
{
    // Initialize low and high
    LL low = 1, high = N * M;
 
    // Perform binary search
    while (low <= high) {
 
        // Find the mid
        LL mid = low + (high - low) / 2;
 
        // Check if the count of
        // elements is less than mid
        if (countLessThanMid(mid, N, M, K))
            low = mid + 1;
        else
            high = mid - 1;
    }
 
    // Return Kth smallest element
    // of the matrix
    return high + 1;
}
 
// Driver Code
int main()
{
    LL N = 2, M = 3;
 
    LL int K = 5;
 
    cout << findKthElement(N, M, K) << endl;
 
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG{
     
// Function that returns true if the
// count of elements is less than mid
public static boolean countLessThanMid(int mid, int N,
                                       int M, int K)
{
     
    // To store count of elements
    // less than mid
    int count = 0;
 
    // Loop through each row
    for(int i = 1;
            i <= Math.min(N, mid); ++i)
    {
         
        // Count elements less than
        // mid in the ith row
        count = count + Math.min(mid / i, M);
    }
 
    if (count >= K)
        return false;
    else
        return true;
}
 
// Function that returns the Kth
// smallest element in the NxM
// Matrix after sorting in an array
public static int findKthElement(int N, int M, int K)
{
     
    // Initialize low and high
    int low = 1, high = N * M;
 
    // Perform binary search
    while (low <= high)
    {
         
        // Find the mid
        int mid = low + (high - low) / 2;
 
        // Check if the count of
        // elements is less than mid
        if (countLessThanMid(mid, N, M, K))
            low = mid + 1;
        else
            high = mid - 1;
    }
 
    // Return Kth smallest element
    // of the matrix
    return high + 1;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 2, M = 3;
    int K = 5;
 
    System.out.println(findKthElement(N, M, K));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
# Function that returns true if the
# count of elements is less than mid
def countLessThanMid(mid, N, M, K):
     
    # To store count of elements
    # less than mid
    count = 0
 
    # Loop through each row
    for i in range (1, min(N, mid) + 1):
 
        # Count elements less than
        # mid in the ith row
        count = count + min(mid // i, M)
     
    if (count >= K):
        return False
    else:
        return True
 
# Function that returns the Kth
# smallest element in the NxM
# Matrix after sorting in an array
def findKthElement(N, M, K):
 
    # Initialize low and high
    low = 1
    high = N * M
 
    # Perform binary search
    while (low <= high):
 
        # Find the mid
        mid = low + (high - low) // 2
 
        # Check if the count of
        # elements is less than mid
        if (countLessThanMid(mid, N, M, K)):
            low = mid + 1
        else:
            high = mid - 1
 
    # Return Kth smallest element
    # of the matrix
    return high + 1
 
# Driver Code
if __name__ == "__main__": 
    N = 2
    M = 3
    K = 5
    print(findKthElement(N, M, K))
     
# This code is contributed by Chitranayal


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function that returns true if the
// count of elements is less than mid
public static bool countLessThanMid(int mid, int N,
                                    int M, int K)
{
     
    // To store count of elements
    // less than mid
    int count = 0;
 
    // Loop through each row
    for(int i = 1;
            i <= Math.Min(N, mid); ++i)
    {
         
        // Count elements less than
        // mid in the ith row
        count = count + Math.Min(mid / i, M);
    }
 
    if (count >= K)
        return false;
    else
        return true;
}
 
// Function that returns the Kth
// smallest element in the NxM
// Matrix after sorting in an array
public static int findKthElement(int N, int M,
                                        int K)
{
     
    // Initialize low and high
    int low = 1, high = N * M;
 
    // Perform binary search
    while (low <= high)
    {
         
        // Find the mid
        int mid = low + (high - low) / 2;
 
        // Check if the count of
        // elements is less than mid
        if (countLessThanMid(mid, N, M, K))
            low = mid + 1;
        else
            high = mid - 1;
    }
 
    // Return Kth smallest element
    // of the matrix
    return high + 1;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 2, M = 3;
    int K = 5;
 
    Console.WriteLine(findKthElement(N, M, K));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

4

时间复杂度: O(N×log(N×M))
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live