From 5f1912bf21e85e5f2b56d492ecbec937d8061531 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Fri, 12 May 2023 22:22:57 +0800 Subject: [PATCH] add: 230512 --- cpp/2305/LC230512.cpp | 29 +++++++++++++++++++++++++++++ cpp/2305/defs.h | 5 +++++ cpp/2305/main.cpp | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 cpp/2305/LC230512.cpp diff --git a/cpp/2305/LC230512.cpp b/cpp/2305/LC230512.cpp new file mode 100644 index 0000000..334ce79 --- /dev/null +++ b/cpp/2305/LC230512.cpp @@ -0,0 +1,29 @@ +#include +#include + +/** + * 2140. Solving Questions With Brainpower + * + * You are given a 0-indexed 2D integer array questions where questions[i] = [pointsi, brainpoweri]. + * + * The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question 0) and make a decision whether to solve or skip each question. Solving question i will earn you pointsi points but you will be unable to solve each of the next brainpoweri questions. If you skip question i, you get to make the decision on the next question. + * + * For example, given questions = [[3, 2], [4, 3], [4, 4], [2, 5]]: + * If question 0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2. + * If instead, question 0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3. + * Return the maximum points you can earn for the exam. + */ + +class LC230512 { +public: + static long long mostPoints(const std::vector>&) noexcept; +}; + +long long LC230512::mostPoints(const std::vector>& q) noexcept { + const int n = q.size(); + std::vector d(n); + d.back() = q.back().front(); + for (int i = n - 2; i >= 0; --i) + d[i] = std::max(d[i + 1], q[i].front() + (i + q[i].back() + 1 >= n ? 0 : d[i + q[i].back() + 1])); + return d[0]; +} diff --git a/cpp/2305/defs.h b/cpp/2305/defs.h index d15923a..94a3a6c 100644 --- a/cpp/2305/defs.h +++ b/cpp/2305/defs.h @@ -124,4 +124,9 @@ public: static int maxUncrossedLines(CVIR, CVIR) noexcept; }; +class LC230512 { +public: + static long long mostPoints(const std::vector>&) noexcept; +}; + #endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2305/main.cpp b/cpp/2305/main.cpp index 61c2f20..ee2c2f8 100644 --- a/cpp/2305/main.cpp +++ b/cpp/2305/main.cpp @@ -14,6 +14,6 @@ std::ostream& operator<<(std::ostream& os, const std::vector& x) noexcept { } int main() { - std::cout << LC230511::maxUncrossedLines({1,3,7,1,7,5},{1,9,2,5,1}); + std::cout << LC230512::mostPoints({{3,2}, {4,3}, {4,4}, {2,5}}); return 0; }