avatar
请教一个数组题# JobHunting - 待字闺中
c*0
1
Given a sorted array of n integers, pick up k elements so that the minimal
difference between consecutive elements is maximal (that is choose the
elements to maximize the quantity min(a[i+1] - a[i]))
avatar
g*y
2
Sliding window maximum problem的变形:
http://www.ihas1337code.com/2011/01/sliding-window-maximum.html

【在 c********0 的大作中提到】
: Given a sorted array of n integers, pick up k elements so that the minimal
: difference between consecutive elements is maximal (that is choose the
: elements to maximize the quantity min(a[i+1] - a[i]))

avatar
s*j
3
dp
f[i][j] means when have already chosen i numbers and the last chosen number
is a[j], the maximum minimum difference
avatar
c*0
6

number
then how do you update? f[i+1][j] = ?

【在 s****j 的大作中提到】
: dp
: f[i][j] means when have already chosen i numbers and the last chosen number
: is a[j], the maximum minimum difference

avatar
g*y
7
对你的题目,我的理解是:找连续的k个数,最大化他们的最小邻近差值。
数组的邻近差值是固定的,所以你可以计算出新数组b[], b[i]=a[i+1]-a[i]。题目就
变成了,在数组b里,找size=k-1的窗口,最大化窗口里的最小数。
这个计算跟sliding window maximum是大同小异的。
如果你的题目是任意的k个数,最大化最小邻近差值,解法就不一样了。

【在 c********0 的大作中提到】
:
: number
: then how do you update? f[i+1][j] = ?

avatar
c*0
8

Thanks! 题目确实是任意k个数

【在 g**********y 的大作中提到】
: 对你的题目,我的理解是:找连续的k个数,最大化他们的最小邻近差值。
: 数组的邻近差值是固定的,所以你可以计算出新数组b[], b[i]=a[i+1]-a[i]。题目就
: 变成了,在数组b里,找size=k-1的窗口,最大化窗口里的最小数。
: 这个计算跟sliding window maximum是大同小异的。
: 如果你的题目是任意的k个数,最大化最小邻近差值,解法就不一样了。

avatar
d*d
9
火鸡,这题应该不是这个意思。

【在 g**********y 的大作中提到】
: 对你的题目,我的理解是:找连续的k个数,最大化他们的最小邻近差值。
: 数组的邻近差值是固定的,所以你可以计算出新数组b[], b[i]=a[i+1]-a[i]。题目就
: 变成了,在数组b里,找size=k-1的窗口,最大化窗口里的最小数。
: 这个计算跟sliding window maximum是大同小异的。
: 如果你的题目是任意的k个数,最大化最小邻近差值,解法就不一样了。

avatar
H*s
10
相邻数之间的差存一个数组 O(n)
然后再选择最大的k个差值 O(n)
总复杂度O(n)
avatar
g*y
11
对任意k个数,就象songlj说的,DP。

【在 c********0 的大作中提到】
:
: Thanks! 题目确实是任意k个数

avatar
H*s
12
Ok, 我也理解错题目了, 是选出的数中maximum minimum difference
avatar
g*i
13
dp复杂度高
我觉得这题类似paint partition problem,应该用binary search,复杂度是O(nlogn+
nlog(Max(a[i])))
avatar
r*g
14
是的,感觉更像你说的,先排序,然后类似paint partition problem, 1337上面有解
法。
貌似不是sliding window

【在 g*****i 的大作中提到】
: dp复杂度高
: 我觉得这题类似paint partition problem,应该用binary search,复杂度是O(nlogn+
: nlog(Max(a[i])))

avatar
c*1
15

怎么binary search啊。。。 感觉很飘渺啊

【在 r*******g 的大作中提到】
: 是的,感觉更像你说的,先排序,然后类似paint partition problem, 1337上面有解
: 法。
: 貌似不是sliding window

avatar
r*g
16
http://www.ihas1337code.com/2011/04/the-painters-partition-prob
你这个题目,其实就是做k partition, 然后要maximize the minimum, 而paint
partition problem是要minimize the maximum,根据他的dp方法应该是可解的。
关于binary search,你要search的就是minimum的值,它最小是目前最小的partition,最大是整个
长度,这个值如果过大,那么k个parition总长度就会超过整个长度。
http://www.ihas1337code.com/2011/04/the-painters-partition-prob

【在 c********1 的大作中提到】
:
: 怎么binary search啊。。。 感觉很飘渺啊

