From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/longest_increasing_subseq.cpp | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 other/longest_increasing_subseq.cpp (limited to 'other/longest_increasing_subseq.cpp') diff --git a/other/longest_increasing_subseq.cpp b/other/longest_increasing_subseq.cpp new file mode 100644 index 0000000..127a984 --- /dev/null +++ b/other/longest_increasing_subseq.cpp @@ -0,0 +1,48 @@ +#include "leetcode.h" + +class maxBit { +public: + vector bit; + maxBit(int size) { bit.resize(size + 1); } + int get(int idx) { + int ans = 0; + for (; idx > 0; idx -= idx & -idx) + ans = max(ans, bit[idx]); + return ans; + } + void update(int idx, int val) { + for (; idx < bit.size(); idx += idx & -idx) + bit[idx] = max(bit[idx], val); + } +}; + +class Solution { +public: + int lengthOisLIS(vector &nums) { + int uniqueNum = compress(nums); + maxBit bit(uniqueNum); + for (int x : nums) { + int subLongest = bit.get(x - 1); + bit.update(x, subLongest + 1); + } + return bit.get(uniqueNum); + } + +private: + int compress(vector &arr) { + vector uniqueSorted(arr); + sort(uniqueSorted.begin(), uniqueSorted.end()); + uniqueSorted.erase(unique(uniqueSorted.begin(), uniqueSorted.end()), + uniqueSorted.end()); + for (int &x : arr) + x = lower_bound(uniqueSorted.begin(), uniqueSorted.end(), x) - + uniqueSorted.begin() + 1; + return uniqueSorted.size(); + } +}; + +int main() { + Solution obj; + vector nums = {10, 9, 2, 5, 3, 7, 101, 18}; + cout << obj.lengthOisLIS(nums); +} -- cgit v1.2.3