📌  相关文章
📜  M个塔到达N个房屋所需的最小广播范围

📅  最后修改于: 2021-09-07 02:35:30             🧑  作者: Mango

给定一个包含N 个房屋位置的数组a和一个包含M 个无线电塔位置的数组b ,每个天线都沿水平线放置,任务是找到最小广播范围,使每个无线电塔都能到达每个房屋。
例子:

方法:遍历两个数组,直到计算出最后一个房子的广播范围。对于每栋房子,分别比较它与左右塔的距离,并考虑最小值。将此最小值与迄今为止获得的最大值进行比较并存储最大值。
注意:左塔到第一所房子的距离被认为是Integer.MIN_VALUE 。如果我们到达塔的尽头,则所有剩余房屋与相应右塔的距离被视为Integer.MAX_VALUE
下面的代码实现了上述方法:

C++
// CPP program to implement the above approach
#include
using namespace std;
 
int minBroadcastRange(int houses[], int towers[],int n,int m)
    {
        // Initialize distance of left
        // tower from first house
        int leftTower = INT_MIN;
 
        // Initialize distance of right
        // tower from first house
        int rightTower = towers[0];
 
        // j: Index of houses[]
        // k: Index of towers[]
        int j = 0, k = 0;
 
        // Store the minimum required range
        int min_range = 0;
 
        while (j < n) {
 
            // If the house lies between
            // left and right towers
            if (houses[j] < rightTower) {
 
                int left = houses[j] - leftTower;
                int right = rightTower - houses[j];
 
                // Compare the distance between the
                // left and right nearest towers
                int local_max = left < right ? left : right;
 
                if (local_max > min_range)
 
                    // updating the maximum value
                    min_range = local_max;
                j++;
            }
            else {
 
                // updating the left tower
                leftTower = towers[k];
 
                if (k < m - 1) {
 
                    k++;
                    // updating the right tower
                    rightTower = towers[k];
                }
                else
                    // updating right tower
                    // to maximum value after
                    // reaching the end of Tower array
                    rightTower = INT_MAX;
            }
        }
        return min_range;
    }
 
    // Driver code
    int main()
    {
        int a[] = { 12, 13, 11, 80 };
        int b[] = { 4, 6, 15, 60 };
        int n = sizeof(a)/sizeof(a[0]);
        int m = sizeof(b)/sizeof(b[0]);
        int max = minBroadcastRange(a, b,n,m);
        cout<


Java
// Java program to implement the above approach
 
import java.io.*;
 
class GFG {
 
    private static int minBroadcastRange(
        int[] houses, int[] towers)
    {
 
        // Store no of houses
        int n = houses.length;
 
        // Store no of towers
        int m = towers.length;
 
        // Initialize distance of left
        // tower from first house
        int leftTower = Integer.MIN_VALUE;
 
        // Initialize distance of right
        // tower from first house
        int rightTower = towers[0];
 
        // j: Index of houses[]
        // k: Index of towers[]
        int j = 0, k = 0;
 
        // Store the minimum required range
        int min_range = 0;
 
        while (j < n) {
 
            // If the house lies between
            // left and right towers
            if (houses[j] < rightTower) {
 
                int left = houses[j] - leftTower;
                int right = rightTower - houses[j];
 
                // Compare the distance between the
                // left and right nearest towers
                int local_max = left < right ? left : right;
 
                if (local_max > min_range)
 
                    // updating the maximum value
                    min_range = local_max;
                j++;
            }
            else {
 
                // updating the left tower
                leftTower = towers[k];
 
                if (k < m - 1) {
 
                    k++;
                    // updating the right tower
                    rightTower = towers[k];
                }
                else
                    // updating right tower
                    // to maximum value after
                    // reaching the end of Tower array
                    rightTower = Integer.MAX_VALUE;
            }
        }
        return min_range;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] a = { 12, 13, 11, 80 };
        int[] b = { 4, 6, 15, 60 };
        int max = minBroadcastRange(a, b);
        System.out.println(max);
    }
}


Python3
# Python 3 program to implement the above approach
import sys
 
def minBroadcastRange( houses, towers, n, m):
 
    # Initialize distance of left
    # tower from first house
    leftTower = -sys.maxsize - 1
 
    # Initialize distance of right
    # tower from first house
    rightTower = towers[0]
 
    # j: Index of houses[]
    # k: Index of towers[]
    j , k = 0 , 0
 
    # Store the minimum required range
    min_range = 0
 
    while (j < n):
 
        # If the house lies between
        # left and right towers
        if (houses[j] < rightTower):
 
            left = houses[j] - leftTower
            right = rightTower - houses[j]
 
            # Compare the distance between the
            # left and right nearest towers
            if left < right :
                local_max = left
            else:
                local_max = right
 
            if (local_max > min_range):
 
                # updating the maximum value
                min_range = local_max
            j += 1
         
        else:
 
            # updating the left tower
            leftTower = towers[k]
 
            if (k < m - 1) :
 
                k += 1
 
                # updating the right tower
                rightTower = towers[k]
             
            else:
                # updating right tower
                # to maximum value after
                # reaching the end of Tower array
                rightTower = sys.maxsize
    return min_range
 
# Driver code
if __name__ == "__main__":
     
    a = [ 12, 13, 11, 80 ]
    b = [ 4, 6, 15, 60 ]
    n = len(a)
    m = len(b)
    max = minBroadcastRange(a, b,n,m)
    print(max)
 
# This code is contributed by chitranayal


C#
// C# program to implement the above approach
 using System;
 
class GFG {
  
    private static int minBroadcastRange(
        int[] houses, int[] towers)
    {
  
        // Store no of houses
        int n = houses.Length;
  
        // Store no of towers
        int m = towers.Length;
  
        // Initialize distance of left
        // tower from first house
        int leftTower = int.MinValue;
  
        // Initialize distance of right
        // tower from first house
        int rightTower = towers[0];
  
        // j: Index of houses[]
        // k: Index of towers[]
        int j = 0, k = 0;
  
        // Store the minimum required range
        int min_range = 0;
  
        while (j < n) {
  
            // If the house lies between
            // left and right towers
            if (houses[j] < rightTower) {
  
                int left = houses[j] - leftTower;
                int right = rightTower - houses[j];
  
                // Compare the distance between the
                // left and right nearest towers
                int local_max = left < right ? left : right;
  
                if (local_max > min_range)
  
                    // updating the maximum value
                    min_range = local_max;
                j++;
            }
            else {
  
                // updating the left tower
                leftTower = towers[k];
  
                if (k < m - 1) {
  
                    k++;
                    // updating the right tower
                    rightTower = towers[k];
                }
                else
                    // updating right tower
                    // to maximum value after
                    // reaching the end of Tower array
                    rightTower = int.MaxValue;
            }
        }
        return min_range;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int[] a = { 12, 13, 11, 80 };
        int[] b = { 4, 6, 15, 60 };
        int max = minBroadcastRange(a, b);
        Console.WriteLine(max);
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:

20

时间复杂度: O(M + N)

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