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

Google : 找数组的波谷

全局:

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

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

x
Suppose we are given an array A[1 .. n] with the special property that A[1] ≥ A[2] and
A[n − 1] ≤ A[n]. We say that an element A[x] is a local minimum if it is less than or equal
to both its neighbors, or more formally, if A[x − 1] ≥ A[x] and A[x] ≤ A[x + 1]. For example,
there are five local minima in the following array:
9 7 7 2 1 3 7 5 4 7 3 3 4 8 6 9
We can obviously find a local minimum in O(n) time by scanning through the array. Describe
and analyze an algorithm that finds a local minimum in O(log n) time

上一篇:问一个关于快速排序的问题
下一篇:Google : Find whether a word is composed by two words
🔗
 楼主| wwwyhx 2011-8-4 00:23:21 | 只看该作者
全局:
三个数即可看出当前的趋势, 对这个"趋势"作二分搜索, 递减向右,递增向左

  1. bool GetPivot(int a[], int n, int& res)
  2. {
  3.         assert(a && n > 0);

  4.         int nBeg = 0;
  5.         int nEnd = n - 1;
  6.         while (nBeg + 2 <= nEnd)
  7.         {
  8.                 int nMid = (nBeg + nEnd)/2;

  9.                 if (a[nMid - 1] >= a[nMid] && a[nMid] <= a[nMid + 1])
  10.                 {
  11.                         res = a[nMid];
  12.                         return false;
  13.                 }
  14.                 else if (a[nMid - 1] >= a[nMid] && a[nMid] > a[nMid + 1])
  15.                         nBeg = nMid + 1;
  16.                 else
  17.                         nEnd = nMid - 1;
  18.         }

  19.         return false;
  20. }
复制代码
回复

使用道具 举报

🔗
xnature 2011-8-4 08:09:02 | 只看该作者
全局:
与其说对趋势二分,不如把F看做连续的函数过A这些数字。根据导函数的介值定理来做二分
回复

使用道具 举报

🔗
xnature 2011-8-4 11:43:14 | 只看该作者
全局:
与其说对趋势二分,不如把F看做连续的函数过A这些数字。根据导函数的介值定理来做二分
xnature 发表于 2011-8-4 08:09



    大概是这个意思,不大严谨
回复

使用道具 举报

🔗
coder 2011-8-24 04:00:26 | 只看该作者
全局:
Correct my if I am wrong. My solution:
  1. public int find(int beg, int end){
  2.                 if(beg+1>end){
  3.                         return -1;
  4.                 }
  5.                 int mid=(beg+end)/2;
  6.                 if(a[mid-1]>=a[mid] && a[mid]<=a[mid+1]){
  7.                         return mid;
  8.                 }
  9.                 else if (a[mid-1]>=a[mid])// go right
  10.                         beg=mid;
  11.                 else //(a[mid-1]<=a[mid])// go left
  12.                         end=mid;
  13.                 return find(beg, end);
  14.         }
复制代码
回复

使用道具 举报

🔗
sing1ee 2013-7-21 09:58:58 | 只看该作者
全局:
我找到六个,是我错了么
回复

使用道具 举报

🔗
lixiang.xjtu 2013-7-21 21:07:27 | 只看该作者
全局:
赞!这题不错!
回复

使用道具 举报

🔗
xusi 2013-7-24 02:01:09 | 只看该作者
全局:

  1. int localmin(int n[], int p, int q){
  2.         int m;

  3.         while(m=(p+q)/2, m>p){
  4.                 if(n[m-1]>=n[m] && n[m]<=n[m+1]) return n[m];
  5.                 else if(n[m-1]<=n[m]) q=m;
  6.                 else p=m;
  7.         }
  8. }
复制代码

回复

使用道具 举报

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

本版积分规则

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