<p>Given an integer array <code>nums</code>, return <em>the length of the longest <strong>strictly increasing </strong></em><span data-keyword="subsequence-array"><em><strong>subsequence</strong></em></span>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [10,9,2,5,3,7,101,18]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,0,3,2,3]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [7,7,7,7,7,7,7]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2500</code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><b>Follow up:</b>&nbsp;Can you come up with an algorithm that runs in&nbsp;<code>O(n log(n))</code> time complexity?</p>

const int maxN = 100000 + 1;

class Solution {
public:
    class SegmentTree {
        public:
        vector<int> seg;
        
        SegmentTree() {
            seg = vector<int>(maxN * 4 + 10, 0);
        }
        
        int query(int l, int r) {
            return query_util(0, 0, maxN - 1, l, r);   
        }
        
        int query_util(int i, int low, int high, int l, int r) {
            if(low >= l && high <= r)
                return seg[i];
            if(low > r || high < l)
                return 0;
            int mid = (low + high) / 2;
            int left = query_util(2 * i + 1, low, mid, l, r);
            int right = query_util(2 * i + 2, mid + 1, high, l, r);
            return max(left, right);
        }
        
        void update(int len, int ele) {
            update_util(0, 0, maxN - 1, ele, len);
        }
        
        void update_util(int i, int low, int high, int ele, int len) {
            if(high == low) {
                seg[i] = len;
                return;
            }
            int mid = (low + high) / 2;
            if(low <= ele && ele <= mid) {
                update_util(2 * i + 1, low, mid, ele, len);
            } else {
                update_util(2 * i + 2, mid + 1, high, ele, len);
            }
            seg[i] = max(seg[2 * i + 1], seg[2 * i + 2]);
        }
    };
    
    int lengthOfLIS(vector<int>& nums) {
        int n = nums.size();
        SegmentTree seg;
        // Offsetting, the minimum element can be -10000
        for(int i = 0; i < n; i++)
            nums[i] += 10000;
        for(auto it : nums) {
            int longestSequenceBefore = 1 + seg.query(0, it - 1);
            seg.update(longestSequenceBefore, it);
        }
        return seg.query(0, maxN - 1);
    }
};
85065