📌  相关文章
📜  要求的最小塔数,以便每所房屋至少在一个塔的范围内

📅  最后修改于: 2021-05-06 10:03:53             🧑  作者: Mango

给定城市和网络范围的地图,任务是确定塔的最小数量,以使每个房屋都在至少一个塔的范围内。每个塔必须安装在现有房屋的顶部。

例子:

Input: range : 1
       house : 1 2 3 4 5
Output: 2

Input: range : 2
       house : 7 2 4 6 5 9 12 11 
Output: 3


通过插入2个塔楼(即2号和4号房屋)可以覆盖所有城市。


通过插入3个塔楼(即4号,9号和12号房屋)可以覆盖所有城市。

算法:-

  1. 首先,对所有元素进行排序。
  2. 仅计数一次,然后遍历至中间房屋。
  3. 此后再次遍历直到塔的范围。
  4. 再次重复1、2、3步,直到所有房屋都被覆盖。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to count the number of tower
int number_of_tower(int house[], int range, int n)
{
  
    // first we sort the house numbers
    sort(house, house + n);
  
    // for count number of twoers
    int numOfTower = 0;
  
    // for iterate all houses
    int i = 0;
    while (i < n) {
  
        // count number of towers
        numOfTower++;
  
        // find find the middle location
        int loc = house[i] + range;
  
        // traverse till middle location
        while (i < n && house[i] <= loc)
            i++;
  
        // this is point to middle
        // house where we insert the tower
        --i;
  
        // now find the last location
        loc = house[i] + range;
  
        // traverse till last house of the range
        while (i < n && house[i] <= loc)
            i++;
    }
  
    // return the number of tower
    return numOfTower;
}
  
// Driver code
int main()
{
    // given elements
    int house[] = { 7, 2, 4, 6, 5, 9, 12, 11 };
    int range = 2;
    int n = sizeof(house) / sizeof(house[0]);
  
    // print number of towers
    cout << number_of_tower(house, range, n);
}


Java
// Java implementation of above approach
  
import java.util.Arrays;
  
public class Improve {
  
    // Function to count the number of tower
    static int number_of_tower(int house[], int range, int n)
    {
  
        // first we sort the house numbers
        Arrays.sort(house);
  
        // for count number of towers
        int numOfTower = 0;
  
        // for iterate all houses
        int i = 0;
        while (i < n) {
  
            // count number of towers
            numOfTower++;
  
            // find find the middle location
            int loc = house[i] + range;
  
            // traverse till middle location
            while (i < n && house[i] <= loc)
                i++;
  
            // this is point to middle
            // house where we insert the tower
            --i;
  
            // now find the last location
            loc = house[i] + range;
  
            // traverse till last house of the range
            while (i < n && house[i] <= loc)
                i++;
        }
  
        // return the number of tower
        return numOfTower;
    }
  
  
      
    public static void main(String args[])
    {
        // given elements
        int house[] = { 7, 2, 4, 6, 5, 9, 12, 11 };
        int range = 2;
        int n = house.length;
  
        // print number of towers
        System.out.println(number_of_tower(house, range, n));
  
          
  
    }
    // This code is contributed by ANKITRAI1
}


Python 3
# Python 3 implementation of 
# above approach
  
# Function to count the
# number of tower
def number_of_tower(house, r, n):
  
    # first we sort the house numbers
    house.sort()
  
    # for count number of twoers
    numOfTower = 0
  
    # for iterate all houses
    i = 0
    while (i < n) :
  
        # count number of towers
        numOfTower += 1
  
        # find find the middle location
        loc = house[i] + r
  
        # traverse till middle location
        while (i < n and house[i] <= loc):
            i += 1
  
        # this is point to middle
        # house where we insert the tower
        i -= 1
  
        # now find the last location
        loc = house[i] + r
  
        # traverse till last house 
        # of the range
        while (i < n and house[i] <= loc):
            i += 1
  
    # return the number of tower
    return numOfTower
  
# Driver code
if __name__ == "__main__":
      
    # given elements
    house = [ 7, 2, 4, 6, 5, 9, 12, 11 ]
    r = 2
    n = len(house)
  
    # print number of towers
    print(number_of_tower(house, r, n))
  
# This code is contributed
# by ChitraNayal


C#
// C# implementation of above approach
  
using System;
  
public class Improve {
  
    // Function to count the number of tower
    static int number_of_tower(int []house, int range, int n)
    {
  
        // first we sort the house numbers
        Array.Sort(house);
  
        // for count number of towers
        int numOfTower = 0;
  
        // for iterate all houses
        int i = 0;
        while (i < n) {
  
            // count number of towers
            numOfTower++;
  
            // find find the middle location
            int loc = house[i] + range;
  
            // traverse till middle location
            while (i < n && house[i] <= loc)
                i++;
  
            // this is point to middle
            // house where we insert the tower
            --i;
  
            // now find the last location
            loc = house[i] + range;
  
            // traverse till last house of the range
            while (i < n && house[i] <= loc)
                i++;
        }
  
        // return the number of tower
        return numOfTower;
    }
  
  
      
    public static void Main()
    {
        // given elements
        int []house = { 7, 2, 4, 6, 5, 9, 12, 11 };
        int range = 2;
        int n = house.Length;
  
        // print number of towers
        Console.WriteLine(number_of_tower(house, range, n));
  
          
 // This code is contributed by inder_verma..
    }
      
}


PHP


输出:
3

时间复杂度: O(nlogn)
空间复杂度: O(1)