📌  相关文章
📜  最大限度地增加 L 长度电线可以覆盖的圆形建筑物的数量

📅  最后修改于: 2021-10-26 06:46:40             🧑  作者: Mango

给定一个大小为N的数组arr[] ,表示N 个圆形建筑物的直径和长度为L的直线。任务是找到最大值 如果绕建筑物绕一圈,电线可以覆盖的连续建筑物的数量。

注:每个建筑物之间的距离为1个单位长度,因此需要1个单位 长度 额外到达一个建筑到下一个建筑。

例子:

方法:这个想法是使用贪婪的方法。请按照以下步骤解决问题:

  • 初始化变量curr_sum、start、curr_count、max_count ,计算当前元素的总和、当前计数、当前子数组的起始索引和被覆盖建筑物的最大计数。
  • [0, N – 1]范围内遍历i的数组
    • 更新当前所需的线长总和, curr_sum += arr[i] * 3.14
    • 如果i大于 0,则将 curr_sum增加 1。
    • 如果curr_sum ≤ L 。将curr_count增加 1
    • 否则,从当前组中排除arr[start]
      • 更新curr_sum = curr_sum – ((double)arr[start] * 3.14)
      • curr_sum减 1
      • 起始指针加 1
      • curr_count减 1
  • 更新max_count,max_count = max(curr_count, max_count)。
  • 完成以上步骤后,打印max_count的值作为结果。

下面是上述方法的实现。

C++14
// C++ program for the above approach
#include 
using namespace std;
const double Pi = 3.141592;
 
// Function to find the maximum
// number of buildings covered
int MaxBuildingsCovered(int arr[], int N, int L)
{
    // Store the current sum
    double curr_sum = 0;
 
    int start = 0, curr_count = 0, max_count = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Add the length of wire required for
        // current building to cur_sum
        curr_sum = curr_sum + ((double)arr[i] * Pi);
 
        // Add extra unit distance 1
        if (i != 0)
            curr_sum += 1;
 
        // If curr_sum <= length of wire
        // increment count by 1
        if (curr_sum <= L) {
            curr_count++;
        }
 
        // If curr_sum > length of wire
        // increment start by 1 and
        // decrement count by 1 and
        // update the new curr_sum
        else if (curr_sum > L) {
            curr_sum = curr_sum - ((double)arr[start] * Pi);
            curr_sum -= 1;
            start++;
            curr_count--;
        }
 
        // Update the max_count
        max_count = max(curr_count, max_count);
    }
 
    // Return the max_count
    return max_count;
}
 
// Driver Code
int main()
{
    // Given Input
    int arr[] = { 4, 1, 6, 2 };
    int L = 24;
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << MaxBuildingsCovered(arr, N, L);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
static final double Pi = 3.141592;
 
// Function to find the maximum
// number of buildings covered
static int MaxBuildingsCovered(int arr[], int N,
                               int L)
{
     
    // Store the current sum
    double curr_sum = 0;
 
    int start = 0, curr_count = 0, max_count = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Add the length of wire required for
        // current building to cur_sum
        curr_sum = curr_sum + ((double)arr[i] * Pi);
 
        // Add extra unit distance 1
        if (i != 0)
            curr_sum += 1;
 
        // If curr_sum <= length of wire
        // increment count by 1
        if (curr_sum <= L)
        {
            curr_count++;
        }
 
        // If curr_sum > length of wire
        // increment start by 1 and
        // decrement count by 1 and
        // update the new curr_sum
        else if (curr_sum > L)
        {
            curr_sum = curr_sum - ((double)arr[start] * Pi);
            curr_sum -= 1;
            start++;
            curr_count--;
        }
 
        // Update the max_count
        max_count = Math.max(curr_count, max_count);
    }
 
    // Return the max_count
    return max_count;
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given Input
    int arr[] = { 4, 1, 6, 2 };
    int L = 24;
 
    // Size of the array
    int N = arr.length;
 
    // Function Call
    System.out.println(MaxBuildingsCovered(arr, N, L));
}
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 program for the above approach
Pi = 3.141592
 
# Function to find the maximum
# number of buildings covered
def MaxBuildingsCovered(arr, N,  L):
    # Store the current sum
    curr_sum = 0
 
    start = 0
    curr_count = 0
    max_count = 0
 
    # Traverse the array
    for i in range(N):
        # Add the length of wire required for
        # current building to cur_sum
        curr_sum = curr_sum + (arr[i] * Pi)
 
        # Add extra unit distance 1
        if (i != 0):
            curr_sum += 1
 
        # If curr_sum <= length of wire
        # increment count by 1
        if (curr_sum <= L):
            curr_count += 1
 
        # If curr_sum > length of wire
        # increment start by 1 and
        # decrement count by 1 and
        # update the new curr_sum
        elif(curr_sum > L):
            curr_sum = curr_sum - (arr[start] * Pi)
            curr_sum -= 1
            start += 1
            curr_count -= 1
 
        # Update the max_count
        max_count = max(curr_count, max_count)
 
    # Return the max_count
    return max_count
 
# Driver Code
if __name__ == '__main__':
    # Given Input
    arr = [4, 1, 6, 2]
    L = 24
 
    # Size of the array
    N = len(arr)
 
    # Function Call
    print(MaxBuildingsCovered(arr, N, L))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
class GFG{
 
static double Pi = 3.141592;
 
// Function to find the maximum
// number of buildings covered
static int MaxBuildingsCovered(int[] arr, int N,
                               int L)
{
     
    // Store the current sum
    double curr_sum = 0;
 
    int start = 0, curr_count = 0, max_count = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Add the length of wire required for
        // current building to cur_sum
        curr_sum = curr_sum + ((double)arr[i] * Pi);
 
        // Add extra unit distance 1
        if (i != 0)
            curr_sum += 1;
 
        // If curr_sum <= length of wire
        // increment count by 1
        if (curr_sum <= L)
        {
            curr_count++;
        }
 
        // If curr_sum > length of wire
        // increment start by 1 and
        // decrement count by 1 and
        // update the new curr_sum
        else if (curr_sum > L)
        {
            curr_sum = curr_sum - ((double)arr[start] * Pi);
            curr_sum -= 1;
            start++;
            curr_count--;
        }
 
        // Update the max_count
        max_count = Math.Max(curr_count, max_count);
    }
 
    // Return the max_count
    return max_count;
}
 
// Driver code
static void Main()
{
     
    // Given Input
    int[] arr = { 4, 1, 6, 2 };
    int L = 24;
 
    // Size of the array
    int N = arr.Length;
 
    // Function Call
    Console.Write(MaxBuildingsCovered(arr, N, L));
}
}
 
// This code is contributed by code_hunt


Javascript


输出
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程