Max Sum in circularly situated Values

Max Sum in circularly situated Values

This question was asked first in FACEBOOK On Campus for Internship.
There are N trees in a circle. Each tree has a fruit value associated with it.  
A bird can sit on a tree for 0.5 sec and then bird have to move to a neighboring tree. It takes the bird 0.5 seconds to move from one tree to another. The bird gets the fruit value when she sits on a tree. 
We are given N and M (the number of seconds the bird has), and the fruit values of the trees. We have to maximize the total fruit value that the bird can gather. The bird can start from any tree.
Solution 

First lets think for data structure to use.
Here we have a tree with fruit value. No need to think deep for tree and traversal, we can simply create a node with value.
They are in circular, hence we can think of circular queue or array or linkelist.
as Circular Array is easiest one, We can take it. 
Queue is also not needed as no need for front and rear.

Now In circular array we have fruit values.
What we need to find is maximum sum of fruits which bird can traverse.
As bird takes 0.5 seconds to fetch value of fruit and 0.5 seconds to move to another Tree.
We can say is bird can traverse 1 node in 1 sec.
So if bird has X seconds, bird can traverse to X Trees.
Main Problem Statement : Hence Finally we need to get a Window of size X in Circular Array such that sum of the values of the Window is maximum.
Solution : 

#include<stdio.h>
#include<math.h>

int maxTreeSum(int a[], int size, int windowsize)
{
   int max_so_far = 0, max_wind_start = -1;
   int i,j,sum=0;
    int w = windowsize;
   int n = size;
   for(i=0;i<n;i++){
       for(j=0;j<w;j++){
           sum=sum+a[(j+i)%n];
       }
       if(max_so_far < sum){ max_wind_start=i ;max_so_far = sum; }
       sum=0;

   }
   printf("Max sum resulting window");
   j=max_wind_start;
   
   while(w--){
        printf(" %d",a[j%n]);
        j++;
    }
    printf("\n");
   return max_so_far;
}

int main()
{
   int a[] = {2, 3, 4, 1, 2, 1, 5, 3}; //fruit values
   int n = sizeof(a)/sizeof(a[0]); // n
   int s = 3; // Bird has 3 seconds

   /* As Bird has 3 seconds it can stay at node for 0.5 sec + 0.5 sec to go to 
   next node so bird can traverse 1 node in 1 sec 
   hence in 3 seconds bird can traverse 3 node*/

   int max_sum = maxTreeSum(a, n, s);
   printf("Maximum contiguous sum is %d\n", max_sum);
    return 0;
}
See code at  http://ideone.com/f7IdMR

Thanks  Dhaval for preparing this article.

O(NM) is time complexity for above code.

Solution 2)
Optimized Solution with DP : 

- Keep one Array of size of N
- Each index will carry the fruit value of next S values.
- Instead of Loop 
 for(i=0;i<n;i++){
       for(j=0;j<w;j++){
           sum=sum+a[(j+i)%n];
        }
 }
Try to keep a loop with another Array as explained in Below diagram.
First circular array is with fruit value.
Second circular array is with next all fruit values which a window of size S contains.


Instead storing smaller values as shown in diagram, we can store always maximum
eg 9,9,9..10,10

Idea here is to eliminate second loop of j to calculate sum of fruit values every time.
When we know that in next sum only last index value subtracted and next index value will be added

Solution from users awaited.

Solution 3 : 
Instead using Another array in Solution 2) Keep only one variable max_so_far.
For i=0, calculate sum of fruits for next windows ( ie. if window is set 3 , then do)

f or i=0,1,2  calculate 
max_so_far = a[0]+a[1]+a[2]; 
then for each i = 1 to n do following
sum = max_so_far - a[i-1] + a[i+w-1];
if (sum < max_so_far ) {
....

}

-> Basically instead searching every time in loop for sum or storing into auxiliary array. we fetched sum in one variable.

Complexity : O(N)
Space Complexity : O(1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
圆极化超表面天线是一种基于超材料的射频天线,具有圆极化辐射特性和可定制的目标工作频率。它使用亚波长级微型天线单元和精密安排的介质贴片来实现对特定频率的波的极化控制。本文以PDF电子文档的形式提供有关圆极化超表面天线的详细信息。 该文献介绍了一种设计和制造圆极化超表面天线的方法。首先,通过对天线的工作频率和辐射特性进行分析和测试,确定了设计所需的电磁参数。然后,利用计算机辅助设计软件和数值模拟方法,设计了一种特定的超材料结构,该结构可以实现所需的圆极化辐射。接下来,通过光刻和薄膜沉积技术,将超材料结构制造在介质贴片上。最后,通过测试和优化,验证了超表面天线的圆极化性能和工作频率。 该文献还详细介绍了圆极化超表面天线的性能评估方法。通过测量其辐射图案、增益、回波损耗和天线效率等指标,评估了天线的性能。结果表明,该天线在目标工作频率范围内实现了良好的圆极化辐射特性,并具有较高的增益和较低的回波损耗。 该文献还讨论了圆极化超表面天线的应用前景。由于其紧凑的尺寸、可定制的工作频率和卓越的辐射特性,该天线在通信、雷达、无人机和卫星通信等领域具有广泛的应用潜力。在未来的研究中,可以进一步探索其他材料和结构的设计,以提高天线性能和实现更广泛的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值