使用自上而下的方法最大化切割段?
Maximize The Cut Segments using top down approach?
给定一个整数N,表示线段的长度。您需要切割线段,每次线段的切割长度为 x 、 y 或 z 。这里 x、y 和 z 是整数。
执行所有切割操作后,您的切割段总数必须是最大的。
示例 1
Input:
N = 4
x = 2, y = 1, z = 1
Output: 4
Explanation:Total length is 4, and the cut
lengths are 2, 1 and 1. We can make
maximum 4 segments each of length 1.
示例 2
Input:
N = 5
x = 5, y = 3, z = 2
Output: 2
Explanation: Here total length is 5, and
the cut lengths are 5, 3 and 2. We can
make two segments of lengths 3 and 2.
这是我的解决方案
int max_seg(int M[], int n, int x, int y, int z)
{
if( (n<=0) && (M[0] != -1) )
return M[0];
else if( (n>0) && M[n] != -1)
return M[n];
int q;
if(n <= 0)
{
q = 0;
M[0] = q;
return M[0];
}
else
{
q = max({1 + max_seg(M, n-x, x, y, z), 1 + max_seg(M, n-y, x, y, z), 1 + max_seg(M, n-y, x, y, z) });
M[n] = q;
return M[n];
}
}
int maximizeTheCuts(int n, int x, int y, int z)
{
//Your code here
int M[n+1] = {0}; // compiler does permit this
for(int i=0; i<=n; i++)
M[i] = -1;
return max_seg(M, n, x, y, z);
}
我的代码有什么问题。任何帮助表示赞赏。
下面的测试用例失败了
N= 4000
x=3 y=4 z=5
returned 1334 instead of 1333
您需要确保永远不会使用负数 n 调用 max_seg,否则,如果使用负数 n 调用,它 returns 将不会被视为一个值最好的解决方案。 – @IgorTandetnik
修复
int max_seg(int M[], int n, int x, int y, int z)
{
if( (n<0) )
return INT_MIN;
else if( (n>=0) && M[n] != -1)
return M[n];
int q;
if(n <= 0)
{
q = 0;
M[0] = q;
return M[0];
}
else
{
q = max({1 + max_seg(M, n-x, x, y, z), 1 + max_seg(M, n-y, x, y, z), 1 + max_seg(M, n-y, x, y, z) });
M[n] = q;
return M[n];
}
}
int maximizeTheCuts(int n, int x, int y, int z)
{
//Your code here
int M[n+1] = {0}; // compiler does permit this
for(int i=0; i<=n; i++)
M[i] = -1;
return max_seg(M, n, x, y, z);
}
给定一个整数N,表示线段的长度。您需要切割线段,每次线段的切割长度为 x 、 y 或 z 。这里 x、y 和 z 是整数。
执行所有切割操作后,您的切割段总数必须是最大的。
示例 1
Input:
N = 4
x = 2, y = 1, z = 1
Output: 4
Explanation:Total length is 4, and the cut
lengths are 2, 1 and 1. We can make
maximum 4 segments each of length 1.
示例 2
Input:
N = 5
x = 5, y = 3, z = 2
Output: 2
Explanation: Here total length is 5, and
the cut lengths are 5, 3 and 2. We can
make two segments of lengths 3 and 2.
这是我的解决方案
int max_seg(int M[], int n, int x, int y, int z)
{
if( (n<=0) && (M[0] != -1) )
return M[0];
else if( (n>0) && M[n] != -1)
return M[n];
int q;
if(n <= 0)
{
q = 0;
M[0] = q;
return M[0];
}
else
{
q = max({1 + max_seg(M, n-x, x, y, z), 1 + max_seg(M, n-y, x, y, z), 1 + max_seg(M, n-y, x, y, z) });
M[n] = q;
return M[n];
}
}
int maximizeTheCuts(int n, int x, int y, int z)
{
//Your code here
int M[n+1] = {0}; // compiler does permit this
for(int i=0; i<=n; i++)
M[i] = -1;
return max_seg(M, n, x, y, z);
}
我的代码有什么问题。任何帮助表示赞赏。
下面的测试用例失败了
N= 4000
x=3 y=4 z=5
returned 1334 instead of 1333
您需要确保永远不会使用负数 n 调用 max_seg,否则,如果使用负数 n 调用,它 returns 将不会被视为一个值最好的解决方案。 – @IgorTandetnik
修复
int max_seg(int M[], int n, int x, int y, int z)
{
if( (n<0) )
return INT_MIN;
else if( (n>=0) && M[n] != -1)
return M[n];
int q;
if(n <= 0)
{
q = 0;
M[0] = q;
return M[0];
}
else
{
q = max({1 + max_seg(M, n-x, x, y, z), 1 + max_seg(M, n-y, x, y, z), 1 + max_seg(M, n-y, x, y, z) });
M[n] = q;
return M[n];
}
}
int maximizeTheCuts(int n, int x, int y, int z)
{
//Your code here
int M[n+1] = {0}; // compiler does permit this
for(int i=0; i<=n; i++)
M[i] = -1;
return max_seg(M, n, x, y, z);
}