Recursion and Dynamic Programming: Uplevel Your Coding Interview by Wong Jack
Author:Wong, Jack [Wong, Jack]
Language: eng
Format: epub
Published: 2018-08-23T16:00:00+00:00
Approach 2: top-down dynamic programming (memoization)
Notice that there are some overlapping subproblems the above code repeatedly solves. We can improve the program run time by caching (saving) the result for quick reference, and therefore avoid the recomputation of the subproblem again. Here is the caching approach:
/**
@param {number} n - balance
@param {number []} denoms - coin denominators. [25, 10, 5, 1] in this case
@param {number} index - current denominator index (start with 0)
@param {Object} memo - the cache object for computed subproblems, where it is initialized to {}
*/
function findChange (n, denoms, index, memo) {
var memoKey = n + ':' + denoms[index],
denom = denoms[index],
ways = 0,
remainingAmount = 0;
if (memo[memoKey]) return memo[memoKey];
if (index >= (denoms.length - 1)) return 1;
for (var i = 0; (i * denom) <= n; i++) {
remainingAmount = n - (i * denom);
ways += findChange(remainingAmount, denoms, index + 1, memo);
}
memo[memoKey] = ways;
return ways;
}
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(19193)
Hello! Python by Anthony Briggs(18601)
Dependency Injection in .NET by Mark Seemann(16949)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(16920)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(16707)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(16405)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(16276)
Kotlin in Action by Dmitry Jemerov(15625)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(15150)
Grails in Action by Glen Smith Peter Ledbrook(14361)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(10070)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(8041)
Microservices with Go by Alexander Shuiskov(7812)
Practical Design Patterns for Java Developers by Miroslav Wengner(7711)
Test Automation Engineering Handbook by Manikandan Sambamurthy(7653)
Angular Projects - Third Edition by Aristeidis Bampakos(7137)
The Art of Crafting User Stories by The Art of Crafting User Stories(6599)
NetSuite for Consultants - Second Edition by Peter Ries(6521)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(6299)