Here is a summary of the problem:
Problems/input-1.txt |
Problems/input-2.txt |
Problems/input-3.txt |
I have 100 more problems in Problems/??.txt. That's what tests.txt runs.
As always, the challenge is to spot the recursion. This is a pretty typical recursion for a DP problem: Call DP(i), where you know that all houses with numbers less then i are painted. Then, in DP(), you try all possible colors, and return the minimum.
It's a little more complex than that though. My recursive procedure was DP(index, target, prev), where:
Let's do example 2.
|
Your memoization cache stores the three variables. I made my a vector of vector of vectors of ints, whose size is O(m * target * n). You can use that as the running time of the program, too.
The other pitfall is to only use the leetcode examples for testing. I know we all want to submit as quickly as possible, but you should test. Basically, your program is going to have a bunch of conditions, and you should concoct an example that tests every single one of those before you submit. I know it's no fun, but that's better than not testing and finding a bug in the condition that you didn't test. It happened to me.
You can of course make this faster by, for example, shrinking houses by condensing groups of consecutive painted houses. I didn't do that, BTW, but it would work. It can also shrink the size of your memoization cache.