Redian新闻
>
Re: 梅西神级过人梦幻1V5, 告诉你肥罗是浮云
avatar
Re: 梅西神级过人梦幻1V5, 告诉你肥罗是浮云# Joke - 肚皮舞运动
r*k
1
1 find kth element in two sorted arrays.
2 write a function to implement aligned memory allocation.
阿三面的,我第一题只给出o(k)的结果,第二题也写的不利索。挂了。我太弱了,还是
要练熟经典题啊!!
★ 发自iPhone App: ChineseWeb - 中文网站浏览器
avatar
m*l
2
【 以下文字转载自 Soccer 讨论区 】
发信人: HOTDELL (干爹), 信区: Soccer
标 题: Re: 梅西神级过人梦幻1V5, 告诉你肥罗是浮云
发信站: BBS 未名空间站 (Wed Oct 19 21:41:19 2011, 美东)
光今年就强暴N次了,各种姿势 已经不屑了
真马处女膜又长出来啦?!
avatar
h*c
3
如果是sorted,应该lg(k)

【在 r*****k 的大作中提到】
: 1 find kth element in two sorted arrays.
: 2 write a function to implement aligned memory allocation.
: 阿三面的,我第一题只给出o(k)的结果,第二题也写的不利索。挂了。我太弱了,还是
: 要练熟经典题啊!!
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
r*k
4
O(log n)+O(log m), where n and m are the length of the two arrays.

★ 发自iPhone App: ChineseWeb - 中文网站浏览器

【在 h*c 的大作中提到】
: 如果是sorted,应该lg(k)
avatar
p*2
5

感觉写起来也不好写,如果没练过的话。有时间应该练练这题。

【在 h*c 的大作中提到】
: 如果是sorted,应该lg(k)
avatar
p*2
6

第二题什么意思。就是内存起始位置是%4的?

【在 r*****k 的大作中提到】
: 1 find kth element in two sorted arrays.
: 2 write a function to implement aligned memory allocation.
: 阿三面的,我第一题只给出o(k)的结果,第二题也写的不利索。挂了。我太弱了,还是
: 要练熟经典题啊!!
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
V*y
7
Re~~
avatar
h*c
8
m,n里面有个大的,小的可以忽略...

【在 r*****k 的大作中提到】
: O(log n)+O(log m), where n and m are the length of the two arrays.
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
h*c
9
新的C
支持alignment了

【在 p*****2 的大作中提到】
:
: 第二题什么意思。就是内存起始位置是%4的?

avatar
r*k
10
就是内存起始地址能被某数整除。
也是经典题,应该可以google到。
mymalloc(int size, int align) 返回一个大小为size 开始地址能被align整除的一块
内存。

★ 发自iPhone App: ChineseWeb - 中文网站浏览器

【在 p*****2 的大作中提到】
:
: 第二题什么意思。就是内存起始位置是%4的?

avatar
r*k
11
是的。但是忽略后也不是logk

★ 发自iPhone App: ChineseWeb - 中文网站浏览器

【在 h*c 的大作中提到】
: m,n里面有个大的,小的可以忽略...
avatar
h*c
12
恩.我以外k是总数...

【在 r*****k 的大作中提到】
: 是的。但是忽略后也不是logk
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
g*8
13
第一题ihascode1337的确给出了两个数组不一样的长度的情况,如果一样的长度可以参
考Algorithm Design (by Kleinberg) Chapter 5 Exercise 1,其实也很简单,就是
binary search 的variant,但是这两道的题的coding都很tricky,要非常注意,主要
是递归函数调用的参数传入
他们的coding让我想起了由tree的in-order, pre-order, reconstruct the
binary tree的题,也是一样的tricky编程,也是递归函数调用的参数传递
另外amazon好像还有很好玩的新题:
1)Given a tree preorder sequence such as “NNNLLLL” where N denotes non-
leaf node and L denotes leaf node, with the constraint that no nodes has 1
child. Rebuild the binary tree
2) Given post order traverse of a BST, rebuild the BST
avatar
p*2
14

多申请几个字节的内存,比如4个字节alignment,就多申请4个字节,然后返回%4=0的
起始地址,内存可能比size长最多4个字节。这样可以吗?还是必须要exact size的。
sizeof()返回值就是size?

