avatar
问一道facebook的面试题# JobHunting - 待字闺中
r*6
1
有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
大值。
例如,
有数组 2,3,4,2,6,7,8,2,4,1,2,3
k = 3
输出
4,4,6,7,8,8,8,4,4,4
有什么好一点的解法吗。
avatar
b*e
2
use heap?

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
n*4
3
use a queue
keep the biggest value at the head of the queue
avatar
P*A
4
Maintain a Binary Search Tree,
with a pointer to the max node,
Every time you insert a new element, update the pointer if necessary.

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
g*y
5
public int[] getWindowMax(int[] a, int w) {
int[] b = new int[a.length - w + 1];

ArrayDeque q = new ArrayDeque();
for (int i=0; iwhile (!q.isEmpty() && a[q.peekLast()]<=a[i]) q.removeLast();
q.addLast(i);
}

b[0] = a[q.peekFirst()];
for (int i=w; iwhile (!q.isEmpty() && q.peekFirst()<=i-w) q.pop();
while (!q.isEmpty() && a[q.peekLast()]<=a[i]) q.removeLast();
q.addLast(i);
b[i-w+1] = a[q.peekFirst()];
}

return b;
}
如果有问题,去读ihasleetcode的网站。
avatar
b*v
6
时间复杂性是什么?

