📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 2776| 回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

Google : Design a class to get average area of sub rectangle in O(1)

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Design a class to process a matrix, and it needs to be able to return the average
for the elements of arbitrary sub-rectangle inside that matrix, in constant time.

上一篇:Google: Find the sole number in triple number array
下一篇:Google : 打印机排版算法
推荐
 楼主| wwwyhx 2011-7-19 21:25:12 | 只看该作者
全局:
Design a class to process a matrix, and it needs to be able to return the average
for the elements of arbitrary sub-rectangle inside that matrix, in constant time.
wwwyhx 发表于 2011-7-15 16:05


use a two dimensional array says "Sum" to record the sum of elements within a rectangle start at (0,0) ended at (i,j) where i < m and j < n.

For every sub rectangle represented by (a,b,c,d):
SumOfRectangle(a,b,c,d) = Sum(c,d)-Sum(c,b-1)-Sum(a-1,d)+Sum(a-1,b-1) if (a>0 && b>0)
SumOfRectangle(a,b,c,d) = Sum(c,d)-Sum(c,b-1) if (a==0 && b>0)
SumOfRectangle(a,b,c,d) = Sum(c,d)-Sum(a-1,d) if (a>0 && b==0)
SumOfRectangle(a,b,c,d) = Sum(c,d) if (a==0 && b==0)

代码:

  1. class CPreProcessArray
  2. {
  3. public:
  4.         CPreProcessArray(int** pArr, int m, int n);
  5.         ~CPreProcessArray();

  6.         float GetAve(int a, int b, int c, int d);

  7. private:
  8.         int** m_pArrSum;
  9.         int m_nRow;
  10.         int m_nCol;
  11. };

  12. inline CPreProcessArray::CPreProcessArray(int **pArr, int m, int n)
  13. {
  14.         assert(pArr && m>0 && n>0);

  15.         m_nRow = m;
  16.         m_nCol = n;
  17.         m_pArrSum = new int*[m];
  18.         for (int i = 0; i < m; i++)
  19.                 m_pArrSum[i] = new int[n];

  20.         for (int i = 0; i < m; i++)
  21.                 m_pArrSum[i][0] = pArr[i][0];
  22.         for (int i = 0; i < n; i++)
  23.                 m_pArrSum[0][i] = pArr[0][i];

  24.         for (int i = 1; i < m; i++)
  25.                 for (int j = 1; j < n; j++)
  26.                         m_pArrSum[i][j] = m_pArrSum[i][j-1] + m_pArrSum[i-1][j] + pArr[i][j] - m_pArrSum[i-1][j-1];
  27. }

  28. inline CPreProcessArray::~CPreProcessArray()
  29. {
  30.         for (int i = 0; i < m_nRow; i++)
  31.                 delete []m_pArrSum[i];
  32.         delete []m_pArrSum;
  33. }

  34. inline float CPreProcessArray::GetAve(int a, int b, int c, int d)
  35. {
  36.         assert(a > 0 && b > 0 && c > 0 && d > 0 && a <= c && b <= d && c < m_nRow && d < m_nCol);

  37.         int nSum = 0;
  38.         if (0 == a && 0 == b)
  39.                 nSum = m_pArrSum[c][d];
  40.         else if (0 == a)
  41.                 nSum = m_pArrSum[c][d] - m_pArrSum[c][b-1];
  42.         else if (0 == b)
  43.                 nSum = m_pArrSum[c][d] - m_pArrSum[a-1][d];
  44.         else
  45.                 nSum = m_pArrSum[c][d] - m_pArrSum[c][b-1] - m_pArrSum[a-1][d] + m_pArrSum[a-1][b-1];

  46.         return ((float)nSum)/((c-a+1) * (d-b+1));
  47. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表