给定一个大小为N的数组arr[] ,表示N 个圆形建筑物的直径和长度为L的直线。任务是找到最大值 如果绕建筑物绕一圈,电线可以覆盖的连续建筑物的数量。
注:每个建筑物之间的距离为1个单位长度,因此需要1个单位 长度 额外到达一个建筑到下一个建筑。
例子:
Input: arr[] = {4, 1, 6}, L = 24
Output: 2
Explanation: 1 round of building will require pi * d length of wire, where pi is 3.14159 and d is the diameter of circle.
For 1st building → 12.566 length of wire required, remaining wire→ 24-12.566=11.434 -1( to reach next building)=10.434
Similarly, for second building 3.141 length of wire required, remaining wire→ 10.434-3.141= 7.292 -1= 6.292
For third building 18.849, which is > remaining wire i.e, 18.849>6.292
Therefore, Maximum of 2 building can be covered.
Input: arr[] = {2, 5, 3, 4}, L = 36
Output: 3
方法:这个想法是使用贪婪的方法。请按照以下步骤解决问题:
- 初始化变量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 现场工作专业课程和学生竞争性编程现场课程。