📌  相关文章
📜  i 和 j 之间的最大距离,使得 i ≤ j 且 A[i] ≤ B[j]

📅  最后修改于: 2022-05-13 01:57:49.508000             🧑  作者: Mango

i 和 j 之间的最大距离,使得 i ≤ j 且 A[i] ≤ B[j]

给定两个大小为N的非递增数组A[]B[] ,任务是找到ij之间的最大距离,使得i ≤ jA[i] ≤ B[j]

例子:

朴素方法:解决问题的最简单方法是遍历数组 A[] 并针对 A[] 的每个元素遍历数组 B[] 并找到点之间的最大距离,使得 j>=i 和 B[j] >= A[i]。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:上述方法可以通过使用二分搜索算法进一步优化。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0以存储两个非递增数组中两个元素之间的最大距离。
  • 使用变量i[0, N-1]范围内迭代,并执行以下步骤:  
    • 初始化两个变量,说i,高N-1。
    • 迭代直到low小于或等于high并执行以下操作
      • 初始化一个变量,比如midlow+(high-low)/2
      • 如果A[i]小于等于B[mid],则更新ans 最大限度 ansmid-i以及中+1。
      • 否则,将high更新到mid-1。
  • 最后,完成以上步骤后,打印ans的值作为答案

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum distance
// between two elements satisfying the
// conditions
int maxDistance(int A[], int B[], int N)
{
    // Stores the result
    int ans = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        int low = i, high = N - 1;
 
        // Iterate until low is less than
        // or equal to high
        while (low <= high) {
 
            int mid = low + (high - low) / 2;
 
            // If A[i] less than equal to B[mid]
            if (A[i] <= B[mid]) {
 
                // Update answer and low
                ans = max(ans, mid - i);
                low = mid + 1;
            }
            // Otherwise
            else {
                // Update high
                high = mid - 1;
            }
        }
    }
 
    // Finally, print the ans
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int A[] = { 2, 2, 2 };
    int B[] = { 10, 10, 1 };
    int N = 3;
 
    // Function Call
    cout << maxDistance(A, B, N);
}


Java
// Java program for the above approach
public class GFG {
 
    // Function to find the maximum distance
    // between two elements satisfying the
    // conditions
    static int maxDistance(int A[], int B[], int N)
    {
       
        // Stores the result
        int ans = 0;
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
 
            int low = i, high = N - 1;
 
            // Iterate until low is less than
            // or equal to high
            while (low <= high) {
 
                int mid = low + (high - low) / 2;
 
                // If A[i] less than equal to B[mid]
                if (A[i] <= B[mid]) {
 
                    // Update answer and low
                    ans = Math.max(ans, mid - i);
                    low = mid + 1;
                }
                // Otherwise
                else {
                    // Update high
                    high = mid - 1;
                }
            }
        }
 
        // Finally, print the ans
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Given Input
        int A[] = { 2, 2, 2 };
        int B[] = { 10, 10, 1 };
        int N = 3;
 
        // Function Call
        System.out.println(maxDistance(A, B, N));
    }
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find the maximum distance
# between two elements satisfying the
# conditions
def maxDistance(A, B, N):
     
    # Stores the result
    ans = 0
 
    # Traverse the array
    for i in range(N):
        low = i
        high = N - 1
 
        # Iterate until low is less than
        # or equal to high
        while (low <= high):
            mid = low + (high - low) // 2
 
            # If A[i] less than equal to B[mid]
            if (A[i] <= B[mid]):
 
                # Update answer and low
                ans = max(ans, mid - i)
                low = mid + 1
 
            # Otherwise
            else:
                 
                # Update high
                high = mid - 1
 
    # Finally, print the ans
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    A = [ 2, 2, 2 ]
    B = [ 10, 10, 1 ]
    N = 3
 
    # Function Call
    print(maxDistance(A, B, N))
     
