Description for today’s challenge is here.
The description for the challenge is quite long and wordy, but ultimately there are only three things we need to remember:
- the total weight of packages need to be evenly divided into 3 groups
- the first group needs to have fewest packages possible
- given the same number of packages (fewest possible), the quantum entanglement (i.e. the product…) of the weights of packages in the first group needs to be the smallest possible
Based on these information, we can have the following:
For my input, all the weights are odd numbers, and the average weight is an even number. Which means we need an even number of packages in each group, furthermore, based on the input the smallest number of packages that can add up to the average weight is 6.
So rather than a brute force approach (which I tried and failed with) we can be smarter and:
- only consider the first group (since that’s all we need to answer the question)
- only consider groups of length 6
So, for each package, we will:
- yield the 6-package groups that can be formed with it
- yield the 6-package groups that can be formed without it
and we’ll short circuit a path if after collecting 6 packages in a group but the total weight of the group doesn’t add up to the average.
This is exactly what the following code snippet does, it returns all the 6-package groups that has a total weight equalling the average weight:
Finally, we just need to find the smallest quantum entanglement value of all the possible combinations:
Same approach as above, but slightly different setup.
Now that we’re dividing the total weight of all the packages by 4 instead of 3, the average is now an even number – meaning we’ll need an odd number of packages per group. Also, the smallest number of packages per group is now 5:
I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.
Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.
Check out my new course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, callbacks, nested workflows, design patterns and best practices.
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong