There are n different sizes of boxes, from 1 to n. There is an unlimited supply of boxes of each siz

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值