日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

[leetcode] Minimum Path Sum

發(fā)布時間:2023/11/27 生活经验 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [leetcode] Minimum Path Sum 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Minimum Path Sum

Given a?m?x?n?grid filled with non-negative numbers, find a path from top left to bottom right which?minimizes?the sum of all numbers along its path.

Note:?You can only move either down or right at any point in time. 分析:動態(tài)規(guī)劃: 狀態(tài)轉移公式為:ret[i][j] = min(ret[i-1][j], ret[i][j-1]) + grid[i][j]; 對于矩陣
123
456
789
它所對應的ret矩陣為:
11+21+2+3
1+41+2+51+2+3+6
1+4+71+2+5+81+2+3+6+9
=
136
5812
121621
代碼如下:
 1 class Solution
 2 {
 3 public:
 4   int minPathSum(vector<vector<int> > &grid)
 5   {
 6     if(grid.size() == 0)
 7       return 0;
 8 
 9     vector<vector<int> > ret(grid);
10 
11     for(int i=1; i<grid.size(); i++)
12       ret[i][0] += ret[i-1][0];
13 
14     for(int j=1; j<grid[0].size(); j++)
15       ret[0][j] += ret[0][j-1];
16 
17     for(int i=1; i<grid.size(); i++)
18       for(int j=1; j<grid[i].size(); j++)
19         ret[i][j] = min(ret[i][j-1], ret[i-1][j]) + grid[i][j];
20 
21     return ret[grid.size()-1][grid[0].size()-1];
22   }
23 };

?

轉載于:https://www.cnblogs.com/lxd2502/p/4371061.html

總結

以上是生活随笔為你收集整理的[leetcode] Minimum Path Sum的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。