From ba5d3bb40935abf2553e3089a8dcb5e3e6d7f8ce Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sat, 25 Mar 2023 15:59:20 +0800 Subject: [PATCH] add: 230325-CN --- cpp/2303/230325-CN.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 cpp/2303/230325-CN.cpp diff --git a/cpp/2303/230325-CN.cpp b/cpp/2303/230325-CN.cpp new file mode 100644 index 0000000..e3727d7 --- /dev/null +++ b/cpp/2303/230325-CN.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +int findLengthOfShortestSUBSEQUENCE(const std::vector& arr) { + const int n = arr.size(); + std::map m; + std::vector dp(n); + m[arr[0]] = dp[0] = 1; + int ans = 1; + for (int i = 1; i < n; ++i) { + auto it = m.upper_bound(arr[i]); + if (it == m.begin()) + ans = std::max(ans, m[arr[i]] = dp[i] = 1); + else + ans = std::max(ans, m[arr[i]] = dp[i] = 1 + (--it)->second); + } + return n - ans; +} + +/** + * 1574. Shortest Subarray to be Removed to Make Array Sorted + * + * Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. + * Return the length of the shortest subarray to remove. + * A subarray is a contiguous subsequence of the array. + */ + +class Solution { +public: + static int findLengthOfShortestSubarray(const std::vector& arr) { + const int n = arr.size(); + const int* d = arr.data(); + int L = 0, R = n - 1; + for (; L + 1 < n && d[L + 1] >= d[L]; ++L); + for (; R && d[R - 1] <= d[R]; --R); + if (L >= R) + return 0; + int ans = ++L; + for (int i = n - 1; i >= R; --i) + ans = std::max(ans, int(std::upper_bound(d, d + L, d[i]) - d + n - i)); + return n - ans; + } +}; + +int main() { + Solution::findLengthOfShortestSubarray({10,13,17,21,15,15,9,17,22,22,13}); + return 0; +}