From e7dba79fa84863d2b092ffbec1e1cb32fce99e2a Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sun, 6 Mar 2022 01:09:56 +0800 Subject: [PATCH] add: 220305 [cpp] --- cpp/2203/220305.cpp | 32 ++++++++++++++++++++++++++++++++ cpp/2203/CMakeLists.txt | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 cpp/2203/220305.cpp diff --git a/cpp/2203/220305.cpp b/cpp/2203/220305.cpp new file mode 100644 index 0000000..275c0e9 --- /dev/null +++ b/cpp/2203/220305.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +/** + * 740. Delete and Earn + * You are given an integer array nums. You want to maximize the number of points you get by performing the following operation any number of times: + * Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every element equal to nums[i] - 1 and every element equal to nums[i] + 1. + * Return the maximum number of points you can earn by applying the above operation some number of times. + */ + +class Solution { +private: + inline static const int maxV = 10002; +public: + static int deleteAndEarn(const std::vector& nums) { + int cnt[maxV]{}, val[maxV]{}, ans[maxV]{}; + for (int i : nums) + ++cnt[i]; + for (int i = 2; i < maxV; ++i) + val[i] = cnt[i] * i; + ans[1] = val[1] = cnt[1]; + for (int i = 2; i < maxV; ++i) + ans[i] = std::max(ans[i - 1], val[i] + ans[i - 2]); + return ans[maxV - 1]; + } +}; + +int main() { + std::cout << Solution::deleteAndEarn({1,1,1,2,4,5,5,5,6}); + return 0; +} diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index 5fd9410..a4b8bd5 100644 --- a/cpp/2203/CMakeLists.txt +++ b/cpp/2203/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2203) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2203 220305-CN.cpp) +ADD_EXECUTABLE(2203 220305.cpp)