diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/longest_valid_course.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/longest_valid_course.cpp')
-rw-r--r-- | other/longest_valid_course.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/other/longest_valid_course.cpp b/other/longest_valid_course.cpp new file mode 100644 index 0000000..87f07a6 --- /dev/null +++ b/other/longest_valid_course.cpp @@ -0,0 +1,49 @@ +// 1964. Find the Longest Valid Obstacle Course at Each Position +#include "leetcode.h" + +/* + * you want to build obstacle courses. you are given a 0-indexed integer array + * 'obstacles' of length 'n' where 'obstacles[i]' describes the height of the + * i'th 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 i'th 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 aboveeeeeeeee + */ + +class Solution { +public: + vector<int> longestObstacleCourseAtEachEachPosition(vector<int> &obstacles) { + vector<int> n; + for (int i = 0; i < obstacles.size(); ++i) { + auto it = upper_bound(n.begin(), n.end(), obstacles[i]); + if (it == n.end()) { + n.push_back(obstacles[i]); + it = prev(n.end()); + } else { + *it = obstacles[i]; + } + obstacles[i] = it - n.begin() + 1; + } + return obstacles; + } +}; + +int main() { + Solution obj; + vector<int> o1 = {1, 2, 3, 2}, o2 = {2, 2, 1}, o3 = {3, 1, 5, 6, 4, 2}; + for (auto i : obj.longestObstacleCourseAtEachEachPosition(o1)) + printf("%d ", i); // expect: 1 2 3 3 + printf("\n"); + for (auto i : obj.longestObstacleCourseAtEachEachPosition(o2)) + printf("%d ", i); // expect: 1 2 1 + printf("\n"); + for (auto i : obj.longestObstacleCourseAtEachEachPosition(o3)) + printf("%d ", i); // expect: 1 1 2 3 2 2 + printf("\n"); +} |