一道google的电面题

threeleafzerg007 2009-10-02 08:58:26
听同学说的:

有一个正整数的数组,具象化为直方图,求此直方图的最大矩形面积是多少?

例子:

1,3,4,2,1

-
--
---
-----

为 (3,4)或者(3,4,2)所围矩形,面积6

要求复杂度时间o(nlogn) 空间o(1)。 (当然是我自己的结果,不知道有没有o(n)的算法) (优秀算法都有分分哦)
...全文
522 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaodaolk 2010-04-24
  • 打赏
  • 举报
回复
这算法最坏时间复杂度不是O(nlogn)啊,明显O(n2)啊
lzx258 2009-10-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <cmath>
using std::cout;
using std::endl;

#define SLEN 27
int getL(int ad)
{
return abs(SLEN-ad)>ad ? abs(SLEN-ad) : ad;
};

int getS(int ad)
{
return SLEN-getL(ad);
};

int main(void)
{
int ads[]={3,7,11,17,23};
int L=0,S=0;
for(int i=0;i<5;++i)
{
if (L<getL(ads[i])) L=getL(ads[i]);
if (S<getS(ads[i])) S=getS(ads[i]);
};
cout<<"最长所需"<<L<<"秒。\n最短所需"<<S<<"秒。"<<endl;
return 0;
}
beifengchuiqi 2009-10-05
  • 打赏
  • 举报
回复
- -
fuzhihong84 2009-10-05
  • 打赏
  • 举报
回复
#include <iostream>

using namespace std;

const int MAX=100;

int endMax(int a[],int n);
int max(int a[],int n);

int main()
{
int a[MAX];
int n;

while(1)
{
cin>>n;
if(n==0)
break;
for(int i=0;i<n;i++)
cin>>a[i];

int m=max(a,n);
cout<<"Max area: "<<m<<endl;
}
}

int max(int a[],int n)
{

if(n==1)
return a[0];
else
{
int temp1=endMax(a,n);
int temp2=max(a,n-1);
return temp1<temp2?temp2:temp1;
}
}

int endMax(int a[],int n)
{
int sum=0,maxSum=0;
int h=a[n-1];
for(int j=n-1;j>=0;j--)
{
if(a[j]>=h)
sum+=h;
else
{
if(sum>maxSum)
maxSum=sum;
sum=a[j]*(n-j);
h=a[j];
}

}

if(sum>maxSum)
return sum;
return max
weixieming 2009-10-04
  • 打赏
  • 举报
回复
这个貌似很不容易啊,谁有好代码,贴上来看看
失落的凡凡 2009-10-04
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 wendll 的回复:]
mark一下,貌似还没有想到比楼主更好的算法
[/Quote]

飞雪不已经给出O(n)的算法了么
shashenyidaoOCEAN 2009-10-04
  • 打赏
  • 举报
回复
那位兄台的算法头文件就看晕了···
wendll 2009-10-04
  • 打赏
  • 举报
回复
mark一下,貌似还没有想到比楼主更好的算法
wanghao111 2009-10-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 baihacker 的回复:]
C/C++ code
很老的题目了:
http://cs.scu.edu.cn/soj/problem.action?id=1773O(n)的算法
#include<iostream>
#include<sstream>
#include<iomanip>
#include<vector>
#include<deque>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<bitset>
#include<string>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<complex>usingnamespace std;

typedeflonglong int64;
int64 pos[100005], value[100005];int main()
{int n;while (scanf("%d",&n), n)
{int top=0;
int64 ans=0, curr;for (int j=0; j< n;++j)
{int t;scanf("%d",&t);while (top&& t< value[top-1])
{--top;if (top) curr= (j-1- pos[top-1])*value[top];else curr= j* value[top];if (curr> ans) ans= curr;
}
pos[top]= j;
value[top++]= t;
}while (top)
{--top;if (top) curr= (n-1- pos[top-1])*value[top];else curr= n* value[top];if (curr> ans) ans= curr;
}
printf("%lld\n", ans);
}return0;
}
[/Quote]
学习
hyram 2009-10-04
  • 打赏
  • 举报
回复
先mark
omegabomb 2009-10-04
  • 打赏
  • 举报
回复
一排#include
24K純帥 2009-10-04
  • 打赏
  • 举报
回复
这头文件多的吓死人。。
mstlq 2009-10-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 baihacker 的回复:]
...玩TC的时候,习惯性地就加一大串include...懒嘛.
[/Quote]
习惯是ctrl+v,粘上一堆include,typedef……
TC,时间就是分啊>_<
menghai1226 2009-10-03
  • 打赏
  • 举报
回复
头文件多了点啊。。。
threeleafzerg007 2009-10-03
  • 打赏
  • 举报
回复
一下子,题目就被秒杀了 :( 555~~~
并且给算法还不给算法描述。。。扣50分。。。。

我自己做出来的只有 o(nlogn) 并且最坏情况还会降至o(n2)

类似于快排:

描述:

f(1,n) 为数列a1....an的最大直方图数值

记idx 为a1...an的最小数的下标

则 f(1,n) = max (f(1,idx-1) , f(idx+1,n) , (n-1)*a[idx])

算法:

#define max3(a,b,c) max2(max2(a,b),max2(b,c))
#define max2(a,b) ((a) > (b) ? (a) : (b))


int smallest(int *narray,int l,int r)
{
int min = narray[l];
int idx = l;

for(int i = l ; i <= r; i++)
{
if(narray[i] < min)
{
min = narray[i];
idx = i;
}
}

return idx;
}

int MaxHistogram(int *narray,int l,int r)
{
if( l == r)
{
return narray[l];
}

int idx = smallest(narray,l,r);

if(idx == l)
return max2(MaxHistogram(narray,idx+1,r),narray[idx]*(r-l+1));
else if(idx == r)
return max2(MaxHistogram(narray,l,idx-1),narray[idx]*(r-l+1));
else
return max3(MaxHistogram(narray,l,idx-1),
MaxHistogram(narray,idx+1,r),
narray[idx]*(r-l+1));
}




cphj 2009-10-03
  • 打赏
  • 举报
回复
这习惯,和我一样
baihacker 2009-10-02
  • 打赏
  • 举报
回复
...玩TC的时候,习惯性地就加一大串include...懒嘛.
Topin27 2009-10-02
  • 打赏
  • 举报
回复
太多include了吧..
thy38 2009-10-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 thy38 的回复:]
没啥,Include多,往往你的代码就少。
[/Quote]
根本不是那么回事!那些头文件都没用到,天知道哪儿的代码
thy38 2009-10-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hpsmouse 的回复:]
那一串 #include 看得我惊心动魄……
[/Quote]
没啥,Include多,往往你的代码就少。
加载更多回复(5)

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