diff --git a/cpp/2206/220612.cpp b/cpp/2206/220612.cpp new file mode 100644 index 0000000..39f2d1a --- /dev/null +++ b/cpp/2206/220612.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +/** + * 1695. Maximum Erasure Value + * You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements. + * Return the maximum score you can get by erasing exactly one subarray. + * An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r). + */ + +class Solution { +public: + static int maximumUniqueSubarray(const std::vector& nums) { + std::unordered_set s; + const int n = nums.size(); + int sum = 0, ret = 0; + for (int i = 0, l = 0; i < n; ++i) { + std::printf("In loop, i = %d, l = %d\nStart inserting: ", i, l); + for (; i < n && !s.count(nums[i]); ++i) { + sum += nums[i]; + s.insert(nums[i]); + std::printf("%d ", nums[i]); + } + ret = std::max(ret, sum); + + if (i == n) + break; + + std::printf("\nEnd inserting at position %d, nums[i] = %d, updating ret <- %d\nStart popping: ", i, nums[i], ret); + + // pop until reach nums[i] + for (; nums[l] != nums[i]; ++l) { + s.erase(nums[l]); + sum -= nums[l]; + std::printf("%d ", nums[l]); + } + ++l; // where nums[l] == nums[i] + std::printf("\nEnd loop, where i = %d, l = %d\n\n", i, l); + } + + return ret; + } +}; + +int main() { + std::printf("%d\n", Solution::maximumUniqueSubarray({ + 187 + })); + + return 0; +} diff --git a/cpp/2206/CMakeLists.txt b/cpp/2206/CMakeLists.txt index 0990e20..9e8f7b2 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 220612-CN.cpp) +ADD_EXECUTABLE(2206 220612.cpp)