From a45c90f4c0fa54887fe0854ff0899b0d5a9d3455 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Mon, 13 Jun 2022 13:46:39 +0800 Subject: [PATCH] add: 220613 [cpp] --- cpp/2206/220613.cpp | 34 ++++++++++++++++++++++++++++++++++ cpp/2206/CMakeLists.txt | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 cpp/2206/220613.cpp diff --git a/cpp/2206/220613.cpp b/cpp/2206/220613.cpp new file mode 100644 index 0000000..23dd2bc --- /dev/null +++ b/cpp/2206/220613.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +/** + * 120. Triangle + * Given a triangle array, return the minimum path sum from top to bottom. + * For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row. + */ + +class Solution { +public: + static int minimumTotal(std::vector>& triangle) { + const int m = triangle.size(); + for (int i = 1; i < m; ++i) { + auto& t = triangle[i]; + const auto& p = triangle[i - 1]; + const int n = t.size(); + t[0] += p[0]; + for (int j = 1; j < n - 1; ++j) { + t[j] += std::min(p[j - 1], p[j]); + } + t[n - 1] += p[n - 2]; + } + return *std::min_element(triangle.back().begin(), triangle.back().end()); + } +}; + +int main() { + std::vector> a {{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}}; + std::cout << Solution::minimumTotal(a); + + return 0; +} diff --git a/cpp/2206/CMakeLists.txt b/cpp/2206/CMakeLists.txt index 09d14d6..c88a883 100644 --- a/cpp/2206/CMakeLists.txt +++ b/cpp/2206/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2206) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2206 220613-CN.cpp) +ADD_EXECUTABLE(2206 220613.cpp)