【在 r*****k 的大作中提到】
: 就是内存起始地址能被某数整除。
: 也是经典题,应该可以google到。
: mymalloc(int size, int align) 返回一个大小为size 开始地址能被align整除的一块
: 内存。
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
p*2
15

1
第二题recursion吧

【在 g*********8 的大作中提到】
: 第一题ihascode1337的确给出了两个数组不一样的长度的情况,如果一样的长度可以参
: 考Algorithm Design (by Kleinberg) Chapter 5 Exercise 1,其实也很简单,就是
: binary search 的variant,但是这两道的题的coding都很tricky,要非常注意,主要
: 是递归函数调用的参数传入
: 他们的coding让我想起了由tree的in-order, pre-order, reconstruct the
: binary tree的题,也是一样的tricky编程,也是递归函数调用的参数传递
: 另外amazon好像还有很好玩的新题:
: 1)Given a tree preorder sequence such as “NNNLLLL” where N denotes non-
: leaf node and L denotes leaf node, with the constraint that no nodes has 1
: child. Rebuild the binary tree

avatar
z*n
16
Amazon的这2题:
第一题:既然叶子数为0或者2,可以利用 Number of leaf = number of non-leaf + 1
, 区分了left 和right child,用DP就可以了;
第二题,节点值都得不同吧,而且光post-order肯定不行,还必须有一个in-order的;
post-order确定root, 然后inorder找left, right

non-
has 1

【在 g*********8 的大作中提到】
: 第一题ihascode1337的确给出了两个数组不一样的长度的情况,如果一样的长度可以参
: 考Algorithm Design (by Kleinberg) Chapter 5 Exercise 1,其实也很简单,就是
: binary search 的variant,但是这两道的题的coding都很tricky,要非常注意,主要
: 是递归函数调用的参数传入
: 他们的coding让我想起了由tree的in-order, pre-order, reconstruct the
: binary tree的题,也是一样的tricky编程,也是递归函数调用的参数传递
: 另外amazon好像还有很好玩的新题:
: 1)Given a tree preorder sequence such as “NNNLLLL” where N denotes non-
: leaf node and L denotes leaf node, with the constraint that no nodes has 1
: child. Rebuild the binary tree

avatar
p*2
17

1
post-order找到root, 然后往前找到一个小于root的就是left tree了,不对吗?

【在 z*****n 的大作中提到】
: Amazon的这2题:
: 第一题:既然叶子数为0或者2,可以利用 Number of leaf = number of non-leaf + 1
: , 区分了left 和right child,用DP就可以了;
: 第二题,节点值都得不同吧,而且光post-order肯定不行,还必须有一个in-order的;
: post-order确定root, 然后inorder找left, right
:
: non-
: has 1

avatar
z*n
18
忘了是BST, 那一个post-order也可以,照你说的往前找第一个小于root的,就可以划
分left

【在 p*****2 的大作中提到】
:
: 1
: post-order找到root, 然后往前找到一个小于root的就是left tree了,不对吗?

avatar
d*9
19
为啥我觉得应该就是log(k)啊... k refers to the k-th smallest

【在 h*c 的大作中提到】
: 恩.我以外k是总数...
avatar
h*c
20
if k = 1

【在 d*******9 的大作中提到】
: 为啥我觉得应该就是log(k)啊... k refers to the k-th smallest
avatar
d*9
21
kth element一定在array A的前k个+array B的前k个里
如果 A[k/2] < B[k/2],那么kth element在A[k/2...k]+B[1...k/2]里
如果 A[k/2] > B[k/2],那么kth element在A[1...k/2]+B[k/2...k]里
then do it recursively

【在 h*c 的大作中提到】
: if k = 1
avatar
h*c
22
好像是对的,其实这个程序我写过,很久以前,现在都忘了

【在 d*******9 的大作中提到】
: kth element一定在array A的前k个+array B的前k个里
: 如果 A[k/2] < B[k/2],那么kth element在A[k/2...k]+B[1...k/2]里
: 如果 A[k/2] > B[k/2],那么kth element在A[1...k/2]+B[k/2...k]里
: then do it recursively

avatar
h*c
23
看了一下我得笔记
lg(k) = lg(n+m)
so ...

【在 h*c 的大作中提到】
: 好像是对的,其实这个程序我写过,很久以前,现在都忘了
avatar
d*9
24
well, yes...

