diff --git a/coding_interviews/algoexpert/non-constructible-change/non-constructible-change.js b/coding_interviews/algoexpert/non-constructible-change/non-constructible-change.js new file mode 100644 index 0000000..e6f666f --- /dev/null +++ b/coding_interviews/algoexpert/non-constructible-change/non-constructible-change.js @@ -0,0 +1,18 @@ +// Runtime: O(nlogn) +// Space: O(1) + +function nonConstructibleChange(coins) { + coins.sort((a, b) => a - b); + + let currentChangeCreated = 0; + + for (let coin of coins) { + if (coin > currentChangeCreated + 1) { + return currentChangeCreated + 1; + } + + currentChangeCreated += coin; + } + + return currentChangeCreated + 1; +}