给定一个整数n ,任务是找到总和等于N的立方体的最小数量。
例子:
Input: N = 496
Output: 3
43 + 63 + 63 = 496
Note that 13 + 33 + 53 + 73 = 496 but it requires 4 cubes.
Input: N = 15
Output: 8
朴素的方法:编写一个递归方法,将每个小于N 的完美立方体(比如X)作为求和的一部分,然后递归求和N – X所需的立方体数量。该解决方案的时间复杂度是指数级的。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum
// number of cubes whose sum is k
int MinOfCubed(int k)
{
// If k is less than the 2^3
if (k < 8)
return k;
// Initialize with the maximum
// number of cubes required
int res = k;
for (int i = 1; i <= k; i++) {
if ((i * i * i) > k)
return res;
res = min(res, MinOfCubed(k - (i * i * i)) + 1);
}
return res;
}
// Driver code
int main()
{
int num = 15;
cout << MinOfCubed(num);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the minimum
// number of cubes whose sum is k
static int MinOfCubed(int k)
{
// If k is less than the 2^3
if (k < 8)
return k;
// Initialize with the maximum
// number of cubes required
int res = k;
for (int i = 1; i <= k; i++) {
if ((i * i * i) > k)
return res;
res = Math.min(res, MinOfCubed(k - (i * i * i)) + 1);
}
return res;
}
// Driver code
public static void main(String[] args)
{
int num = 15;
System.out.println(MinOfCubed(num));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to return the minimum
# number of cubes whose sum is k
def MinOfCubed(k):
# If k is less than the 2 ^ 3
if (k < 8):
return k;
# Initialize with the maximum
# number of cubes required
res = k;
for i in range(1, k + 1):
if ((i * i * i) > k):
return res;
res = min(res, MinOfCubed(k - (i * i * i)) + 1);
return res;
# Driver code
num = 15;
print(MinOfCubed(num));
# This code contributed by PrinciRaj1992
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the minimum
// number of cubes whose sum is k
static int MinOfCubed(int k)
{
// If k is less than the 2^3
if (k < 8)
return k;
// Initialize with the maximum
// number of cubes required
int res = k;
for (int i = 1; i <= k; i++) {
if ((i * i * i) > k)
return res;
res = Math.Min(res, MinOfCubed(k - (i * i * i)) + 1);
}
return res;
}
// Driver code
static public void Main()
{
int num = 15;
Console.WriteLine(MinOfCubed(num));
}
}
// This code has been contributed by ajit.
PHP
$k)
return $res;
$res = min($res, MinOfCubed($k - ($i *$i * $i)) + 1);
}
return $res;
}
// Driver code
$num = 15;
echo MinOfCubed($num);
// This code is contributed by Ryuga
?>
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum
// number of cubes whose sum is k
int MinOfCubedDP(int k)
{
int *DP = new int[k + 1], j = 1, t = 1;
DP[0] = 0;
for (int i = 1; i <= k; i++) {
DP[i] = INT_MAX;
// While current perfect cube
// is less than current element
while (j <= i) {
// If i is a perfect cube
if (j == i)
DP[i] = 1;
// i = (i - 1) + 1^3
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1;
// Next perfect cube
t++;
j = t * t * t;
}
// Re-initialization for next element
t = j = 1;
}
return DP[k];
}
// Driver code
int main()
{
int num = 15;
cout << MinOfCubedDP(num);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the minimum
// number of cubes whose sum is k
static int MinOfCubedDP(int k)
{
int[] DP = new int[k + 1];
int j = 1, t = 1;
DP[0] = 0;
for (int i = 1; i <= k; i++) {
DP[i] = Integer.MAX_VALUE;
// While current perfect cube
// is less than current element
while (j <= i) {
// If i is a perfect cube
if (j == i)
DP[i] = 1;
// i = (i - 1) + 1^3
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1;
// Next perfect cube
t++;
j = t * t * t;
}
// Re-initialization for next element
t = j = 1;
}
return DP[k];
}
// Driver code
public static void main(String[] args)
{
int num = 15;
System.out.println(MinOfCubedDP(num));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python implementation of the approach
import sys
# Function to return the minimum
# number of cubes whose sum is k
def MinOfCubedDP(k):
DP = [0] * (k + 1);
j = 1;
t = 1;
DP[0] = 0;
for i in range(1, k + 1):
DP[i] = sys.maxsize;
# While current perfect cube
# is less than current element
while (j <= i):
# If i is a perfect cube
if (j == i):
DP[i] = 1;
# i = (i - 1) + 1^3
elif (DP[i] > DP[i - j]):
DP[i] = DP[i - j] + 1;
# Next perfect cube
t += 1;
j = t * t * t;
# Re-initialization for next element
t = j = 1;
return DP[k];
# Driver code
num = 15;
print(MinOfCubedDP(num));
# This code contributed by Rajput-Ji
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the minimum
// number of cubes whose sum is k
static int MinOfCubedDP(int k)
{
int[] DP = new int[k + 1];
int j = 1, t = 1;
DP[0] = 0;
for (int i = 1; i <= k; i++) {
DP[i] = int.MaxValue;
// While current perfect cube
// is less than current element
while (j <= i) {
// If i is a perfect cube
if (j == i)
DP[i] = 1;
// i = (i - 1) + 1^3
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1;
// Next perfect cube
t++;
j = t * t * t;
}
// Re-initialization for next element
t = j = 1;
}
return DP[k];
}
// Driver code
public static void Main()
{
int num = 15;
Console.WriteLine(MinOfCubedDP(num));
}
}
/* This code contributed by Code_Mech */
PHP
$DP[$i - $j])
$DP[$i] = $DP[$i - $j] + 1;
// Next perfect cube
$t++;
$j = $t * $t * $t;
}
// Re-initialization for next element
$t = $j = 1;
}
return $DP[$k];
}
// Driver code
$num = 15;
echo(MinOfCubedDP($num));
// This code contributed by Code_Mech
?>
Javascript
输出:
8
有效的方法:如果我们为上述解决方案绘制完整的递归树,我们可以看到许多子问题一次又一次地解决,因此我们可以看到这个问题具有重叠子问题的性质。这导致我们使用动态规划范式解决问题。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum
// number of cubes whose sum is k
int MinOfCubedDP(int k)
{
int *DP = new int[k + 1], j = 1, t = 1;
DP[0] = 0;
for (int i = 1; i <= k; i++) {
DP[i] = INT_MAX;
// While current perfect cube
// is less than current element
while (j <= i) {
// If i is a perfect cube
if (j == i)
DP[i] = 1;
// i = (i - 1) + 1^3
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1;
// Next perfect cube
t++;
j = t * t * t;
}
// Re-initialization for next element
t = j = 1;
}
return DP[k];
}
// Driver code
int main()
{
int num = 15;
cout << MinOfCubedDP(num);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the minimum
// number of cubes whose sum is k
static int MinOfCubedDP(int k)
{
int[] DP = new int[k + 1];
int j = 1, t = 1;
DP[0] = 0;
for (int i = 1; i <= k; i++) {
DP[i] = Integer.MAX_VALUE;
// While current perfect cube
// is less than current element
while (j <= i) {
// If i is a perfect cube
if (j == i)
DP[i] = 1;
// i = (i - 1) + 1^3
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1;
// Next perfect cube
t++;
j = t * t * t;
}
// Re-initialization for next element
t = j = 1;
}
return DP[k];
}
// Driver code
public static void main(String[] args)
{
int num = 15;
System.out.println(MinOfCubedDP(num));
}
}
/* This code contributed by PrinciRaj1992 */
蟒蛇3
# Python implementation of the approach
import sys
# Function to return the minimum
# number of cubes whose sum is k
def MinOfCubedDP(k):
DP = [0] * (k + 1);
j = 1;
t = 1;
DP[0] = 0;
for i in range(1, k + 1):
DP[i] = sys.maxsize;
# While current perfect cube
# is less than current element
while (j <= i):
# If i is a perfect cube
if (j == i):
DP[i] = 1;
# i = (i - 1) + 1^3
elif (DP[i] > DP[i - j]):
DP[i] = DP[i - j] + 1;
# Next perfect cube
t += 1;
j = t * t * t;
# Re-initialization for next element
t = j = 1;
return DP[k];
# Driver code
num = 15;
print(MinOfCubedDP(num));
# This code contributed by Rajput-Ji
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the minimum
// number of cubes whose sum is k
static int MinOfCubedDP(int k)
{
int[] DP = new int[k + 1];
int j = 1, t = 1;
DP[0] = 0;
for (int i = 1; i <= k; i++) {
DP[i] = int.MaxValue;
// While current perfect cube
// is less than current element
while (j <= i) {
// If i is a perfect cube
if (j == i)
DP[i] = 1;
// i = (i - 1) + 1^3
else if (DP[i] > DP[i - j])
DP[i] = DP[i - j] + 1;
// Next perfect cube
t++;
j = t * t * t;
}
// Re-initialization for next element
t = j = 1;
}
return DP[k];
}
// Driver code
public static void Main()
{
int num = 15;
Console.WriteLine(MinOfCubedDP(num));
}
}
/* This code contributed by Code_Mech */
PHP
$DP[$i - $j])
$DP[$i] = $DP[$i - $j] + 1;
// Next perfect cube
$t++;
$j = $t * $t * $t;
}
// Re-initialization for next element
$t = $j = 1;
}
return $DP[$k];
}
// Driver code
$num = 15;
echo(MinOfCubedDP($num));
// This code contributed by Code_Mech
?>
Javascript
输出:
8
乍一看,该算法似乎在多项式时间内工作,因为我们有两个嵌套循环,外循环使用 O(n),内循环使用 O(n^(1/3))。所以整个算法需要O(n*n^(1/3))。但是作为输入长度的函数的复杂度是多少?要表示一个大小为 n 的数字,我们需要 m=log(n) –(以 2 为底的对数)位。在这种情况下,n=2^m。如果我们在公式 O(n*n^(1/3)) 中将 2^m 设置为 n,我们看到时间复杂度仍然是指数级的。该算法称为伪多项式。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。