【在 h*c 的大作中提到】
: 看了一下我得笔记
: lg(k) = lg(n+m)
: so ...

avatar
a*m
25
是lgk吧。 n,m无限大也只考虑前2k个数字。

【在 h*c 的大作中提到】
: 看了一下我得笔记
: lg(k) = lg(n+m)
: so ...

avatar
p*2
26

恩。同意。

【在 a********m 的大作中提到】
: 是lgk吧。 n,m无限大也只考虑前2k个数字。
avatar
a*m
27
而且k<=n+m。
avatar
H*e
28
这两题电面也太阴了

【在 r*****k 的大作中提到】
: 1 find kth element in two sorted arrays.
: 2 write a function to implement aligned memory allocation.
: 阿三面的,我第一题只给出o(k)的结果,第二题也写的不利索。挂了。我太弱了,还是
: 要练熟经典题啊!!
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
S*w
29
咋啦 这2题也算简单的吧

【在 H***e 的大作中提到】
: 这两题电面也太阴了
avatar
l*a
30
1。不能算很简单吧
2。电话中写的很好,再念出来,比较难吧

【在 S*******w 的大作中提到】
: 咋啦 这2题也算简单的吧
avatar
H*e
31
你写过么第一题?
try it out 呵呵 lg的复杂度那种
想起来比较简单,真正写,谁能在一小时内写加调对(想算法时间不算),算大大牛了。

【在 l*****a 的大作中提到】
: 1。不能算很简单吧
: 2。电话中写的很好,再念出来,比较难吧

avatar
H*s
32
这两道题不简单吧,第一题思路不难,但coding算top 5%的难题了。
说实在,问第一题够阴的。

【在 S*******w 的大作中提到】
: 咋啦 这2题也算简单的吧
avatar
B*1
33
是啊,老印这不是明坑人吗? 这题onsite还差不多。
avatar
m*l
34
恩,第一体比较tricky

【在 B*******1 的大作中提到】
: 是啊,老印这不是明坑人吗? 这题onsite还差不多。
avatar
l*a
35
咱俩说的不是一个意思?

【在 H***e 的大作中提到】
: 你写过么第一题?
: try it out 呵呵 lg的复杂度那种
: 想起来比较简单,真正写,谁能在一小时内写加调对(想算法时间不算),算大大牛了。

avatar
p*2
36

他应该是回的你楼上的。

【在 l*****a 的大作中提到】
: 咱俩说的不是一个意思?
avatar
l*a
37
then it is better to reply directly while not reply my post

【在 p*****2 的大作中提到】
:
: 他应该是回的你楼上的。

avatar
H*e
38

我确实是回你的
你说不能算很简单,其实我的意思是很难勒

【在 l*****a 的大作中提到】
: then it is better to reply directly while not reply my post
avatar
w*g
39
赞犀利!

【在 d*******9 的大作中提到】
: kth element一定在array A的前k个+array B的前k个里
: 如果 A[k/2] < B[k/2],那么kth element在A[k/2...k]+B[1...k/2]里
: 如果 A[k/2] > B[k/2],那么kth element在A[1...k/2]+B[k/2...k]里
: then do it recursively

avatar
z*y
40
第一题1337里不就有么
avatar
r*k
41
没看啊。。

★ 发自iPhone App: ChineseWeb - 中文网站浏览器

