There are n different sizes of boxes, from 1 to n. There is an unlimited supply of boxes of each size t, each with a value vt . A box of size t can hold several smaller boxes of sizes a1, a2, . . . , ak as long as the sum of sizes a1 + a2 + . . . + ak is strictly less than t. Each of these boxes may be filled with yet more boxes, and so on.
Design an algorithm which runs in O(n2 ) time and finds the maximum value that can be attained by taking one box, potentially with smaller boxes nested inside it.(本coursehero资源由赶due网info.gandue.net赞助)
Intro
Hi there, Please find your solution below, I hope you would find my solution useful and helpful.
Thank you!
arrow_forward
Information got from your statement :-
The information that I got from your statement is:-
Given an array size[] of box sizes, our task is to find the number of boxes left at the end, after putting the smaller-sized box into a bigger one.
Example:-
Input: size[] = {1, 2, 3}
Output: 1
Explanation:
Here, the box of size 1 can fit inside the box of size 2 and the box of size 2 can fit inside the box of size 3. So, at last, we have only one box of size 3.
arrow_forward
Approach
Approach:-
Sort the given array size[] in increasing order.
Check if the current box size is bigger than the next box size. If this is the case, lower the starting box number.
else, the current box size is the same as the next box size, then see if the current box can fit within the next box size. If true, move the current box pointing variable forward; else, move the next pointing variable forward.
arrow_forward
Code with output
Computer Science homework question answer, step 4, image 1
arrow_forward
Code in c++
#include <bits/stdc++.h>
using namespace std;
void Minimum_Box(int a[], int n)
{
int box = n;
sort(a, a + n);
int curr_box = 0, next_box = 1;
while (curr_box < n && next_box < n) {
if (a[curr_box] < a[next_box]) {
box--;
curr_box++;
next_box++;
}
else if (a[curr_box] == a[next_box])
next_box++;
}
cout << box << endl;
}
int main()
{
int size[] = { 1, 2, 3 };
int n = sizeof(size) / sizeof(size[0]);
Minimum_Box(size, n);
return 0;
}
arrow_forward
Output
Computer Science homework question answer, step 6, image 1
The time complexity of this algorithm is:- O(n log n)
I hope you would find my solution useful and helpful.
Thank you!