feat(content): more test cases

This commit is contained in:
2025-07-13 19:25:39 +01:00
parent e28da5def2
commit 0f264ef603
94 changed files with 1840 additions and 0 deletions

View File

@@ -9,6 +9,22 @@ categories:
patterns:
- two-pointers
function_signature: "def add_two_numbers(l1: ListNode | None, l2: ListNode | None) -> ListNode | None:"
test_cases:
visible:
- input: { l1: [2, 4, 3], l2: [5, 6, 4] }
expected: [7, 0, 8]
- input: { l1: [0], l2: [0] }
expected: [0]
- input: { l1: [9, 9, 9, 9, 9, 9, 9], l2: [9, 9, 9, 9] }
expected: [8, 9, 9, 9, 0, 0, 0, 1]
hidden:
- input: { l1: [1], l2: [9, 9] }
expected: [0, 0, 1]
- input: { l1: [5], l2: [5] }
expected: [0, 1]
description: |
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- monotonic-stack
function_signature: "def asteroid_collision(asteroids: list[int]) -> list[int]:"
test_cases:
visible:
- input: { asteroids: [5, 10, -5] }
expected: [5, 10]
- input: { asteroids: [8, -8] }
expected: []
- input: { asteroids: [10, 2, -5] }
expected: [10]
hidden:
- input: { asteroids: [-2, -1, 1, 2] }
expected: [-2, -1, 1, 2]
- input: { asteroids: [1, -1, -2, -2] }
expected: [-2, -2]
- input: { asteroids: [1, -2, -2, -2] }
expected: [-2, -2, -2]
description: |
We are given an array `asteroids` of integers representing asteroids in a row. The indices of the asteroids in the array represent their relative position in space.

View File

@@ -10,6 +10,26 @@ categories:
patterns:
- two-pointers
function_signature: "def backspace_compare(s: str, t: str) -> bool:"
test_cases:
visible:
- input: { s: "ab#c", t: "ad#c" }
expected: true
- input: { s: "ab##", t: "c#d#" }
expected: true
- input: { s: "a#c", t: "b" }
expected: false
hidden:
- input: { s: "a", t: "a" }
expected: true
- input: { s: "###", t: "" }
expected: true
- input: { s: "a##b", t: "b" }
expected: true
- input: { s: "xywrrmp", t: "xywrrmu#p" }
expected: true
description: |
Given two strings `s` and `t`, return `true` *if they are equal when both are typed into empty text editors*. `'#'` means a backspace character.

View File

@@ -10,6 +10,26 @@ categories:
patterns:
- monotonic-stack
function_signature: "def calculate(s: str) -> int:"
test_cases:
visible:
- input: { s: "3+2*2" }
expected: 7
- input: { s: " 3/2 " }
expected: 1
- input: { s: " 3+5 / 2 " }
expected: 5
hidden:
- input: { s: "42" }
expected: 42
- input: { s: "1+1+1" }
expected: 3
- input: { s: "2*3*4" }
expected: 24
- input: { s: "14-3/2" }
expected: 13
description: |
Given a string `s` which represents an expression, *evaluate this expression and return its value*.

View File

@@ -10,6 +10,24 @@ patterns:
- bfs
- tree-traversal
function_signature: "def level_order(root: TreeNode | None) -> list[list[int]]:"
test_cases:
visible:
- input: { root: [3, 9, 20, null, null, 15, 7] }
expected: [[3], [9, 20], [15, 7]]
- input: { root: [1] }
expected: [[1]]
- input: { root: [] }
expected: []
hidden:
- input: { root: [1, 2, 3, 4, 5] }
expected: [[1], [2, 3], [4, 5]]
- input: { root: [1, 2, null, 3] }
expected: [[1], [2], [3]]
- input: { root: [1, null, 2, null, 3] }
expected: [[1], [2], [3]]
description: |
Given the `root` of a binary tree, return *the level order traversal of its nodes' values*. (i.e., from left to right, level by level).

View File

@@ -9,6 +9,28 @@ categories:
patterns:
- dynamic-programming
function_signature: "def max_coins(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [3, 1, 5, 8] }
expected: 167
- input: { nums: [1, 5] }
expected: 10
hidden:
- input: { nums: [1] }
expected: 1
- input: { nums: [9, 76, 64, 21] }
expected: 116718
- input: { nums: [5, 7, 8] }
expected: 329
- input: { nums: [7, 9, 8, 0, 7, 1, 3, 5, 5, 2, 3] }
expected: 1654
- input: { nums: [35, 16, 83, 87, 32, 45, 81, 26] }
expected: 1310780
- input: { nums: [8, 2, 6, 8, 9, 8, 1, 4, 1, 5, 3, 0, 7, 7, 0, 4, 2, 2, 5] }
expected: 3630
description: |
You are given `n` balloons, indexed from `0` to `n - 1`. Each balloon is painted with a number on it represented by an array `nums`. You are asked to burst all the balloons.

View File

@@ -8,6 +8,30 @@ categories:
patterns:
- greedy
function_signature: "def candy(ratings: list[int]) -> int:"
test_cases:
visible:
- input: { ratings: [1, 0, 2] }
expected: 5
- input: { ratings: [1, 2, 2] }
expected: 4
hidden:
- input: { ratings: [1] }
expected: 1
- input: { ratings: [1, 2, 3, 4, 5] }
expected: 15
- input: { ratings: [5, 4, 3, 2, 1] }
expected: 15
- input: { ratings: [1, 3, 2, 2, 1] }
expected: 7
- input: { ratings: [1, 2, 3, 1, 0] }
expected: 9
- input: { ratings: [1, 3, 4, 5, 2] }
expected: 11
- input: { ratings: [1, 1, 1, 1] }
expected: 4
description: |
There are `n` children standing in a line. Each child is assigned a rating value given in the integer array `ratings`.

View File

@@ -10,6 +10,22 @@ patterns:
- dfs
- bfs
function_signature: "def clone_graph(node: Node | None) -> Node | None:"
test_cases:
visible:
- input: { adjList: [[2, 4], [1, 3], [2, 4], [1, 3]] }
expected: [[2, 4], [1, 3], [2, 4], [1, 3]]
- input: { adjList: [[]] }
expected: [[]]
- input: { adjList: [] }
expected: []
hidden:
- input: { adjList: [[2], [1]] }
expected: [[2], [1]]
- input: { adjList: [[2, 3], [1, 3], [1, 2]] }
expected: [[2, 3], [1, 3], [1, 2]]
description: |
Given a reference of a node in a **connected** undirected graph.

View File

@@ -9,6 +9,30 @@ categories:
patterns:
- dynamic-programming
function_signature: "def change(amount: int, coins: list[int]) -> int:"
test_cases:
visible:
- input: { amount: 5, coins: [1, 2, 5] }
expected: 4
- input: { amount: 3, coins: [2] }
expected: 0
- input: { amount: 10, coins: [10] }
expected: 1
hidden:
- input: { amount: 0, coins: [1, 2, 5] }
expected: 1
- input: { amount: 1, coins: [1] }
expected: 1
- input: { amount: 100, coins: [1, 5, 10, 25] }
expected: 242
- input: { amount: 7, coins: [2, 3, 5] }
expected: 2
- input: { amount: 500, coins: [1, 2, 5] }
expected: 12701
- input: { amount: 4, coins: [1, 2, 3] }
expected: 4
description: |
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.

View File

@@ -9,6 +9,26 @@ categories:
patterns:
- dynamic-programming
function_signature: "def coin_change(coins: list[int], amount: int) -> int:"
test_cases:
visible:
- input: { coins: [1, 2, 5], amount: 11 }
expected: 3
- input: { coins: [2], amount: 3 }
expected: -1
- input: { coins: [1], amount: 0 }
expected: 0
hidden:
- input: { coins: [1], amount: 1 }
expected: 1
- input: { coins: [1, 3, 4], amount: 6 }
expected: 2
- input: { coins: [2, 5, 10], amount: 7 }
expected: 2
- input: { coins: [186, 419, 83, 408], amount: 6249 }
expected: 20
description: |
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.

View File