【在 z*y 的大作中提到】
: 第一题1337里不就有么
avatar
C*u
42
迷糊第一题,既然是array,读Kth element不是O(1),直接就找到了吗?为啥我看不
懂题目. :(
avatar
p*2
43

两个array呀。

【在 C*********u 的大作中提到】
: 迷糊第一题,既然是array,读Kth element不是O(1),直接就找到了吗?为啥我看不
: 懂题目. :(

avatar
C*u
44
噢,那就是找从小到大的第k个或者是从大到小的第k个?

【在 p*****2 的大作中提到】
:
: 两个array呀。

avatar
a*n
45
里面有的题都算比较难的了,嘿嘿,否则也不值得大牛写。。。

【在 z*y 的大作中提到】
: 第一题1337里不就有么
avatar
H*1
46
是log k
第一个array里二分作为pilot在第二个array里二分查找
如果m > k; n > k,只用搜索a[0:k-1] 和 b[0:k-1]

【在 r*****k 的大作中提到】
: 是的。但是忽略后也不是logk
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
g*e
47
第一题是log(k)吧 每次都少一半
难点在于M,n值不同,并且一开始就有一个要是m=n>=k 还容易的
avatar
w*x
48
第一题我在1337什么里看了, 复杂的一塌糊涂, 不大相信有多少人就算知道思路的情况
下能现场写出来lgk的, 特别是如果没见过的情况下.
我估计出这个题的人就像考考merge sort
avatar
h*w
49
我弱了,第一题啥意思?能细说么?为啥大家都能理解?
avatar
r*k
50
1 find kth element in two sorted arrays.
2 write a function to implement aligned memory allocation.
阿三面的,我第一题只给出o(k)的结果,第二题也写的不利索。挂了。我太弱了,还是
要练熟经典题啊!!
★ 发自iPhone App: ChineseWeb - 中文网站浏览器
avatar
h*c
51
如果是sorted,应该lg(k)

【在 r*****k 的大作中提到】
: 1 find kth element in two sorted arrays.
: 2 write a function to implement aligned memory allocation.
: 阿三面的,我第一题只给出o(k)的结果,第二题也写的不利索。挂了。我太弱了,还是
: 要练熟经典题啊!!
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
r*k
52
O(log n)+O(log m), where n and m are the length of the two arrays.

★ 发自iPhone App: ChineseWeb - 中文网站浏览器

【在 h*c 的大作中提到】
: 如果是sorted,应该lg(k)
avatar
p*2
53

感觉写起来也不好写,如果没练过的话。有时间应该练练这题。

【在 h*c 的大作中提到】
: 如果是sorted,应该lg(k)
avatar
p*2
54

第二题什么意思。就是内存起始位置是%4的?

【在 r*****k 的大作中提到】
: 1 find kth element in two sorted arrays.
: 2 write a function to implement aligned memory allocation.
: 阿三面的,我第一题只给出o(k)的结果,第二题也写的不利索。挂了。我太弱了,还是
: 要练熟经典题啊!!
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
V*y
55
Re~~
avatar
h*c
56
m,n里面有个大的,小的可以忽略...

【在 r*****k 的大作中提到】
: O(log n)+O(log m), where n and m are the length of the two arrays.
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
h*c
57
新的C
支持alignment了

【在 p*****2 的大作中提到】
:
: 第二题什么意思。就是内存起始位置是%4的?

avatar
r*k
58
就是内存起始地址能被某数整除。
也是经典题,应该可以google到。
mymalloc(int size, int align) 返回一个大小为size 开始地址能被align整除的一块
内存。

★ 发自iPhone App: ChineseWeb - 中文网站浏览器

【在 p*****2 的大作中提到】
:
: 第二题什么意思。就是内存起始位置是%4的?

avatar
r*k
59
是的。但是忽略后也不是logk

★ 发自iPhone App: ChineseWeb - 中文网站浏览器

【在 h*c 的大作中提到】
: m,n里面有个大的,小的可以忽略...
avatar
h*c
60
恩.我以外k是总数...

【在 r*****k 的大作中提到】
: 是的。但是忽略后也不是logk
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
g*8
61
第一题ihascode1337的确给出了两个数组不一样的长度的情况,如果一样的长度可以参
考Algorithm Design (by Kleinberg) Chapter 5 Exercise 1,其实也很简单,就是
binary search 的variant,但是这两道的题的coding都很tricky,要非常注意,主要
是递归函数调用的参数传入
他们的coding让我想起了由tree的in-order, pre-order, reconstruct the
binary tree的题,也是一样的tricky编程,也是递归函数调用的参数传递
另外amazon好像还有很好玩的新题:
1)Given a tree preorder sequence such as “NNNLLLL” where N denotes non-
leaf node and L denotes leaf node, with the constraint that no nodes has 1
child. Rebuild the binary tree
2) Given post order traverse of a BST, rebuild the BST
avatar
p*2
62

多申请几个字节的内存,比如4个字节alignment,就多申请4个字节,然后返回%4=0的
起始地址,内存可能比size长最多4个字节。这样可以吗?还是必须要exact size的。
sizeof()返回值就是size?

【在 r*****k 的大作中提到】
: 就是内存起始地址能被某数整除。
: 也是经典题,应该可以google到。
: mymalloc(int size, int align) 返回一个大小为size 开始地址能被align整除的一块
: 内存。
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
p*2
63

