0
(java) Quick but nice question from today's class.... Wondering any other different ways to do it!! Thanks! 🤘✌
Write a program to input number on packets (n) and output number of different types of cartons are used to package those packets.... capacity of cartons are 24 ; 12 ; 9 ; 6 . Try to use least number of cartons to pack all the packets! https://code.sololearn.com/cZb3vp7B0jSb/?ref=app
1 Odpowiedź
0
Use / and %, you don't need the loops.
i.e. 25 is the value, if you do 25/7, you'll get 3. suppose 7 is the biggest packet. and then, if you %, you'll get 4, that means that's what you have left to work on. So suppose you have 6, 6 is greater than 4, so you won't take it, then suppose the next smallest is 3, you do a 3/4 = 1, mod to get the remainder. (...)
This kind of approach is called greedy approach, if you want you can look it up. another way to approach it will be dynamic approach, if you're interested, google.
Difference between the time complexity of your program and if you do it with / and %,
for your it's O(n) atleast.
for the other, O(1).