【在 g**********y 的大作中提到】
: public int[] getWindowMax(int[] a, int w) {
: int[] b = new int[a.length - w + 1];
:
: ArrayDeque q = new ArrayDeque();
: for (int i=0; i: while (!q.isEmpty() && a[q.peekLast()]<=a[i]) q.removeLast();
: q.addLast(i);
: }
:
: b[0] = a[q.peekFirst()];

avatar
y*z
7
ST 算法 + RMQ
avatar
R*9
8
#include
#include
#include
using namespace std;
void PrintMaxInWindow(int a[], int n, int k){
set s;
int i=0;
for(;is.insert(a[i]);
set::reverse_iterator rit;
for(;i<=n;i++){
rit = s.rbegin();
cout<s.erase(a[i-k]);
s.insert(a[i]);
}
}

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
h*r
9
单调队列吧~
avatar
b*z
10
Use max-heap.

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
P*P
11
这题o(n)
就是implement a queue with getMax().
有点像crack150里面的stack with getMax()
public void addLast(T data) {
// insert data to the data queue.
dataQ.add(data);
// adjust the auxiliary queue.
while (!auxiQ.isEmpty() && auxiQ.getLast().compareTo(data) < 0) {
auxiQ.removeLast();
}
auxiQ.add(data); //append to the end.
}
public T getMax() {
return auxiQ.getFirst();
}
public T removeFirst() {
T data = dataQ.removeFirst();
// adjust the auxiliary queue. Because the previous max is removed, the
// head (which is the current max) should be removed too.
if (data == auxiQ.getFirst()) {
auxiQ.removeFirst();
}
return data;
}
avatar
b*u
12
可以用c++的map,每次存 reverse iterator的第一个,删find i-2
不过好像挺作弊。

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
b*u
13
heap找arr[i-3]不是很容易吧。

【在 b***e 的大作中提到】
: use heap?
avatar
b*u
14
set好像不是sorted的吧?要用map

【在 R******9 的大作中提到】
: #include
: #include
: #include
: using namespace std;
: void PrintMaxInWindow(int a[], int n, int k){
: set s;
: int i=0;
: for(;i: s.insert(a[i]);
: set::reverse_iterator rit;

avatar
z*t
15
Share a blog with two solutions of this problem:
http://codercareer.blogspot.com/2012/02/no-33-maximums-in-slidi
Enjoy it:)

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
r*6
16
有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
大值。
例如,
有数组 2,3,4,2,6,7,8,2,4,1,2,3
k = 3
输出
4,4,6,7,8,8,8,4,4,4
有什么好一点的解法吗。
avatar
b*e
17
use heap?

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
n*4
18
use a queue
keep the biggest value at the head of the queue
avatar
P*A
19
Maintain a Binary Search Tree,
with a pointer to the max node,
Every time you insert a new element, update the pointer if necessary.

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
g*y
20
public int[] getWindowMax(int[] a, int w) {
int[] b = new int[a.length - w + 1];

ArrayDeque q = new ArrayDeque();
for (int i=0; iwhile (!q.isEmpty() && a[q.peekLast()]<=a[i]) q.removeLast();
q.addLast(i);
}

b[0] = a[q.peekFirst()];
for (int i=w; iwhile (!q.isEmpty() && q.peekFirst()<=i-w) q.pop();
while (!q.isEmpty() && a[q.peekLast()]<=a[i]) q.removeLast();
q.addLast(i);
b[i-w+1] = a[q.peekFirst()];
}

return b;
}
如果有问题,去读ihasleetcode的网站。
avatar
b*v
21
时间复杂性是什么?

【在 g**********y 的大作中提到】
: public int[] getWindowMax(int[] a, int w) {
: int[] b = new int[a.length - w + 1];
:
: ArrayDeque q = new ArrayDeque();
: for (int i=0; i: while (!q.isEmpty() && a[q.peekLast()]<=a[i]) q.removeLast();
: q.addLast(i);
: }
:
: b[0] = a[q.peekFirst()];

avatar
y*z
22
ST 算法 + RMQ
avatar
R*9
23
#include
#include
#include
using namespace std;
void PrintMaxInWindow(int a[], int n, int k){
set s;
int i=0;
for(;is.insert(a[i]);
set::reverse_iterator rit;
for(;i<=n;i++){
rit = s.rbegin();
cout<s.erase(a[i-k]);
s.insert(a[i]);
}
}

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
h*r
24
单调队列吧~
avatar
b*z
25
Use max-heap.

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
P*P
26
这题o(n)
就是implement a queue with getMax().
有点像crack150里面的stack with getMax()
public void addLast(T data) {
// insert data to the data queue.
dataQ.add(data);
// adjust the auxiliary queue.
while (!auxiQ.isEmpty() && auxiQ.getLast().compareTo(data) < 0) {
auxiQ.removeLast();
}
auxiQ.add(data); //append to the end.
}
public T getMax() {
return auxiQ.getFirst();
}
public T removeFirst() {
T data = dataQ.removeFirst();
// adjust the auxiliary queue. Because the previous max is removed, the
// head (which is the current max) should be removed too.
if (data == auxiQ.getFirst()) {
auxiQ.removeFirst();
}
return data;
}
avatar
b*u
27
可以用c++的map,每次存 reverse iterator的第一个,删find i-2
不过好像挺作弊。

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
b*u
28
heap找arr[i-3]不是很容易吧。

【在 b***e 的大作中提到】
: use heap?
avatar
b*u
29
set好像不是sorted的吧?要用map

【在 R******9 的大作中提到】
: #include
: #include
: #include
: using namespace std;
: void PrintMaxInWindow(int a[], int n, int k){
: set s;
: int i=0;
: for(;i: s.insert(a[i]);
: set::reverse_iterator rit;

avatar
z*t
30
Share a blog with two solutions of this problem:
http://codercareer.blogspot.com/2012/02/no-33-maximums-in-slidi
Enjoy it:)

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
t*j
31
用数组实现的Q...
当然咯,用vector可以省去很多code。
void subMost(int* parentArray, int parentSize, int childSize)
{
int* valueArray = new int[childSize];
int* positionArray = new int[childSize];
int actualSize = 0;
int childFrontIndex = 0;
int childEndIndex = -1;
for(int i=0; i< parentSize+childSize-1; i++)
{
if(i>=childSize)
{
if(positionArray[childFrontIndex] == i-childSize)
{
childFrontIndex = (childFrontIndex++)%childSize;
actualSize --;
}
}
if(i < parentSize)
{
while((valueArray[childEndIndex] < parentArray[i]) && (
actualSize >0))
{
childEndIndex --;
actualSize --;
if((childEndIndex < 0) && (actualSize > 0) )
{
childEndIndex = childSize-1;
}
}
childEndIndex = (childEndIndex ++)%childSize;
valueArray[childEndIndex] = parentArray[i];
positionArray[childEndIndex] = i;
actualSize = actualSize++;
}
cout << valueArray[childFrontIndex] << endl;
}
delete valueArray;
delete positionArray;
}
test过了...

【在 r***6 的大作中提到】
: 有一无序数组长度为n,以及一个可以从左到右移动的子数组,长度为k。
: 假设子数组起始位置为0,要求在子数组从最左到最右的移动过程中,输出子数组的最
: 大值。
: 例如,
: 有数组 2,3,4,2,6,7,8,2,4,1,2,3
: k = 3
: 输出
: 4,4,6,7,8,8,8,4,4,4
: 有什么好一点的解法吗。

avatar
g*e
32
leetcode上有这道题,貌似是两个queue
avatar
p*2
33

我前几天做了,好像一个就可以了。

【在 g*********e 的大作中提到】
: leetcode上有这道题,貌似是两个queue
avatar
M*t
34
你看2月16号的帖子,何海涛本人给了他的解

【在 p*****2 的大作中提到】
:
: 我前几天做了,好像一个就可以了。

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