diff --git a/cpp/2305/LC230507.cpp b/cpp/2305/LC230507.cpp new file mode 100644 index 0000000..7a31c8a --- /dev/null +++ b/cpp/2305/LC230507.cpp @@ -0,0 +1,89 @@ +#include +#include + +/** + * 1964. Find the Longest Valid Obstacle Course at Each Position + * + * You want to build some obstacle courses. You are given a 0-indexed integer array obstacles of length n, where obstacles[i] describes the height of the ith obstacle. + * + * For every index i between 0 and n - 1 (inclusive), find the length of the longest obstacle course in obstacles such that: + * + * You choose any number of obstacles between 0 and i inclusive. + * You must include the ith obstacle in the course. + * You must put the chosen obstacles in the same order as they appear in obstacles. + * Every obstacle (except the first) is taller than or the same height as the obstacle immediately before it. + * Return an array ans of length n, where ans[i] is the length of the longest obstacle course for index i as described above. + */ + +class LC230507 { +public: + static std::vector longestObstacleCourseAtEachPosition(const std::vector&) noexcept; +}; + +constexpr inline int tree_size(int N) noexcept { + int MSB = 0; + for (; N > 1; N >>= 1) + ++MSB; + return 1 << (MSB + 2); +} + +template +class MaxSegTree final { +private: + int tree[tree_size(N) + 5]{}; + + int update_interval(int, int, int, int, int) noexcept; + + [[nodiscard]] int query_interval(int, int, int, int, int) const noexcept; + +public: + void update(int, int) noexcept; + + [[nodiscard]] int query(int, int) const noexcept; +}; + +template +int MaxSegTree::query_interval(int L, int R, int index, int r_L, int r_R) const noexcept { + if (L == r_L && R == r_R) + return tree[index]; + const int r_M = (r_L + r_R) >> 1; + if (R <= r_M) + return query_interval(L, R, index << 1, r_L, r_M); + if (r_M < L) + return query_interval(L, R, index << 1 | 1, r_M + 1, r_R); + return std::max(query_interval(L, r_M, index << 1, r_L, r_M), query_interval(r_M + 1, R, index << 1 | 1, r_M + 1, r_R)); +} + +template +int MaxSegTree::update_interval(int L, int R, int index, int val, int cur_index) noexcept { + if (L == R && L == index) + return tree[cur_index] = val; + const int M = (L + R) >> 1; + if (index <= M) + return tree[cur_index] = std::max(tree[cur_index << 1 | 1], update_interval(L, M, index, val, cur_index << 1)); + return tree[cur_index] = std::max(tree[cur_index << 1], update_interval(M + 1, R, index, val, cur_index << 1 | 1)); +} + +template +int MaxSegTree::query(int L, int R) const noexcept { + return query_interval(L, R, 1, 0, N - 1); +} + +template +void MaxSegTree::update(int index, int val) noexcept { + update_interval(0, N - 1, index, val, 1); +} + +std::vector LC230507::longestObstacleCourseAtEachPosition(const std::vector& obstacles) noexcept { + const int n = obstacles.size(); + std::vector ret(n), tmp; + for (int i = 0; i < n; ++i) { + int idx = std::upper_bound(tmp.begin(), tmp.end(), obstacles[i]) - tmp.begin(); + if (tmp.size() == idx) + tmp.push_back(obstacles[i]); + else + tmp[idx] = obstacles[i]; + ret[i] = idx + 1; + } + return ret; +} diff --git a/cpp/2305/defs.h b/cpp/2305/defs.h index 841d4ab..60deb48 100644 --- a/cpp/2305/defs.h +++ b/cpp/2305/defs.h @@ -70,4 +70,9 @@ public: static int minNumberOfFrogs(const std::string&) noexcept; }; +class LC230507 { +public: + static std::vector longestObstacleCourseAtEachPosition(const std::vector&) noexcept; +}; + #endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2305/main.cpp b/cpp/2305/main.cpp index 4ec3ca9..14ef021 100644 --- a/cpp/2305/main.cpp +++ b/cpp/2305/main.cpp @@ -12,6 +12,6 @@ std::ostream& operator<<(std::ostream& os, const std::vector& x) noexcept { } int main() { - std::cout << LC230506CN::minNumberOfFrogs("crcoakroak"); + std::cout << LC230507::longestObstacleCourseAtEachPosition({110,2,35,160,121,7,138,7,112,170,42,197,5,35,115,61,149,81,198,82,42,56,36,199,6,74,151,50,13,116,89,23,81,112,19,199,15,100,128,129,22,165,145,12,170,123,74,6,125,114,94,26,176,32,173,37,131,17,29,8,123,53,15,22,160,29,2,158,75,136,104,3,14,126,134,6,183,131,72,183,71,183,157,57,71,24,200,117,109,184,151,84,87,167,57,79,186,122,140,35,164,28,64,123,78,188,15,178,84,165,92,156,77,45,176,167,3,176,107,87,28,31,173,134,43,128,143,109,84,63,123,156,154,157,171,50,94,2,167,9,159,135,55,177,122,142,149,194,83,37,198,148,66,90,41,64,198,142,92,178,4,152,118,182,91,135,29,197,185,84,124,134,100,49,124,140,131,156,165,194,1,74,83,58,52,101,91,28,183,93,31,144,107,133,110,83,43,16,119,146,35,57,13,91,125,141,149,105,175,47,38,96,94,95,169,55,156,11,70,36,52,103,64,164,77,2,40,126,173,155,164,120,125,38,178,49,27,53,63,128,27,140,110,13,100,181,38,8,88,140,12,103,173,155,53,169,137,1,106,1,132,169,17,42,55,17,135,151,93,164,22,110,144,200,86,13,94,198,177,142,97,2,154,57,188,130,150,83,63,128,22,179,75,21,173,32,185,197,29,25,107,87,158,111,91,137,155,175,80,120,40,88,19,160,74,199,65,14,133,106,76,28,162,192,2,71,200,140,174,82,181,79,117,101,146,146,65,118,35,125,105,84,137,196,73,119,4,51,61,111,66,37,93,85,83,48,148,147,46,81,8,134,73,3,116,35,64,103,109,20,199,119,143,161,35,84,143,34,168,54,178,150,43,144,85,118,38,97,39,101,68,47,139,85,200,109,45,96,185,47,156,183,167,48,91,169,6,108,147,183,145,77,133,111,143,160,31,127,17,186,172,89,104,182,107,157,130,86,182,42,121,113,38,13,138,69,194,195,105,81,107,152,115,107,166,43,75,154,180,115,18,195,22,62,122,144,200,142,137,196,153,90,199,85,9,51,143,46,174,26,103,167,140,116,35,113,162,86,48,120,100,102,197,177,186,11,13,138,45,91,66,2,3,53,33,148,15,8,117,97,93,9,199,24,161,184,140,78,111,90,110,84,193,142,48,106,114,69,127,38,69,79,180,56,8,81,79,28,12,185,50,9,69,59,41,36,92,129,24,100,64,14,57,75,62,22,3,52,67,105,180,143,137,47,53,151,116,100,162,86,139,39,148,26,50,48,116,178,8,141,171,56,28,28,166,177,122,114,195,3,34,161,132,98,146,44,194,87,184,76,62,9,178,33,94,165,122,100,20,171,138,197,108,48,64,74,64,108,60,20,104,173,69,81,31,12,2,146,62,27,170,181,123,12,4,28,153,6,5,126,191,32,140,28,175,155,176,90,112,137,38,108,106,195,84,67,10,3,123,173,72,130,191,2,39,112,42,190,81,167,60,196,90,20,110,57,199,114,162,130,108,190,9,25,27,11,165,170,49,93,171,169,180,108,177,118,128,122,29,48,70,86,138,29,34,39,79,146,88,6,38,105,42,35,60,6,87,199,25,150,101,93,194,139,126,193,85,59,60,50,180,155,1,186,183,120,92,177,155,124,136,98,122,61,173,180,67,114,62,135,41,4,54,149,100,61,18,95,46,70,53,40,58,97,85,116,34,1,6,82,120,170,26,116,159,45,185,122,93,92,54,101,33,21,175,94,133,61,38,162,172,80,56,175,197,167,123,65,196,85,131,107,116,31,41,185,114,9,24,166,123,136,176,16,96,58,19,90,61,110,194,193,57,109,29,82,111,13,137,112,20,71,178,92,136,199,101,158,101,111,49,73,15,166,59,72,22,136,8,126,137,177,44,43,17,66,96,66,86,61,89,72,102,167,11,193,132,141,59,145,54,127,156,25,100,35,36,173,52,23,9,92,14,162,185,146,86,1,181,125,152,153,52,150,57,12,14,166,128,151,151,121,125,110,145,150,143,133,152,75,152,175,87,186,42,90,141,139,161,68,154,78,68,58,140,8,50,83,172,150,183,42,57,122,52,97,139,2,190,114,111,198,155,29,135,88,183,76,18,157,121,158,95,36,171,54,142,150,25,76,71,8,108,115,165,155,168,25,170,181,135,195,191,30,55,122,128,159,196,197,153,88,194,131,111,178,64,10,146,142,113,30,153,188,199,138,157,141,7,63,199,71,94,158,136,141,182,99,168,65}); return 0; }