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

facebook : Print the largest number within a moving window

全局:

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

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

x
A long array A[] is given to you. There is a sliding window of size w
which is moving from the very left of the array to the very right.
You can only see the w numbers in the window. Each time the sliding window
moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and w is 3.

上一篇:Google Intern : two sorted array 中,求和等于指定数的组合
下一篇:Microsoft : Find missing number in a sorted array
🔗
2339923 2011-8-14 15:47:50 | 只看该作者
全局:
void PrintMax(A, 0, -1)调用函数。请斑竹指教,怎么感觉好像有点理解错题意了?

void PrintMax(long *A, int max, int k){
   if (k > A.size()){
     cout << max;
     exit(0);
   }
   for(int i = 1; i < w; i++){
        if(A[k + i] > max)
             max = A[k + i];
             k += i;
   }
    PrintMax(A.substr(1), max, k);  // 这里substr应该不可以用在long类型吧,不过写个移动的小函数也不难,怕麻烦。。不写了呵呵
  
}
回复

使用道具 举报

🔗
coder 2011-8-17 09:59:07 | 只看该作者
全局:
不知理解是否正确,有没有好些的算法。

public class Sliding_window {

        private int[] max;
        public int[] a;

        public Sliding_window(int[] b, int width) {
                // TODO Auto-generated constructor stub
                a = b;
                max = new int[a.length - width + 1];
                for (int i = 0; i + width <= a.length; i++) {
                        if (width <= a.length) {
                                find_max(i, i + width - 1);
                        }
                }
        }

        public void find_max(int from, int to) {
                max[from] = a[from];
                for (int i = from; i <= to; i++) {
                        if (max[from] <= a[i]) {
                                max[from] = a[i];
                        }
                }
        }

        public void display() {
                for (int i = 0; i < max.length; i++) {
                        System.out.println(max[i]);
                }
        }

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int[] a = { 4, 5, 7, 3, 4, 5, 6, 8, 9, 1 };
                Sliding_window test = new Sliding_window(a, 5);
                test.display();
        }
}
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-8-18 13:12:31 | 只看该作者
全局:
void PrintMax(A, 0, -1)调用函数。请斑竹指教,怎么感觉好像有点理解错题意了?

void PrintMax(long *A, int max, int k){
   if (k > A.size()){
     cout  max)
             max = A[k + i];
             k += i;
   }
    PrintMax(A.substr(1), max, k);  // 这里substr应该不可以用在long类型吧,不过写个移动的小函数也不难,怕麻烦。。不写了呵呵
  
}
2339923 发表于 2011-8-14 15:47


你的意思就是维护一个大小为w的数组,每次更新这个数组并找最大值.
这样时间复杂度就是O(wn).

应为每次找最大值,可以考虑用最大堆来替代这个数组,这样时间复杂度就是O(logw * n)

仔细考虑可以发现没有必要每次维护w个数,比如当window是1,2,5,4,3,2时, window
右移一位或两位的时候, 答案都是5, 所以,没必要维护全部数据,维护    5432就ok了.考虑
用一个队列来维护窗口,时间复杂度可以做到O(n), 代码如下:

  1. void AddOne(deque<int>& que, int a[], int i)
  2. {
  3.         assert(a && i >= 0);

  4.         while (!que.empty() && a[que.back()] < a[i])
  5.                 que.pop_back();

  6.         que.push_back(i);
  7. }

  8. void PrintWindow(int a[], int n, int w)
  9. {
  10.         assert(a && n > 0 && w > 0 && n >= w);

  11.         deque<int> que;
  12.         for (int i = 0; i < w; i++)
  13.                 AddOne(que, a, i);

  14.         cout<<a[que.front()]<<endl;

  15.         for (int i = 1; i+w-1 < n; i++)
  16.         {
  17.                 if (i-1 == que.front())
  18.                         que.pop_front();

  19.                 AddOne(que, a, i+w-1);

  20.                 cout<<a[que.front()]<<endl;
  21.         }
  22. }
复制代码
回复

使用道具 举报

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

本版积分规则

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