当前位置:网站首页>Dynamic planning idea "from getting started to giving up"
Dynamic planning idea "from getting started to giving up"
2022-07-07 01:04:00 【rabbit_ zli】
Definition of dynamic programming
Break up the original problem into several sub problems , At the same time, save the answers to the sub questions , So that each subproblem can be solved only once , Finally get the answer to the original question .
The general process of Dynamic Planning

Example 1: Dynamic programming of one-dimensional space
subject : Find the Fibonacci sequence
- Violent recursive solution
// Use recursion to solve
int fibonacci(int i) {
return i <= 1 : i : fibonacci(i - 1) + fibonacci(i - 2);
}
The time complexity of violent recursion is exponential , We need to use memory search to solve this problem
- Memory search ( Dynamic planning ideas )
// Using the idea of dynamic planning Memory search
int fibonacci(int fib) {
// Define an array Store the N Fibonacci number of items
int[] cache = new int[fib + 1];
// Traverse
for (int i = 0; i < cache.length; i++) {
if (fib <= 1) {
cache[i] = i;
continue;
}
cache[i] = cache[i - 2] + cache[i - 1];
}
return cache[fib];
}
Complex dynamic programming
Complex dynamic programming :
- Dimensions have changed It may be two-dimensional or three-dimensional space ;
- There may be a trade-off optimal substructure in the middle
subject 2: Different paths
Title Description : A robot is in a m x n The top left corner of the grid ( The starting point is marked as “Start” ).
The robot can only move down or right one step at a time . The robot tries to reach the bottom right corner of the grid ( In the figure below, it is marked as “Finish” ).
Ask how many different paths there are in total ?
For the above questions , Because you can only Right or down go , So we can turn it into a sub problem :
Sub problem 1: about A How to get to the lower right corner
Sub problem 2: about B How to get to the lower right corner
So the total walking method is equal to 【A】 The solution of the subproblem +【B】 The solution of the subproblem
- Solution 1 : Use the conventional recursive solution
// Using recursive solutions
int paths(int m, int n) {
// Define a two-dimensional mesh
int[][] table = new int[m][n];
// Call recursive functions
return dfs(table, 0, 0);
}
int dfs(int[][] table, int row, int col) {
// Recursive termination condition
// 1.1 Dealing with boundary values
if (row < 0 || row >= table.length || col < 0 || col >= table[0].length) {
return 0;
}
// 1.2 If you go to your destination Then return to 1
if (row == table.length - 1 && col == table[0].length - 1) {
return 1;
}
// Transform into the solution of the subproblem
return dfs(table, row + 1, col) + dfs(table, row, col + 1);
}
- Solution 2 : Memory search
/** Use the idea of dynamic programming to solve You can find The number of paths in each grid is determined by the total number of paths in the upper grid and the left grid */
int paths(int m, int n) {
// Define a two-dimensional matrix
int[][] table = new table[m][n];
// First initialize the first row and first column
for (int i = 0; i < m; i++) {
table[m][0] = 1;
}
for (int i = 0; i < n; i++) {
table[0][n] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
table[i][j] = table[i - 1][j] + table[i][j - 1];
}
}
return table[m - 1][n - 1];
}
边栏推荐
- Make a simple graphical interface with Tkinter
- Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
- 第四篇,STM32中断控制编程
- Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
- tensorflow 1.14指定gpu运行设置
- [force buckle]41 Missing first positive number
- 用tkinter做一个简单图形界面
- Dell笔记本周期性闪屏故障
- 城联优品入股浩柏国际进军国际资本市场,已完成第一步
- Telerik UI 2022 R2 SP1 Retail-Not Crack
猜你喜欢
![[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error](/img/a5/41d4cbc070d421093323dc189a05cf.png)
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error

Periodic flash screen failure of Dell notebook

Service asynchronous communication

随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP

第五篇,STM32系统定时器和通用定时器编程

Niuke cold training camp 6B (Freund has no green name level)
![[牛客] [NOIP2015]跳石头](/img/9f/b48f3c504e511e79935a481b15045e.png)
[牛客] [NOIP2015]跳石头

Equals() and hashcode()

筑梦数字时代,城链科技战略峰会西安站顺利落幕

学习使用代码生成美观的接口文档!!!
随机推荐
Leetcode (547) - number of provinces
C9 colleges and universities, doctoral students make a statement of nature!
Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
Dell Notebook Periodic Flash Screen Fault
The printf function is realized through the serial port, and the serial port data reception is realized by interrupt
Make a simple graphical interface with Tkinter
Dell笔记本周期性闪屏故障
深度学习框架TF安装
【JokerのZYNQ7020】AXI_ EMC。
第四篇,STM32中断控制编程
Levels - UE5中的暴雨效果
Leetcode(547)——省份数量
Linear algebra of deep learning
随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP
View remote test data and records anytime, anywhere -- ipehub2 and ipemotion app
[yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
做微服务研发工程师的一年来的总结
迈动互联中标北京人寿保险,助推客户提升品牌价值
BFS realizes breadth first traversal of adjacency matrix (with examples)
[C language] dynamic address book