由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 有疑问的一题
相关主题
问个算法题求教一道面试题
说一题恶心题怎么用nlog n来解。请教一个题目
Google onsite一题菜鸟问个C++的pointer问题
worst case O(nlogn) quicksort?最近连着几个面试都是印度人。
说说4sum的复杂度吧construct a binary tree from in-order and level-order trav
2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么弱问:不好意思,这个CODE问题在哪里?
一个NxN矩阵每行每列都sort好,如何排序?Insert bounding box
问个简单的GooG题目GOOG phone interview question
相关话题的讨论汇总
话题: block话题: valid话题: blocks话题: block2话题: array
进入JobHunting版参与讨论
1 (共1页)
m*****f
发帖数: 1243
1
Given an array A which holds a permutation of 1,2,...,n. A sub-block A[i..j]
of an array A is called a valid block if all the numbers appearing in A[i..j]
are consecutive numbers (may not be in order).
Given an array A= [ 7 3 4 1 2 6 5 8] the valid blocks are [3 4], [1,2], [6,5
],
[3 4 1 2], [3 4 1 2 6 5], [7 3 4 1 2 6 5], [7 3 4 1 2 6 5 8]
Give an O( n log n) algorithm to count the number of valid blocks.
m*****f
发帖数: 1243
2
我只有一个很不成熟的想法. 假设 array有1-n个number, 先找到所有包含最小数字的
长度为2-n的valid block, 然后把数组在1处分成两部分,递归解各部分, 有很多细节没
想清楚, 而且worst time 也是O(n^2), 求更好的方法, 以前讨论过么
g*******y
发帖数: 1930
3
呼唤牛人krone出马!呵呵~
n**********8
发帖数: 188
4
divide conquer
先找所有经过中间的blocks,逐一左右扩展maintain min&max来判断是否是block
然后劈成两半recursive

【在 m*****f 的大作中提到】
: 我只有一个很不成熟的想法. 假设 array有1-n个number, 先找到所有包含最小数字的
: 长度为2-n的valid block, 然后把数组在1处分成两部分,递归解各部分, 有很多细节没
: 想清楚, 而且worst time 也是O(n^2), 求更好的方法, 以前讨论过么

s*****r
发帖数: 773
5
怎么判断一个block是一个valid block? 比如 [5 1 2 4], 排序?

【在 n**********8 的大作中提到】
: divide conquer
: 先找所有经过中间的blocks,逐一左右扩展maintain min&max来判断是否是block
: 然后劈成两半recursive

k*k
发帖数: 49
6
some thoughts...
build tree bottom up. nlogn tree construction.
(7) (3) (4) (1) (2) (6) (5) (8)
1) traverse:
merge adj element to produce
(7) (3,4) (1,2) (6,5) (8)
2) traverse again:
(7) (3,4,1,2) (6,5) (8)
3) traverse again
(7) (3,4,1,2,6,5) (8)
4) again
(7, 3, 4, 1, 2, 6, 5) (8)
5) again
(7, 3, 4, 1, 2, 6, 5, 8)
two adj. blocks merge if max(max(Block1), max(Block2)) - min(min(Block1), min(Block2)) == sizeof(block1 + block2)
h**k
发帖数: 3368
7
how about the case
(3) (5) (1, 2) (6) (4)
Could your algorithm to get the valid block (3,5,1,2,6,4)?

min(Block2)) == sizeof(block1 + block2)

【在 k*k 的大作中提到】
: some thoughts...
: build tree bottom up. nlogn tree construction.
: (7) (3) (4) (1) (2) (6) (5) (8)
: 1) traverse:
: merge adj element to produce
: (7) (3,4) (1,2) (6,5) (8)
: 2) traverse again:
: (7) (3,4,1,2) (6,5) (8)
: 3) traverse again
: (7) (3,4,1,2,6,5) (8)

k*k
发帖数: 49
8
(3) (5) (1, 2) (6) (4)
(3) (5) failed
(5) (1,2) failed
(1,2) (6) failed
(6) (4) failed
(3) (5) (1, 2) failed
(5) (1,2) (6) failed
(1,2) (6) (4) failed
(3) (5) (1, 2) (6) (4)
k adj blocks can merge if
max(max(block 1...k)) - min(min(block 1...k)) == sizeof(block 1...k)
the case u give... may be one of the worst case scenario...
it is similar to the case that all nodes in the tree lines up as a straight
line, so the insertion will take n instead of log n then the complexity
becomes n^2
h**k
发帖数: 3368
9
en, so your algorithm is also O(n*n).

【在 k*k 的大作中提到】
: (3) (5) (1, 2) (6) (4)
: (3) (5) failed
: (5) (1,2) failed
: (1,2) (6) failed
: (6) (4) failed
: (3) (5) (1, 2) failed
: (5) (1,2) (6) failed
: (1,2) (6) (4) failed
: (3) (5) (1, 2) (6) (4)
: k adj blocks can merge if

k*k
发帖数: 49
10
i think....
my "sol":
average case n log n, worst case n^2
just like constructing a binary search tree from a sequence of number
n log n average, n^2 worst... but we generally consider construction of a
binary search tree has complexity of nlogn
h**k
发帖数: 3368
11

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
你可以再去查查看

【在 k*k 的大作中提到】
: i think....
: my "sol":
: average case n log n, worst case n^2
: just like constructing a binary search tree from a sequence of number
: n log n average, n^2 worst... but we generally consider construction of a
: binary search tree has complexity of nlogn

k*k
发帖数: 49
12
http://en.wikipedia.org/wiki/Binary_search_tree
under insertion:
In either version, this operation requires time proportional to the height
of the tree in the worst case, which is O(log n) time in the average case
over all trees, but Ω(n) time in the worst case.
u have n element to insert... so worst case n^2
V**0
发帖数: 889
13
我也觉得这题得用tree

【在 k*k 的大作中提到】
: http://en.wikipedia.org/wiki/Binary_search_tree
: under insertion:
: In either version, this operation requires time proportional to the height
: of the tree in the worst case, which is O(log n) time in the average case
: over all trees, but Ω(n) time in the worst case.
: u have n element to insert... so worst case n^2

h**k
发帖数: 3368
14

当然是建balanced binary tree了,那样worst case 就是O(nlogn)

【在 k*k 的大作中提到】
: http://en.wikipedia.org/wiki/Binary_search_tree
: under insertion:
: In either version, this operation requires time proportional to the height
: of the tree in the worst case, which is O(log n) time in the average case
: over all trees, but Ω(n) time in the worst case.
: u have n element to insert... so worst case n^2

1 (共1页)
进入JobHunting版参与讨论
相关主题
GOOG phone interview question说说4sum的复杂度吧
Goldman Sachs 说了给offer一直下不来2SUM, unsorted, print all index including duplicates 能O(nlogn)解决么
BST的insertion一个NxN矩阵每行每列都sort好,如何排序?
what's the difference of back_inserter and inserter in c++问个简单的GooG题目
问个算法题求教一道面试题
说一题恶心题怎么用nlog n来解。请教一个题目
Google onsite一题菜鸟问个C++的pointer问题
worst case O(nlogn) quicksort?最近连着几个面试都是印度人。
相关话题的讨论汇总
话题: block话题: valid话题: blocks话题: block2话题: array