1
第二题recursion吧

【在 g*********8 的大作中提到】
: 第一题ihascode1337的确给出了两个数组不一样的长度的情况,如果一样的长度可以参
: 考Algorithm Design (by Kleinberg) Chapter 5 Exercise 1,其实也很简单,就是
: binary search 的variant,但是这两道的题的coding都很tricky,要非常注意,主要
: 是递归函数调用的参数传入
: 他们的coding让我想起了由tree的in-order, pre-order, reconstruct the
: binary tree的题,也是一样的tricky编程,也是递归函数调用的参数传递
: 另外amazon好像还有很好玩的新题:
: 1)Given a tree preorder sequence such as “NNNLLLL” where N denotes non-
: leaf node and L denotes leaf node, with the constraint that no nodes has 1
: child. Rebuild the binary tree

avatar
z*n
64
Amazon的这2题:
第一题:既然叶子数为0或者2,可以利用 Number of leaf = number of non-leaf + 1
, 区分了left 和right child,用DP就可以了;
第二题,节点值都得不同吧,而且光post-order肯定不行,还必须有一个in-order的;
post-order确定root, 然后inorder找left, right

non-
has 1

【在 g*********8 的大作中提到】
: 第一题ihascode1337的确给出了两个数组不一样的长度的情况,如果一样的长度可以参
: 考Algorithm Design (by Kleinberg) Chapter 5 Exercise 1,其实也很简单,就是
: binary search 的variant,但是这两道的题的coding都很tricky,要非常注意,主要
: 是递归函数调用的参数传入
: 他们的coding让我想起了由tree的in-order, pre-order, reconstruct the
: binary tree的题,也是一样的tricky编程,也是递归函数调用的参数传递
: 另外amazon好像还有很好玩的新题:
: 1)Given a tree preorder sequence such as “NNNLLLL” where N denotes non-
: leaf node and L denotes leaf node, with the constraint that no nodes has 1
: child. Rebuild the binary tree

avatar
p*2
65

1
post-order找到root, 然后往前找到一个小于root的就是left tree了,不对吗?

【在 z*****n 的大作中提到】
: Amazon的这2题:
: 第一题:既然叶子数为0或者2,可以利用 Number of leaf = number of non-leaf + 1
: , 区分了left 和right child,用DP就可以了;
: 第二题,节点值都得不同吧,而且光post-order肯定不行,还必须有一个in-order的;
: post-order确定root, 然后inorder找left, right
:
: non-
: has 1

avatar
z*n
66
忘了是BST, 那一个post-order也可以,照你说的往前找第一个小于root的,就可以划
分left

【在 p*****2 的大作中提到】
:
: 1
: post-order找到root, 然后往前找到一个小于root的就是left tree了,不对吗?

avatar
d*9
67
为啥我觉得应该就是log(k)啊... k refers to the k-th smallest

【在 h*c 的大作中提到】
: 恩.我以外k是总数...
avatar
h*c
68
if k = 1

【在 d*******9 的大作中提到】
: 为啥我觉得应该就是log(k)啊... k refers to the k-th smallest
avatar
d*9
69
kth element一定在array A的前k个+array B的前k个里
如果 A[k/2] < B[k/2],那么kth element在A[k/2...k]+B[1...k/2]里
如果 A[k/2] > B[k/2],那么kth element在A[1...k/2]+B[k/2...k]里
then do it recursively

【在 h*c 的大作中提到】
: if k = 1
avatar
h*c
70
好像是对的,其实这个程序我写过,很久以前,现在都忘了

【在 d*******9 的大作中提到】
: kth element一定在array A的前k个+array B的前k个里
: 如果 A[k/2] < B[k/2],那么kth element在A[k/2...k]+B[1...k/2]里
: 如果 A[k/2] > B[k/2],那么kth element在A[1...k/2]+B[k/2...k]里
: then do it recursively

avatar
h*c
71
看了一下我得笔记
lg(k) = lg(n+m)
so ...

【在 h*c 的大作中提到】
: 好像是对的,其实这个程序我写过,很久以前,现在都忘了
avatar
d*9
72
well, yes...

【在 h*c 的大作中提到】
: 看了一下我得笔记
: lg(k) = lg(n+m)
: so ...