@@ -9,6 +9,28 @@ categories:
patterns:
- backtracking
function_signature: "def combination_sum2(candidates: list[int], target: int) -> list[list[int]]:"
test_cases:
visible:
- input: { candidates: [10, 1, 2, 7, 6, 1, 5], target: 8 }
expected: [[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
- input: { candidates: [2, 5, 2, 1, 2], target: 5 }
expected: [[1, 2, 2], [5]]
hidden:
- input: { candidates: [1], target: 1 }
expected: [[1]]
- input: { candidates: [1], target: 2 }
expected: []
- input: { candidates: [1, 1, 1, 1, 1], target: 3 }
expected: [[1, 1, 1]]
- input: { candidates: [3, 1, 3, 5, 1, 1], target: 8 }
expected: [[1, 1, 1, 5], [1, 1, 3, 3], [3, 5]]
- input: { candidates: [2, 3, 6, 7], target: 7 }
expected: [[7]]
- input: { candidates: [4, 4, 2, 1, 4, 2, 2, 1, 3], target: 6 }
expected: [[1, 1, 2, 2], [1, 1, 4], [1, 2, 3], [2, 2, 2], [2, 4]]
description: |
Given a collection of candidate numbers (`candidates`) and a target number (`target`), find all unique combinations in `candidates` where the candidate numbers sum to `target`.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- backtracking
function_signature: "def combination_sum(candidates: list[int], target: int) -> list[list[int]]:"
test_cases:
visible:
- input: { candidates: [2, 3, 6, 7], target: 7 }
expected: [[2, 2, 3], [7]]
- input: { candidates: [2, 3, 5], target: 8 }
expected: [[2, 2, 2, 2], [2, 3, 3], [3, 5]]
- input: { candidates: [2], target: 1 }
expected: []
hidden:
- input: { candidates: [1], target: 1 }
expected: [[1]]
- input: { candidates: [1], target: 2 }
expected: [[1, 1]]
- input: { candidates: [2, 3, 5], target: 5 }
expected: [[2, 3], [5]]
description: |
Given an array of **distinct** integers `candidates` and a target integer `target`, return *a list of all **unique combinations** of* `candidates` *where the chosen numbers sum to* `target`. You may return the combinations in **any order**.

View File

@@ -9,6 +9,22 @@ categories:
patterns:
- backtracking
function_signature: "def combine(n: int, k: int) -> list[list[int]]:"
test_cases:
visible:
- input: { n: 4, k: 2 }
expected: [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
- input: { n: 1, k: 1 }
expected: [[1]]
hidden:
- input: { n: 5, k: 3 }
expected: [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
- input: { n: 3, k: 1 }
expected: [[1], [2], [3]]
- input: { n: 2, k: 2 }
expected: [[1, 2]]
description: |
Given two integers `n` and `k`, return *all possible combinations of* `k` *numbers chosen from the range* `[1, n]`.

View File

@@ -10,6 +10,24 @@ patterns:
- two-pointers
- greedy
function_signature: "def max_area(height: list[int]) -> int:"
test_cases:
visible:
- input: { height: [1, 8, 6, 2, 5, 4, 8, 3, 7] }
expected: 49
- input: { height: [1, 1] }
expected: 1
hidden:
- input: { height: [4, 3, 2, 1, 4] }
expected: 16
- input: { height: [1, 2, 1] }
expected: 2
- input: { height: [2, 3, 10, 5, 7, 8, 9] }
expected: 36
- input: { height: [1, 2, 4, 3] }
expected: 4
description: |
You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the i<sup>th</sup> line are `(i, 0)` and `(i, height[i])`.

View File

@@ -9,6 +9,28 @@ patterns:
- bfs
- dfs
function_signature: "def find_order(num_courses: int, prerequisites: list[list[int]]) -> list[int]:"
test_cases:
visible:
- input: { num_courses: 2, prerequisites: [[1, 0]] }
expected: [0, 1]
- input: { num_courses: 4, prerequisites: [[1, 0], [2, 0], [3, 1], [3, 2]] }
expected: [0, 1, 2, 3]
- input: { num_courses: 1, prerequisites: [] }
expected: [0]
hidden:
- input: { num_courses: 2, prerequisites: [[1, 0], [0, 1]] }
expected: []
- input: { num_courses: 3, prerequisites: [] }
expected: [0, 1, 2]
- input: { num_courses: 3, prerequisites: [[0, 1], [0, 2], [1, 2]] }
expected: [2, 1, 0]
- input: { num_courses: 4, prerequisites: [[3, 0], [0, 1]] }
expected: [1, 2, 0, 3]
- input: { num_courses: 2, prerequisites: [] }
expected: [0, 1]
description: |
There are a total of `numCourses` courses you have to take, labelled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [a_i, b_i]` indicates that you **must** take course `b_i` first if you want to take course `a_i`.

View File

@@ -9,6 +9,24 @@ patterns:
- dfs
- bfs
function_signature: "def can_finish(num_courses: int, prerequisites: list[list[int]]) -> bool:"
test_cases:
visible:
- input: { num_courses: 2, prerequisites: [[1, 0]] }
expected: true
- input: { num_courses: 2, prerequisites: [[1, 0], [0, 1]] }
expected: false
- input: { num_courses: 3, prerequisites: [[1, 0], [2, 1]] }
expected: true
hidden:
- input: { num_courses: 1, prerequisites: [] }
expected: true
- input: { num_courses: 4, prerequisites: [[1, 0], [2, 1], [3, 2], [1, 3]] }
expected: false
- input: { num_courses: 5, prerequisites: [[1, 0], [2, 0], [3, 1], [4, 2]] }
expected: true
description: |
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [a`<sub>`i`</sub>`, b`<sub>`i`</sub>`]` indicates that you **must** take course `b`<sub>`i`</sub> first if you want to take course `a`<sub>`i`</sub>.

View File

@@ -9,6 +9,30 @@ categories:
patterns:
- monotonic-stack
function_signature: "def daily_temperatures(temperatures: list[int]) -> list[int]:"
test_cases:
visible:
- input: { temperatures: [73, 74, 75, 71, 69, 72, 76, 73] }
expected: [1, 1, 4, 2, 1, 1, 0, 0]
- input: { temperatures: [30, 40, 50, 60] }
expected: [1, 1, 1, 0]
- input: { temperatures: [30, 60, 90] }
expected: [1, 1, 0]
hidden:
- input: { temperatures: [50] }
expected: [0]
- input: { temperatures: [90, 80, 70, 60] }
expected: [0, 0, 0, 0]
- input: { temperatures: [60, 70, 80, 90] }
expected: [1, 1, 1, 0]
- input: { temperatures: [55, 55, 55, 55] }
expected: [0, 0, 0, 0]
- input: { temperatures: [40, 35, 32, 37, 50] }
expected: [4, 2, 1, 1, 0]
- input: { temperatures: [89, 62, 70, 58, 47, 47, 46, 76, 100, 70] }
expected: [8, 1, 5, 4, 3, 2, 1, 1, 0, 0]
description: |
Given an array of integers `temperatures` represents the daily temperatures, return *an array* `answer` *such that* `answer[i]` *is the number of days you have to wait after the* i<sup>th</sup> *day to get a warmer temperature*.

View File

@@ -10,6 +10,32 @@ categories:
patterns:
- monotonic-stack
function_signature: "def decode_string(s: str) -> str:"
test_cases:
visible:
- input: { s: "3[a]2[bc]" }
expected: "aaabcbc"
- input: { s: "3[a2[c]]" }
expected: "accaccacc"
- input: { s: "2[abc]3[cd]ef" }
expected: "abcabccdcdcdef"
hidden:
- input: { s: "abc" }
expected: "abc"
- input: { s: "10[a]" }
expected: "aaaaaaaaaa"
- input: { s: "2[2[2[b]]]" }
expected: "bbbbbbbb"
- input: { s: "1[ab]" }
expected: "ab"
- input: { s: "2[xy3[z]]" }
expected: "xyzzzxyzzz"
- input: { s: "2[a2[b]]" }
expected: "abbabb"
- input: { s: "100[x]" }
expected: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
description: |
Given an encoded string, return its decoded string.

View File

@@ -9,6 +9,26 @@ categories:
patterns:
- dynamic-programming
function_signature: "def num_decodings(s: str) -> int:"
test_cases:
visible:
- input: { s: "12" }
expected: 2
- input: { s: "226" }
expected: 3
- input: { s: "06" }
expected: 0
hidden:
- input: { s: "1" }
expected: 1
- input: { s: "10" }
expected: 1
- input: { s: "2101" }
expected: 1
- input: { s: "11106" }
expected: 2
description: |
You have intercepted a secret message encoded as a string of numbers. The message is **decoded** via the following mapping:

View File

@@ -10,6 +10,26 @@ patterns:
- dfs
- tree-traversal
function_signature: "def diameter_of_binary_tree(root: TreeNode) -> int:"
test_cases:
visible:
- input: { root: [1, 2, 3, 4, 5] }
expected: 3
- input: { root: [1, 2] }
expected: 1
hidden:
- input: { root: [1] }
expected: 0
- input: { root: [1, 2, 3] }
expected: 2
- input: { root: [1, 2, null, 3, null, 4] }
expected: 3
- input: { root: [1, 2, 3, 4, 5, 6, 7] }
expected: 4
- input: { root: [4, -7, -3, null, null, -9, -3, 9, -7, -4, null, 6, null, -6, -6, null, null, 0, 6, 5, null, 9, null, null, -1, -4, null, null, null, -2] }
expected: 8
description: |
Given the `root` of a binary tree, return *the length of the **diameter** of the tree*.

View File

@@ -9,6 +9,32 @@ categories:
patterns:
- dynamic-programming
function_signature: "def min_distance(word1: str, word2: str) -> int:"
test_cases:
visible:
- input: { word1: "horse", word2: "ros" }
expected: 3
- input: { word1: "intention", word2: "execution" }
expected: 5
- input: { word1: "abc", word2: "abc" }
expected: 0
hidden:
- input: { word1: "", word2: "" }
expected: 0
- input: { word1: "abc", word2: "" }
expected: 3
- input: { word1: "", word2: "xyz" }
expected: 3
- input: { word1: "a", word2: "b" }
expected: 1
- input: { word1: "kitten", word2: "sitting" }
expected: 3
- input: { word1: "saturday", word2: "sunday" }
expected: 3
- input: { word1: "algorithm", word2: "altruistic" }
expected: 6
description: |
Given two strings `word1` and `word2`, return *the minimum number of operations required to convert `word1` to `word2`*.

View File

@@ -10,6 +10,28 @@ categories:
patterns:
- monotonic-stack
function_signature: "def eval_rpn(tokens: list[str]) -> int:"
test_cases:
visible:
- input: { tokens: ["2", "1", "+", "3", "*"] }
expected: 9
- input: { tokens: ["4", "13", "5", "/", "+"] }
expected: 6
- input: { tokens: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] }
expected: 22
hidden:
- input: { tokens: ["3"] }
expected: 3
- input: { tokens: ["1", "2", "+"] }
expected: 3
- input: { tokens: ["5", "1", "2", "+", "4", "*", "+", "3", "-"] }
expected: 14
- input: { tokens: ["15", "7", "1", "1", "+", "-", "/", "3", "*", "2", "1", "1", "+", "+", "-"] }
expected: 5
- input: { tokens: ["-78", "-33", "196", "+", "-19", "-", "115", "+", "-", "-99", "/", "-18", "8", "*", "-86", "-", "-", "16", "/", "26", "-14", "-", "-", "47", "-", "101", "-", "163", "*", "143", "-", "0", "-", "171", "+", "120", "*", "-60", "+", "156", "/", "173", "/", "-24", "11", "+", "21", "/", "*", "44", "*", "180", "70", "-", "-40", "*", "86", "132", "-", "-", "-126", "/", "-3", "170", "-", "-","-95", "-", "-", "153", "/", "-", "11", "+", "-60", "191", "-", "/", "-", "27", "/", "21", "/", "+", "/", "-89", "218", "-", "-", "7", "-", "-", "-27", "31", "*", "/", "23", "-", "-88", "123", "-", "-38", "/", "*", "-90", "-83", "/", "+", "-20", "+", "-31", "/", "-116", "79", "-", "*", "*", "238", "-69", "*", "-", "-", "192", "-", "/", "-", "170", "-", "59", "*", "37", "*", "-", "/", "-64", "139", "-", "-", "-58", "-", "25", "-11", "/", "/", "-", "-15", "*", "-", "39", "/", "-", "20", "-175", "/", "-114", "*", "-40", "*", "*", "17", "+", "+", "-10", "-", "*", "56", "/", "-", "-43", "-", "-104", "-", "+", "179", "/", "169", "-50", "-", "-52", "-10", "-", "+", "-", "247", "-192", "+", "-24", "-117", "-", "-51", "-", "/", "-", "+", "/", "*"] }
expected: 2
description: |
You are given an array of strings `tokens` that represents an arithmetic expression in a [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).

View File

@@ -9,6 +9,26 @@ categories:
patterns:
- binary-search
function_signature: "def find_min(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [3, 4, 5, 1, 2] }
expected: 1
- input: { nums: [4, 5, 6, 7, 0, 1, 2] }
expected: 0
- input: { nums: [11, 13, 15, 17] }
expected: 11
hidden:
- input: { nums: [1] }
expected: 1
- input: { nums: [2, 1] }
expected: 1
- input: { nums: [1, 2, 3, 4, 5] }
expected: 1
- input: { nums: [5, 1, 2, 3, 4] }
expected: 1
description: |
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become:

View File

@@ -10,6 +10,24 @@ patterns:
- fast-slow-pointers
- binary-search
function_signature: "def find_duplicate(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [1, 3, 4, 2, 2] }
expected: 2
- input: { nums: [3, 1, 3, 4, 2] }
expected: 3
- input: { nums: [3, 3, 3, 3, 3] }
expected: 3
hidden:
- input: { nums: [1, 1] }
expected: 1
- input: { nums: [2, 2, 2, 2, 2] }
expected: 2
- input: { nums: [1, 4, 4, 2, 4] }
expected: 4
description: |
Given an array of integers `nums` containing `n + 1` integers where each integer is in the range `[1, n]` inclusive.

View File

@@ -10,6 +10,22 @@ categories:
patterns:
- two-pointers
function_signature: "def four_sum(nums: list[int], target: int) -> list[list[int]]:"
test_cases:
visible:
- input: { nums: [1, 0, -1, 0, -2, 2], target: 0 }
expected: [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
- input: { nums: [2, 2, 2, 2, 2], target: 8 }
expected: [[2, 2, 2, 2]]
hidden:
- input: { nums: [0, 0, 0, 0], target: 0 }
expected: [[0, 0, 0, 0]]
- input: { nums: [1, 2, 3, 4], target: 10 }
expected: [[1, 2, 3, 4]]
- input: { nums: [1, 2, 3, 4], target: 100 }
expected: []
description: |
Given an array `nums` of `n` integers, return *an array of all the **unique** quadruplets* `[nums[a], nums[b], nums[c], nums[d]]` such that:

View File

@@ -8,6 +8,26 @@ categories:
patterns:
- greedy
function_signature: "def can_complete_circuit(gas: list[int], cost: list[int]) -> int:"
test_cases:
visible:
- input: { gas: [1, 2, 3, 4, 5], cost: [3, 4, 5, 1, 2] }
expected: 3
- input: { gas: [2, 3, 4], cost: [3, 4, 3] }
expected: -1
hidden:
- input: { gas: [5, 1, 2, 3, 4], cost: [4, 4, 1, 5, 1] }
expected: 4
- input: { gas: [3], cost: [3] }
expected: 0
- input: { gas: [1, 2], cost: [2, 1] }
expected: 1
- input: { gas: [5, 8, 2, 8], cost: [6, 5, 6, 6] }
expected: 3
- input: { gas: [3, 3, 4], cost: [3, 4, 4] }
expected: -1
description: |
There are `n` gas stations along a circular route, where the amount of gas at the i<sup>th</sup> station is `gas[i]`.

View File

@@ -9,6 +9,20 @@ categories:
patterns:
- backtracking
function_signature: "def generate_parenthesis(n: int) -> list[str]:"
test_cases:
visible:
- input: { n: 3 }
expected: ["((()))", "(()())", "(())()", "()(())", "()()()"]
- input: { n: 1 }
expected: ["()"]
hidden:
- input: { n: 2 }
expected: ["(())", "()()"]
- input: { n: 4 }
expected: ["(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()", "(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()"]
description: |
Given `n` pairs of parentheses, write a function to *generate all combinations of well-formed parentheses*.

View File

@@ -10,6 +10,24 @@ categories:
patterns:
- hashing
function_signature: "def group_anagrams(strs: list[str]) -> list[list[str]]:"
test_cases:
visible:
- input: { strs: ["eat", "tea", "tan", "ate", "nat", "bat"] }
expected: [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
- input: { strs: [""] }
expected: [[""]]
- input: { strs: ["a"] }
expected: [["a"]]
hidden:
- input: { strs: ["abc", "bca", "cab", "xyz", "zyx"] }
expected: [["abc", "bca", "cab"], ["xyz", "zyx"]]
- input: { strs: ["", ""] }
expected: [["", ""]]
- input: { strs: ["listen", "silent", "enlist"] }
expected: [["listen", "silent", "enlist"]]
description: |
Given an array of strings `strs`, group the **anagrams** together. You can return the answer in **any order**.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- dynamic-programming
function_signature: "def rob(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [1, 2, 3, 1] }
expected: 4
- input: { nums: [2, 7, 9, 3, 1] }
expected: 12
hidden:
- input: { nums: [1] }
expected: 1
- input: { nums: [2, 1] }
expected: 2
- input: { nums: [1, 2, 3, 4, 5] }
expected: 9
- input: { nums: [0, 0, 0, 0] }
expected: 0
description: |
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.

View File

@@ -8,6 +8,24 @@ categories:
patterns:
- intervals
function_signature: "def insert(intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]:"
test_cases:
visible:
- input: { intervals: [[1, 3], [6, 9]], newInterval: [2, 5] }
expected: [[1, 5], [6, 9]]
- input: { intervals: [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], newInterval: [4, 8] }
expected: [[1, 2], [3, 10], [12, 16]]
- input: { intervals: [], newInterval: [5, 7] }
expected: [[5, 7]]
hidden:
- input: { intervals: [[1, 5]], newInterval: [2, 3] }
expected: [[1, 5]]
- input: { intervals: [[1, 5]], newInterval: [6, 8] }
expected: [[1, 5], [6, 8]]
- input: { intervals: [[3, 5], [12, 15]], newInterval: [6, 6] }
expected: [[3, 5], [6, 6], [12, 15]]
description: |
You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [start_i, end_i]` represent the start and the end of the i<sup>th</sup> interval and `intervals` is sorted in ascending order by `start_i`. You are also given an interval `newInterval = [start, end]` that represents the start and end of another interval.

View File

@@ -10,6 +10,32 @@ patterns:
- dynamic-programming
- greedy
function_signature: "def integer_break(n: int) -> int:"
test_cases:
visible:
- input: { n: 2 }
expected: 1
- input: { n: 10 }
expected: 36
hidden:
- input: { n: 3 }
expected: 2
- input: { n: 4 }
expected: 4
- input: { n: 5 }
expected: 6
- input: { n: 6 }
expected: 9
- input: { n: 7 }
expected: 12
- input: { n: 8 }
expected: 18
- input: { n: 11 }
expected: 54
- input: { n: 58 }
expected: 1549681956
description: |
Given an integer `n`, break it into the sum of `k` **positive integers**, where `k >= 2`, and maximize the product of those integers.

View File

@@ -11,6 +11,24 @@ patterns:
- dfs
- bfs
function_signature: "def invert_tree(root: TreeNode | None) -> TreeNode | None:"
test_cases:
visible:
- input: { root: [4, 2, 7, 1, 3, 6, 9] }
expected: [4, 7, 2, 9, 6, 3, 1]
- input: { root: [2, 1, 3] }
expected: [2, 3, 1]
- input: { root: [] }
expected: []
hidden:
- input: { root: [1] }
expected: [1]
- input: { root: [1, 2] }
expected: [1, null, 2]
- input: { root: [1, null, 2] }
expected: [1, 2]
description: |
Given the `root` of a binary tree, invert the tree, and return *its root*.

View File

@@ -9,6 +9,30 @@ categories:
patterns:
- greedy
function_signature: "def jump(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [2, 3, 1, 1, 4] }
expected: 2
- input: { nums: [2, 3, 0, 1, 4] }
expected: 2
- input: { nums: [1] }
expected: 0
hidden:
- input: { nums: [1, 2, 3] }
expected: 2
- input: { nums: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 0] }
expected: 2
- input: { nums: [1, 1, 1, 1, 1] }
expected: 4
- input: { nums: [5, 6, 4, 4, 6, 9, 4, 4, 7, 4, 4, 8, 2, 6, 8, 1, 5, 9, 6, 5, 2, 7, 9, 7, 9, 6, 9, 4, 1, 6, 8, 8, 4, 4, 2, 0, 3, 8, 5] }
expected: 5
- input: { nums: [7, 0, 9, 6, 9, 6, 1, 7, 9, 0, 1, 2, 9, 0, 3] }
expected: 2
- input: { nums: [2, 1] }
expected: 1
description: |
You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at index `0`.

View File

@@ -10,6 +10,24 @@ patterns:
- greedy
- dynamic-programming
function_signature: "def can_jump(nums: list[int]) -> bool:"
test_cases:
visible:
- input: { nums: [2, 3, 1, 1, 4] }
expected: true
- input: { nums: [3, 2, 1, 0, 4] }
expected: false
hidden:
- input: { nums: [0] }
expected: true
- input: { nums: [2, 0, 0] }
expected: true
- input: { nums: [1, 0, 1, 0] }
expected: false
- input: { nums: [5, 4, 3, 2, 1, 0, 0] }
expected: true
description: |
You are given an integer array `nums`. You are initially positioned at the array's **first index**, and each element in the array represents your maximum jump length at that position.

View File

@@ -11,6 +11,30 @@ patterns:
- heap
- binary-search
function_signature: "def find_kth_largest(nums: list[int], k: int) -> int:"
test_cases:
visible:
- input: { nums: [3, 2, 1, 5, 6, 4], k: 2 }
expected: 5
- input: { nums: [3, 2, 3, 1, 2, 4, 5, 5, 6], k: 4 }
expected: 4
- input: { nums: [1], k: 1 }
expected: 1
hidden:
- input: { nums: [7, 7, 7, 7], k: 2 }
expected: 7
- input: { nums: [1, 2, 3, 4, 5], k: 5 }
expected: 1
- input: { nums: [1, 2, 3, 4, 5], k: 1 }
expected: 5
- input: { nums: [-1, -2, -3, -4], k: 1 }
expected: -1
- input: { nums: [5, 2, 4, 1, 3, 6, 0], k: 3 }
expected: 4
- input: { nums: [99, 99], k: 1 }
expected: 99
description: |
Given an integer array `nums` and an integer `k`, return *the* `k`<sup>th</sup> *largest element in the array*.

View File

@@ -10,6 +10,24 @@ patterns:
- dfs
- tree-traversal
function_signature: "def kth_smallest(root: TreeNode | None, k: int) -> int:"
test_cases:
visible:
- input: { root: [3, 1, 4, null, 2], k: 1 }
expected: 1
- input: { root: [5, 3, 6, 2, 4, null, null, 1], k: 3 }
expected: 3
- input: { root: [1], k: 1 }
expected: 1
hidden:
- input: { root: [2, 1, 3], k: 2 }
expected: 2
- input: { root: [5, 3, 6, 2, 4, null, null, 1], k: 6 }
expected: 6
- input: { root: [3, 1, 4, null, 2], k: 4 }
expected: 4
description: |
Given the `root` of a binary search tree, and an integer `k`, return the `k`<sup>th</sup> smallest value (**1-indexed**) of all the values of the nodes in the tree.

View File

@@ -9,6 +9,32 @@ categories:
patterns:
- monotonic-stack
function_signature: "def largest_rectangle_area(heights: list[int]) -> int:"
test_cases:
visible:
- input: { heights: [2, 1, 5, 6, 2, 3] }
expected: 10
- input: { heights: [2, 4] }
expected: 4
- input: { heights: [1, 1] }
expected: 2
hidden:
- input: { heights: [1] }
expected: 1
- input: { heights: [0] }
expected: 0
- input: { heights: [2, 1, 2] }
expected: 3
- input: { heights: [1, 2, 3, 4, 5] }
expected: 9
- input: { heights: [5, 4, 3, 2, 1] }
expected: 9
- input: { heights: [3, 6, 5, 7, 4, 8, 1, 0] }
expected: 20
- input: { heights: [4, 2, 0, 3, 2, 5] }
expected: 6
description: |
Given an array of integers `heights` representing the histogram's bar height where the width of each bar is `1`, return *the area of the largest rectangle in the histogram*.

View File

@@ -10,6 +10,22 @@ categories:
patterns:
- backtracking
function_signature: "def letter_combinations(digits: str) -> list[str]:"
test_cases:
visible:
- input: { digits: "23" }
expected: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
- input: { digits: "" }
expected: []
- input: { digits: "2" }
expected: ["a", "b", "c"]
hidden:
- input: { digits: "7" }
expected: ["p", "q", "r", "s"]
- input: { digits: "79" }
expected: ["pw", "px", "py", "pz", "qw", "qx", "qy", "qz", "rw", "rx", "ry", "rz", "sw", "sx", "sy", "sz"]
description: |
Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent. Return the answer in **any order**.

View File

@@ -10,6 +10,24 @@ categories:
patterns:
- fast-slow-pointers
function_signature: "def has_cycle(head: ListNode | None) -> bool:"
test_cases:
visible:
- input: { head: [3, 2, 0, -4], pos: 1 }
expected: true
- input: { head: [1, 2], pos: 0 }
expected: true
- input: { head: [1], pos: -1 }
expected: false
hidden:
- input: { head: [], pos: -1 }
expected: false
- input: { head: [1, 2, 3, 4, 5], pos: -1 }
expected: false
- input: { head: [1, 2, 3, 4, 5], pos: 4 }
expected: true
description: |
Given `head`, the head of a linked list, determine if the linked list has a cycle in it.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- dynamic-programming
function_signature: "def longest_common_subsequence(text1: str, text2: str) -> int:"
test_cases:
visible:
- input: { text1: "abcde", text2: "ace" }
expected: 3
- input: { text1: "abc", text2: "abc" }
expected: 3
- input: { text1: "abc", text2: "def" }
expected: 0
hidden:
- input: { text1: "a", text2: "a" }
expected: 1
- input: { text1: "a", text2: "b" }
expected: 0
- input: { text1: "oxcpqrsvwf", text2: "shmtulqrypy" }
expected: 2
description: |
Given two strings `text1` and `text2`, return *the length of their longest **common subsequence***. If there is no **common subsequence**, return `0`.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- union-find
function_signature: "def longest_consecutive(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [100, 4, 200, 1, 3, 2] }
expected: 4
- input: { nums: [0, 3, 7, 2, 5, 8, 4, 6, 0, 1] }
expected: 9
hidden:
- input: { nums: [] }
expected: 0
- input: { nums: [1, 0, 1, 2] }
expected: 3
- input: { nums: [9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6] }
expected: 7
- input: { nums: [1] }
expected: 1
description: |
Given an unsorted array of integers `nums`, return *the length of the longest consecutive elements sequence*.

View File

@@ -11,6 +11,26 @@ patterns:
- dynamic-programming
- binary-search
function_signature: "def length_of_lis(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [10, 9, 2, 5, 3, 7, 101, 18] }
expected: 4
- input: { nums: [0, 1, 0, 3, 2, 3] }
expected: 4
- input: { nums: [7, 7, 7, 7, 7, 7, 7] }
expected: 1
hidden:
- input: { nums: [1] }
expected: 1
- input: { nums: [1, 2, 3, 4, 5] }
expected: 5
- input: { nums: [5, 4, 3, 2, 1] }
expected: 1
- input: { nums: [4, 10, 4, 3, 8, 9] }
expected: 3
description: |
Given an integer array `nums`, return *the length of the longest **strictly increasing subsequence***.

View File

@@ -10,6 +10,24 @@ patterns:
- two-pointers
- dynamic-programming
function_signature: "def longest_palindrome(s: str) -> str:"
test_cases:
visible:
- input: { s: "babad" }
expected: "bab"
- input: { s: "cbbd" }
expected: "bb"
hidden:
- input: { s: "a" }
expected: "a"
- input: { s: "ac" }
expected: "a"
- input: { s: "racecar" }
expected: "racecar"
- input: { s: "aacabdkacaa" }
expected: "aca"
description: |
Given a string `s`, return *the longest palindromic substring* in `s`.

View File

@@ -9,6 +9,26 @@ categories:
patterns:
- sliding-window
function_signature: "def length_of_longest_substring(s: str) -> int:"
test_cases:
visible:
- input: { s: "abcabcbb" }
expected: 3
- input: { s: "bbbbb" }
expected: 1
- input: { s: "pwwkew" }
expected: 3
hidden:
- input: { s: "" }
expected: 0
- input: { s: " " }
expected: 1
- input: { s: "au" }
expected: 2
- input: { s: "dvdf" }
expected: 3
description: |
Given a string `s`, find the length of the **longest substring** without repeating characters.

View File

@@ -9,6 +9,24 @@ patterns:
- tree-traversal
- binary-search
function_signature: "def lowest_common_ancestor(root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:"
test_cases:
visible:
- input: { root: [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5], p: 2, q: 8 }
expected: 6
- input: { root: [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5], p: 2, q: 4 }
expected: 2
- input: { root: [2, 1], p: 2, q: 1 }
expected: 2
hidden:
- input: { root: [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5], p: 3, q: 5 }
expected: 4
- input: { root: [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5], p: 7, q: 9 }
expected: 8
- input: { root: [3, 1, 4, null, 2], p: 1, q: 4 }
expected: 3
description: |
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.

View File

@@ -11,6 +11,24 @@ patterns:
- bfs
- tree-traversal
function_signature: "def max_depth(root: TreeNode | None) -> int:"
test_cases:
visible:
- input: { root: [3, 9, 20, null, null, 15, 7] }
expected: 3
- input: { root: [1, null, 2] }
expected: 2
- input: { root: [] }
expected: 0
hidden:
- input: { root: [1] }
expected: 1
- input: { root: [1, 2, 3, 4, 5] }
expected: 3
- input: { root: [1, 2, null, 3, null, 4] }
expected: 4
description: |
Given the `root` of a binary tree, return *its maximum depth*.

View File

@@ -9,6 +9,32 @@ categories:
patterns:
- dynamic-programming
function_signature: "def max_product(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [2, 3, -2, 4] }
expected: 6
- input: { nums: [-2, 0, -1] }
expected: 0
- input: { nums: [2, -5, -2, -4, 3] }
expected: 24
hidden:
- input: { nums: [0] }
expected: 0
- input: { nums: [-2] }
expected: -2
- input: { nums: [1, 2, 3, 4] }
expected: 24
- input: { nums: [-1, -2, -3] }
expected: 6
- input: { nums: [0, 2] }
expected: 2
- input: { nums: [-2, 3, -4] }
expected: 24
- input: { nums: [2, -1, 1, 1] }
expected: 2
description: |
Given an integer array `nums`, find a subarray that has the largest product, and return *the product*.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- binary-search
function_signature: "def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float:"
test_cases:
visible:
- input: { nums1: [1, 3], nums2: [2] }
expected: 2.0
- input: { nums1: [1, 2], nums2: [3, 4] }
expected: 2.5
hidden:
- input: { nums1: [], nums2: [1] }
expected: 1.0
- input: { nums1: [2], nums2: [] }
expected: 2.0
- input: { nums1: [1, 2, 3], nums2: [4, 5, 6] }
expected: 3.5
- input: { nums1: [1, 3], nums2: [2, 4] }
expected: 2.5
description: |
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- intervals
function_signature: "def merge(intervals: list[list[int]]) -> list[list[int]]:"
test_cases:
visible:
- input: { intervals: [[1, 3], [2, 6], [8, 10], [15, 18]] }
expected: [[1, 6], [8, 10], [15, 18]]
- input: { intervals: [[1, 4], [4, 5]] }
expected: [[1, 5]]
- input: { intervals: [[1, 4], [0, 4]] }
expected: [[0, 4]]
hidden:
- input: { intervals: [[1, 4]] }
expected: [[1, 4]]
- input: { intervals: [[1, 4], [0, 0]] }
expected: [[0, 0], [1, 4]]
- input: { intervals: [[1, 4], [2, 3]] }
expected: [[1, 4]]
description: |
Given an array of `intervals` where `intervals[i] = [start_i, end_i]`, merge all overlapping intervals, and return *an array of the non-overlapping intervals that cover all the intervals in the input*.

View File

@@ -10,6 +10,24 @@ categories:
patterns:
- two-pointers
function_signature: "def merge(nums1: list[int], m: int, nums2: list[int], n: int) -> list[int]:"
test_cases:
visible:
- input: { nums1: [1, 2, 3, 0, 0, 0], m: 3, nums2: [2, 5, 6], n: 3 }
expected: [1, 2, 2, 3, 5, 6]
- input: { nums1: [1], m: 1, nums2: [], n: 0 }
expected: [1]
- input: { nums1: [0], m: 0, nums2: [1], n: 1 }
expected: [1]
hidden:
- input: { nums1: [4, 5, 6, 0, 0, 0], m: 3, nums2: [1, 2, 3], n: 3 }
expected: [1, 2, 3, 4, 5, 6]
- input: { nums1: [1, 2, 3, 0, 0, 0], m: 3, nums2: [4, 5, 6], n: 3 }
expected: [1, 2, 3, 4, 5, 6]
- input: { nums1: [2, 0], m: 1, nums2: [1], n: 1 }
expected: [1, 2]
description: |
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively.

View File

@@ -9,6 +9,22 @@ categories:
patterns:
- two-pointers
function_signature: "def merge_two_lists(list1: ListNode | None, list2: ListNode | None) -> ListNode | None:"
test_cases:
visible:
- input: { list1: [1, 2, 4], list2: [1, 3, 4] }
expected: [1, 1, 2, 3, 4, 4]
- input: { list1: [], list2: [] }
expected: []
- input: { list1: [], list2: [0] }
expected: [0]
hidden:
- input: { list1: [1], list2: [2] }
expected: [1, 2]
- input: { list1: [5], list2: [1, 2, 4] }
expected: [1, 2, 4, 5]
description: |
You are given the heads of two sorted linked lists `list1` and `list2`.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- dynamic-programming
function_signature: "def min_cost_climbing_stairs(cost: list[int]) -> int:"
test_cases:
visible:
- input: { cost: [10, 15, 20] }
expected: 15
- input: { cost: [1, 100, 1, 1, 1, 100, 1, 1, 100, 1] }
expected: 6
- input: { cost: [0, 0, 0, 0] }
expected: 0
hidden:
- input: { cost: [1, 2] }
expected: 1
- input: { cost: [10, 15] }
expected: 10
- input: { cost: [1, 100] }
expected: 1
description: |
You are given an integer array `cost` where `cost[i]` is the cost of the i<sup>th</sup> step on a staircase. Once you pay the cost, you can either climb one or two steps.

View File

@@ -9,6 +9,26 @@ categories:
patterns:
- sliding-window
function_signature: "def min_window(s: str, t: str) -> str:"
test_cases:
visible:
- input: { s: "ADOBECODEBANC", t: "ABC" }
expected: "BANC"
- input: { s: "a", t: "a" }
expected: "a"
- input: { s: "a", t: "aa" }
expected: ""
hidden:
- input: { s: "ab", t: "a" }
expected: "a"
- input: { s: "ab", t: "b" }
expected: "b"
- input: { s: "bba", t: "ab" }
expected: "ba"
- input: { s: "abc", t: "cba" }
expected: "abc"
description: |
Given two strings `s` and `t` of lengths `m` and `n` respectively, return *the **minimum window substring*** of `s` such that every character in `t` (**including duplicates**) is included in the window.

View File

@@ -10,6 +10,26 @@ patterns:
- bfs
- heap
function_signature: "def network_delay_time(times: list[list[int]], n: int, k: int) -> int:"
test_cases:
visible:
- input: { times: [[2, 1, 1], [2, 3, 1], [3, 4, 1]], n: 4, k: 2 }
expected: 2
- input: { times: [[1, 2, 1]], n: 2, k: 1 }
expected: 1
- input: { times: [[1, 2, 1]], n: 2, k: 2 }
expected: -1
hidden:
- input: { times: [[1, 2, 1], [2, 3, 2], [1, 3, 4]], n: 3, k: 1 }
expected: 3
- input: { times: [[1, 2, 1], [2, 1, 3]], n: 2, k: 2 }
expected: 3
- input: { times: [[1, 2, 1], [2, 3, 7], [1, 3, 4], [2, 1, 2]], n: 3, k: 1 }
expected: 4
- input: { times: [[3, 5, 78], [2, 1, 1], [1, 3, 0], [4, 3, 59], [5, 3, 85], [5, 2, 22], [2, 4, 23], [1, 4, 43], [4, 5, 75], [5, 1, 15], [1, 5, 91], [4, 1, 16], [3, 2, 98], [3, 4, 22], [5, 4, 31], [1, 2, 0], [2, 5, 4], [4, 2, 51], [3, 1, 36], [2, 3, 59]], n: 5, k: 5 }
expected: 31
description: |
You are given a network of `n` nodes, labeled from `1` to `n`. You are also given `times`, a list of travel times as directed edges `times[i] = (u_i, v_i, w_i)`, where `u_i` is the source node, `v_i` is the target node, and `w_i` is the time it takes for a signal to travel from source to target.

View File

@@ -11,6 +11,24 @@ patterns:
- bfs
- matrix-traversal
function_signature: "def num_islands(grid: list[list[str]]) -> int:"
test_cases:
visible:
- input: { grid: [["1", "1", "1", "1", "0"], ["1", "1", "0", "1", "0"], ["1", "1", "0", "0", "0"], ["0", "0", "0", "0", "0"]] }
expected: 1
- input: { grid: [["1", "1", "0", "0", "0"], ["1", "1", "0", "0", "0"], ["0", "0", "1", "0", "0"], ["0", "0", "0", "1", "1"]] }
expected: 3
hidden:
- input: { grid: [["1"]] }
expected: 1
- input: { grid: [["0"]] }
expected: 0
- input: { grid: [["1", "0", "1", "0", "1"]] }
expected: 3
- input: { grid: [["1", "1"], ["1", "1"]] }
expected: 1
description: |
Given an `m × n` 2D binary grid `grid` which represents a map of `'1'`s (land) and `'0'`s (water), return *the number of islands*.

View File

@@ -10,6 +10,20 @@ patterns:
- dfs
- matrix-traversal
function_signature: "def pacific_atlantic(heights: list[list[int]]) -> list[list[int]]:"
test_cases:
visible:
- input: { heights: [[1, 2, 2, 3, 5], [3, 2, 3, 4, 4], [2, 4, 5, 3, 1], [6, 7, 1, 4, 5], [5, 1, 1, 2, 4]] }
expected: [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]]
- input: { heights: [[1]] }
expected: [[0, 0]]
hidden:
- input: { heights: [[1, 1], [1, 1]] }
expected: [[0, 0], [0, 1], [1, 0], [1, 1]]
- input: { heights: [[1, 2], [4, 3]] }
expected: [[0, 1], [1, 0], [1, 1]]
description: |
There is an `m x n` rectangular island that borders both the **Pacific Ocean** and **Atlantic Ocean**. The **Pacific Ocean** touches the island's left and top edges, and the **Atlantic Ocean** touches the island's right and bottom edges.

View File

@@ -11,6 +11,28 @@ patterns:
- backtracking
- dynamic-programming
function_signature: "def partition(s: str) -> list[list[str]]:"
test_cases:
visible:
- input: { s: "aab" }
expected: [["a", "a", "b"], ["aa", "b"]]
- input: { s: "a" }
expected: [["a"]]
hidden:
- input: { s: "aa" }
expected: [["a", "a"], ["aa"]]
- input: { s: "abc" }
expected: [["a", "b", "c"]]
- input: { s: "aba" }
expected: [["a", "b", "a"], ["aba"]]
- input: { s: "aabb" }
expected: [["a", "a", "b", "b"], ["a", "a", "bb"], ["aa", "b", "b"], ["aa", "bb"]]
- input: { s: "abba" }
expected: [["a", "b", "b", "a"], ["a", "bb", "a"], ["abba"]]
- input: { s: "racecar" }
expected: [["r", "a", "c", "e", "c", "a", "r"], ["r", "a", "cec", "a", "r"], ["r", "aceca", "r"], ["racecar"]]
description: |
Given a string `s`, partition `s` such that every substring of the partition is a **palindrome**.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- dynamic-programming
function_signature: "def can_partition(nums: list[int]) -> bool:"
test_cases:
visible:
- input: { nums: [1, 5, 11, 5] }
expected: true
- input: { nums: [1, 2, 3, 5] }
expected: false
- input: { nums: [1, 2, 5] }
expected: false
hidden:
- input: { nums: [1, 1] }
expected: true
- input: { nums: [2, 2, 1, 1] }
expected: true
- input: { nums: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 99, 97] }
expected: false
description: |
Given an integer array `nums`, return `true` *if you can partition the array into two subsets such that the sum of the elements in both subsets is equal* or `false` *otherwise*.

View File

@@ -11,6 +11,30 @@ patterns:
- greedy
- two-pointers
function_signature: "def partition_labels(s: str) -> list[int]:"
test_cases:
visible:
- input: { s: "ababcbacadefegdehijhklij" }
expected: [9, 7, 8]
- input: { s: "eccbbbbdec" }
expected: [10]
- input: { s: "abc" }
expected: [1, 1, 1]
hidden:
- input: { s: "a" }
expected: [1]
- input: { s: "aaa" }
expected: [3]
- input: { s: "abab" }
expected: [4]
- input: { s: "caedbdedda" }
expected: [1, 9]
- input: { s: "qiejxqfnqceocmy" }
expected: [13, 1, 1]
- input: { s: "vhaagbqkaq" }
expected: [1, 8, 1]
description: |
You are given a string `s`. We want to partition the string into as many parts as possible so that each letter appears in **at most one part**.

View File

@@ -10,6 +10,32 @@ patterns:
- dynamic-programming
- bfs
function_signature: "def num_squares(n: int) -> int:"
test_cases:
visible:
- input: { n: 12 }
expected: 3
- input: { n: 13 }
expected: 2
- input: { n: 1 }
expected: 1
hidden:
- input: { n: 4 }
expected: 1
- input: { n: 2 }
expected: 2
- input: { n: 3 }
expected: 3
- input: { n: 7 }
expected: 4
- input: { n: 100 }
expected: 1
- input: { n: 99 }
expected: 3
- input: { n: 48 }
expected: 3
description: |
Given an integer `n`, return *the least number of perfect square numbers that sum to* `n`.

View File

@@ -10,6 +10,26 @@ categories:
patterns:
- sliding-window
function_signature: "def check_inclusion(s1: str, s2: str) -> bool:"
test_cases:
visible:
- input: { s1: "ab", s2: "eidbaooo" }
expected: true
- input: { s1: "ab", s2: "eidboaoo" }
expected: false
- input: { s1: "adc", s2: "dcda" }
expected: true
hidden:
- input: { s1: "a", s2: "a" }
expected: true
- input: { s1: "ab", s2: "a" }
expected: false
- input: { s1: "abc", s2: "ccccbbbbaaaa" }
expected: false
- input: { s1: "hello", s2: "ooolleoooleh" }
expected: false
description: |
Given two strings `s1` and `s2`, return `true` if `s2` contains a permutation of `s1`, or `false` otherwise.

View File

@@ -9,6 +9,26 @@ categories:
patterns:
- backtracking
function_signature: "def permute(nums: list[int]) -> list[list[int]]:"
test_cases:
visible:
- input: { nums: [1, 2, 3] }
expected: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
- input: { nums: [0, 1] }
expected: [[0, 1], [1, 0]]
- input: { nums: [1] }
expected: [[1]]
hidden:
- input: { nums: [5, 4] }
expected: [[5, 4], [4, 5]]
- input: { nums: [-1, 0, 1] }
expected: [[-1, 0, 1], [-1, 1, 0], [0, -1, 1], [0, 1, -1], [1, -1, 0], [1, 0, -1]]
- input: { nums: [1, 2, 3, 4] }
expected: [[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1], [3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]]
- input: { nums: [0] }
expected: [[0]]
description: |
Given an array `nums` of distinct integers, return *all the possible permutations*. You can return the answer in **any order**.

View File

@@ -8,6 +8,24 @@ categories:
patterns:
- prefix-sum
function_signature: "def product_except_self(nums: list[int]) -> list[int]:"
test_cases:
visible:
- input: { nums: [1, 2, 3, 4] }
expected: [24, 12, 8, 6]
- input: { nums: [-1, 1, 0, -3, 3] }
expected: [0, 0, 9, 0, 0]
hidden:
- input: { nums: [2, 3] }
expected: [3, 2]
- input: { nums: [1, 1, 1, 1] }
expected: [1, 1, 1, 1]
- input: { nums: [0, 0] }
expected: [0, 0]
- input: { nums: [5, -2, 4] }
expected: [-8, 20, -10]
description: |
Given an integer array `nums`, return an array `answer` such that `answer[i]` is equal to the product of all the elements of `nums` except `nums[i]`.

View File

@@ -8,6 +8,22 @@ categories:
patterns:
- dfs
function_signature: "def find_itinerary(tickets: list[list[str]]) -> list[str]:"
test_cases:
visible:
- input: { tickets: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]] }
expected: ["JFK", "MUC", "LHR", "SFO", "SJC"]
- input: { tickets: [["JFK", "SFO"], ["JFK", "ATL"], ["SFO", "ATL"], ["ATL", "JFK"], ["ATL", "SFO"]] }
expected: ["JFK", "ATL", "JFK", "SFO", "ATL", "SFO"]
hidden:
- input: { tickets: [["JFK", "KUL"], ["JFK", "NRT"], ["NRT", "JFK"]] }
expected: ["JFK", "NRT", "JFK", "KUL"]
- input: { tickets: [["EZE", "AXA"], ["TIA", "ANU"], ["ANU", "JFK"], ["JFK", "ANU"], ["ANU", "EZE"], ["TIA", "ANU"], ["AXA", "TIA"], ["TIA", "JFK"], ["ANU", "TIA"], ["JFK", "TIA"]] }
expected: ["JFK", "ANU", "EZE", "AXA", "TIA", "ANU", "JFK", "TIA", "ANU", "TIA", "JFK"]
- input: { tickets: [["JFK", "A"], ["A", "B"], ["B", "JFK"]] }
expected: ["JFK", "A", "B", "JFK"]
description: |
You are given a list of airline `tickets` where `tickets[i] = [from_i, to_i]` represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.

View File

@@ -9,6 +9,24 @@ patterns:
- union-find
- dfs
function_signature: "def find_redundant_connection(edges: list[list[int]]) -> list[int]:"
test_cases:
visible:
- input: { edges: [[1, 2], [1, 3], [2, 3]] }
expected: [2, 3]
- input: { edges: [[1, 2], [2, 3], [3, 4], [1, 4], [1, 5]] }
expected: [1, 4]
hidden:
- input: { edges: [[1, 2], [2, 3], [3, 1]] }
expected: [3, 1]
- input: { edges: [[1, 4], [3, 4], [1, 3], [1, 2], [4, 5]] }
expected: [1, 3]
- input: { edges: [[1, 5], [3, 4], [3, 5], [4, 5], [2, 4]] }
expected: [4, 5]
- input: { edges: [[9, 10], [5, 8], [2, 6], [1, 5], [3, 8], [4, 9], [8, 10], [4, 10], [6, 8], [7, 9]] }
expected: [4, 10]
description: |
In this problem, a tree is an **undirected graph** that is connected and has no cycles.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- two-pointers
function_signature: "def remove_duplicates(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [1, 1, 2] }
expected: 2
- input: { nums: [0, 0, 1, 1, 1, 2, 2, 3, 3, 4] }
expected: 5
- input: { nums: [1, 2, 3] }
expected: 3
hidden:
- input: { nums: [1] }
expected: 1
- input: { nums: [1, 1, 1, 1, 1] }
expected: 1
- input: { nums: [-1, 0, 0, 1, 1, 2] }
expected: 4
description: |
Given an integer array `nums` sorted in **non-decreasing order**, remove the duplicates *in-place* such that each unique element appears only **once**. The **relative order** of the elements should be kept the **same**.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- two-pointers
function_signature: "def remove_element(nums: list[int], val: int) -> int:"
test_cases:
visible:
- input: { nums: [3, 2, 2, 3], val: 3 }
expected: 2
- input: { nums: [0, 1, 2, 2, 3, 0, 4, 2], val: 2 }
expected: 5
- input: { nums: [1], val: 1 }
expected: 0
hidden:
- input: { nums: [], val: 0 }
expected: 0
- input: { nums: [1, 2, 3, 4, 5], val: 6 }
expected: 5
- input: { nums: [4, 4, 4, 4], val: 4 }
expected: 0
description: |
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` **in-place**. The order of the elements may be changed. Then return *the number of elements in* `nums` *which are not equal to* `val`.

View File

@@ -10,6 +10,26 @@ patterns:
- fast-slow-pointers
- linkedlist-reversal
function_signature: "def reorder_list(head: ListNode) -> None:"
test_cases:
visible:
- input: { head: [1, 2, 3, 4] }
expected: [1, 4, 2, 3]
- input: { head: [1, 2, 3, 4, 5] }
expected: [1, 5, 2, 4, 3]
hidden:
- input: { head: [1] }
expected: [1]
- input: { head: [1, 2] }
expected: [1, 2]
- input: { head: [1, 2, 3] }
expected: [1, 3, 2]
- input: { head: [1, 2, 3, 4, 5, 6] }
expected: [1, 6, 2, 5, 3, 4]
- input: { head: [1, 2, 3, 4, 5, 6, 7] }
expected: [1, 7, 2, 6, 3, 5, 4]
description: |
You are given the head of a singly linked-list. The list can be represented as:

View File

@@ -8,6 +8,32 @@ categories:
patterns:
- greedy
function_signature: "def reverse(x: int) -> int:"
test_cases:
visible:
- input: { x: 123 }
expected: 321
- input: { x: -123 }
expected: -321
- input: { x: 120 }
expected: 21
hidden:
- input: { x: 0 }
expected: 0
- input: { x: -120 }
expected: -21
- input: { x: 1534236469 }
expected: 0
- input: { x: -2147483648 }
expected: 0
- input: { x: 2147483647 }
expected: 0
- input: { x: 1463847412 }
expected: 2147483641
- input: { x: -1 }
expected: -1
description: |
Given a signed 32-bit integer `x`, return `x` *with its digits reversed*. If reversing `x` causes the value to go outside the signed 32-bit integer range `[-2^31, 2^31 - 1]`, then return `0`.

View File

@@ -9,6 +9,22 @@ categories:
patterns:
- linkedlist-reversal
function_signature: "def reverse_list(head: ListNode | None) -> ListNode | None:"
test_cases:
visible:
- input: { head: [1, 2, 3, 4, 5] }
expected: [5, 4, 3, 2, 1]
- input: { head: [1, 2] }
expected: [2, 1]
- input: { head: [] }
expected: []
hidden:
- input: { head: [1] }
expected: [1]
- input: { head: [1, 2, 3] }
expected: [3, 2, 1]
description: |
Given the `head` of a singly linked list, reverse the list, and return *the reversed list*.

View File

@@ -10,6 +10,24 @@ categories:
patterns:
- two-pointers
function_signature: "def rotate(nums: list[int], k: int) -> list[int]:"
test_cases:
visible:
- input: { nums: [1, 2, 3, 4, 5, 6, 7], k: 3 }
expected: [5, 6, 7, 1, 2, 3, 4]
- input: { nums: [-1, -100, 3, 99], k: 2 }
expected: [3, 99, -1, -100]
- input: { nums: [1, 2], k: 1 }
expected: [2, 1]
hidden:
- input: { nums: [1, 2, 3], k: 4 }
expected: [3, 1, 2]
- input: { nums: [1], k: 0 }
expected: [1]
- input: { nums: [1, 2, 3, 4, 5], k: 5 }
expected: [1, 2, 3, 4, 5]
description: |
Given an integer array `nums`, rotate the array to the right by `k` steps, where `k` is non-negative.

View File

@@ -9,6 +9,22 @@ categories:
patterns:
- matrix-manipulation
function_signature: "def rotate(matrix: list[list[int]]) -> None:"
test_cases:
visible:
- input: { matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] }
expected: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
- input: { matrix: [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]] }
expected: [[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]]
hidden:
- input: { matrix: [[1]] }
expected: [[1]]
- input: { matrix: [[1, 2], [3, 4]] }
expected: [[3, 1], [4, 2]]
- input: { matrix: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] }
expected: [[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]
description: |
You are given an `n × n` 2D matrix representing an image. Rotate the image by **90 degrees** (clockwise).

View File

@@ -10,6 +10,24 @@ patterns:
- bfs
- matrix-traversal
function_signature: "def oranges_rotting(grid: list[list[int]]) -> int:"
test_cases:
visible:
- input: { grid: [[2, 1, 1], [1, 1, 0], [0, 1, 1]] }
expected: 4
- input: { grid: [[2, 1, 1], [0, 1, 1], [1, 0, 1]] }
expected: -1
- input: { grid: [[0, 2]] }
expected: 0
hidden:
- input: { grid: [[0]] }
expected: 0
- input: { grid: [[1]] }
expected: -1
- input: { grid: [[2, 1, 1], [1, 1, 1], [1, 1, 2]] }
expected: 2
description: |
You are given an `m x n` `grid` where each cell can have one of three values:

View File

@@ -10,6 +10,24 @@ patterns:
- dfs
- tree-traversal
function_signature: "def is_same_tree(p: TreeNode | None, q: TreeNode | None) -> bool:"
test_cases:
visible:
- input: { p: [1, 2, 3], q: [1, 2, 3] }
expected: true
- input: { p: [1, 2], q: [1, null, 2] }
expected: false
- input: { p: [1, 2, 1], q: [1, 1, 2] }
expected: false
hidden:
- input: { p: [], q: [] }
expected: true
- input: { p: [1], q: [] }
expected: false
- input: { p: [1, 2, 3, 4, 5], q: [1, 2, 3, 4, 5] }
expected: true
description: |
Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.

View File

@@ -9,6 +9,26 @@ categories:
patterns:
- binary-search
function_signature: "def search(nums: list[int], target: int) -> int:"
test_cases:
visible:
- input: { nums: [4, 5, 6, 7, 0, 1, 2], target: 0 }
expected: 4
- input: { nums: [4, 5, 6, 7, 0, 1, 2], target: 3 }
expected: -1
- input: { nums: [1], target: 0 }
expected: -1
hidden:
- input: { nums: [1], target: 1 }
expected: 0
- input: { nums: [3, 1], target: 1 }
expected: 1
- input: { nums: [5, 1, 3], target: 5 }
expected: 0
- input: { nums: [1, 2, 3, 4, 5], target: 3 }
expected: 2
description: |
There is an integer array `nums` sorted in ascending order (with **distinct** values).

View File

@@ -9,6 +9,22 @@ categories:
patterns:
- matrix-traversal
function_signature: "def set_zeroes(matrix: list[list[int]]) -> None:"
test_cases:
visible:
- input: { matrix: [[1, 1, 1], [1, 0, 1], [1, 1, 1]] }
expected: [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
- input: { matrix: [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]] }
expected: [[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]
hidden:
- input: { matrix: [[1, 2, 3], [4, 5, 6]] }
expected: [[1, 2, 3], [4, 5, 6]]
- input: { matrix: [[0]] }
expected: [[0]]
- input: { matrix: [[1, 0, 3]] }
expected: [[0, 0, 0]]
description: |
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s.

View File

@@ -11,6 +11,30 @@ patterns:
- sliding-window
- monotonic-stack
function_signature: "def max_sliding_window(nums: list[int], k: int) -> list[int]:"
test_cases:
visible:
- input: { nums: [1, 3, -1, -3, 5, 3, 6, 7], k: 3 }
expected: [3, 3, 5, 5, 6, 7]
- input: { nums: [1], k: 1 }
expected: [1]
- input: { nums: [1, -1], k: 1 }
expected: [1, -1]
hidden:
- input: { nums: [9, 11], k: 2 }
expected: [11]
- input: { nums: [4, 3, 2, 1], k: 2 }
expected: [4, 3, 2]
- input: { nums: [1, 2, 3, 4], k: 2 }
expected: [2, 3, 4]
- input: { nums: [7, 7, 7, 7], k: 3 }
expected: [7, 7]
- input: { nums: [1, 3, 1, 2, 0, 5], k: 3 }
expected: [3, 3, 2, 5]
- input: { nums: [-7, -8, 7, 5, 7, 1, 6, 0], k: 4 }
expected: [7, 7, 7, 7, 7]
description: |
You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position.

View File

@@ -10,6 +10,24 @@ categories:
patterns:
- two-pointers
function_signature: "def sort_colors(nums: list[int]) -> None:"
test_cases:
visible:
- input: { nums: [2, 0, 2, 1, 1, 0] }
expected: [0, 0, 1, 1, 2, 2]
- input: { nums: [2, 0, 1] }
expected: [0, 1, 2]
hidden:
- input: { nums: [0] }
expected: [0]
- input: { nums: [1, 0] }
expected: [0, 1]
- input: { nums: [2, 2, 2, 1, 1, 0, 0] }
expected: [0, 0, 1, 1, 2, 2, 2]
- input: { nums: [1, 2, 0] }
expected: [0, 1, 2]
description: |
Given an array `nums` with `n` objects colored red, white, or blue, sort them **in-place** so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

View File

@@ -8,6 +8,24 @@ categories:
patterns:
- matrix-traversal
function_signature: "def spiral_order(matrix: list[list[int]]) -> list[int]:"
test_cases:
visible:
- input: { matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] }
expected: [1, 2, 3, 6, 9, 8, 7, 4, 5]
- input: { matrix: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] }
expected: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
hidden:
- input: { matrix: [[1]] }
expected: [1]
- input: { matrix: [[1, 2, 3, 4]] }
expected: [1, 2, 3, 4]
- input: { matrix: [[1], [2], [3]] }
expected: [1, 2, 3]
- input: { matrix: [[1, 2], [3, 4]] }
expected: [1, 2, 4, 3]
description: |
Given an `m x n` `matrix`, return *all elements of the matrix in spiral order*.

View File

@@ -9,6 +9,24 @@ categories:
patterns:
- prefix-sum
function_signature: "def subarray_sum(nums: list[int], k: int) -> int:"
test_cases:
visible:
- input: { nums: [1, 1, 1], k: 2 }
expected: 2
- input: { nums: [1, 2, 3], k: 3 }
expected: 2
hidden:
- input: { nums: [1], k: 1 }
expected: 1
- input: { nums: [1], k: 0 }
expected: 0
- input: { nums: [-1, -1, 1], k: 0 }
expected: 1
- input: { nums: [1, -1, 0], k: 0 }
expected: 3
description: |
Given an array of integers `nums` and an integer `k`, return *the total number of subarrays whose sum equals to* `k`.

View File

@@ -10,6 +10,26 @@ categories:
patterns:
- backtracking
function_signature: "def subsets_with_dup(nums: list[int]) -> list[list[int]]:"
test_cases:
visible:
- input: { nums: [1, 2, 2] }
expected: [[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]
- input: { nums: [0] }
expected: [[], [0]]
hidden:
- input: { nums: [4, 4, 4, 1, 4] }
expected: [[], [1], [1, 4], [1, 4, 4], [1, 4, 4, 4], [1, 4, 4, 4, 4], [4], [4, 4], [4, 4, 4], [4, 4, 4, 4]]
- input: { nums: [1, 1] }
expected: [[], [1], [1, 1]]
- input: { nums: [1, 2, 3] }
expected: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
- input: { nums: [5, 5, 5] }
expected: [[], [5], [5, 5], [5, 5, 5]]
- input: { nums: [3, 1, 2, 1] }
expected: [[], [1], [1, 1], [1, 1, 2], [1, 1, 2, 3], [1, 1, 3], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
description: |
Given an integer array `nums` that may contain duplicates, return *all possible subsets* (the power set).

View File

@@ -9,6 +9,28 @@ categories:
patterns:
- backtracking
function_signature: "def subsets(nums: list[int]) -> list[list[int]]:"
test_cases:
visible:
- input: { nums: [1, 2, 3] }
expected: [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
- input: { nums: [0] }
expected: [[], [0]]
- input: { nums: [1, 2] }
expected: [[], [1], [2], [1, 2]]
hidden:
- input: { nums: [] }
expected: [[]]
- input: { nums: [5, 10, 15] }
expected: [[], [5], [10], [5, 10], [15], [5, 15], [10, 15], [5, 10, 15]]
- input: { nums: [-1, 0, 1] }
expected: [[], [-1], [0], [-1, 0], [1], [-1, 1], [0, 1], [-1, 0, 1]]
- input: { nums: [1, 2, 3, 4] }
expected: [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3], [4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]
- input: { nums: [9] }
expected: [[], [9]]
description: |
Given an integer array `nums` of **unique** elements, return *all possible subsets* (the power set).

View File

@@ -10,6 +10,24 @@ patterns:
- dfs
- tree-traversal
function_signature: "def is_subtree(root: TreeNode | None, sub_root: TreeNode | None) -> bool:"
test_cases:
visible:
- input: { root: [3, 4, 5, 1, 2], sub_root: [4, 1, 2] }
expected: true
- input: { root: [3, 4, 5, 1, 2, null, null, null, null, 0], sub_root: [4, 1, 2] }
expected: false
- input: { root: [1, 1], sub_root: [1] }
expected: true
hidden:
- input: { root: [1], sub_root: [1] }
expected: true
- input: { root: [1, 2, 3], sub_root: [2] }
expected: true
- input: { root: [1, 2, 3], sub_root: [3, 1] }
expected: false
description: |
Given the roots of two binary trees `root` and `subRoot`, return `true` if there is a subtree of `root` with the same structure and node values of `subRoot` and `false` otherwise.

View File

@@ -10,6 +10,32 @@ patterns:
- dynamic-programming
- backtracking
function_signature: "def find_target_sum_ways(nums: list[int], target: int) -> int:"
test_cases:
visible:
- input: { nums: [1, 1, 1, 1, 1], target: 3 }
expected: 5
- input: { nums: [1], target: 1 }
expected: 1
- input: { nums: [1, 2, 1], target: 0 }
expected: 2
hidden:
- input: { nums: [1], target: 2 }
expected: 0
- input: { nums: [0, 0, 0, 0, 0], target: 0 }
expected: 32
- input: { nums: [2, 3, 5], target: 0 }
expected: 0
- input: { nums: [1, 0], target: 1 }
expected: 2
- input: { nums: [1, 2, 3, 4, 5], target: 3 }
expected: 3
- input: { nums: [100], target: -200 }
expected: 0
- input: { nums: [7, 9, 3, 8, 0, 2, 4, 8, 3, 9], target: 0 }
expected: 0
description: |
You are given an integer array `nums` and an integer `target`.

View File

@@ -11,6 +11,28 @@ patterns:
- greedy
- heap
function_signature: "def least_interval(tasks: list[str], n: int) -> int:"
test_cases:
visible:
- input: { tasks: ["A", "A", "A", "B", "B", "B"], n: 2 }
expected: 8
- input: { tasks: ["A", "C", "A", "B", "D", "B"], n: 1 }
expected: 6
- input: { tasks: ["A", "A", "A", "B", "B", "B"], n: 3 }
expected: 10
hidden:
- input: { tasks: ["A", "A", "A", "B", "B", "B"], n: 0 }
expected: 6
- input: { tasks: ["A", "A", "A", "A", "A", "A", "B", "C", "D", "E", "F", "G"], n: 2 }
expected: 16
- input: { tasks: ["A"], n: 5 }
expected: 1
- input: { tasks: ["A", "B", "C", "D", "E", "A", "B", "C", "D", "E"], n: 4 }
expected: 10
- input: { tasks: ["A", "A", "B", "B", "C", "C", "D", "D"], n: 3 }
expected: 8
description: |
You are given an array of CPU `tasks`, each labelled with a letter from A to Z, and a number `n`. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there's a constraint: there has to be a gap of **at least** `n` intervals between two tasks with the same label.

View File

@@ -10,6 +10,24 @@ categories:
patterns:
- two-pointers
function_signature: "def three_sum(nums: list[int]) -> list[list[int]]:"
test_cases:
visible:
- input: { nums: [-1, 0, 1, 2, -1, -4] }
expected: [[-1, -1, 2], [-1, 0, 1]]
- input: { nums: [0, 1, 1] }
expected: []
- input: { nums: [0, 0, 0] }
expected: [[0, 0, 0]]
hidden:
- input: { nums: [-2, 0, 1, 1, 2] }
expected: [[-2, 0, 2], [-2, 1, 1]]
- input: { nums: [1, 2, -2, -1] }
expected: []
- input: { nums: [-4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6] }
expected: [[-4, -2, 6], [-4, 0, 4], [-4, 1, 3], [-4, 2, 2], [-2, -2, 4], [-2, 0, 2]]
description: |
Given an integer array `nums`, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.

View File

@@ -11,6 +11,24 @@ categories:
patterns:
- heap
function_signature: "def top_k_frequent(nums: list[int], k: int) -> list[int]:"
test_cases:
visible:
- input: { nums: [1, 1, 1, 2, 2, 3], k: 2 }
expected: [1, 2]
- input: { nums: [1], k: 1 }
expected: [1]
- input: { nums: [4, 1, -1, 2, -1, 2, 3], k: 2 }
expected: [-1, 2]
hidden:
- input: { nums: [1, 2], k: 2 }
expected: [1, 2]
- input: { nums: [5, 5, 5, 5, 5], k: 1 }
expected: [5]
- input: { nums: [1, 1, 2, 2, 3, 3, 4], k: 3 }
expected: [1, 2, 3]
description: |
Given an integer array `nums` and an integer `k`, return *the* `k` *most frequent elements*. You may return the answer in **any order**.

View File

@@ -11,6 +11,24 @@ patterns:
- two-pointers
- monotonic-stack
function_signature: "def trap(height: list[int]) -> int:"
test_cases:
visible:
- input: { height: [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] }
expected: 6
- input: { height: [4, 2, 0, 3, 2, 5] }
expected: 9
hidden:
- input: { height: [0, 0, 0] }
expected: 0
- input: { height: [3, 0, 3] }
expected: 3
- input: { height: [1, 2, 3, 4, 5] }
expected: 0
- input: { height: [5, 4, 1, 2] }
expected: 1
description: |
Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water it can trap after raining.

View File

@@ -10,6 +10,30 @@ patterns:
- dynamic-programming
- matrix-traversal
function_signature: "def unique_paths_with_obstacles(obstacle_grid: list[list[int]]) -> int:"
test_cases:
visible:
- input: { obstacle_grid: [[0, 0, 0], [0, 1, 0], [0, 0, 0]] }
expected: 2
- input: { obstacle_grid: [[0, 1], [0, 0]] }
expected: 1
- input: { obstacle_grid: [[0, 0], [0, 0]] }
expected: 2
hidden:
- input: { obstacle_grid: [[1]] }
expected: 0
- input: { obstacle_grid: [[0]] }
expected: 1
- input: { obstacle_grid: [[1, 0]] }
expected: 0
- input: { obstacle_grid: [[0, 0, 0, 0], [0, 0, 0, 0]] }
expected: 4
- input: { obstacle_grid: [[0, 1, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] }
expected: 0
- input: { obstacle_grid: [[0, 0, 0], [0, 0, 0], [0, 0, 1]] }
expected: 0
description: |
You are given an `m x n` integer array `grid`. There is a robot initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m - 1][n - 1]`). The robot can only move either down or right at any point in time.

View File

@@ -10,6 +10,24 @@ categories:
patterns:
- dynamic-programming
function_signature: "def unique_paths(m: int, n: int) -> int:"
test_cases:
visible:
- input: { m: 3, n: 7 }
expected: 28
- input: { m: 3, n: 2 }
expected: 3
- input: { m: 1, n: 1 }
expected: 1
hidden:
- input: { m: 7, n: 3 }
expected: 28
- input: { m: 3, n: 3 }
expected: 6
- input: { m: 10, n: 10 }
expected: 48620
description: |
There is a robot on an `m x n` grid. The robot is initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m - 1][n - 1]`). The robot can only move either down or right at any point in time.

View File

@@ -10,6 +10,24 @@ patterns:
- dfs
- tree-traversal
function_signature: "def is_valid_bst(root: TreeNode | None) -> bool:"
test_cases:
visible:
- input: { root: [2, 1, 3] }
expected: true
- input: { root: [5, 1, 4, null, null, 3, 6] }
expected: false
- input: { root: [1] }
expected: true
hidden:
- input: { root: [5, 4, 6, null, null, 3, 7] }
expected: false
- input: { root: [2, 2, 2] }
expected: false
- input: { root: [10, 5, 15, null, null, 6, 20] }
expected: false
description: |
Given the `root` of a binary tree, determine if it is a valid **binary search tree (BST)**.

View File

@@ -10,6 +10,24 @@ categories:
patterns:
- dynamic-programming
function_signature: "def word_break(s: str, word_dict: list[str]) -> bool:"
test_cases:
visible:
- input: { s: "leetcode", word_dict: ["leet", "code"] }
expected: true
- input: { s: "applepenapple", word_dict: ["apple", "pen"] }
expected: true
- input: { s: "catsandog", word_dict: ["cats", "dog", "sand", "and", "cat"] }
expected: false
hidden:
- input: { s: "a", word_dict: ["a"] }
expected: true
- input: { s: "aaaaaaa", word_dict: ["aaaa", "aaa"] }
expected: true
- input: { s: "cars", word_dict: ["car", "ca", "rs"] }
expected: true
description: |
Given a string `s` and a dictionary of strings `wordDict`, return `true` if `s` can be segmented into a space-separated sequence of one or more dictionary words.

View File

@@ -10,6 +10,28 @@ categories:
patterns:
- bfs
function_signature: "def ladder_length(begin_word: str, end_word: str, word_list: list[str]) -> int:"
test_cases:
visible:
- input: { begin_word: "hit", end_word: "cog", word_list: ["hot", "dot", "dog", "lot", "log", "cog"] }
expected: 5
- input: { begin_word: "hit", end_word: "cog", word_list: ["hot", "dot", "dog", "lot", "log"] }
expected: 0
hidden:
- input: { begin_word: "a", end_word: "c", word_list: ["a", "b", "c"] }
expected: 2
- input: { begin_word: "hot", end_word: "dog", word_list: ["hot", "dog"] }
expected: 0
- input: { begin_word: "hot", end_word: "dog", word_list: ["hot", "dog", "dot"] }
expected: 3
- input: { begin_word: "leet", end_word: "code", word_list: ["lest", "leet", "lose", "code", "lode", "robe", "lost"] }
expected: 0
- input: { begin_word: "cat", end_word: "dog", word_list: ["cat", "bat", "bet", "bot", "dot", "dog"] }
expected: 6
- input: { begin_word: "red", end_word: "tax", word_list: ["ted", "tex", "red", "tax", "tad", "den", "rex", "pee"] }
expected: 4
description: |
A **transformation sequence** from word `beginWord` to word `endWord` using a dictionary `wordList` is a sequence of words `beginWord -> s1 -> s2 -> ... -> sk` such that:

View File

@@ -10,6 +10,24 @@ patterns:
- backtracking
- dfs
function_signature: "def exist(board: list[list[str]], word: str) -> bool:"
test_cases:
visible:
- input: { board: [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], word: "ABCCED" }
expected: true
- input: { board: [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], word: "SEE" }
expected: true
- input: { board: [["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], word: "ABCB" }
expected: false
hidden:
- input: { board: [["A"]], word: "A" }
expected: true
- input: { board: [["A", "B"], ["C", "D"]], word: "ABCD" }
expected: false
- input: { board: [["A", "B"], ["C", "D"]], word: "ABDC" }
expected: true
description: |
Given an `m × n` grid of characters `board` and a string `word`, return `true` if `word` exists in the grid.