由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 9 个苹果 in 40 个罐, 怎么解?
相关主题
考古--用户最多的3连击问题问个Facebook 电面题
提供Rackspace的refer,地点 San Antonio, Austin, Blacksburg, San Franciscoleetcode 这题insert interval怎么做?
APPLE RF电面leetcode的online judge runtime error是指什么?
iOS太烂新鲜G面筋(Fail)
Probability quesiton把n个interval 放到一个container里
问个算法题, 关于区间 overlap的讨论一道面试题
FB interview question狗电面
Interval tree解法leetcode 的 Insert Interval 就是过不了大的
相关话题的讨论汇总
话题: int话题: cnt话题: list话题: apples
进入JobHunting版参与讨论
1 (共1页)
p*****i
发帖数: 197
1
You have 40 cans, all placed in a line at exact intervals of one meter.
You also have 9 apples. You wish to place all the apples in the cans, no
more than one apple in each can, so that there are no three apples A, B,
and C such that the distance between A and B is equal to the distance
between B and C. How many ways can you arrange the apples in the cans?
怎么解?
t**r
发帖数: 3428
2
mark
p*****i
发帖数: 197
3


【在 t**r 的大作中提到】
: mark
b******u
发帖数: 81
4
7555794
1, 2, 6, 7, 9, 14, 15, 18, 20
bruite force or
public static int GetCount2(int numOfApples, int numOfLocations)
{
List set = new List();
for (int i = 1; i <= numOfApples; i++ )
{
set = GetSetForAddingOneMoreAppleAtTheEnd(set,
numOfLocations - numOfApples + i);
}
return set.Count;
}
public static List GetSetForAddingOneMoreAppleAtTheEnd(List<
int[]> existingSet, int numOfLocations )
{
List result = new List();
if (existingSet.Count == 0)
{
for (int i = 1; i <= numOfLocations; i++)
{
int[] loc = { i };
result.Add(loc);
}
return result;
}
foreach (int[] locations in existingSet)
{
int max = locations.Max(i => i);
int cnt = locations.Length;
for (int i = max + 1; i <= numOfLocations; i++)
{
if (IsTheNewAppleOk(locations, cnt, i))
{
int[] newLocations = new int[cnt + 1];
for (int j = 0; j < cnt; j++)
{
newLocations[j] = locations[j];
}
newLocations[cnt] = i;
result.Add(newLocations);
}
}
}
return result;
}
public static bool IsTheNewAppleOk(int[] applPos, int cnt, int
newPos)
{
if (cnt <= 1) return true;
for (int i = 0; i < cnt; i++)
{
for (int j = i + 1; j < cnt; j++)
{
if (applPos[i] + newPos == 2 * applPos[j])
{
return false;
}
}
}
return true;
}
p*****2
发帖数: 21240
5
recursion。
至少需要37个苹果。第一个苹果可以放在1-4的位置上。下一个苹果可以放到下1到10
的位置上。
然后要剪枝。苹果之间的间隔可以是1-10,用hashtable放已经用过的间隔。通过统计
还可以剪掉8-10已经不能用的。
c*****l
发帖数: 20
6
bajiezhu 提供的解答 (#4)
IsTheNewAppleOk() 的 time complexity 是 O(n^2)
用下面的方法可以更快一些
使用一个数组 postion[1..40],初始值设为0,
如果 apple i 可以放在 position[j], position[j] == i
如果position j 因为已放进罐子里的 apples 的限制, 不可使用,position[j] 将设
为负数(请看下面的算法)
搜寻下一个 j, and position[j] == 0
那么 apple i 可以放在 position[j], position[j] = i;
计算所有已经放进罐子 apple[1..i-1] 的距离
如果 apple k 和 apple i 的距离是 d
position[j+d] -= k
C***U
发帖数: 2406
7
dp?

【在 p*****i 的大作中提到】
: You have 40 cans, all placed in a line at exact intervals of one meter.
: You also have 9 apples. You wish to place all the apples in the cans, no
: more than one apple in each can, so that there are no three apples A, B,
: and C such that the distance between A and B is equal to the distance
: between B and C. How many ways can you arrange the apples in the cans?
: 怎么解?

C***U
发帖数: 2406
8
brute force的算法我写过 大概3秒中能算出来

【在 p*****i 的大作中提到】
: You have 40 cans, all placed in a line at exact intervals of one meter.
: You also have 9 apples. You wish to place all the apples in the cans, no
: more than one apple in each can, so that there are no three apples A, B,
: and C such that the distance between A and B is equal to the distance
: between B and C. How many ways can you arrange the apples in the cans?
: 怎么解?

1 (共1页)
进入JobHunting版参与讨论
相关主题
leetcode 的 Insert Interval 就是过不了大的Probability quesiton
Insert Interval large case测试没过,怎么优化?问个算法题, 关于区间 overlap的
Merge Interval那道题FB interview question
JAVA里sort的algorithm time complexity是多少Interval tree解法
考古--用户最多的3连击问题问个Facebook 电面题
提供Rackspace的refer,地点 San Antonio, Austin, Blacksburg, San Franciscoleetcode 这题insert interval怎么做?
APPLE RF电面leetcode的online judge runtime error是指什么?
iOS太烂新鲜G面筋(Fail)
相关话题的讨论汇总
话题: int话题: cnt话题: list话题: apples