当前位置:网站首页>动态规划入门一,队列的bfs(70.121.279.200)
动态规划入门一,队列的bfs(70.121.279.200)
2022-07-02 12:13:00 【-小小白-】
什么是动态规划
动态规划就是利用历史数据避免重复计算,历史数据用一维数组或二维数组保存。
利用动态规划解题最基本需要经历两个步骤:1.先定义数组并明确数组的意义,给数组赋初值,2.再找到各个元素之间联系(即动态转移方程)最后得出结果。
例题
接下来用三道例题入门动态规划。
1.爬楼梯

1.定义数组dp[ n ],dp[ n ]的值表示走到n阶有几种方法。
2.赋初值,显然dp[ 1 ] = 1, dp[ 2 ] = 2.
3.寻找元素间的联系(即动态转移方程),容易得出n>=3之后有dp [ n ] = dp[ n - 1 ] + dp[ n - 2 ];因为走到n阶可以由n - 1阶一次走一个台阶到达,也可以是由n - 2阶一次走两个台阶到达。
int climbStairs(int n){
if(n == 1)return 1;
if(n == 2)return 2;
int dp[n + 1];
dp[1] = 1;
dp[2] = 2;
for(int i = 3; i <= n; i++){
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}2.买卖股票的最佳时机

此题更适合一次遍历数组,用两个变量记录历史数据变化,即用变量min记录截止到第n天,股票的最低售价。再用gap记录用第n天的价格卖出股票,能得到的利润,用tgap记录利润的最大值。给min,gap,tgap赋初值0,一次遍历完数组后tgap存储的数据即为最大利润。
#define max(a, b) a>b ? a : b
#define min(a, b) a<b ? a : b
int maxProfit(int* prices, int pricesSize){
int m = prices[0], gap = 0, tgap = 0;
for(int i = 0; i < pricesSize; i++){
m = min(m, prices[i]);
if(prices[i] > m){
gap = prices[i] - m;
tgap = max(gap, tgap);
}
}
return tgap;
}3.完全平方数

1.定义数组dp[ n ], dp[ n ]的值表示和为n的完全平方数的最少数量。
2.赋初值,让dp[ n ] = n(最坏情况,因子为n个1)。
3.寻找元素间的联系(动态转移方程),观察可得,两个连续数据之间存在最优联系,例如dp[ 5 ] = 2,那么dp[ 6 ]就可以理解为dp[ 5 ]+dp[ 1 ] = 2 + 1 = 4。说明n的最优解与前面的数据有关联。两个解之间的联系为 j*j,枚举j = 1;j * j < i; j++;有dp[ i ] = min(dp[ i ], dp[ i - j * j ] + 1);
#define min(a, b) a<b ? a : b
int numSquares(int n){
int dp[n + 1];
for(int i = 0; i <= n; i++){
dp[i] = i;
for(int j = 1; j * j <= i; j++){
dp[i] = min(dp[i], dp[i - j * j] + 1);
}
}
return dp[n];
}队列的bfs
岛屿数量

-解题方法:沉没法,每发现一片陆地就把其整片沉没(变成0)。
-具体思路:遍历二维数组,发现1后进入bfs,在函数内把整片陆地沉没。
-技巧:1.创建方向数组dir[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1}}发现1时,for循环四次判断该块陆地的上下左右有无陆地,有则入列。
2.使用循环队列,front指针在前rear指针在后,对于每个front,判断其上下左右有无陆地,有则沉没该块陆地然后将这个位置入队(每次循环rear可能前移0~3位),入队完成后front前移一位,重复上述步骤直至front == rear整片陆地。
int dir[4][2] = {
{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
typedef struct{
int x;
int y;
}node;
#define MAX 305
bool inside(int i, int j, int row, int col){
return (i >= 0)&&(j >= 0)&&(i < row)&&(j < col);
}
void bfs(char** grid, int i, int j, int gridSize, int colSize){
node* queue[MAX + 1];
node* cur = (node*)malloc(sizeof(node));
cur->x = i;
cur->y = j;
int front = 0, rear = 0;
queue[rear++] = cur;
while(front != rear){
cur = queue[front];
front = (front + 1) % MAX;
for(int k = 0; k < 4; k++){
int ox = cur->x + dir[k][0];
int oy = cur->y + dir[k][1];
if(inside(ox, oy, gridSize, colSize) && grid[ox][oy] == '1'){
grid[ox][oy] = '0';
node* next = (node*)malloc(sizeof(node));
next->x = ox;
next->y = oy;
queue[rear] = next;
rear = (rear + 1) % MAX;
}
}
}
}
int numIslands(char** grid, int gridSize, int* gridColSize){
int colSize = gridColSize[0], ans = 0;
for(int i = 0; i < gridSize; i++){
for(int j = 0; j < colSize; j++){
if(grid[i][j] == '1'){
ans++;
bfs(grid, i, j, gridSize, colSize);
}
}
}
return ans;
}边栏推荐
- 【LeetCode】486-预测赢家
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- Custom exception
- 【LeetCode】877-石子游戏
- Yolo format data set processing (XML to txt)
- 语义分割学习笔记(一)
- 【Salesforce】如何确认你的Salesforce版本?
- 已知兩種遍曆序列構造二叉樹
- 工程师评测 | RK3568开发板上手测试
- [development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
猜你喜欢

Beijing rental data analysis

Download blender on Alibaba cloud image station

Case introduction and problem analysis of microservice

Leetcode skimming -- sum of two integers 371 medium
![[network security] network asset collection](/img/3e/6665b5af0dedfcbc7bd548cc486878.png)
[network security] network asset collection

Redux——详解

Pytoch saves tensor to Mat file

How to find a sense of career direction

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
![[salesforce] how to confirm your salesforce version?](/img/ce/4c844b1b686397faa1b6aa3d57e034.png)
[salesforce] how to confirm your salesforce version?
随机推荐
[leetcode] 977 square of ordered array
[leetcode] 200 number of islands
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
MD5 encryption
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
【LeetCode】417-太平洋大西洋水流问题
【LeetCode】1254-统计封闭岛屿的数量
Markdown tutorial
NBA player analysis
[leetcode] 189 rotation array
(4) Flink's table API and SQL table schema
Force deduction solution summarizes the lucky numbers in 1380 matrix
Beijing rental data analysis
Folium, diagnosis and close contact trajectory above
Steps for Navicat to create a new database
6.12 critical moment of Unified Process Platform
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
[leetcode] 1140 stone game II