给定一个数组n房屋的含有位置和数组b含M无线电塔,每个沿着水平线放置的位置,则任务是找到最小广播范围,使得每个无线电塔到达每一个房子。
例子:
Input: a[] = {1, 5, 11, 20}, b[] = {4, 8, 15}
Output: 5
Explanation:
The minimum range required is 5, since that would be required for the tower at position 15 to reach the house at position 20
Input: a[] = {12, 13, 11, 80}, b[] = {4, 6, 15, 60}
Output: 20
Explanation:
The minimum range required is 20, since that would be required for the tower at position 60 to reach the house at position 80
方法:遍历两个阵列,直到计算出最后一所房子的广播范围。对于每座房屋,分别比较其与左塔和右塔的距离,并考虑最小值。将该最小值与到目前为止获得的最大值进行比较,然后存储最大值。
注意:左塔到第一个房屋的距离被视为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
20
时间复杂度: O(M + N)