由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 找最大俩数的代码怎么写?
相关主题
Leetcode 上面的 Best Time to Buy and Sell Stock IIICS intern面经
备考google onsite, 讨论堆排序的时间复杂度刚面完 google,题目
关于atoi的overflow也问一个median的问题
贡献一道面试题目:找出在一个数组里面只出现1次的两个数字两道2009算法题
问一算法题问个老题,find the next larger in BST
三妹比三哥还威武啊Quick sort为什么需要logN的memory?
问个面试题一道题目
amazon一面面经priority_queue C++ implementation
相关话题的讨论汇总
话题: max话题: max2话题: int话题: start话题: im2
进入JobHunting版参与讨论
1 (共1页)
l*****a
发帖数: 14598
1
算法大家都知道
N-1 find the biggest one
then log2n-1 find from all those compared with the biggest one just now
写代码咋写,给个例子看看吧
g*********s
发帖数: 1782
2
你说的哪个题?

【在 l*****a 的大作中提到】
: 算法大家都知道
: N-1 find the biggest one
: then log2n-1 find from all those compared with the biggest one just now
: 写代码咋写,给个例子看看吧

l*****a
发帖数: 14598
3
我说得还不够明确?
算法都给出来了,但是不知道怎么写,至少不用额外空间的。。

【在 g*********s 的大作中提到】
: 你说的哪个题?
g*********s
发帖数: 1782
4
什么是”all those compared with the max"?难道不是每个都要做比较?

【在 l*****a 的大作中提到】
: 我说得还不够明确?
: 算法都给出来了,但是不知道怎么写,至少不用额外空间的。。

p******r
发帖数: 2999
5
真的没看懂

【在 l*****a 的大作中提到】
: 我说得还不够明确?
: 算法都给出来了,但是不知道怎么写,至少不用额外空间的。。

f*****w
发帖数: 2602
6
我也没懂为什么次大只要logN
r*******e
发帖数: 7583
7
两两分组直到比出最大,像一个自底向上的二叉树
次大必然在最大的直接对手中产生,对手数量就是树的高度吧

【在 f*****w 的大作中提到】
: 我也没懂为什么次大只要logN
r*******e
发帖数: 7583
8
不用额外空间应该是不可能的
没法keep trace of the opponents

【在 l*****a 的大作中提到】
: 我说得还不够明确?
: 算法都给出来了,但是不知道怎么写,至少不用额外空间的。。

S****z
发帖数: 666
9
别说那么玄乎,什么两两分组直到比出最大?
怎么分?怎么比?

【在 r*******e 的大作中提到】
: 两两分组直到比出最大,像一个自底向上的二叉树
: 次大必然在最大的直接对手中产生,对手数量就是树的高度吧

c***2
发帖数: 838
10
doing the tournament with complexity N+lgN in the cost of extra stack spaces
used for recursion
straightforward 2-pass scanning takes N-1+N-2=2N-3 with no extra space
Tournament:
void find_max_2max(int a[], int start, int end, int *max, int *max2)
{
int i, mid;
int max00,max01,max10,max11;
int temp1,temp2;

if(start==end){
*max=a[start];
*max2=a[start];
return;
}

if(end-start==1){
*max=MAX(a[start],a[end]);
*max2=MIN(a[start],a[end]);
return;
}

mid=(start+end)/2;

find_max_2max(a, start, mid, &max00, &max01);
find_max_2max(a, mid+1, end, &max10, &max11);

temp1=MAX(max00,max01);
temp2=MAX(max10,max11);

if(temp1>temp2){
*max=temp1;
*max2=MAX(temp2, MIN(max00,max01));
}
else {
*max=temp2;
*max2=MAX(temp1, MIN(max10,max11));
}
}
Scan twice:
void find_max_2max(int a[], int size, int *max, int *max2)
{
int i, im, im2;
*max=0;
*max2=0;
if(size<=0) return;
*max=a[0];
im=0;
for(i=1;i if(*max im=i;
*max=a[i];
}
if(im==0)
im2=1;
else
im2=0;
*max2=a[im2];
for(i=0;i if((i!=im) && (*max2 *max2=a[i];
im2=i;
}
}
相关主题
三妹比三哥还威武啊CS intern面经
问个面试题刚面完 google,题目
amazon一面面经也问一个median的问题
进入JobHunting版参与讨论
r*******e
发帖数: 7583
11
http://en.wikipedia.org/wiki/Selection_algorithm#Tournament_Alg

【在 S****z 的大作中提到】
: 别说那么玄乎,什么两两分组直到比出最大?
: 怎么分?怎么比?

g*********s
发帖数: 1782
12
说说怎么跟踪和最大数直接比较过的数?最大数不是要到最后一步才知道吗?

【在 r*******e 的大作中提到】
: http://en.wikipedia.org/wiki/Selection_algorithm#Tournament_Alg
S****z
发帖数: 666
13
其实不用跟踪吧,算法第一遍找最大的时候把二叉树层次结构都建好了
次最大只能在跟最大比较过的集合里面,
那接下来把最大数所在的叶子的值改成最小,
然后从该叶子节点开始两两比较的往上走到顶,比较出来的就是次最大了吧

【在 g*********s 的大作中提到】
: 说说怎么跟踪和最大数直接比较过的数?最大数不是要到最后一步才知道吗?
x*****p
发帖数: 1707
14
建二叉树所要的空间岂不是更大?
l*****a
发帖数: 14598
15
we are focus on the time complexity for this issue

【在 x*****p 的大作中提到】
: 建二叉树所要的空间岂不是更大?
1 (共1页)
进入JobHunting版参与讨论
相关主题
priority_queue C++ implementation问一算法题
Citibank 第二轮三妹比三哥还威武啊
O(1)space解法到底能不能用递归?问个面试题
share一下最近三个电话面试题Amazon, Groupon, Googleamazon一面面经
Leetcode 上面的 Best Time to Buy and Sell Stock IIICS intern面经
备考google onsite, 讨论堆排序的时间复杂度刚面完 google,题目
关于atoi的overflow也问一个median的问题
贡献一道面试题目:找出在一个数组里面只出现1次的两个数字两道2009算法题
相关话题的讨论汇总
话题: max话题: max2话题: int话题: start话题: im2