avatar
a*m
73
是lgk吧。 n,m无限大也只考虑前2k个数字。

【在 h*c 的大作中提到】
: 看了一下我得笔记
: lg(k) = lg(n+m)
: so ...

avatar
p*2
74

恩。同意。

【在 a********m 的大作中提到】
: 是lgk吧。 n,m无限大也只考虑前2k个数字。
avatar
a*m
75
而且k<=n+m。
avatar
H*e
76
这两题电面也太阴了

【在 r*****k 的大作中提到】
: 1 find kth element in two sorted arrays.
: 2 write a function to implement aligned memory allocation.
: 阿三面的,我第一题只给出o(k)的结果,第二题也写的不利索。挂了。我太弱了,还是
: 要练熟经典题啊!!
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
S*w
77
咋啦 这2题也算简单的吧

【在 H***e 的大作中提到】
: 这两题电面也太阴了
avatar
l*a
78
1。不能算很简单吧
2。电话中写的很好,再念出来,比较难吧

【在 S*******w 的大作中提到】
: 咋啦 这2题也算简单的吧
avatar
H*e
79
你写过么第一题?
try it out 呵呵 lg的复杂度那种
想起来比较简单,真正写,谁能在一小时内写加调对(想算法时间不算),算大大牛了。

【在 l*****a 的大作中提到】
: 1。不能算很简单吧
: 2。电话中写的很好,再念出来,比较难吧

avatar
H*s
80
这两道题不简单吧,第一题思路不难,但coding算top 5%的难题了。
说实在,问第一题够阴的。

【在 S*******w 的大作中提到】
: 咋啦 这2题也算简单的吧
avatar
B*1
81
是啊,老印这不是明坑人吗? 这题onsite还差不多。
avatar
m*l
82
恩,第一体比较tricky

【在 B*******1 的大作中提到】
: 是啊,老印这不是明坑人吗? 这题onsite还差不多。
avatar
l*a
83
咱俩说的不是一个意思?

【在 H***e 的大作中提到】
: 你写过么第一题?
: try it out 呵呵 lg的复杂度那种
: 想起来比较简单,真正写,谁能在一小时内写加调对(想算法时间不算),算大大牛了。

avatar
p*2
84

他应该是回的你楼上的。

【在 l*****a 的大作中提到】
: 咱俩说的不是一个意思?
avatar
l*a
85
then it is better to reply directly while not reply my post

【在 p*****2 的大作中提到】
:
: 他应该是回的你楼上的。

avatar
H*e
86

我确实是回你的
你说不能算很简单,其实我的意思是很难勒

【在 l*****a 的大作中提到】
: then it is better to reply directly while not reply my post
avatar
w*g
87
赞犀利!

【在 d*******9 的大作中提到】
: kth element一定在array A的前k个+array B的前k个里
: 如果 A[k/2] < B[k/2],那么kth element在A[k/2...k]+B[1...k/2]里
: 如果 A[k/2] > B[k/2],那么kth element在A[1...k/2]+B[k/2...k]里
: then do it recursively

avatar
z*y
88
第一题1337里不就有么
avatar
r*k
89
没看啊。。

★ 发自iPhone App: ChineseWeb - 中文网站浏览器