avatar
c*1
17

partition,最大是整个
我觉得我没把问题讲清楚。。。这个不是partition的问题
比如说给一个数组 [1,4,6,7] k = 2
那么我们选1和7就是最优的 因为差是6最大
如果k=3我们选1,4,7
差是3 (maximize the minimal difference)

【在 r*******g 的大作中提到】
: http://www.ihas1337code.com/2011/04/the-painters-partition-prob
: 你这个题目,其实就是做k partition, 然后要maximize the minimum, 而paint
: partition problem是要minimize the maximum,根据他的dp方法应该是可解的。
: 关于binary search,你要search的就是minimum的值,它最小是目前最小的partition,最大是整个
: 长度,这个值如果过大,那么k个parition总长度就会超过整个长度。
: http://www.ihas1337code.com/2011/04/the-painters-partition-prob

avatar
g*y
18
Java code =>
public class MaxMinDiff {
public int max(int[] a, int k) {
int N = a.length;
if (N < k || k < 2) return -1;

int lo = 0;
int hi = (a[N-1] - a[0]) / (k-1) + 1;

while (hi > lo+1) {
int mid = (hi + lo) / 2;
if (test(a, mid, k)) {
lo = mid;
}
else {
hi = mid;
}
}

return lo;
}

private boolean test(int[] a, int diff, int k) {
int prev = a[0];
int j = 1;
for (int i=1; iwhile (jif (j >= a.length) return false;
prev = a[j];
}
return true;
}
}

【在 c********1 的大作中提到】
:
: partition,最大是整个
: 我觉得我没把问题讲清楚。。。这个不是partition的问题
: 比如说给一个数组 [1,4,6,7] k = 2
: 那么我们选1和7就是最优的 因为差是6最大
: 如果k=3我们选1,4,7
: 差是3 (maximize the minimal difference)

avatar
B*1
19
这样子求出来的序列似乎一定要从index 0 开始的
是吗?
private boolean test(int[] a, int diff, int k) {
int prev = a[0];
int j = 1;
for (int i=1; iwhile (jif (j >= a.length) return false;
prev = a[j];
}
return true;
}

【在 g**********y 的大作中提到】
: Java code =>
: public class MaxMinDiff {
: public int max(int[] a, int k) {
: int N = a.length;
: if (N < k || k < 2) return -1;
:
: int lo = 0;
: int hi = (a[N-1] - a[0]) / (k-1) + 1;
:
: while (hi > lo+1) {

avatar
g*y
20
It's sorted array. There must be an optimum solution start with a0, end with
a(n-1)

【在 B*******1 的大作中提到】
: 这样子求出来的序列似乎一定要从index 0 开始的
: 是吗?
: private boolean test(int[] a, int diff, int k) {
: int prev = a[0];
: int j = 1;
: for (int i=1; i: while (j: if (j >= a.length) return false;
: prev = a[j];
: }

avatar
B*1
21
thanks, 现在懂了,这个发现是解这题的关键。其实你的方法跟这哥们的一样
http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=ri

with

【在 g**********y 的大作中提到】
: It's sorted array. There must be an optimum solution start with a0, end with
: a(n-1)

avatar
c*1
22

Good one, thanks!

【在 g**********y 的大作中提到】
: Java code =>
: public class MaxMinDiff {
: public int max(int[] a, int k) {
: int N = a.length;
: if (N < k || k < 2) return -1;
:
: int lo = 0;
: int hi = (a[N-1] - a[0]) / (k-1) + 1;
:
: while (hi > lo+1) {

avatar
r*g
23
这个其实就是partition问题,排序后对最大和最小的差值进行分段,如何选择分段的
问题,code已经有人贴出来了。

【在 c********1 的大作中提到】
:
: Good one, thanks!

avatar
c*0
24
Given a sorted array of n integers, pick up k elements so that the minimal
difference between consecutive elements is maximal (that is choose the
elements to maximize the quantity min(a[i+1] - a[i]))
avatar
g*y
25
Sliding window maximum problem的变形:
http://www.ihas1337code.com/2011/01/sliding-window-maximum.html

【在 c********0 的大作中提到】
: Given a sorted array of n integers, pick up k elements so that the minimal
: difference between consecutive elements is maximal (that is choose the
: elements to maximize the quantity min(a[i+1] - a[i]))

avatar
s*j
26
dp
f[i][j] means when have already chosen i numbers and the last chosen number
is a[j], the maximum minimum difference
avatar
c*0
29

number
then how do you update? f[i+1][j] = ?

【在 s****j 的大作中提到】
: dp
: f[i][j] means when have already chosen i numbers and the last chosen number
: is a[j], the maximum minimum difference

avatar
g*y
30
对你的题目,我的理解是:找连续的k个数,最大化他们的最小邻近差值。
数组的邻近差值是固定的,所以你可以计算出新数组b[], b[i]=a[i+1]-a[i]。题目就
变成了,在数组b里,找size=k-1的窗口,最大化窗口里的最小数。
这个计算跟sliding window maximum是大同小异的。
如果你的题目是任意的k个数,最大化最小邻近差值,解法就不一样了。

【在 c********0 的大作中提到】
:
: number
: then how do you update? f[i+1][j] = ?

avatar
c*0
31

Thanks! 题目确实是任意k个数

【在 g**********y 的大作中提到】
: 对你的题目,我的理解是:找连续的k个数,最大化他们的最小邻近差值。
: 数组的邻近差值是固定的,所以你可以计算出新数组b[], b[i]=a[i+1]-a[i]。题目就
: 变成了,在数组b里,找size=k-1的窗口,最大化窗口里的最小数。
: 这个计算跟sliding window maximum是大同小异的。
: 如果你的题目是任意的k个数,最大化最小邻近差值,解法就不一样了。

avatar
d*d
32
火鸡,这题应该不是这个意思。

【在 g**********y 的大作中提到】
: 对你的题目,我的理解是:找连续的k个数,最大化他们的最小邻近差值。
: 数组的邻近差值是固定的,所以你可以计算出新数组b[], b[i]=a[i+1]-a[i]。题目就
: 变成了,在数组b里,找size=k-1的窗口,最大化窗口里的最小数。
: 这个计算跟sliding window maximum是大同小异的。
: 如果你的题目是任意的k个数,最大化最小邻近差值,解法就不一样了。

avatar
H*s
33
相邻数之间的差存一个数组 O(n)
然后再选择最大的k个差值 O(n)
总复杂度O(n)
avatar
g*y
34
对任意k个数,就象songlj说的,DP。

【在 c********0 的大作中提到】
:
: Thanks! 题目确实是任意k个数

avatar
H*s
35
Ok, 我也理解错题目了, 是选出的数中maximum minimum difference
avatar
g*i
36
dp复杂度高
我觉得这题类似paint partition problem,应该用binary search,复杂度是O(nlogn+
nlog(Max(a[i])))
avatar
r*g
37
是的,感觉更像你说的,先排序,然后类似paint partition problem, 1337上面有解
法。
貌似不是sliding window

【在 g*****i 的大作中提到】
: dp复杂度高
: 我觉得这题类似paint partition problem,应该用binary search,复杂度是O(nlogn+
: nlog(Max(a[i])))

avatar
c*1
38

怎么binary search啊。。。 感觉很飘渺啊

【在 r*******g 的大作中提到】
: 是的,感觉更像你说的,先排序,然后类似paint partition problem, 1337上面有解
: 法。
: 貌似不是sliding window

avatar
r*g
39
http://www.ihas1337code.com/2011/04/the-painters-partition-prob
你这个题目,其实就是做k partition, 然后要maximize the minimum, 而paint
partition problem是要minimize the maximum,根据他的dp方法应该是可解的。
关于binary search,你要search的就是minimum的值,它最小是目前最小的partition,最大是整个
长度,这个值如果过大,那么k个parition总长度就会超过整个长度。
http://www.ihas1337code.com/2011/04/the-painters-partition-prob

【在 c********1 的大作中提到】
:
: 怎么binary search啊。。。 感觉很飘渺啊

avatar
c*1
40

partition,最大是整个
我觉得我没把问题讲清楚。。。这个不是partition的问题
比如说给一个数组 [1,4,6,7] k = 2
那么我们选1和7就是最优的 因为差是6最大
如果k=3我们选1,4,7
差是3 (maximize the minimal difference)

【在 r*******g 的大作中提到】
: http://www.ihas1337code.com/2011/04/the-painters-partition-prob
: 你这个题目,其实就是做k partition, 然后要maximize the minimum, 而paint
: partition problem是要minimize the maximum,根据他的dp方法应该是可解的。
: 关于binary search,你要search的就是minimum的值,它最小是目前最小的partition,最大是整个
: 长度,这个值如果过大,那么k个parition总长度就会超过整个长度。
: http://www.ihas1337code.com/2011/04/the-painters-partition-prob

avatar
g*y
41
Java code =>
public class MaxMinDiff {
public int max(int[] a, int k) {
int N = a.length;
if (N < k || k < 2) return -1;

int lo = 0;
int hi = (a[N-1] - a[0]) / (k-1) + 1;

while (hi > lo+1) {
int mid = (hi + lo) / 2;
if (test(a, mid, k)) {
lo = mid;
}
else {
hi = mid;
}
}

return lo;
}

private boolean test(int[] a, int diff, int k) {
int prev = a[0];
int j = 1;
for (int i=1; iwhile (jif (j >= a.length) return false;
prev = a[j];
}
return true;
}
}

【在 c********1 的大作中提到】
:
: partition,最大是整个
: 我觉得我没把问题讲清楚。。。这个不是partition的问题
: 比如说给一个数组 [1,4,6,7] k = 2
: 那么我们选1和7就是最优的 因为差是6最大
: 如果k=3我们选1,4,7
: 差是3 (maximize the minimal difference)

avatar
B*1
42
这样子求出来的序列似乎一定要从index 0 开始的
是吗?
private boolean test(int[] a, int diff, int k) {
int prev = a[0];
int j = 1;
for (int i=1; iwhile (jif (j >= a.length) return false;
prev = a[j];
}
return true;
}

【在 g**********y 的大作中提到】
: Java code =>
: public class MaxMinDiff {
: public int max(int[] a, int k) {
: int N = a.length;
: if (N < k || k < 2) return -1;
:
: int lo = 0;
: int hi = (a[N-1] - a[0]) / (k-1) + 1;
:
: while (hi > lo+1) {

avatar
g*y
43
It's sorted array. There must be an optimum solution start with a0, end with
a(n-1)

【在 B*******1 的大作中提到】
: 这样子求出来的序列似乎一定要从index 0 开始的
: 是吗?
: private boolean test(int[] a, int diff, int k) {
: int prev = a[0];
: int j = 1;
: for (int i=1; i: while (j: if (j >= a.length) return false;
: prev = a[j];
: }

avatar
B*1
44
thanks, 现在懂了,这个发现是解这题的关键。其实你的方法跟这哥们的一样
http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=ri

with

【在 g**********y 的大作中提到】
: It's sorted array. There must be an optimum solution start with a0, end with
: a(n-1)

avatar
c*1
45

Good one, thanks!

【在 g**********y 的大作中提到】
: Java code =>
: public class MaxMinDiff {
: public int max(int[] a, int k) {
: int N = a.length;
: if (N < k || k < 2) return -1;
:
: int lo = 0;
: int hi = (a[N-1] - a[0]) / (k-1) + 1;
:
: while (hi > lo+1) {

avatar
r*g
46
这个其实就是partition问题,排序后对最大和最小的差值进行分段,如何选择分段的
问题,code已经有人贴出来了。

【在 c********1 的大作中提到】
:
: Good one, thanks!

avatar
k*t
47
Isn't test() function use Greedy approach?
Can some one explain the reason why the greedy algorithm work?
Any link to algorithm explanation?

附Link上火鸡的Code:
Java code =>
public class MaxMinDiff {
public int max(int[] a, int k) {
int N = a.length;
if (N < k || k < 2) return -1;

int lo = 0;
int hi = (a[N-1] - a[0]) / (k-1) + 1;

while (hi > lo+1) {
int mid = (hi + lo) / 2;
if (test(a, mid, k)) {
lo = mid;
}
else {
hi = mid;
}
}

return lo;
}

private boolean test(int[] a, int diff, int k) {
int prev = a[0];
int j = 1;
for (int i=1; iwhile (jif (j >= a.length) return false;
prev = a[j];
}
return true;
}

【在 g**********y 的大作中提到】
: Java code =>
: public class MaxMinDiff {
: public int max(int[] a, int k) {
: int N = a.length;
: if (N < k || k < 2) return -1;
:
: int lo = 0;
: int hi = (a[N-1] - a[0]) / (k-1) + 1;
:
: while (hi > lo+1) {

avatar
H*e
48
要是真面试就用DP了
思路比较清晰
我还是觉得binary search的有些疑问。。

【在 c********0 的大作中提到】
: Given a sorted array of n integers, pick up k elements so that the minimal
: difference between consecutive elements is maximal (that is choose the
: elements to maximize the quantity min(a[i+1] - a[i]))

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