Problem solution · C++

Longest Increasing Subsequence II

Longest Increasing Subsequence II: a C++ solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Segment tree or range structure
Source
walkccc LeetCode Solutions
Length
95 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For Longest Increasing Subsequence II, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 95 lines of C++ from the credited upstream file 2407.cpp.
  • The implementation visibly relies on sequence storage.
  • 1 loop block detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeLongest Increasing Subsequence II · C++C++
Use this to learn the idea, then write your own version.
struct SegmentTreeNode {  int lo;  int hi;  int maxLength;  std::unique_ptr<SegmentTreeNode> left;  std::unique_ptr<SegmentTreeNode> right;  // maxLength := the maximum length of LIS ending in [lo..hi]  SegmentTreeNode(int lo, int hi, int maxLength,                  std::unique_ptr<SegmentTreeNode> left = nullptr,                  std::unique_ptr<SegmentTreeNode> right = nullptr)      : lo(lo),        hi(hi),        maxLength(maxLength),        left(std::move(left)),        right(std::move(right)) {}}; class SegmentTree { public:  explicit SegmentTree() : root(make_unique<SegmentTreeNode>(0, 1e5 + 1, 0)) {}   void updateRange(int i, int j, int maxLength) {    update(root, i, j, maxLength);  }   // Returns the maximum length of LIS ending in [i..j].  int queryRange(int i, int j) {    return query(root, i, j);  }  private:  std::unique_ptr<SegmentTreeNode> root;   void update(std::unique_ptr<SegmentTreeNode>& root, int i, int j,              int maxLength) {    if (root->lo == i && root->hi == j) {      root->maxLength = maxLength;      root->left = nullptr;      root->right = nullptr;      return;    }    const int mid = root->lo + (root->hi - root->lo) / 2;    if (root->left == nullptr) {      root->left = make_unique<SegmentTreeNode>(root->lo, mid, root->maxLength);      root->right =          make_unique<SegmentTreeNode>(mid + 1, root->hi, root->maxLength);    }    if (j <= mid)      update(root->left, i, j, maxLength);    else if (i > mid)      update(root->right, i, j, maxLength);    else {      update(root->left, i, mid, maxLength);      update(root->right, mid + 1, j, maxLength);    }    root->maxLength = merge(root->left->maxLength, root->right->maxLength);  }   int query(std::unique_ptr<SegmentTreeNode>& root, int i, int j) {    if (root->left == nullptr)      return root->maxLength;    if (root->lo == i && root->hi == j)      return root->maxLength;    const int mid = root->lo + (root->hi - root->lo) / 2;    if (j <= mid)      return query(root->left, i, j);    if (i > mid)      return query(root->right, i, j);    return merge(query(root->left, i, mid), query(root->right, mid + 1, j));  }   int merge(int left, int right) const {    return max(left, right);  };}; class Solution { public:  int lengthOfLIS(vector<int>& nums, int k) {    int ans = 1;    SegmentTree tree;     for (const int num : nums) {      const int left = max(1, num - k);      const int right = num - 1;      // the maximum length of LIS ending in [left..right] + the current number      const int maxLength = tree.queryRange(left, right) + 1;      ans = max(ans, maxLength);      tree.updateRange(num, num, maxLength);    }     return ans;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