【在 z*y 的大作中提到】
: 第一题1337里不就有么
avatar
C*u
90
迷糊第一题,既然是array,读Kth element不是O(1),直接就找到了吗?为啥我看不
懂题目. :(
avatar
p*2
91

两个array呀。

【在 C*********u 的大作中提到】
: 迷糊第一题,既然是array,读Kth element不是O(1),直接就找到了吗?为啥我看不
: 懂题目. :(

avatar
C*u
92
噢,那就是找从小到大的第k个或者是从大到小的第k个?

【在 p*****2 的大作中提到】
:
: 两个array呀。

avatar
a*n
93
里面有的题都算比较难的了,嘿嘿,否则也不值得大牛写。。。

【在 z*y 的大作中提到】
: 第一题1337里不就有么
avatar
H*1
94
是log k
第一个array里二分作为pilot在第二个array里二分查找
如果m > k; n > k,只用搜索a[0:k-1] 和 b[0:k-1]

【在 r*****k 的大作中提到】
: 是的。但是忽略后也不是logk
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
g*e
95
第一题是log(k)吧 每次都少一半
难点在于M,n值不同,并且一开始就有一个要是m=n>=k 还容易的
avatar
w*x
96
第一题我在1337什么里看了, 复杂的一塌糊涂, 不大相信有多少人就算知道思路的情况
下能现场写出来lgk的, 特别是如果没见过的情况下.
我估计出这个题的人就像考考merge sort
avatar
h*w
97
我弱了,第一题啥意思?能细说么?为啥大家都能理解?
avatar
r*m
98

what is 1337?

【在 z*y 的大作中提到】
: 第一题1337里不就有么
avatar
j*2
99
哪位给个正解?

【在 r*****k 的大作中提到】
: 就是内存起始地址能被某数整除。
: 也是经典题,应该可以google到。
: mymalloc(int size, int align) 返回一个大小为size 开始地址能被align整除的一块
: 内存。
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
j*o
100
cc150不是有答案吗

【在 j********2 的大作中提到】
: 哪位给个正解?
avatar
w*x
101

哪??

【在 j*****o 的大作中提到】
: cc150不是有答案吗
avatar
g*y
102
尝试着写了一下第一题:
def findk(alist, blist, k):
"""Given two sorted array, find kth minimum element"""
m = len(alist)
n = len(blist)
assert (k <= m + n)

if m == 0:
return blist[k - 1]
elif n == 0:
return alist[k - 1]

if k == 1:
return min(alist[0], blist[0])

# divide k into two even parts as much as possible
ai = min(k // 2, m)
bi = k - ai
if bi > n:
bi = n
ai = k - bi

if alist[ai - 1] == blist[bi - 1]:
return alist[ai - 1]
elif alist[ai - 1] > blist[bi - 1]:
return findk(alist[:ai], blist[bi:], k - bi)
else:
return findk(alist[ai:], blist[:bi], k - ai)
avatar
s*0
103
我也没找到啊。。。

【在 w****x 的大作中提到】
:
: 哪??

avatar
w*x
104
半年前觉得挺不好写, 现在写也挺快的
int GetMedian(int a[], int n, int b[], int m, int k)
{
assert(a && b);
if (n <= 0) return b[k-1];
if (m <= 0) return a[k-1];
if (k <= 1) return min(a[0], b[0]);
if (b[m/2] >= a[n/2])
{
if ((n/2 + 1 + m/2) >= k)
return GetMedian(a, n, b, m/2, k);
else
return GetMedian(a + n/2 + 1, n - (n/2 + 1), b, m, k - (n/2 + 1)
);
}
else
{
if ((m/2 + 1 + n/2) >= k)
return GetMedian(b, m, a, n/2, k);
else
return GetMedian(b + m/2 + 1, m - (m/2 + 1), a, n, k - (m/2 + 1)
);
}
}
avatar
s*u
105
第一题为啥要用DP,遇到‘L’做成一个leaf 然后返回给父节点 不就好了么?

1

【在 z*****n 的大作中提到】
: Amazon的这2题:
: 第一题:既然叶子数为0或者2,可以利用 Number of leaf = number of non-leaf + 1
: , 区分了left 和right child,用DP就可以了;
: 第二题,节点值都得不同吧,而且光post-order肯定不行,还必须有一个in-order的;
: post-order确定root, 然后inorder找left, right
:
: non-
: has 1

avatar
l*a
106
I tried. Your code is not working.
#include
#include
#include
#include
#include
#include
using namespace std;
double GetMedian (int* a, int n, int* b, int m, int k)
{
assert(a && b);
if (n <= 0) return b[k-1];
if (m <= 0) return a[k-1];
if (k <= 1) return min(a[0], b[0]);
if (b[m/2] >= a[n/2])
{
if ((n/2 + 1 + m/2) >= k)
return GetMedian(a, n, b, m/2, k);
else
return GetMedian(a + n/2 + 1, n - (n/2 + 1), b, m, k - (n/2 + 1)
);
}
else
{
if ((m/2 + 1 + n/2) >= k)
return GetMedian(b, m, a, n/2, k);
else
return GetMedian(b + m/2 + 1, m - (m/2 + 1), a, n, k - (m/2 + 1)
);
}
}
int main()
{
for(int k = 1000; k < 10000;k++){
int length1 = rand()%k;
int length2 = rand()%k;
double median;
int* A = new int[length1];
int* B = new int[length2] ;
for(int i = 0; i < length1; i++) A[i] = rand() % 10000 ;
sort (A, A+length1 );
for(int i = 0; i < length2; i++) B[i] = rand() % 10000 ;
sort (B, B+length2 );
if ( length1 <= length2 )
median = GetMedian (A, length1, B, length2, (length1 + length2) / 2) ;
else
median = GetMedian (B, length2, A, length1, (length1 + length2) / 2) ;
cout << "median : " << median << endl;
//verfication
vector c (A, A+length1 );
double verifiedMedian;
c.insert (c.end(), B, B+length2 );
sort ( c.begin(), c.end() ) ;
int sz = c.size();
verifiedMedian = c [sz/2] ;
cout << "VERIFIED MEDIAN : " << verifiedMedian << endl;
assert (median == verifiedMedian);
delete[] A;
delete[] B;
}
//verification end
system("pause");
return 0;
}

【在 w****x 的大作中提到】
: 半年前觉得挺不好写, 现在写也挺快的
: int GetMedian(int a[], int n, int b[], int m, int k)
: {
: assert(a && b);
: if (n <= 0) return b[k-1];
: if (m <= 0) return a[k-1];
: if (k <= 1) return min(a[0], b[0]);
: if (b[m/2] >= a[n/2])
: {
: if ((n/2 + 1 + m/2) >= k)

avatar
C*U
107
是logk 你这个是错的

【在 r*****k 的大作中提到】
: O(log n)+O(log m), where n and m are the length of the two arrays.
:
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

avatar
w*x
108

恩,这个leetcode都没过

【在 l*****a 的大作中提到】
: I tried. Your code is not working.
: #include
: #include
: #include
: #include
: #include
: #include
: using namespace std;
: double GetMedian (int* a, int n, int* b, int m, int k)
: {

avatar
w*0
109
试着写了一下,好像可以,没有仔细测试。
public class FindKthFromTwoArray{
public static void main(String[] args)
{
int[] a = {2,4,6,7,8,10,19,20,24,98};
int[] b = {2,5,15,18,27,34,56,67,100,140};
// int[] a = {2,4,19,29,39,49,59};
// int[] b = {1,5,6,7,12,29,30,33,45,67,78};
// int[] a = {2};
// int[] b = {1};
int k = 16;
int result = FindKthFromTwoArray.findKth(a,b,k);
System.out.println("The kth number is: " + result);

}
public static int findKth(int[] a, int[] b, int k){
return findKth(a,0,b,0,k);
}
private static int findKth(int[] a, int aoffset, int[] b, int boffset,
int k){
assert a.length + b.length >= k+1;
int sa,sb;
if (k == 0) return a[aoffset]< b[boffset]? a[aoffset]:b[boffset];
if (k%2 == 0){
sa= k/2; sb = k/2-1;
}
else {
sa= k/2; sb = k/2;
}
int amid = sa + aoffset;
int bmid = sb + boffset;

if (amid > a.length-2)
return linearSearch(a,aoffset,b,boffset,k);
if (bmid > b.length-2)
return linearSearch(a,aoffset,b,boffset,k);
if (a[amid] < b[bmid])
return findKth(a,amid+1,b,boffset,k-sa-1);
else
return findKth(a,aoffset,b,bmid+1,k-sb-1);

}
public static int linearSearch(int[] a, int aStart, int[] b, int bStart,
int k){
int count = 0;
int i=aStart,j=bStart;
while(count < k){
if (i == a.length-1 && j == b.length-1)
return a[i]> b[j]? a[i]:b[j];
if (i == a.length-1){
j++;
if (b[j] >= a[i])
return b[k-a.length];
}
else if (j == b.length-1){
i++;
if (a[i] >= b[j])
return a[k-b.length];
}
else{
if(a[i] < b[j])
i++;
else
j++;
}
count++;
}
return a[i]< b[j]? a[i]:b[j];
}
}
avatar
g*n
110
请问楼主是网申的么?怎么拿到电面的?
谢谢!

【在 r*****k 的大作中提到】
: 1 find kth element in two sorted arrays.
: 2 write a function to implement aligned memory allocation.
: 阿三面的,我第一题只给出o(k)的结果,第二题也写的不利索。挂了。我太弱了,还是
: 要练熟经典题啊!!
: ★ 发自iPhone App: ChineseWeb - 中文网站浏览器

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。