Longest Increasing Subsequence (C# and Python3)

Given an integer array nums, return the length of the longest strictly increasing subsequence.

subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].

Example 1:

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

Solution

Initialize an array with the same nums.Length, where all indexes contains the value 1.

Using two pointers i and j, for each num, we look behind, every time we find that the current is greater than the one behind we increment the value of the current.

C#

Python3

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

Open chat
Powered by