-
Notifications
You must be signed in to change notification settings - Fork 8
DP Container
zmjack edited this page Jun 12, 2023
·
1 revision
DpContainer is used for dynamic programming calculations.
Take the Fibonacci function as an example.
int Fib(int n)
{
if (n == 0 || n == 1) return 1;
else return Fib(n - 1) + Fib(n - 2);
}
void Main()
{
Fib(42).Dump();
}
class DpFib : DpContainer<int, int>
{
public override int StateTransfer(int n)
{
if (n == 0 || n == 1) return 1;
else return this[n - 1] + this[n - 2];
}
}
void Main()
{
var fib = new DpFib();
fib[42].Dump();
}
int Fib(DefaultDpContainer<int, int> dp, int n)
{
if (n == 0 || n == 1) return 1;
else return dp[n - 1] + dp[n - 2];
}
void Main()
{
var fib = DpContainer.Create<int, int>(Fib);
fib[42].Dump(); // same as Fib(fib, 42).Dump();
}
The correct answer is 433494437.
And the time required for the above code to get the correct result is roughly as follows:
Name | Elapsed | |
---|---|---|
Recursion Code | about 3.823 seconds. | |
* | DP Code 1 (Using class) | about 0.010 seconds. |
DP Code 2 (Using function) | about 0.011 seconds. |
English | 中文 |
- Array initialization
- Array mapping
- Array reallocation
- Chain loop
- Compose / Pipeline
- Date & Time
- Flat elements
- Index-Value Pairs
- Traverse by depth
- Zip sequences
- [Come soon] Counter
- DP container
- Evaluator
- Fixed size queue
- Scope
- Sequence input stream
- Sliding
- State
- Value with unit
- Variant string