由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请教一道面试题
相关主题
继续贴几个题目算法面试题
再问个coin change的问题也问两个算法题
这个题目怎么做的啊?来不急准备了
Google onsite问题请教一个DP的题
新鲜C3 energy面经facebook电话二面题目
Given coins of value {k1, k2, ..., km}, 用最少硬币数组成一个sum 咋做啊找零钱的变体
F家的一道题。看起来好像很凶残的样子。求大家给思路给想法。。囧问个amazon面试题。
一个数组给一个int n, 求数组内能相加得到n的所有组合求本书 Cracking Coding Interviews,
相关话题的讨论汇总
话题: int话题: dp话题: amount话题: value话题: coins
进入JobHunting版参与讨论
1 (共1页)
a*u
发帖数: 97
1
You are given some denominations of coins in an array (int denom[])and
infinite supply of all of them. Given an amount (int amount), find the
minimum number of coins required to get the exact amount
for example, 面值数组 denom = {7, 5, 3}, target amount = 32, minimum number
of coins needed is 6 (2x7 + 3x5 + 1x3 = 32)
想到的只有greedy。。。有没有优雅一点的解法?
d*******8
发帖数: 785
2
Greedy有些面值数组算出来是错的,会没有解。
DP可以做,但是就是很慢

number

【在 a*u 的大作中提到】
: You are given some denominations of coins in an array (int denom[])and
: infinite supply of all of them. Given an amount (int amount), find the
: minimum number of coins required to get the exact amount
: for example, 面值数组 denom = {7, 5, 3}, target amount = 32, minimum number
: of coins needed is 6 (2x7 + 3x5 + 1x3 = 32)
: 想到的只有greedy。。。有没有优雅一点的解法?

a*u
发帖数: 97
3
恩,DP可以O(CN)解出来。
另外一个想法:保持n个queue for each denomination,用backtrack的方法解? 好处就
是可以跳过不可能的amount,但是还没想清楚具体操作。
y**i
发帖数: 1112
4
我刚写了一个,不知道有没有什么地方可以优化的,感兴趣的可以看一下,我们可以讨
论一下,也让我学习一下,谢谢了!
// Find minimum number of coins to get the exact amount
int FindCoins(int demon[], int nd, int amount)
{
int* N = new int[nd]; // save the number of each coins
memset(N, 0, nd*sizeof(int));
int n = 0, sum = 0;
while (sum != amount)
{
if (sum < amount) // continue to add current denomination
{
sum += demon[n];
++N[n];
}
else // back-track to subtract current coins
{
while

【在 a*u 的大作中提到】
: 恩,DP可以O(CN)解出来。
: 另外一个想法:保持n个queue for each denomination,用backtrack的方法解? 好处就
: 是可以跳过不可能的amount,但是还没想清楚具体操作。

x******3
发帖数: 245
5
linear programming, simplex method
assume given array A[1:n], each value is a coin value
need to compute x[1:n], each value is a integer >= 0, equals to the number
of a particular coin
solve this minimization
x[1]+x[2]+...+x[n]
subject to
x >= 0
A*transpose(x) = amount
s*********t
发帖数: 1663
6
interesting!

【在 x******3 的大作中提到】
: linear programming, simplex method
: assume given array A[1:n], each value is a coin value
: need to compute x[1:n], each value is a integer >= 0, equals to the number
: of a particular coin
: solve this minimization
: x[1]+x[2]+...+x[n]
: subject to
: x >= 0
: A*transpose(x) = amount

x******3
发帖数: 245
7
simplex method 不一定给出整数解
可能还是用DP比较好

【在 s*********t 的大作中提到】
: interesting!
a*u
发帖数: 97
8
感兴趣!
明天要去onsite, 回来再拜读

【在 y**i 的大作中提到】
: 我刚写了一个,不知道有没有什么地方可以优化的,感兴趣的可以看一下,我们可以讨
: 论一下,也让我学习一下,谢谢了!
: // Find minimum number of coins to get the exact amount
: int FindCoins(int demon[], int nd, int amount)
: {
: int* N = new int[nd]; // save the number of each coins
: memset(N, 0, nd*sizeof(int));
: int n = 0, sum = 0;
: while (sum != amount)
: {

x****r
发帖数: 99
9
c#的解,请帮我看看对不对,谢谢
测试过{3, 5, 7}和32 的时候答案是6
public static void findDenominations(int[] denom, int Value){
int[] DP = new int[Value + 1];
DP[0] = 0;

for (int i = 1; i <= Value ++i){
int min = 9999;
for (int j = 0; j < denom.Length; ++j){
int prev = Value - denom[j];
if (prev > -1 && DP[prev] < min)
min = DP[prev];
}
DP[i] = min + 1;
}
Console.WriteLine(DP[Value]);
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
求本书 Cracking Coding Interviews,新鲜C3 energy面经
MathWorks Interview 求祝福!Given coins of value {k1, k2, ..., km}, 用最少硬币数组成一个sum 咋做啊
两道题目F家的一道题。看起来好像很凶残的样子。求大家给思路给想法。。囧
G家面经(已被HC挂,求分析)一个数组给一个int n, 求数组内能相加得到n的所有组合
继续贴几个题目算法面试题
再问个coin change的问题也问两个算法题
这个题目怎么做的啊?来不急准备了
Google onsite问题请教一个DP的题
相关话题的讨论汇总
话题: int话题: dp话题: amount话题: value话题: coins