I'm working through algoexpert.io coding challenges and I'm having trouble undersatnding the suggested solution to one of the questions titled Non-Constructible Change
Here's the challenge question:
Given an array of positive integers representing the values of coins in your possession, write a function that returns the minimum amount of change (the minimum sum of money) that you cannot create. The given coins can have any positive integer value and aren't necessarily unique (i.e., you can have multiple coins of the same value).
For example, if you're given coins = [1, 2, 5], the minimum amount of change that you can't create is 4. If you're given no coins, the minimum amount of change that you can't create is 1.
// O(nlogn) time, O(n) size.
function nonConstructibleChange(coins) {
coins = coins.sort((a, b) => a - b); // O(nlogn) time operation
let change = 0;
for (coin of coins) {
if (coin > change + 1) return change + 1;
change += coin;
}
return change + 1;
}
My problem
I am not completely sure how did the author of the solution come up with the intuition that
if the current coin is greater than `change + 1`, the smallest impossible change is equal to `change + 1`.
I can see how it tracks, and indeed the algorithm passes all tests, but I'd like to know more about a process I could use to devise this rule.
Thank you for taking the time to read the question!
Given an array of positive integers representing the values of coins in your possession, write a function that returns the minimum amount of change (the minimum sum of money) that you cannot create.
# of change (the minimum sum of money) that you cannot create. The given. # coins can have any positive integer value and aren't necessarily unique. # (i.e., you can have multiple coins of the same value).
If you’ve been frustrated by other resources that only offer support in Java (i.e. Cracking the Coding Interview ), AlgoExpert’s language diversity comes as a long-overdue feature. As of July 2020, the AlgoExpert platform contains 2 new sections: 1. Coding Interview Assessments These practice assessments mimic a typical coding interview day.
Overall, if you enjoy video explanations of specific questions, AlgoExpert is probably the better choice. But if you want a more holistic approach to solving problems using patterns, Grokking the Coding Interview is the better option. AlgoExpert was founded by Clément Mihailescu and Antoine Pourchet.
AlgoExpert costs $99 annually. LeetCode promises software developers a wide range of coding problems. This includes numerous problems featured on AlgoExpert. One big difference between LeetCode and AlgoExpert is that LeetCode’s basic tier is free. Secondly, LeetCode is more focused on peer competition and scoring.
[Algoexpert.io review] Is AlgoExpert worth it in 2021? [Algoexpert.io review] AlgoExpert is one of the newest platforms to serve software engineers who aspire to work at a FAANG. It has a robust feature set, rich multimedia content and lots of buzz.
This one took me a while too, but this was how I made sense of it:
Assume you've proven you can make 1-8 cents.
You go to the next iteration and want to know if you can make 9 cents. So you iterate to the next new coin in the sorted list.
if newCoin < 9:
if newCoin == 9:
if newCoin > 9:
And that's where the change + 1 comes from. (your variable change = 8)
if the current coin is greater than `change + 1`, the smallest impossible change is equal to `change + 1`.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With