diff --git a/cpp/2303/230327.cpp b/cpp/2303/230327.cpp new file mode 100644 index 0000000..357eb68 --- /dev/null +++ b/cpp/2303/230327.cpp @@ -0,0 +1,22 @@ +#include + +/** + * 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>& 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]; + } +}; diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index 645f57d..f81fef4 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230327-CN.cpp) +ADD_EXECUTABLE(2303 230327.cpp)