由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - FB interview question
相关主题
最近找工的一点总结g phone interv--有没有o(n) 解法
问一道salesforce面试题求帮忙解答一个面试算法题==
Palantir店面题目Probability quesiton
讨论一道面试题问个算法题, 关于区间 overlap的
Given an int array and an int value. Find all pairs in arrInterval tree解法
问个题目,找不在区间内的所有数问个Facebook 电面题
这道题怎么做的?leetcode 这题insert interval怎么做?
facebook面经leetcode的online judge runtime error是指什么?
相关话题的讨论汇总
话题: intervals话题: output话题: array话题: 0s话题: find
进入JobHunting版参与讨论
1 (共1页)
z**z
发帖数: 222
1
You have an array of 0s and 1s and you want to output all the intervals (i,
j) where the number of 0s and numbers of 1s are equal.
Example
index = 0 1 2 3 4 5 6 7 8
array = 0 1 0 0 1 1 1 1 0
One interval is (0, 1) because there the number of 0 and 1 are equal. There
are many other intervals, find all of them in linear time.
How can this be done in O(n)??
find all intervals, not find the longest interval.
Thanks in advance.
F**r
发帖数: 84
2
不可能,给你个反例
0101010101....
最好的情况O(n^2)

intervals (i,
equal. There

【在 z**z 的大作中提到】
: You have an array of 0s and 1s and you want to output all the intervals (i,
: j) where the number of 0s and numbers of 1s are equal.
: Example
: index = 0 1 2 3 4 5 6 7 8
: array = 0 1 0 0 1 1 1 1 0
: One interval is (0, 1) because there the number of 0 and 1 are equal. There
: are many other intervals, find all of them in linear time.
: How can this be done in O(n)??
: find all intervals, not find the longest interval.
: Thanks in advance.

M**u
发帖数: 10158
3
how about
0 1 0 1 0 1 ...
it will have O(n^2) answers... how can it be done in linear time?

,
There

【在 z**z 的大作中提到】
: You have an array of 0s and 1s and you want to output all the intervals (i,
: j) where the number of 0s and numbers of 1s are equal.
: Example
: index = 0 1 2 3 4 5 6 7 8
: array = 0 1 0 0 1 1 1 1 0
: One interval is (0, 1) because there the number of 0 and 1 are equal. There
: are many other intervals, find all of them in linear time.
: How can this be done in O(n)??
: find all intervals, not find the longest interval.
: Thanks in advance.

b**********r
发帖数: 91
4
Build an array S[i] = NumOfOnes - NumberOfZeros from index 0 to i of the
original array
then if the interval i to j contains the same number of zero and ones
<==> S[i] == S[j]
Scan through S and use hash map, then all pairs has the same value gives
you all pairs of i, j. It's O(n) time

intervals (i,
There

【在 z**z 的大作中提到】
: You have an array of 0s and 1s and you want to output all the intervals (i,
: j) where the number of 0s and numbers of 1s are equal.
: Example
: index = 0 1 2 3 4 5 6 7 8
: array = 0 1 0 0 1 1 1 1 0
: One interval is (0, 1) because there the number of 0 and 1 are equal. There
: are many other intervals, find all of them in linear time.
: How can this be done in O(n)??
: find all intervals, not find the longest interval.
: Thanks in advance.

r*******y
发帖数: 1081
5
smart

【在 b**********r 的大作中提到】
: Build an array S[i] = NumOfOnes - NumberOfZeros from index 0 to i of the
: original array
: then if the interval i to j contains the same number of zero and ones
: <==> S[i] == S[j]
: Scan through S and use hash map, then all pairs has the same value gives
: you all pairs of i, j. It's O(n) time
:
: intervals (i,
: There

g**e
发帖数: 6127
6
output the pairs would cost O(n^2)
DP is the right approach, but I think it takes O(n^2)

【在 b**********r 的大作中提到】
: Build an array S[i] = NumOfOnes - NumberOfZeros from index 0 to i of the
: original array
: then if the interval i to j contains the same number of zero and ones
: <==> S[i] == S[j]
: Scan through S and use hash map, then all pairs has the same value gives
: you all pairs of i, j. It's O(n) time
:
: intervals (i,
: There

r*******y
发帖数: 1081
7
Using a hashmap to scan S to find the pairs will just cost O(n)

【在 g**e 的大作中提到】
: output the pairs would cost O(n^2)
: DP is the right approach, but I think it takes O(n^2)

g**e
发帖数: 6127
8
how do you output at most n^2 pairs in O(n)?

【在 r*******y 的大作中提到】
: Using a hashmap to scan S to find the pairs will just cost O(n)
s*****n
发帖数: 5488
9
看不太懂算法。是说,(2,3) (1,2)是一对output? 漏了(0,1)?
0 1
s[0] = -1;
s[i] = 0;
0 1 0 1
[s0] = -1;
s[1]= 0;
s[2] = -1;
s[3] = 0;

【在 b**********r 的大作中提到】
: Build an array S[i] = NumOfOnes - NumberOfZeros from index 0 to i of the
: original array
: then if the interval i to j contains the same number of zero and ones
: <==> S[i] == S[j]
: Scan through S and use hash map, then all pairs has the same value gives
: you all pairs of i, j. It's O(n) time
:
: intervals (i,
: There

M**u
发帖数: 10158
10
good!

【在 b**********r 的大作中提到】
: Build an array S[i] = NumOfOnes - NumberOfZeros from index 0 to i of the
: original array
: then if the interval i to j contains the same number of zero and ones
: <==> S[i] == S[j]
: Scan through S and use hash map, then all pairs has the same value gives
: you all pairs of i, j. It's O(n) time
:
: intervals (i,
: There

r*******y
发帖数: 1081
11
you are right.
say S[2] = 1, S[4] = 1, S[7] = 1, S[8] = 1
then in the hashmap H we will have
H[1] = {2, 4, 7, 8}
if we only output 2 4 7 8, then it is O(n)

【在 g**e 的大作中提到】
: how do you output at most n^2 pairs in O(n)?
c*******n
发帖数: 63
12
mark
1 (共1页)
进入JobHunting版参与讨论
相关主题
leetcode的online judge runtime error是指什么?Given an int array and an int value. Find all pairs in arr
新鲜G面筋(Fail)问个题目,找不在区间内的所有数
把n个interval 放到一个container里这道题怎么做的?
讨论一道面试题facebook面经
最近找工的一点总结g phone interv--有没有o(n) 解法
问一道salesforce面试题求帮忙解答一个面试算法题==
Palantir店面题目Probability quesiton
讨论一道面试题问个算法题, 关于区间 overlap的
相关话题的讨论汇总
话题: intervals话题: output话题: array话题: 0s话题: find