# This code is contributed by bgangwar59


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
    // Function to find the maximum distance
    // between two elements satisfying the
    // conditions
    static int maxDistance(int[] A, int[] B, int N)
    {
       
        // Stores the result
        int ans = 0;
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
 
            int low = i, high = N - 1;
 
            // Iterate until low is less than
            // or equal to high
            while (low <= high) {
 
                int mid = low + (high - low) / 2;
 
                // If A[i] less than equal to B[mid]
                if (A[i] <= B[mid]) {
 
                    // Update answer and low
                    ans = Math.Max(ans, mid - i);
                    low = mid + 1;
                }
                // Otherwise
                else {
                    // Update high
                    high = mid - 1;
                }
            }
        }
 
        // Finally, print the ans
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        // Given Input
        int[] A = { 2, 2, 2 };
        int[] B = { 10, 10, 1 };
        int N = 3;
 
        // Function Call
        Console.Write(maxDistance(A, B, N));
    }
}
 
// This code is contributed by shubhamsingh10


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum distance
// between two elements in two non increasing
// arrays
 
int maxDistance(int A[], int B[], int N)
{
 
    int i = 0, j = 0;
    // Stores the result
    int ans = 0;
 
    // Iterate while i and j are less than N
    while (i < N && j < N) {
 
        // If A[i] is greater than B[j]
        if (A[i] > B[j]) {
            ++i;
        }
        // Otherwise,
        else {
 
            // Update the answer
            ans = max(ans, j - i);
 
            // Increment j
            j++;
        }
    }
 
    // Finally, print the ans
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int A[] = { 2, 2, 2 };
    int B[] = { 10, 10, 1 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    cout << maxDistance(A, B, N);
}


Java
// Java program for the above approach
import java.io.*;
class GFG {
     
    // Function to find the maximum distance
    // between two elements in two non increasing
    // arrays   
    static int maxDistance(int A[], int B[], int N)
    {
     
        int i = 0, j = 0;
       
        // Stores the result
        int ans = 0;
     
        // Iterate while i and j are less than N
        while (i < N && j < N) {
     
            // If A[i] is greater than B[j]
            if (A[i] > B[j]) {
                ++i;
            }
            // Otherwise,
            else {
     
                // Update the answer
                ans = Math.max(ans, j - i);
     
                // Increment j
                j++;
            }
        }
     
        // Finally, print the ans
        return ans;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
       
        // Given Input
        int A[] = { 2, 2, 2 };
        int B[] = { 10, 10, 1 };
        int N = A.length;
     
        // Function Call
        System.out.println(maxDistance(A, B, N));
    }
}
 
// This code is contributed by Shubhamsingh10


Python3
# Python3 program for the above approach
 
# Function to find the maximum distance
# between two elements in two non increasing
# arrays
def maxDistance(A, B, N):
 
    i = 0
    j = 0
     
    # Stores the result
    ans = 0
 
    # Iterate while i and j are less than N
    while (i < N and j < N):
 
        # If A[i] is greater than B[j]
        if (A[i] > B[j]):
            i += 1
             
        # Otherwise,
        else:
 
            # Update the answer
            ans = max(ans, j - i)
 
            # Increment j
            j += 1
 
    # Finally, print the ans
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    A = [ 2, 2, 2 ]
    B = [ 10, 10, 1 ]
    N = len(A)
 
    # Function Call
    print(maxDistance(A, B, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG {
     
    // Function to find the maximum distance
    // between two elements in two non increasing
    // arrays   
    static int maxDistance(int []A, int []B, int N)
    {
     
        int i = 0, j = 0;
       
        // Stores the result
        int ans = 0;
     
        // Iterate while i and j are less than N
        while (i < N && j < N) {
     
            // If A[i] is greater than B[j]
            if (A[i] > B[j]) {
                ++i;
            }
            // Otherwise,
            else {
     
                // Update the answer
                ans = Math.Max(ans, j - i);
     
                // Increment j
                j++;
            }
        }
     
        // Finally, print the ans
        return ans;
    }
     
    // Driver Code
    public static void Main (String[] args)
    {
       
        // Given Input
        int []A = { 2, 2, 2 };
        int []B = { 10, 10, 1 };
        int N = A.Length;
     
        // Function Call
        Console.Write(maxDistance(A, B, N));
    }
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出
1

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

高效方法:上述方法可以通过使用两个指针技术进一步优化。请按照以下步骤解决问题:

  • 初始化 要执行的两个变量,例如ij 一个两分球。
  • 迭代直到ij小于N并执行以下步骤
    • 如果A[i]大于B[j],则将i递增1,因为数组是按非递增排序的。
    • 否则,更新ans 最大限度 ansji 的值,并将j1。
  • 最后,完成以上步骤后,打印ans的值作为答案。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum distance
// between two elements in two non increasing
// arrays
 
int maxDistance(int A[], int B[], int N)
{
 
    int i = 0, j = 0;
    // Stores the result
    int ans = 0;
 
    // Iterate while i and j are less than N
    while (i < N && j < N) {
 
        // If A[i] is greater than B[j]
        if (A[i] > B[j]) {
            ++i;
        }
        // Otherwise,
        else {
 
            // Update the answer
            ans = max(ans, j - i);
 
            // Increment j
            j++;
        }
    }
 
    // Finally, print the ans
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int A[] = { 2, 2, 2 };
    int B[] = { 10, 10, 1 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    cout << maxDistance(A, B, N);
}

Java

// Java program for the above approach
import java.io.*;
class GFG {
     
    // Function to find the maximum distance
    // between two elements in two non increasing
    // arrays   
    static int maxDistance(int A[], int B[], int N)
    {
     
        int i = 0, j = 0;
       
        // Stores the result
        int ans = 0;
     
        // Iterate while i and j are less than N
        while (i < N && j < N) {
     
            // If A[i] is greater than B[j]
            if (A[i] > B[j]) {
                ++i;
            }
            // Otherwise,
            else {
     
                // Update the answer
                ans = Math.max(ans, j - i);
     
                // Increment j
                j++;
            }
        }
     
        // Finally, print the ans
        return ans;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
       
        // Given Input
        int A[] = { 2, 2, 2 };
        int B[] = { 10, 10, 1 };
        int N = A.length;
     
        // Function Call
        System.out.println(maxDistance(A, B, N));
    }
}
 
// This code is contributed by Shubhamsingh10

Python3

# Python3 program for the above approach
 
# Function to find the maximum distance
# between two elements in two non increasing
# arrays
def maxDistance(A, B, N):
 
    i = 0
    j = 0
     
    # Stores the result
    ans = 0
 
    # Iterate while i and j are less than N
    while (i < N and j < N):
 
        # If A[i] is greater than B[j]
        if (A[i] > B[j]):
            i += 1
             
        # Otherwise,
        else:
 
            # Update the answer
            ans = max(ans, j - i)
 
            # Increment j
            j += 1
 
    # Finally, print the ans
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    A = [ 2, 2, 2 ]
    B = [ 10, 10, 1 ]
    N = len(A)
 
    # Function Call
    print(maxDistance(A, B, N))
 
# This code is contributed by mohit kumar 29

C#

// C# program for the above approach
using System;
class GFG {
     
    // Function to find the maximum distance
    // between two elements in two non increasing
    // arrays   
    static int maxDistance(int []A, int []B, int N)
    {
     
        int i = 0, j = 0;
       
        // Stores the result
        int ans = 0;
     
        // Iterate while i and j are less than N
        while (i < N && j < N) {
     
            // If A[i] is greater than B[j]
            if (A[i] > B[j]) {
                ++i;
            }
            // Otherwise,
            else {
     
                // Update the answer
                ans = Math.Max(ans, j - i);
     
                // Increment j
                j++;
            }
        }
     
        // Finally, print the ans
        return ans;
    }
     
    // Driver Code
    public static void Main (String[] args)
    {
       
        // Given Input
        int []A = { 2, 2, 2 };
        int []B = { 10, 10, 1 };
        int N = A.Length;
     
        // Function Call
        Console.Write(maxDistance(A, B, N));
    }
}
 
// This code is contributed by shivanisinghss2110

Javascript


输出
1

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