add: 230327

This commit is contained in:
Eatswap 2023-03-27 23:02:54 +08:00
parent c0825640b4
commit 0fee3db3b8
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 23 additions and 1 deletions

22
cpp/2303/230327.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <vector>
/**
* 64. 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.
*/
class Solution {
public:
static int minPathSum(std::vector<std::vector<int>>& G) {
const int m = G.size(), n = G.front().size();
for (int i = m - 1; i >= 0; --i)
for (int j = n - 1; j >= 0; --j)
G[i][j] +=
(i + 1 < m && j + 1 < n) ?
std::min(G[i + 1][j], G[i][j + 1]) :
((i + 1 < m ? G[i + 1][j] : 0) + (j + 1 < n ? G[i][j + 1] : 0));
return G[0][0];
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2303)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2303 230327-CN.cpp) ADD_EXECUTABLE(2303 230327.cpp)