Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to devise this solution to Non-Constructible Change challenge from Algoexpert.io

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!

like image 837
Daniel Kaczmarczyk Avatar asked Apr 14 '21 22:04

Daniel Kaczmarczyk


People also ask

What is non constructible change?

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.

What is non constructible?

# 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).

What's new in algoexpert for Java?

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.

Is algoexpert better than grokking the coding interview?

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.

What is the difference between algoexpert and leetcode?

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.

Is algoexpert worth it in 2021?

[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.


1 Answers

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:

  • You know for a fact that you can make 9 cents.
  • Example if the new coin is 5: Use that new coin and subtract it from the total you're trying to make. 9 - 5 = 4. Then however way you made 4 previously just do that again. (You've already proven you can make 1-8 cents)

if newCoin == 9:

  • You know for a fact that you can make 9 cents.
  • Just use the 9 cent coin

if newCoin > 9:

  • You know for a fact that you CANNOT make 9 cents
  • This is because you can't use the new coin. For example, a 10 cent coin is useless to you when you're trying to make 9 cents since it's too big (can't make 9)
  • You're also screwed if you don't use the new coin because if you add up all the coins you've seen so far, you've only been able to make 8 (can't make 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`.
like image 73
Jack Windensky Avatar answered Oct 12 '22 13:10

Jack Windensky