{"id":"6b093ddebd4755deb67ee07a","slug":"longest-increasing-subsequence","title":"Longest increasing subsequence","summary":"Pick the longest strictly increasing subsequence from a series of measurements – and return it, not just its length. The classic O(n²) DP, then \"patience sorting\" with binary search in O(n log n).","servedLang":"en","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"hard","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["dynamic-programming","binary-search","patience-sorting","arrays"],"languages":[{"lang":"typescript","runner":"browser","starter":"/** One of the longest strictly increasing subsequences (values in original order). */\nfunction longestIncreasingSubsequence(nums: number[]): number[] {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"function assertLis(nums, result, expectedLength) {\n  expect(result).toHaveLength(expectedLength);\n  for (let k = 1; k < result.length; k++) {\n    if (!(result[k] > result[k - 1])) throw new Error(`not strictly increasing at position ${k}: ${result[k - 1]}, ${result[k]}`);\n  }\n  let j = 0;\n  for (const x of nums) if (j < result.length && x === result[j]) j++;\n  if (j !== result.length) throw new Error('the result is not a subsequence of the input');\n}\n\ntest('classic examples', () => {\n  const a = [10, 9, 2, 5, 3, 7, 101, 18];\n  assertLis(a, longestIncreasingSubsequence(a), 4);\n  const b = [0, 1, 0, 3, 2, 3];\n  assertLis(b, longestIncreasingSubsequence(b), 4);\n});\n\ntest('strictly increasing: equal values do not count', () => {\n  expect(longestIncreasingSubsequence([7, 7, 7, 7])).toEqual([7]);\n  const c = [1, 3, 3, 3, 2, 4];\n  assertLis(c, longestIncreasingSubsequence(c), 3);\n});\n\ntest('empty and single-element input', () => {\n  expect(longestIncreasingSubsequence([])).toEqual([]);\n  expect(longestIncreasingSubsequence([42])).toEqual([42]);\n});\n\ntest('monotonic input', () => {\n  expect(longestIncreasingSubsequence([1, 2, 3, 4, 5])).toEqual([1, 2, 3, 4, 5]);\n  expect(longestIncreasingSubsequence([5, 4, 3, 2, 1])).toHaveLength(1);\n});\n\ntest('the tails array alone is not the answer', () => {\n  expect(longestIncreasingSubsequence([3, 4, 1])).toEqual([3, 4]);\n  const d = [3, 10, 2, 1, 20, 4, 6, 7, 8];\n  assertLis(d, longestIncreasingSubsequence(d), 5);\n});\n\ntest('negative and extreme values', () => {\n  const e = [-3, -1, -2, 0];\n  assertLis(e, longestIncreasingSubsequence(e), 3);\n  expect(longestIncreasingSubsequence([2147483647, -2147483648, 0])).toEqual([-2147483648, 0]);\n});\n\ntest('performance: 100 000 elements, twice', () => {\n  const n = 100000;\n  const a = Array.from({ length: n }, (_, i) => (i * 7919) % 100003);\n  assertLis(a, longestIncreasingSubsequence(a), 306);\n  const b = Array.from({ length: n }, (_, i) => (i * i * 31 + i * 17) % 1000003);\n  assertLis(b, longestIncreasingSubsequence(b), 732);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * One of the longest strictly increasing subsequences (values in original order).\n * @param {number[]} nums\n * @returns {number[]}\n */\nfunction longestIncreasingSubsequence(nums) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":7,"tests":"function assertLis(nums, result, expectedLength) {\n  expect(result).toHaveLength(expectedLength);\n  for (let k = 1; k < result.length; k++) {\n    if (!(result[k] > result[k - 1])) throw new Error(`not strictly increasing at position ${k}: ${result[k - 1]}, ${result[k]}`);\n  }\n  let j = 0;\n  for (const x of nums) if (j < result.length && x === result[j]) j++;\n  if (j !== result.length) throw new Error('the result is not a subsequence of the input');\n}\n\ntest('classic examples', () => {\n  const a = [10, 9, 2, 5, 3, 7, 101, 18];\n  assertLis(a, longestIncreasingSubsequence(a), 4);\n  const b = [0, 1, 0, 3, 2, 3];\n  assertLis(b, longestIncreasingSubsequence(b), 4);\n});\n\ntest('strictly increasing: equal values do not count', () => {\n  expect(longestIncreasingSubsequence([7, 7, 7, 7])).toEqual([7]);\n  const c = [1, 3, 3, 3, 2, 4];\n  assertLis(c, longestIncreasingSubsequence(c), 3);\n});\n\ntest('empty and single-element input', () => {\n  expect(longestIncreasingSubsequence([])).toEqual([]);\n  expect(longestIncreasingSubsequence([42])).toEqual([42]);\n});\n\ntest('monotonic input', () => {\n  expect(longestIncreasingSubsequence([1, 2, 3, 4, 5])).toEqual([1, 2, 3, 4, 5]);\n  expect(longestIncreasingSubsequence([5, 4, 3, 2, 1])).toHaveLength(1);\n});\n\ntest('the tails array alone is not the answer', () => {\n  expect(longestIncreasingSubsequence([3, 4, 1])).toEqual([3, 4]);\n  const d = [3, 10, 2, 1, 20, 4, 6, 7, 8];\n  assertLis(d, longestIncreasingSubsequence(d), 5);\n});\n\ntest('negative and extreme values', () => {\n  const e = [-3, -1, -2, 0];\n  assertLis(e, longestIncreasingSubsequence(e), 3);\n  expect(longestIncreasingSubsequence([2147483647, -2147483648, 0])).toEqual([-2147483648, 0]);\n});\n\ntest('performance: 100 000 elements, twice', () => {\n  const n = 100000;\n  const a = Array.from({ length: n }, (_, i) => (i * 7919) % 100003);\n  assertLis(a, longestIncreasingSubsequence(a), 306);\n  const b = Array.from({ length: n }, (_, i) => (i * i * 31 + i * 17) % 1000003);\n  assertLis(b, longestIncreasingSubsequence(b), 732);\n});\n"},{"lang":"python","runner":"browser","starter":"def longest_increasing_subsequence(nums: list[int]) -> list[int]:\n    \"\"\"One of the longest strictly increasing subsequences (values in original order).\"\"\"\n    raise NotImplementedError\n","testCount":7,"tests":"def assert_lis(nums, result, expected_length):\n    assert len(result) == expected_length, f\"expected length {expected_length}, got {len(result)}: {result[:10]}\"\n    for k in range(1, len(result)):\n        assert result[k] > result[k - 1], f\"not strictly increasing at position {k}: {result[k - 1]}, {result[k]}\"\n    j = 0\n    for x in nums:\n        if j < len(result) and x == result[j]:\n            j += 1\n    assert j == len(result), \"the result is not a subsequence of the input\"\n\n\ndef test_classic_examples():\n    a = [10, 9, 2, 5, 3, 7, 101, 18]\n    assert_lis(a, longest_increasing_subsequence(a), 4)\n    b = [0, 1, 0, 3, 2, 3]\n    assert_lis(b, longest_increasing_subsequence(b), 4)\n\n\ndef test_strictly_increasing_equal_values_do_not_count():\n    got = longest_increasing_subsequence([7, 7, 7, 7])\n    assert got == [7], f\"expected [7], got {got}\"\n    c = [1, 3, 3, 3, 2, 4]\n    assert_lis(c, longest_increasing_subsequence(c), 3)\n\n\ndef test_empty_and_single_element():\n    assert longest_increasing_subsequence([]) == [], \"empty input\"\n    assert longest_increasing_subsequence([42]) == [42], \"single element\"\n\n\ndef test_monotonic_input():\n    got = longest_increasing_subsequence([1, 2, 3, 4, 5])\n    assert got == [1, 2, 3, 4, 5], f\"increasing input: {got}\"\n    assert len(longest_increasing_subsequence([5, 4, 3, 2, 1])) == 1, \"decreasing input\"\n\n\ndef test_tails_array_alone_is_not_the_answer():\n    got = longest_increasing_subsequence([3, 4, 1])\n    assert got == [3, 4], f\"expected [3, 4], got {got}\"\n    d = [3, 10, 2, 1, 20, 4, 6, 7, 8]\n    assert_lis(d, longest_increasing_subsequence(d), 5)\n\n\ndef test_negative_and_extreme_values():\n    e = [-3, -1, -2, 0]\n    assert_lis(e, longest_increasing_subsequence(e), 3)\n    got = longest_increasing_subsequence([2147483647, -2147483648, 0])\n    assert got == [-2147483648, 0], f\"expected [-2147483648, 0], got {got}\"\n\n\ndef test_performance_100k_twice():\n    n = 100_000\n    a = [(i * 7919) % 100003 for i in range(n)]\n    assert_lis(a, longest_increasing_subsequence(a), 306)\n    b = [(i * i * 31 + i * 17) % 1000003 for i in range(n)]\n    assert_lis(b, longest_increasing_subsequence(b), 732)\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\nusing System.Collections.Generic;\n\npublic static class Solution\n{\n    /// <summary>One of the longest strictly increasing subsequences (values in original order).</summary>\n    public static List<int> LongestIncreasingSubsequence(int[] nums)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":7},{"lang":"java","runner":"judge0","starter":"import java.util.*;\n\nclass Solution {\n    /** One of the longest strictly increasing subsequences (values in original order). */\n    static List<Integer> longestIncreasingSubsequence(int[] nums) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":7},{"lang":"go","runner":"judge0","starter":"package main\n\n// LongestIncreasingSubsequence returns one of the longest strictly increasing subsequences\n// (values in original order).\nfunc LongestIncreasingSubsequence(nums []int) []int {\n\tpanic(\"not implemented\")\n}\n","testCount":7}],"questionCount":null,"xp":40,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:54.492Z","requestedLang":"en","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"An analyst looks at successive performance results of an application (benchmark scores of consecutive builds) and wants to chart the longest story of "},{"type":"text","text":"continuous improvement","marks":[{"type":"bold"}]},{"type":"text","text":": pick as many builds as possible (not necessarily adjacent, but in the original order) whose scores "},{"type":"text","text":"strictly increase","marks":[{"type":"bold"}]},{"type":"text","text":"."}]},{"type":"heading","attrs":{"level":2,"id":"task"},"content":[{"type":"text","text":"Task"}]},{"type":"paragraph","content":[{"type":"text","text":"Return one of the longest strictly increasing subsequences of "},{"type":"text","text":"nums","marks":[{"type":"code"}]},{"type":"text","text":" – as a list of values in order of appearance. If there are several, any of them is correct (the tests check the length, that it increases, and that it is a subsequence of the input). For an empty array – an empty list."}]},{"type":"paragraph","content":[{"type":"text","text":"Signatures: "},{"type":"text","text":"longestIncreasingSubsequence(nums)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"longest_increasing_subsequence(nums)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.LongestIncreasingSubsequence","marks":[{"type":"code"}]},{"type":"text","text":" (C#, "},{"type":"text","text":"List<int>","marks":[{"type":"code"}]},{"type":"text","text":"), "},{"type":"text","text":"Solution.longestIncreasingSubsequence","marks":[{"type":"code"}]},{"type":"text","text":" (Java, "},{"type":"text","text":"List<Integer>","marks":[{"type":"code"}]},{"type":"text","text":"), "},{"type":"text","text":"LongestIncreasingSubsequence","marks":[{"type":"code"}]},{"type":"text","text":" (Go, "},{"type":"text","text":"[]int","marks":[{"type":"code"}]},{"type":"text","text":")."}]},{"type":"heading","attrs":{"level":2,"id":"examples"},"content":[{"type":"text","text":"Examples"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"nums","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Sample result"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Length"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[10, 9, 2, 5, 3, 7, 101, 18]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[2, 3, 7, 18]","marks":[{"type":"code"}]},{"type":"text","text":" (or "},{"type":"text","text":"[2, 5, 7, 101]","marks":[{"type":"code"}]},{"type":"text","text":" …)"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"4"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[0, 1, 0, 3, 2, 3]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[0, 1, 2, 3]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"4"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[7, 7, 7, 7]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[7]","marks":[{"type":"code"}]},{"type":"text","text":" – \"strictly\" rules out equal values"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"1"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"0"}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"constraints"},"content":[{"type":"text","text":"Constraints"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"0 ≤ n ≤ 100,000","marks":[{"type":"code"}]},{"type":"text","text":", values in the 32-bit "},{"type":"text","text":"int","marks":[{"type":"code"}]},{"type":"text","text":" range."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"expected-complexity"},"content":[{"type":"text","text":"Expected complexity"}]},{"type":"paragraph","content":[{"type":"text","text":"O(n log n)","marks":[{"type":"bold"}]},{"type":"text","text":" time, "},{"type":"text","text":"O(n)","marks":[{"type":"bold"}]},{"type":"text","text":" memory. An O(n²) solution is correct but will not fit in the time limit for 100,000 elements."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/longest-increasing-subsequence/","title":"LeetCode 300 – Longest Increasing Subsequence","difficulty":null}],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:54.495Z","updatedAt":"2026-11-27T08:22:54.495Z"}