avatar
问个google面试题# JobHunting - 待字闺中
B*1
1
Suppose we can compare two arrays like:
{4,2,3} > {3,5,6}
{4,2,3} < {4,3,0}
In each move, you can only switch a number with one of its neighbor. Given
an array and a number n, design an algorithm to make this array maximum
using n moves. (needs clarification)
avatar
f*t
2
想了一下greedy不对
avatar
s*x
3
say array A[0..(k-1)] contains k numbers
while (n)
{
x = min(n, k-1)
find i: 0<=i<=x such that A[i] is max among A[0..x]
for (j = i; j>0; j--)
Swap(A[j],A[j-1]);
n -= i;
}
avatar
B*1
4
i thought the same way as you. Not sure whether it is right.
avatar
f*t
5
我觉得是对的

【在 s**x 的大作中提到】
: say array A[0..(k-1)] contains k numbers
: while (n)
: {
: x = min(n, k-1)
: find i: 0<=i<=x such that A[i] is max among A[0..x]
: for (j = i; j>0; j--)
: Swap(A[j],A[j-1]);
: n -= i;
: }

avatar
s*x
6
sorry forgot to take care of a few conditions:
for (m=0; m0; m++)
{
x = min(n, k-m-1)
find i: m<=i<=x such that A[i] is max among A[m..x]
for (j = i; j>m; j--)
Swap(A[j],A[j-1]);
n -= i;
}
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。