← Meta (Facebook) bank

Meta (Facebook) · Question 69 · Dynamic Programming

Longest Increasing Subsequence

Dynamic ProgrammingPythoncode⏱ O(nlogn)💾 O(n)
Product-based

Problem

You are given array of integers. Find length of longest strictly increasing subsequence.

Approach

Patience sorting: binary-search each number into tail piles.

Time O(nlogn)Space O(n)

How it works

  1. 1Keep smallest tails per subsequence length.
  2. 2Bisect each number into place; piles count is the answer.

Sample input / output

Input:
8
10 9 2 5 3 7 101 18
Output:
4

Constraints

1 <= nums.length <= 2500.

Solution — read it, then walk it

Time O(nlogn)Space O(n)Loops 1Branches 3Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
yesno↻ repeatyesnoSTARTfrom bisect importbisect_leftDEFINE lengthOfLIS(nums)dp = []for num in numspos = bisect_left(dp, num)if pos == len(dp)dp.append(num)ELSEdp[pos] = numRETURN len(dp)DEFINE main()import sysdata =sys.stdin.read().split()it = iter(data)n = int(next(it))nums = [int(next(it)) for _in range(n)]print(lengthOfLIS(nums))if __name__ =="__main__"main()END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/20

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits