feat(content): function signatures + test cases

This commit is contained in:
2025-07-13 19:53:34 +01:00
parent b434eb3a49
commit a7d29b7cce
203 changed files with 4526 additions and 0 deletions

View File

@@ -11,6 +11,26 @@ patterns:
- matrix-traversal - matrix-traversal
- dynamic-programming - dynamic-programming
function_signature: "def update_matrix(mat: list[list[int]]) -> list[list[int]]:"
test_cases:
visible:
- input: { mat: [[0, 0, 0], [0, 1, 0], [0, 0, 0]] }
expected: [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
- input: { mat: [[0, 0, 0], [0, 1, 0], [1, 1, 1]] }
expected: [[0, 0, 0], [0, 1, 0], [1, 2, 1]]
hidden:
- input: { mat: [[0]] }
expected: [[0]]
- input: { mat: [[1, 0, 1], [1, 1, 1], [1, 1, 1]] }
expected: [[1, 0, 1], [2, 1, 2], [3, 2, 3]]
- input: { mat: [[1, 1, 1], [1, 1, 1], [1, 1, 0]] }
expected: [[4, 3, 2], [3, 2, 1], [2, 1, 0]]
- input: { mat: [[0, 1], [1, 1]] }
expected: [[0, 1], [1, 2]]
- input: { mat: [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]] }
expected: [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]
description: | description: |
Given an `m x n` binary matrix `mat`, return *the distance of the nearest* `0` *for each cell*. Given an `m x n` binary matrix `mat`, return *the distance of the nearest* `0` *for each cell*.

View File

@@ -9,6 +9,30 @@ categories:
patterns: patterns:
- monotonic-stack - monotonic-stack
function_signature: "def find132pattern(nums: list[int]) -> bool:"
test_cases:
visible:
- input: { nums: [1, 2, 3, 4] }
expected: false
- input: { nums: [3, 1, 4, 2] }
expected: true
- input: { nums: [-1, 3, 2, 0] }
expected: true
hidden:
- input: { nums: [1, 2] }
expected: false
- input: { nums: [3, 5, 0, 3, 4] }
expected: true
- input: { nums: [1, 0, 1, -4, -3] }
expected: false
- input: { nums: [1, 2, 2, 2] }
expected: false
- input: { nums: [-2, 1, 2, -2, 1, 2] }
expected: true
- input: { nums: [1, 3, 2, 4, 5, 6, 7, 8, 9, 10] }
expected: true
description: | description: |
Given an array of `n` integers `nums`, a **132 pattern** is a subsequence of three integers `nums[i]`, `nums[j]` and `nums[k]` such that `i < j < k` and `nums[i] < nums[k] < nums[j]`. Given an array of `n` integers `nums`, a **132 pattern** is a subsequence of three integers `nums[i]`, `nums[j]` and `nums[k]` such that `i < j < k` and `nums[i] < nums[k] < nums[j]`.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def min_steps(n: int) -> int:"
test_cases:
visible:
- input: { n: 3 }
expected: 3
- input: { n: 1 }
expected: 0
hidden:
- input: { n: 4 }
expected: 4
- input: { n: 6 }
expected: 5
- input: { n: 12 }
expected: 7
- input: { n: 17 }
expected: 17
- input: { n: 100 }
expected: 14
- input: { n: 1000 }
expected: 21
description: | description: |
There is only one character `'A'` on the screen of a notepad. You can perform one of two operations on this notepad for each step: There is only one character `'A'` on the screen of a notepad. You can perform one of two operations on this notepad for each step:

View File

@@ -10,6 +10,28 @@ categories:
patterns: patterns:
- backtracking - backtracking
function_signature: "def judge_point24(cards: list[int]) -> bool:"
test_cases:
visible:
- input: { cards: [4, 1, 8, 7] }
expected: true
- input: { cards: [1, 2, 1, 2] }
expected: false
hidden:
- input: { cards: [8, 3, 8, 3] }
expected: true
- input: { cards: [1, 1, 1, 1] }
expected: false
- input: { cards: [1, 5, 5, 5] }
expected: true
- input: { cards: [3, 3, 8, 8] }
expected: true
- input: { cards: [1, 9, 1, 2] }
expected: true
- input: { cards: [1, 1, 7, 7] }
expected: false
description: | description: |
You are given an integer array `cards` of length `4`. You have four cards, each containing a number in the range `[1, 9]`. You should arrange the numbers on these cards in a mathematical expression using the operators `['+', '-', '*', '/']` and the parentheses `'('` and `')'` to get the value 24. You are given an integer array `cards` of length `4`. You have four cards, each containing a number in the range `[1, 9]`. You should arrange the numbers on these cards in a mathematical expression using the operators `['+', '-', '*', '/']` and the parentheses `'('` and `')'` to get the value 24.

View File

@@ -10,6 +10,28 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def three_sum_multi(arr: list[int], target: int) -> int:"
test_cases:
visible:
- input: { arr: [1, 1, 2, 2, 3, 3, 4, 4, 5, 5], target: 8 }
expected: 20
- input: { arr: [1, 1, 2, 2, 2, 2], target: 5 }
expected: 12
- input: { arr: [2, 1, 3], target: 6 }
expected: 1
hidden:
- input: { arr: [0, 0, 0], target: 0 }
expected: 1
- input: { arr: [0, 0, 0, 0, 0], target: 0 }
expected: 10
- input: { arr: [1, 2, 3, 4, 5, 6], target: 12 }
expected: 4
- input: { arr: [3, 3, 3, 3, 3], target: 9 }
expected: 10
- input: { arr: [1, 1, 1, 1, 1, 1], target: 3 }
expected: 20
description: | description: |
Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`. Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`.

View File

@@ -9,6 +9,24 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def four_sum_count(nums1: list[int], nums2: list[int], nums3: list[int], nums4: list[int]) -> int:"
test_cases:
visible:
- input: { nums1: [1, 2], nums2: [-2, -1], nums3: [-1, 2], nums4: [0, 2] }
expected: 2
- input: { nums1: [0], nums2: [0], nums3: [0], nums4: [0] }
expected: 1
hidden:
- input: { nums1: [-1, -1], nums2: [-1, 1], nums3: [-1, 1], nums4: [1, -1] }
expected: 6
- input: { nums1: [1], nums2: [-1], nums3: [0], nums4: [0] }
expected: 1
- input: { nums1: [0, 1, -1], nums2: [-1, 1, 0], nums3: [0, 0, 1], nums4: [-1, 1, 1] }
expected: 17
- input: { nums1: [1, 1, 1], nums2: [1, 1, 1], nums3: [-1, -1, -1], nums4: [-1, -1, -1] }
expected: 81
description: | description: |
Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that: Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:

View File

@@ -8,6 +8,28 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def is_same_after_reversals(num: int) -> bool:"
test_cases:
visible:
- input: { num: 526 }
expected: true
- input: { num: 1800 }
expected: false
- input: { num: 0 }
expected: true
hidden:
- input: { num: 1 }
expected: true
- input: { num: 10 }
expected: false
- input: { num: 100 }
expected: false
- input: { num: 123 }
expected: true
- input: { num: 12300 }
expected: false
description: | description: |
**Reversing** an integer means to reverse all its digits. **Reversing** an integer means to reverse all its digits.

View File

@@ -11,6 +11,22 @@ patterns:
- union-find - union-find
- dfs - dfs
function_signature: "def accounts_merge(accounts: list[list[str]]) -> list[list[str]]:"
test_cases:
visible:
- input: { accounts: [["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["John", "johnsmith@mail.com", "john00@mail.com"], ["Mary", "mary@mail.com"], ["John", "johnnybravo@mail.com"]] }
expected: [["John", "john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com"], ["Mary", "mary@mail.com"], ["John", "johnnybravo@mail.com"]]
- input: { accounts: [["Gabe", "Gabe0@m.co", "Gabe3@m.co", "Gabe1@m.co"], ["Kevin", "Kevin3@m.co", "Kevin5@m.co", "Kevin0@m.co"]] }
expected: [["Gabe", "Gabe0@m.co", "Gabe1@m.co", "Gabe3@m.co"], ["Kevin", "Kevin0@m.co", "Kevin3@m.co", "Kevin5@m.co"]]
hidden:
- input: { accounts: [["Alex", "a@mail.com"]] }
expected: [["Alex", "a@mail.com"]]
- input: { accounts: [["David", "d1@m.co", "d2@m.co"], ["David", "d2@m.co", "d3@m.co"], ["David", "d3@m.co", "d4@m.co"]] }
expected: [["David", "d1@m.co", "d2@m.co", "d3@m.co", "d4@m.co"]]
- input: { accounts: [["A", "a@a.com"], ["B", "b@b.com"], ["A", "a@a.com"]] }
expected: [["A", "a@a.com"], ["B", "b@b.com"]]
description: | description: |
Given a list of `accounts` where each element `accounts[i]` is a list of strings, where the first element `accounts[i][0]` is a name, and the rest of the elements are **emails** representing emails of the account. Given a list of `accounts` where each element `accounts[i]` is a list of strings, where the first element `accounts[i][0]` is a name, and the rest of the elements are **emails** representing emails of the account.

View File

@@ -8,6 +8,28 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def add_rungs(rungs: list[int], dist: int) -> int:"
test_cases:
visible:
- input: { rungs: [1, 3, 5, 10], dist: 2 }
expected: 2
- input: { rungs: [3, 6, 8, 10], dist: 3 }
expected: 0
- input: { rungs: [3, 4, 6, 7], dist: 2 }
expected: 1
hidden:
- input: { rungs: [5], dist: 10 }
expected: 0
- input: { rungs: [10], dist: 3 }
expected: 3
- input: { rungs: [1, 100], dist: 10 }
expected: 9
- input: { rungs: [1, 2, 3, 4, 5], dist: 1 }
expected: 0
- input: { rungs: [4, 8, 12, 16], dist: 3 }
expected: 4
description: | description: |
You are given a **strictly increasing** integer array `rungs` that represents the **height** of rungs on a ladder. You are currently on the **floor** at height `0`, and you want to reach the last rung. You are given a **strictly increasing** integer array `rungs` that represents the **height** of rungs on a ladder. You are currently on the **floor** at height `0`, and you want to reach the last rung.

View File

@@ -12,6 +12,24 @@ patterns:
- dfs - dfs
- tree-traversal - tree-traversal
function_signature: "def add_one_row(root: TreeNode, val: int, depth: int) -> TreeNode:"
test_cases:
visible:
- input: { root: [4, 2, 6, 3, 1, 5], val: 1, depth: 2 }
expected: [4, 1, 1, 2, null, null, 6, 3, 1, 5]
- input: { root: [4, 2, null, 3, 1], val: 1, depth: 3 }
expected: [4, 2, null, 1, 1, 3, null, null, 1]
hidden:
- input: { root: [1], val: 2, depth: 1 }
expected: [2, 1]
- input: { root: [1, 2, 3], val: 5, depth: 2 }
expected: [1, 5, 5, 2, null, null, 3]
- input: { root: [1], val: 2, depth: 2 }
expected: [1, 2, 2]
- input: { root: [4, 2, 6, 3, 1, 5], val: 1, depth: 1 }
expected: [1, 4, null, 2, 6, 3, 1, 5]
description: | description: |
Given the `root` of a binary tree and two integers `val` and `depth`, add a row of nodes with value `val` at the given depth `depth`. Given the `root` of a binary tree and two integers `val` and `depth`, add a row of nodes with value `val` at the given depth `depth`.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def add_to_array_form(num: list[int], k: int) -> list[int]:"
test_cases:
visible:
- input: { num: [1, 2, 0, 0], k: 34 }
expected: [1, 2, 3, 4]
- input: { num: [2, 7, 4], k: 181 }
expected: [4, 5, 5]
- input: { num: [2, 1, 5], k: 806 }
expected: [1, 0, 2, 1]
hidden:
- input: { num: [0], k: 1 }
expected: [1]
- input: { num: [9, 9, 9], k: 1 }
expected: [1, 0, 0, 0]
- input: { num: [1], k: 99 }
expected: [1, 0, 0]
- input: { num: [0], k: 10000 }
expected: [1, 0, 0, 0, 0]
- input: { num: [5, 5, 5], k: 555 }
expected: [1, 1, 1, 0]
description: | description: |
The **array-form** of an integer `num` is an array representing its digits in left to right order. The **array-form** of an integer `num` is an array representing its digits in left to right order.

View File

@@ -7,6 +7,26 @@ categories:
- math - math
patterns: [] patterns: []
function_signature: "def sum(num1: int, num2: int) -> int:"
test_cases:
visible:
- input: { num1: 12, num2: 5 }
expected: 17
- input: { num1: -10, num2: 4 }
expected: -6
hidden:
- input: { num1: 0, num2: 0 }
expected: 0
- input: { num1: -100, num2: 100 }
expected: 0
- input: { num1: 50, num2: 50 }
expected: 100
- input: { num1: -50, num2: -50 }
expected: -100
- input: { num1: 1, num2: -1 }
expected: 0
description: | description: |
Given two integers `num1` and `num2`, return *the **sum** of the two integers*. Given two integers `num1` and `num2`, return *the **sum** of the two integers*.

View File

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

View File

@@ -10,6 +10,26 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def add_spaces(s: str, spaces: list[int]) -> str:"
test_cases:
visible:
- input: { s: "LeetcodeHelpsMeLearn", spaces: [8, 13, 15] }
expected: "Leetcode Helps Me Learn"
- input: { s: "icodeinpython", spaces: [1, 5, 7, 9] }
expected: "i code in py thon"
- input: { s: "spacing", spaces: [0, 1, 2, 3, 4, 5, 6] }
expected: " s p a c i n g"
hidden:
- input: { s: "abc", spaces: [1] }
expected: "a bc"
- input: { s: "abc", spaces: [0] }
expected: " abc"
- input: { s: "EnjoyYourCoffee", spaces: [5, 9] }
expected: "Enjoy Your Coffee"
- input: { s: "hello", spaces: [1, 2, 3, 4] }
expected: "h e l l o"
description: | description: |
You are given a **0-indexed** string `s` and a **0-indexed** integer array `spaces` that describes the indices in the original string where spaces will be added. Each space should be inserted **before** the character at the given index. You are given a **0-indexed** string `s` and a **0-indexed** integer array `spaces` that describes the indices in the original string where spaces will be added. Each space should be inserted **before** the character at the given index.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def add_negabinary(arr1: list[int], arr2: list[int]) -> list[int]:"
test_cases:
visible:
- input: { arr1: [1, 1, 1, 1, 1], arr2: [1, 0, 1] }
expected: [1, 0, 0, 0, 0]
- input: { arr1: [0], arr2: [0] }
expected: [0]
- input: { arr1: [0], arr2: [1] }
expected: [1]
hidden:
- input: { arr1: [1], arr2: [1] }
expected: [1, 1, 0]
- input: { arr1: [1, 0], arr2: [1, 0] }
expected: [1, 1, 0, 0]
- input: { arr1: [1, 1], arr2: [1, 1] }
expected: [0]
- input: { arr1: [1, 0, 1], arr2: [1, 0, 1] }
expected: [1, 1, 1, 1, 0]
description: | description: |
Given two numbers `arr1` and `arr2` in base **-2** (negabinary), return the result of adding them together. Given two numbers `arr1` and `arr2` in base **-2** (negabinary), return the result of adding them together.

View File

@@ -9,6 +9,30 @@ categories:
patterns: patterns:
- backtracking - backtracking
function_signature: "def is_additive_number(num: str) -> bool:"
test_cases:
visible:
- input: { num: "112358" }
expected: true
- input: { num: "199100199" }
expected: true
- input: { num: "1023" }
expected: false
hidden:
- input: { num: "123" }
expected: true
- input: { num: "12" }
expected: false
- input: { num: "101" }
expected: true
- input: { num: "000" }
expected: true
- input: { num: "111" }
expected: false
- input: { num: "1212122436" }
expected: true
description: | description: |
An **additive number** is a string whose digits can form an **additive sequence**. An **additive number** is a string whose digits can form an **additive sequence**.

View File

@@ -10,6 +10,24 @@ patterns:
- greedy - greedy
- two-pointers - two-pointers
function_signature: "def advantage_count(nums1: list[int], nums2: list[int]) -> list[int]:"
test_cases:
visible:
- input: { nums1: [2, 7, 11, 15], nums2: [1, 10, 4, 11] }
expected: [2, 11, 7, 15]
- input: { nums1: [12, 24, 8, 32], nums2: [13, 25, 32, 11] }
expected: [24, 32, 8, 12]
hidden:
- input: { nums1: [1], nums2: [1] }
expected: [1]
- input: { nums1: [5, 5, 5], nums2: [1, 2, 3] }
expected: [5, 5, 5]
- input: { nums1: [1, 2, 3], nums2: [3, 2, 1] }
expected: [1, 3, 2]
- input: { nums1: [2, 0, 4, 1, 2], nums2: [1, 3, 0, 0, 2] }
expected: [2, 4, 1, 2, 0]
description: | description: |
You are given two integer arrays `nums1` and `nums2` both of the same length. The **advantage** of `nums1` with respect to `nums2` is the number of indices `i` for which `nums1[i] > nums2[i]`. You are given two integer arrays `nums1` and `nums2` both of the same length. The **advantage** of `nums1` with respect to `nums2` is the number of indices `i` for which `nums1[i] > nums2[i]`.

View File

@@ -9,6 +9,24 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def nth_person_gets_nth_seat(n: int) -> float:"
test_cases:
visible:
- input: { n: 1 }
expected: 1.0
- input: { n: 2 }
expected: 0.5
hidden:
- input: { n: 3 }
expected: 0.5
- input: { n: 10 }
expected: 0.5
- input: { n: 100 }
expected: 0.5
- input: { n: 1000 }
expected: 0.5
description: | description: |
`n` passengers board an airplane with exactly `n` seats. The first passenger has lost their ticket and picks a seat randomly. After that, the rest of the passengers will: `n` passengers board an airplane with exactly `n` seats. The first passenger has lost their ticket and picks a seat randomly. After that, the rest of the passengers will:

View File

@@ -11,6 +11,24 @@ categories:
patterns: patterns:
- sliding-window - sliding-window
function_signature: "def alert_names(key_name: list[str], key_time: list[str]) -> list[str]:"
test_cases:
visible:
- input: { key_name: ["daniel", "daniel", "daniel", "luis", "luis", "luis", "luis"], key_time: ["10:00", "10:40", "11:00", "09:00", "11:00", "13:00", "15:00"] }
expected: ["daniel"]
- input: { key_name: ["alice", "alice", "alice", "bob", "bob", "bob", "bob"], key_time: ["12:01", "12:00", "18:00", "21:00", "21:20", "21:30", "23:00"] }
expected: ["bob"]
hidden:
- input: { key_name: ["a", "a", "a"], key_time: ["01:00", "01:30", "02:00"] }
expected: ["a"]
- input: { key_name: ["a", "b", "c"], key_time: ["01:00", "02:00", "03:00"] }
expected: []
- input: { key_name: ["a", "a", "a", "b", "b", "b"], key_time: ["00:00", "00:30", "01:00", "23:00", "23:30", "23:59"] }
expected: ["a", "b"]
- input: { key_name: ["john", "john", "john"], key_time: ["08:00", "09:01", "10:00"] }
expected: []
description: | description: |
LeetCode company workers use key-cards to unlock office doors. Each time a worker uses their key-card, the security system saves the worker's name and the time when it was used. The system emits an **alert** if any worker uses the key-card **three or more times** in a one-hour period. LeetCode company workers use key-cards to unlock office doors. Each time a worker uses their key-card, the security system saves the worker's name and the time when it was used. The system emits an **alert** if any worker uses the key-card **three or more times** in a one-hour period.

View File

@@ -8,6 +8,28 @@ categories:
patterns: patterns:
- prefix-sum - prefix-sum
function_signature: "def max_score_indices(nums: list[int]) -> list[int]:"
test_cases:
visible:
- input: { nums: [0, 0, 1, 0] }
expected: [2, 4]
- input: { nums: [0, 0, 0] }
expected: [3]
- input: { nums: [1, 1] }
expected: [0]
hidden:
- input: { nums: [0] }
expected: [1]
- input: { nums: [1] }
expected: [0]
- input: { nums: [0, 1, 0, 1] }
expected: [2]
- input: { nums: [1, 0, 1, 0, 1] }
expected: [0, 2, 4]
- input: { nums: [0, 0, 0, 0] }
expected: [4]
description: | description: |
You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n`) into two arrays (possibly empty) `nums_left` and `nums_right`: You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n`) into two arrays (possibly empty) `nums_left` and `nums_right`:

View File

@@ -10,6 +10,24 @@ patterns:
- tree-traversal - tree-traversal
- two-pointers - two-pointers
function_signature: "def get_all_elements(root1: TreeNode, root2: TreeNode) -> list[int]:"
test_cases:
visible:
- input: { root1: [2, 1, 4], root2: [1, 0, 3] }
expected: [0, 1, 1, 2, 3, 4]
- input: { root1: [1, null, 8], root2: [8, 1] }
expected: [1, 1, 8, 8]
hidden:
- input: { root1: [], root2: [5, 1, 7] }
expected: [1, 5, 7]
- input: { root1: [1], root2: [] }
expected: [1]
- input: { root1: [], root2: [] }
expected: []
- input: { root1: [0, -10, 10], root2: [5, 1, 7, 0, 2] }
expected: [-10, 0, 0, 1, 2, 5, 7, 10]
description: | description: |
Given two binary search trees `root1` and `root2`, return *a list containing all the integers from both trees sorted in **ascending** order*. Given two binary search trees `root1` and `root2`, return *a list containing all the integers from both trees sorted in **ascending** order*.

View File

@@ -11,6 +11,26 @@ patterns:
- bfs - bfs
- dfs - dfs
function_signature: "def distance_k(root: TreeNode, target: TreeNode, k: int) -> list[int]:"
test_cases:
visible:
- input: { root: [3, 5, 1, 6, 2, null, 8, null, null, 7, 4], target: 5, k: 2 }
expected: [7, 4, 1]
- input: { root: [1], target: 1, k: 3 }
expected: []
hidden:
- input: { root: [0, 1, null, 3, 2], target: 2, k: 1 }
expected: [1]
- input: { root: [0, 1, 2], target: 0, k: 1 }
expected: [1, 2]
- input: { root: [0, 1, 2], target: 0, k: 0 }
expected: [0]
- input: { root: [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4], target: 3, k: 1 }
expected: [5, 1]
- input: { root: [1, 2, 3, 4], target: 4, k: 2 }
expected: [1]
description: | description: |
Given the `root` of a binary tree, the value of a target node `target`, and an integer `k`, return *an array of the values of all nodes that have a distance* `k` *from the target node*. Given the `root` of a binary tree, the value of a target node `target`, and an integer `k`, return *an array of the values of all nodes that have a distance* `k` *from the target node*.

View File

@@ -10,6 +10,24 @@ patterns:
- dfs - dfs
- backtracking - backtracking
function_signature: "def all_paths_source_target(graph: list[list[int]]) -> list[list[int]]:"
test_cases:
visible:
- input: { graph: [[1, 2], [3], [3], []] }
expected: [[0, 1, 3], [0, 2, 3]]
- input: { graph: [[4, 3, 1], [3, 2, 4], [3], [4], []] }
expected: [[0, 4], [0, 3, 4], [0, 1, 3, 4], [0, 1, 2, 3, 4], [0, 1, 4]]
hidden:
- input: { graph: [[1], []] }
expected: [[0, 1]]
- input: { graph: [[1, 2, 3], [4], [4], [4], []] }
expected: [[0, 1, 4], [0, 2, 4], [0, 3, 4]]
- input: { graph: [[2, 1], [2], []] }
expected: [[0, 2], [0, 1, 2]]
- input: { graph: [[1, 3], [2], [3], []] }
expected: [[0, 1, 2, 3], [0, 3]]
description: | description: |
Given a directed acyclic graph (**DAG**) of `n` nodes labeled from `0` to `n - 1`, find all possible paths from node `0` to node `n - 1` and return them in **any order**. Given a directed acyclic graph (**DAG**) of `n` nodes labeled from `0` to `n - 1`, find all possible paths from node `0` to node `n - 1` and return them in **any order**.

View File

@@ -11,6 +11,26 @@ patterns:
- backtracking - backtracking
- dynamic-programming - dynamic-programming
function_signature: "def all_possible_fbt(n: int) -> list[TreeNode]:"
test_cases:
visible:
- input: { n: 7 }
expected: 5
- input: { n: 3 }
expected: 1
hidden:
- input: { n: 1 }
expected: 1
- input: { n: 2 }
expected: 0
- input: { n: 5 }
expected: 2
- input: { n: 9 }
expected: 14
- input: { n: 4 }
expected: 0
description: | description: |
Given an integer `n`, return *a list of all possible **full binary trees** with* `n` *nodes*. Each node of each tree in the answer must have `Node.val == 0`. Given an integer `n`, return *a list of all possible **full binary trees** with* `n` *nodes*. Each node of each tree in the answer must have `Node.val == 0`.

View File

@@ -11,6 +11,26 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def min_distance(houses: list[int], k: int) -> int:"
test_cases:
visible:
- input: { houses: [1, 4, 8, 10, 20], k: 3 }
expected: 5
- input: { houses: [2, 3, 5, 12, 18], k: 2 }
expected: 9
hidden:
- input: { houses: [1, 2, 3], k: 1 }
expected: 2
- input: { houses: [1, 2, 3], k: 3 }
expected: 0
- input: { houses: [7, 4, 6, 1], k: 1 }
expected: 8
- input: { houses: [3, 6, 14, 10], k: 4 }
expected: 0
- input: { houses: [1, 10, 100], k: 2 }
expected: 9
description: | description: |
Given the array `houses` where `houses[i]` is the location of the i<sup>th</sup> house along a street and an integer `k`, allocate `k` mailboxes in the street. Given the array `houses` where `houses[i]` is the location of the i<sup>th</sup> house along a street and an integer `k`, allocate `k` mailboxes in the street.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- matrix-traversal - matrix-traversal
function_signature: "def alphabet_board_path(target: str) -> str:"
test_cases:
visible:
- input: { target: "leet" }
expected: "DDR!UURRR!!DDD!"
- input: { target: "code" }
expected: "RR!DDRR!UUL!R!"
hidden:
- input: { target: "a" }
expected: "!"
- input: { target: "z" }
expected: "DDDDD!"
- input: { target: "zdz" }
expected: "DDDDD!UUUUURRR!LLLDDDDD!"
- input: { target: "aa" }
expected: "!!"
- input: { target: "abc" }
expected: "!R!R!"
description: | description: |
On an alphabet board, we start at position `(0, 0)`, corresponding to character `board[0][0]`. On an alphabet board, we start at position `(0, 0)`, corresponding to character `board[0][0]`.

View File

@@ -8,6 +8,28 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def alternate_digit_sum(n: int) -> int:"
test_cases:
visible:
- input: { n: 521 }
expected: 4
- input: { n: 111 }
expected: 1
- input: { n: 886996 }
expected: 0
hidden:
- input: { n: 1 }
expected: 1
- input: { n: 10 }
expected: 1
- input: { n: 99 }
expected: 0
- input: { n: 12345 }
expected: 3
- input: { n: 54321 }
expected: 3
description: | description: |
You are given a positive integer `n`. Each digit of `n` has a sign according to the following rules: You are given a positive integer `n`. Each digit of `n` has a sign according to the following rules:

View File

@@ -9,6 +9,22 @@ categories:
patterns: patterns:
- backtracking - backtracking
function_signature: "def ambiguous_coordinates(s: str) -> list[str]:"
test_cases:
visible:
- input: { s: "(123)" }
expected: ["(1, 2.3)", "(1, 23)", "(1.2, 3)", "(12, 3)"]
- input: { s: "(0123)" }
expected: ["(0, 1.23)", "(0, 12.3)", "(0, 123)", "(0.1, 2.3)", "(0.1, 23)", "(0.12, 3)"]
hidden:
- input: { s: "(00011)" }
expected: ["(0, 0.011)", "(0.001, 1)"]
- input: { s: "(100)" }
expected: ["(10, 0)", "(1, 0.0)"]
- input: { s: "(12)" }
expected: ["(1, 2)"]
description: | description: |
We had some 2-dimensional coordinates, like `"(1, 3)"` or `"(2, 0.5)"`. Then, we removed all commas, decimal points, and spaces and ended up with the string `s`. We had some 2-dimensional coordinates, like `"(1, 3)"` or `"(2, 0.5)"`. Then, we removed all commas, decimal points, and spaces and ended up with the string `s`.

View File

@@ -11,6 +11,26 @@ patterns:
- bfs - bfs
- tree-traversal - tree-traversal
function_signature: "def amount_of_time(root: TreeNode, start: int) -> int:"
test_cases:
visible:
- input: { root: [1, 5, 3, null, 4, 10, 6, 9, 2], start: 3 }
expected: 4
- input: { root: [1], start: 1 }
expected: 0
hidden:
- input: { root: [1, 2, null, 3], start: 3 }
expected: 2
- input: { root: [1, 2, 3], start: 2 }
expected: 2
- input: { root: [1, 2, 3, 4, 5, 6, 7], start: 4 }
expected: 4
- input: { root: [1, 2, 3, 4, 5], start: 1 }
expected: 2
- input: { root: [2, 5, null, null, 3, 4, null, 10, 9, null, null, 1], start: 9 }
expected: 5
description: | description: |
You are given the `root` of a binary tree with **unique** values, and an integer `start`. At minute `0`, an **infection** starts from the node with value `start`. You are given the `root` of a binary tree with **unique** values, and an integer `start`. At minute `0`, an **infection** starts from the node with value `start`.

View File

@@ -8,6 +8,28 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def angle_clock(hour: int, minutes: int) -> float:"
test_cases:
visible:
- input: { hour: 12, minutes: 30 }
expected: 165.0
- input: { hour: 3, minutes: 30 }
expected: 75.0
- input: { hour: 3, minutes: 15 }
expected: 7.5
hidden:
- input: { hour: 12, minutes: 0 }
expected: 0.0
- input: { hour: 6, minutes: 0 }
expected: 180.0
- input: { hour: 1, minutes: 57 }
expected: 76.5
- input: { hour: 4, minutes: 50 }
expected: 155.0
- input: { hour: 9, minutes: 0 }
expected: 90.0
description: | description: |
Given two numbers, `hour` and `minutes`, return *the smaller angle (in degrees) formed between the hour and the minute hand*. Given two numbers, `hour` and `minutes`, return *the smaller angle (in degrees) formed between the hour and the minute hand*.

View File

@@ -10,6 +10,28 @@ patterns:
- two-pointers - two-pointers
- greedy - greedy
function_signature: "def append_characters(s: str, t: str) -> int:"
test_cases:
visible:
- input: { s: "coaching", t: "coding" }
expected: 4
- input: { s: "abcde", t: "a" }
expected: 0
- input: { s: "z", t: "abcde" }
expected: 5
hidden:
- input: { s: "abc", t: "abc" }
expected: 0
- input: { s: "abc", t: "d" }
expected: 1
- input: { s: "aaa", t: "aaaa" }
expected: 1
- input: { s: "xyz", t: "xyz" }
expected: 0
- input: { s: "abc", t: "axbxc" }
expected: 4
description: | description: |
You are given two strings `s` and `t` consisting of only lowercase English letters. You are given two strings `s` and `t` consisting of only lowercase English letters.

View File

@@ -10,6 +10,28 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def min_sum(nums: list[int], k: int) -> int:"
test_cases:
visible:
- input: { nums: [1, 4, 25, 10, 25], k: 2 }
expected: 5
- input: { nums: [5, 6], k: 6 }
expected: 25
hidden:
- input: { nums: [1], k: 1 }
expected: 2
- input: { nums: [1, 2, 3], k: 2 }
expected: 9
- input: { nums: [2, 4, 6], k: 3 }
expected: 6
- input: { nums: [1000000000], k: 1 }
expected: 1
- input: { nums: [1, 2, 3, 4, 5], k: 5 }
expected: 40
- input: { nums: [3, 3, 3], k: 4 }
expected: 12
description: | description: |
You are given an integer array `nums` and an integer `k`. Append `k` **unique positive** integers that do **not** appear in `nums` to `nums` such that the resulting total sum is **minimum**. You are given an integer array `nums` and an integer `k`. Append `k` **unique positive** integers that do **not** appear in `nums` to `nums` such that the resulting total sum is **minimum**.

View File

@@ -9,6 +9,30 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def make_strings_equal(s: str, target: str) -> bool:"
test_cases:
visible:
- input: { s: "1010", target: "0110" }
expected: true
- input: { s: "11", target: "00" }
expected: false
hidden:
- input: { s: "00", target: "00" }
expected: true
- input: { s: "00", target: "11" }
expected: false
- input: { s: "11", target: "11" }
expected: true
- input: { s: "10", target: "01" }
expected: true
- input: { s: "0000", target: "0000" }
expected: true
- input: { s: "1111", target: "0001" }
expected: true
- input: { s: "0001", target: "1110" }
expected: true
description: | description: |
You are given two **0-indexed binary** strings `s` and `target` of the same length `n`. You can do the following operation on `s` **any** number of times: You are given two **0-indexed binary** strings `s` and `target` of the same length `n`. You can do the following operation on `s` **any** number of times:

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def apply_operations(nums: list[int]) -> list[int]:"
test_cases:
visible:
- input: { nums: [1, 2, 2, 1, 1, 0] }
expected: [1, 4, 2, 0, 0, 0]
- input: { nums: [0, 1] }
expected: [1, 0]
hidden:
- input: { nums: [1, 1] }
expected: [2, 0]
- input: { nums: [2, 2, 2] }
expected: [4, 2, 0]
- input: { nums: [0, 0, 0] }
expected: [0, 0, 0]
- input: { nums: [1, 2, 3, 4] }
expected: [1, 2, 3, 4]
- input: { nums: [1, 1, 1, 1] }
expected: [2, 2, 0, 0]
description: | description: |
You are given a **0-indexed** array `nums` of size `n` consisting of **non-negative** integers. You are given a **0-indexed** array `nums` of size `n` consisting of **non-negative** integers.

View File

@@ -10,6 +10,30 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def number_of_arithmetic_slices(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [2, 4, 6, 8, 10] }
expected: 7
- input: { nums: [7, 7, 7, 7, 7] }
expected: 16
hidden:
- input: { nums: [1] }
expected: 0
- input: { nums: [1, 2] }
expected: 0
- input: { nums: [1, 2, 3] }
expected: 1
- input: { nums: [1, 3, 5, 7] }
expected: 3
- input: { nums: [0, 0, 0, 0] }
expected: 5
- input: { nums: [1, 1, 1, 1, 1, 1] }
expected: 42
- input: { nums: [1, 2, 3, 4, 5, 6] }
expected: 12
description: | description: |
Given an integer array `nums`, return *the number of all the **arithmetic subsequences** of* `nums`. Given an integer array `nums`, return *the number of all the **arithmetic subsequences** of* `nums`.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def number_of_arithmetic_slices(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [1, 2, 3, 4] }
expected: 3
- input: { nums: [1] }
expected: 0
hidden:
- input: { nums: [1, 2] }
expected: 0
- input: { nums: [1, 2, 3] }
expected: 1
- input: { nums: [1, 2, 3, 4, 5] }
expected: 6
- input: { nums: [7, 7, 7, 7] }
expected: 3
- input: { nums: [1, 2, 3, 5, 7] }
expected: 1
description: | description: |
An integer array is called *arithmetic* if it consists of **at least three elements** and if the difference between any two consecutive elements is the same. An integer array is called *arithmetic* if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.

View File

@@ -10,6 +10,30 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def check_arithmetic_subarrays(nums: list[int], l: list[int], r: list[int]) -> list[bool]:"
test_cases:
visible:
- input: { nums: [4, 6, 5, 9, 3, 7], l: [0, 0, 2], r: [2, 3, 5] }
expected: [true, false, true]
- input: { nums: [-12, -9, -3, -12, -6, 15, 20, -25, -20, -15, -10], l: [0, 1, 6, 4, 8, 7], r: [4, 4, 9, 7, 9, 10] }
expected: [false, true, false, false, true, true]
hidden:
- input: { nums: [1, 2, 3], l: [0], r: [2] }
expected: [true]
- input: { nums: [7, 7, 7, 7], l: [0], r: [3] }
expected: [true]
- input: { nums: [1, 2, 4], l: [0], r: [2] }
expected: [false]
- input: { nums: [5, 3], l: [0], r: [1] }
expected: [true]
- input: { nums: [1, 3, 3, 5], l: [0], r: [3] }
expected: [false]
- input: { nums: [10, 20, 30, 40, 50], l: [0, 1, 2], r: [4, 3, 4] }
expected: [true, true, true]
- input: { nums: [100, -100], l: [0], r: [1] }
expected: [true]
description: | description: |
A sequence of numbers is called **arithmetic** if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence `s` is arithmetic if and only if `s[i+1] - s[i] == s[1] - s[0]` for all valid `i`. A sequence of numbers is called **arithmetic** if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence `s` is arithmetic if and only if `s[i+1] - s[i] == s[1] - s[0]` for all valid `i`.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- binary-search - binary-search
function_signature: "def arrange_coins(n: int) -> int:"
test_cases:
visible:
- input: { n: 5 }
expected: 2
- input: { n: 8 }
expected: 3
hidden:
- input: { n: 1 }
expected: 1
- input: { n: 3 }
expected: 2
- input: { n: 6 }
expected: 3
- input: { n: 10 }
expected: 4
- input: { n: 100 }
expected: 13
description: | description: |
You have `n` coins and you want to build a staircase with these coins. The staircase consists of `k` rows where the i<sup>th</sup> row has exactly `i` coins. The last row of the staircase **may be** incomplete. You have `n` coins and you want to build a staircase with these coins. The staircase consists of `k` rows where the i<sup>th</sup> row has exactly `i` coins. The last row of the staircase **may be** incomplete.

View File

@@ -8,6 +8,26 @@ categories:
patterns: patterns:
- dfs - dfs
function_signature: "def array_nesting(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [5, 4, 0, 3, 1, 6, 2] }
expected: 4
- input: { nums: [0, 1, 2] }
expected: 1
hidden:
- input: { nums: [0] }
expected: 1
- input: { nums: [1, 0] }
expected: 2
- input: { nums: [0, 2, 1] }
expected: 2
- input: { nums: [2, 0, 1] }
expected: 3
- input: { nums: [1, 2, 3, 0] }
expected: 4
description: | description: |
You are given an integer array `nums` of length `n` where `nums` is a permutation of the numbers in the range `[0, n - 1]`. You are given an integer array `nums` of length `n` where `nums` is a permutation of the numbers in the range `[0, n - 1]`.

View File

@@ -10,6 +10,32 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def can_reorder_doubled(arr: list[int]) -> bool:"
test_cases:
visible:
- input: { arr: [3, 1, 3, 6] }
expected: false
- input: { arr: [2, 1, 2, 6] }
expected: false
- input: { arr: [4, -2, 2, -4] }
expected: true
hidden:
- input: { arr: [1, 2, 4, 8] }
expected: true
- input: { arr: [0, 0] }
expected: true
- input: { arr: [0, 0, 0, 0] }
expected: true
- input: { arr: [-2, -4] }
expected: true
- input: { arr: [-1, -2, -4, -8] }
expected: true
- input: { arr: [1, 2, 1, 2] }
expected: true
- input: { arr: [1, 3] }
expected: false
description: | description: |
Given an integer array of even length `arr`, return `true` *if it is possible to reorder* `arr` *such that* `arr[2 * i + 1] = 2 * arr[2 * i]` *for every* `0 <= i < len(arr) / 2`, *or* `false` *otherwise*. Given an integer array of even length `arr`, return `true` *if it is possible to reorder* `arr` *such that* `arr[2 * i + 1] = 2 * arr[2 * i]` *for every* `0 <= i < len(arr) / 2`, *or* `false` *otherwise*.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def array_pair_sum(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [1, 4, 3, 2] }
expected: 4
- input: { nums: [6, 2, 6, 5, 1, 2] }
expected: 9
hidden:
- input: { nums: [1, 2] }
expected: 1
- input: { nums: [1, 1, 1, 1] }
expected: 2
- input: { nums: [-1, -2, -3, -4] }
expected: -6
- input: { nums: [-5, 5, -3, 3] }
expected: -2
- input: { nums: [1, 2, 3, 4, 5, 6] }
expected: 9
description: | description: |
Given an integer array `nums` of `2n` integers, group these integers into `n` pairs `(a1, b1), (a2, b2), ..., (an, bn)` such that the sum of `min(ai, bi)` for all `i` is **maximised**. Given an integer array `nums` of `2n` integers, group these integers into `n` pairs `(a1, b1), (a2, b2), ..., (an, bn)` such that the sum of `min(ai, bi)` for all `i` is **maximised**.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def rearrange_array(nums: list[int]) -> list[int]:"
test_cases:
visible:
- input: { nums: [1, 2, 3, 4, 5] }
expected: [1, 2, 4, 5, 3]
- input: { nums: [6, 2, 0, 9, 7] }
expected: [9, 7, 6, 2, 0]
hidden:
- input: { nums: [1, 2, 3] }
expected: [1, 3, 2]
- input: { nums: [10, 20, 30, 40] }
expected: [10, 30, 20, 40]
- input: { nums: [5, 10, 15, 20, 25, 30] }
expected: [5, 15, 10, 25, 20, 30]
- input: { nums: [100, 200, 300] }
expected: [100, 300, 200]
- input: { nums: [1, 3, 5, 7, 9, 11, 13] }
expected: [1, 5, 3, 9, 7, 13, 11]
description: | description: |
You are given a **0-indexed** array `nums` of **distinct** integers. You want to rearrange the elements in the array such that every element in the rearranged array is **not** equal to the **average** of its neighbors. You are given a **0-indexed** array `nums` of **distinct** integers. You want to rearrange the elements in the array such that every element in the rearranged array is **not** equal to the **average** of its neighbors.

View File

@@ -10,6 +10,26 @@ patterns:
- bfs - bfs
- matrix-traversal - matrix-traversal
function_signature: "def max_distance(grid: list[list[int]]) -> int:"
test_cases:
visible:
- input: { grid: [[1, 0, 1], [0, 0, 0], [1, 0, 1]] }
expected: 2
- input: { grid: [[1, 0, 0], [0, 0, 0], [0, 0, 0]] }
expected: 4
hidden:
- input: { grid: [[1, 1, 1], [1, 1, 1], [1, 1, 1]] }
expected: -1
- input: { grid: [[0, 0, 0], [0, 0, 0], [0, 0, 0]] }
expected: -1
- input: { grid: [[1, 0], [0, 0]] }
expected: 2
- input: { grid: [[0, 0, 0], [0, 1, 0], [0, 0, 0]] }
expected: 2
- input: { grid: [[1, 1], [1, 0]] }
expected: 1
description: | description: |
Given an `n x n` `grid` containing only values `0` and `1`, where `0` represents water and `1` represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. Given an `n x n` `grid` containing only values `0` and `1`, where `0` represents water and `1` represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance.

View File

@@ -11,6 +11,28 @@ patterns:
- greedy - greedy
- two-pointers - two-pointers
function_signature: "def find_content_children(g: list[int], s: list[int]) -> int:"
test_cases:
visible:
- input: { g: [1, 2, 3], s: [1, 1] }
expected: 1
- input: { g: [1, 2], s: [1, 2, 3] }
expected: 2
hidden:
- input: { g: [1, 2, 3], s: [] }
expected: 0
- input: { g: [10, 9, 8, 7], s: [5, 6, 7, 8] }
expected: 2
- input: { g: [1, 1, 1, 1], s: [1, 1, 1, 1] }
expected: 4
- input: { g: [1], s: [1] }
expected: 1
- input: { g: [5], s: [4] }
expected: 0
- input: { g: [2, 3, 4], s: [1, 2, 3, 4, 5] }
expected: 3
description: | description: |
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

View File

@@ -8,6 +8,28 @@ categories:
patterns: patterns:
- matrix-traversal - matrix-traversal
function_signature: "def num_rook_captures(board: list[list[str]]) -> int:"
test_cases:
visible:
- input: { board: [[".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", "p", ".", ".", ".", "."], [".", ".", ".", "R", ".", ".", ".", "p"], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", "p", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."]] }
expected: 3
- input: { board: [[".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", "R", ".", ".", "."]] }
expected: 0
- input: { board: [[".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", "p", ".", ".", ".", "."], [".", ".", ".", "p", ".", ".", ".", "."], ["p", "p", ".", "R", ".", "p", "B", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", "B", ".", ".", ".", "."], [".", ".", ".", "p", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."]] }
expected: 3
hidden:
- input: { board: [["R", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."]] }
expected: 0
- input: { board: [["p", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], ["R", ".", ".", ".", ".", ".", ".", "p"], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."]] }
expected: 2
- input: { board: [[".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", "B", ".", "R", ".", "B", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."]] }
expected: 0
- input: { board: [[".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], ["p", ".", ".", "R", ".", ".", ".", "p"], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."]] }
expected: 2
- input: { board: [[".", ".", ".", "p", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", "R", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", "."], [".", ".", ".", "p", ".", ".", ".", "."]] }
expected: 2
description: | description: |
You are given an `8 x 8` **matrix** representing a chessboard. There is **exactly one** white rook represented by `'R'`, some number of white bishops `'B'`, and some number of black pawns `'p'`. Empty squares are represented by `'.'`. You are given an `8 x 8` **matrix** representing a chessboard. There is **exactly one** white rook represented by `'R'`, some number of white bishops `'B'`, and some number of black pawns `'p'`. Empty squares are represented by `'.'`.

View File

@@ -10,6 +10,24 @@ patterns:
- bfs - bfs
- tree-traversal - tree-traversal
function_signature: "def average_of_levels(root: TreeNode) -> list[float]:"
test_cases:
visible:
- input: { root: [3, 9, 20, null, null, 15, 7] }
expected: [3.0, 14.5, 11.0]
- input: { root: [3, 9, 20, 15, 7] }
expected: [3.0, 14.5, 11.0]
hidden:
- input: { root: [1] }
expected: [1.0]
- input: { root: [1, 2, 3, 4, 5, 6, 7] }
expected: [1.0, 2.5, 5.5]
- input: { root: [1, 2, null, 3, null, 4] }
expected: [1.0, 2.0, 3.0, 4.0]
- input: { root: [0, -1, 1] }
expected: [0.0, 0.0]
description: | description: |
Given the `root` of a binary tree, return *the average value of the nodes on each level in the form of an array*. Given the `root` of a binary tree, return *the average value of the nodes on each level in the form of an array*.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def average(salary: list[int]) -> float:"
test_cases:
visible:
- input: { salary: [4000, 3000, 1000, 2000] }
expected: 2500.0
- input: { salary: [1000, 2000, 3000] }
expected: 2000.0
hidden:
- input: { salary: [6000, 5000, 4000, 3000, 2000, 1000] }
expected: 3500.0
- input: { salary: [8000, 9000, 2000, 3000, 6000, 1000] }
expected: 4750.0
- input: { salary: [48000, 59000, 99000, 13000, 78000, 45000, 31000, 17000, 39000, 37000, 93000, 77000, 33000, 28000, 4000, 54000, 67000, 6000, 1000, 11000] }
expected: 41111.11111111111
- input: { salary: [1000, 1001, 1002] }
expected: 1001.0
- input: { salary: [100000, 200000, 300000] }
expected: 200000.0
description: | description: |
You are given an array of **unique** integers `salary` where `salary[i]` is the salary of the i<sup>th</sup> employee. You are given an array of **unique** integers `salary` where `salary[i]` is the salary of the i<sup>th</sup> employee.

View File

@@ -9,6 +9,30 @@ categories:
patterns: patterns:
- prefix-sum - prefix-sum
function_signature: "def average_value(nums: list[int]) -> int:"
test_cases:
visible:
- input: { nums: [1, 3, 6, 10, 12, 15] }
expected: 9
- input: { nums: [1, 2, 4, 7, 10] }
expected: 0
hidden:
- input: { nums: [6] }
expected: 6
- input: { nums: [6, 12, 18] }
expected: 12
- input: { nums: [1, 2, 3, 4, 5] }
expected: 0
- input: { nums: [6, 6, 6, 6] }
expected: 6
- input: { nums: [12, 24, 36, 48, 60] }
expected: 36
- input: { nums: [5, 7, 11, 13, 17, 19] }
expected: 0
- input: { nums: [6, 12, 1, 2, 3, 18] }
expected: 12
description: | description: |
Given an integer array `nums` of **positive** integers, return *the average value of all even integers that are divisible by* `3`. Given an integer array `nums` of **positive** integers, return *the average value of all even integers that are divisible by* `3`.

View File

@@ -8,6 +8,26 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def average_waiting_time(customers: list[list[int]]) -> float:"
test_cases:
visible:
- input: { customers: [[1, 2], [2, 5], [4, 3]] }
expected: 5.0
- input: { customers: [[5, 2], [5, 4], [10, 3], [20, 1]] }
expected: 3.25
hidden:
- input: { customers: [[1, 1]] }
expected: 1.0
- input: { customers: [[1, 5], [2, 3], [3, 2]] }
expected: 5.0
- input: { customers: [[1, 1], [2, 1], [3, 1]] }
expected: 1.0
- input: { customers: [[1, 10], [1, 10], [1, 10]] }
expected: 20.0
- input: { customers: [[10, 5], [20, 5], [30, 5]] }
expected: 5.0
description: | description: |
There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrival_i, time_i]`: There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrival_i, time_i]`:

View File

@@ -10,6 +10,28 @@ patterns:
- greedy - greedy
- binary-search - binary-search
function_signature: "def avoid_flood(rains: list[int]) -> list[int]:"
test_cases:
visible:
- input: { rains: [1, 2, 3, 4] }
expected: [-1, -1, -1, -1]
- input: { rains: [1, 2, 0, 0, 2, 1] }
expected: [-1, -1, 2, 1, -1, -1]
- input: { rains: [1, 2, 0, 1, 2] }
expected: []
hidden:
- input: { rains: [1, 0, 1] }
expected: [-1, 1, -1]
- input: { rains: [0, 1, 1] }
expected: []
- input: { rains: [1, 1] }
expected: []
- input: { rains: [0] }
expected: [1]
- input: { rains: [1] }
expected: [-1]
description: | description: |
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the n<sup>th</sup> lake, that lake becomes full of water. If it rains over a lake that is **full of water**, there will be a **flood**. Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the n<sup>th</sup> lake, that lake becomes full of water. If it rains over a lake that is **full of water**, there will be a **flood**.

View File

@@ -11,6 +11,28 @@ patterns:
- two-pointers - two-pointers
- greedy - greedy
function_signature: "def bag_of_tokens_score(tokens: list[int], power: int) -> int:"
test_cases:
visible:
- input: { tokens: [100], power: 50 }
expected: 0
- input: { tokens: [200, 100], power: 150 }
expected: 1
- input: { tokens: [100, 200, 300, 400], power: 200 }
expected: 2
hidden:
- input: { tokens: [], power: 100 }
expected: 0
- input: { tokens: [100], power: 100 }
expected: 1
- input: { tokens: [100, 100, 100], power: 200 }
expected: 2
- input: { tokens: [71, 55, 82], power: 54 }
expected: 0
- input: { tokens: [26, 33, 59], power: 45 }
expected: 1
description: | description: |
You start with an initial **power** of `power`, an initial **score** of `0`, and a bag of tokens given as an integer array `tokens`, where each `tokens[i]` denotes the value of the i<sup>th</sup> token. You start with an initial **power** of `power`, an initial **score** of `0`, and a bag of tokens given as an integer array `tokens`, where each `tokens[i]` denotes the value of the i<sup>th</sup> token.

View File

@@ -9,6 +9,26 @@ patterns:
- tree-traversal - tree-traversal
- dfs - dfs
function_signature: "def balance_bst(root: TreeNode) -> TreeNode:"
test_cases:
visible:
- input: { root: [1, null, 2, null, 3, null, 4] }
expected: [2, 1, 3, null, null, null, 4]
- input: { root: [2, 1, 3] }
expected: [2, 1, 3]
hidden:
- input: { root: [1] }
expected: [1]
- input: { root: [1, null, 2] }
expected: [1, null, 2]
- input: { root: [3, 2, null, 1] }
expected: [2, 1, 3]
- input: { root: [1, null, 2, null, 3, null, 4, null, 5] }
expected: [3, 1, 4, null, 2, null, 5]
- input: { root: [5, 4, null, 3, null, 2, null, 1] }
expected: [3, 1, 4, null, 2, null, 5]
description: | description: |
Given the `root` of a binary search tree, return *a **balanced** binary search tree with the same node values*. If there is more than one answer, return **any of them**. Given the `root` of a binary search tree, return *a **balanced** binary search tree with the same node values*. If there is more than one answer, return **any of them**.

View File

@@ -10,6 +10,28 @@ patterns:
- dfs - dfs
- tree-traversal - tree-traversal
function_signature: "def is_balanced(root: TreeNode) -> bool:"
test_cases:
visible:
- input: { root: [3, 9, 20, null, null, 15, 7] }
expected: true
- input: { root: [1, 2, 2, 3, 3, null, null, 4, 4] }
expected: false
- input: { root: [] }
expected: true
hidden:
- input: { root: [1] }
expected: true
- input: { root: [1, 2, null, 3] }
expected: false
- input: { root: [1, null, 2, null, 3] }
expected: false
- input: { root: [1, 2, 3, 4, 5, 6, 7] }
expected: true
- input: { root: [1, 2, 2, 3, null, null, 3, 4, null, null, 4] }
expected: false
description: | description: |
Given a binary tree, determine if it is **height-balanced**. Given a binary tree, determine if it is **height-balanced**.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def convert_to_base7(num: int) -> str:"
test_cases:
visible:
- input: { num: 100 }
expected: "202"
- input: { num: -7 }
expected: "-10"
hidden:
- input: { num: 0 }
expected: "0"
- input: { num: 7 }
expected: "10"
- input: { num: 1 }
expected: "1"
- input: { num: -1 }
expected: "-1"
- input: { num: 49 }
expected: "100"
- input: { num: -100 }
expected: "-202"
description: | description: |
Given an integer `num`, return *a string of its **base 7** representation*. Given an integer `num`, return *a string of its **base 7** representation*.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- monotonic-stack - monotonic-stack
function_signature: "def cal_points(operations: list[str]) -> int:"
test_cases:
visible:
- input: { operations: ["5", "2", "C", "D", "+"] }
expected: 30
- input: { operations: ["5", "-2", "4", "C", "D", "9", "+", "+"] }
expected: 27
- input: { operations: ["1", "C"] }
expected: 0
hidden:
- input: { operations: ["1"] }
expected: 1
- input: { operations: ["5", "10", "+"] }
expected: 30
- input: { operations: ["3", "D", "D", "D"] }
expected: 45
- input: { operations: ["-5", "10", "+", "D"] }
expected: 20
- input: { operations: ["1", "2", "3", "4", "5"] }
expected: 15
description: | description: |
You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record. You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.

View File

@@ -12,6 +12,30 @@ categories:
patterns: patterns:
- backtracking - backtracking
function_signature: "def basic_calculator_iv(expression: str, evalvars: list[str], evalints: list[int]) -> list[str]:"
test_cases:
visible:
- input: { expression: "e + 8 - a + 5", evalvars: ["e"], evalints: [1] }
expected: ["-1*a", "14"]
- input: { expression: "e - 8 + temperature - pressure", evalvars: ["e", "temperature"], evalints: [1, 12] }
expected: ["-1*pressure", "5"]
- input: { expression: "(e + 8) * (e - 8)", evalvars: [], evalints: [] }
expected: ["1*e*e", "-64"]
hidden:
- input: { expression: "0", evalvars: [], evalints: [] }
expected: []
- input: { expression: "a * b", evalvars: [], evalints: [] }
expected: ["1*a*b"]
- input: { expression: "a * b * c", evalvars: ["a"], evalints: [2] }
expected: ["2*b*c"]
- input: { expression: "(a + b) * (a - b)", evalvars: [], evalints: [] }
expected: ["1*a*a", "-1*b*b"]
- input: { expression: "a + b + c", evalvars: ["a", "b", "c"], evalints: [1, 2, 3] }
expected: ["6"]
- input: { expression: "a * a * a - a * a + a - 1", evalvars: [], evalints: [] }
expected: ["1*a*a*a", "-1*a*a", "1*a", "-1"]
description: | description: |
Given an expression such as `expression = "e + 8 - a + 5"` and an evaluation map such as `{"e": 1}` (given in terms of `evalvars = ["e"]` and `evalints = [1]`), return a list of tokens representing the **simplified expression**, such as `["-1*a","14"]`. Given an expression such as `expression = "e + 8 - a + 5"` and an evaluation map such as `{"e": 1}` (given in terms of `evalvars = ["e"]` and `evalints = [1]`), return a list of tokens representing the **simplified expression**, such as `["-1*a","14"]`.

View File

@@ -10,6 +10,30 @@ categories:
patterns: patterns:
- monotonic-stack - monotonic-stack
function_signature: "def calculate(s: str) -> int:"
test_cases:
visible:
- input: { s: "1 + 1" }
expected: 2
- input: { s: " 2-1 + 2 " }
expected: 3
- input: { s: "(1+(4+5+2)-3)+(6+8)" }
expected: 23
hidden:
- input: { s: "1" }
expected: 1
- input: { s: "-1" }
expected: -1
- input: { s: "-(3+2)" }
expected: -5
- input: { s: "1-(5)" }
expected: -4
- input: { s: "2147483647" }
expected: 2147483647
- input: { s: "1-(-2)" }
expected: 3
description: | description: |
Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return *the result of the evaluation*. Given a string `s` representing a valid expression, implement a basic calculator to evaluate it, and return *the result of the evaluation*.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- matrix-traversal - matrix-traversal
function_signature: "def count_battleships(board: list[list[str]]) -> int:"
test_cases:
visible:
- input: { board: [["X", ".", ".", "X"], [".", ".", ".", "X"], [".", ".", ".", "X"]] }
expected: 2
- input: { board: [["."]] }
expected: 0
hidden:
- input: { board: [["X"]] }
expected: 1
- input: { board: [["X", "X", "X"]] }
expected: 1
- input: { board: [["X"], ["X"], ["X"]] }
expected: 1
- input: { board: [["X", ".", "X"], [".", ".", "."], ["X", ".", "X"]] }
expected: 4
- input: { board: [[".", ".", "."], [".", ".", "."], [".", ".", "."]] }
expected: 0
description: | description: |
Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return *the number of **battleships** on the board*. Given an `m x n` matrix `board` where each cell is a battleship `'X'` or empty `'.'`, return *the number of **battleships** on the board*.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def construct_array(n: int, k: int) -> list[int]:"
test_cases:
visible:
- input: { n: 3, k: 1 }
expected: [1, 2, 3]
- input: { n: 3, k: 2 }
expected: [1, 3, 2]
hidden:
- input: { n: 2, k: 1 }
expected: [1, 2]
- input: { n: 5, k: 1 }
expected: [1, 2, 3, 4, 5]
- input: { n: 5, k: 4 }
expected: [1, 5, 2, 4, 3]
- input: { n: 6, k: 3 }
expected: [1, 4, 2, 3, 5, 6]
- input: { n: 10, k: 5 }
expected: [1, 6, 2, 5, 3, 4, 7, 8, 9, 10]
- input: { n: 4, k: 3 }
expected: [1, 4, 2, 3]
description: | description: |
Given two integers `n` and `k`, construct a list `answer` that contains `n` different positive integers ranging from `1` to `n` and obeys the following requirement: Given two integers `n` and `k`, construct a list `answer` that contains `n` different positive integers ranging from `1` to `n` and obeys the following requirement:

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- backtracking - backtracking
function_signature: "def count_arrangement(n: int) -> int:"
test_cases:
visible:
- input: { n: 2 }
expected: 2
- input: { n: 1 }
expected: 1
hidden:
- input: { n: 3 }
expected: 3
- input: { n: 4 }
expected: 8
- input: { n: 5 }
expected: 10
- input: { n: 6 }
expected: 36
- input: { n: 10 }
expected: 700
description: | description: |
Suppose you have `n` integers labelled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true: Suppose you have `n` integers labelled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true:

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def beautiful_array(n: int) -> list[int]:"
test_cases:
visible:
- input: { n: 4 }
expected: [2, 1, 4, 3]
- input: { n: 5 }
expected: [3, 1, 2, 5, 4]
hidden:
- input: { n: 1 }
expected: [1]
- input: { n: 2 }
expected: [1, 2]
- input: { n: 3 }
expected: [1, 3, 2]
- input: { n: 6 }
expected: [3, 1, 2, 5, 6, 4]
- input: { n: 10 }
expected: [5, 3, 1, 7, 9, 6, 2, 4, 10, 8]
- input: { n: 8 }
expected: [5, 3, 1, 7, 6, 2, 4, 8]
description: | description: |
An array `nums` of length `n` is **beautiful** if: An array `nums` of length `n` is **beautiful** if:

View File

@@ -9,6 +9,30 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def best_hand(ranks: list[int], suits: list[str]) -> str:"
test_cases:
visible:
- input: { ranks: [13, 2, 3, 1, 9], suits: ["a", "a", "a", "a", "a"] }
expected: "Flush"
- input: { ranks: [4, 4, 2, 4, 4], suits: ["d", "a", "a", "b", "c"] }
expected: "Three of a Kind"
- input: { ranks: [10, 10, 2, 12, 9], suits: ["a", "b", "c", "a", "d"] }
expected: "Pair"
hidden:
- input: { ranks: [1, 2, 3, 4, 5], suits: ["a", "b", "c", "d", "a"] }
expected: "High Card"
- input: { ranks: [3, 3, 3, 3, 5], suits: ["a", "b", "c", "d", "a"] }
expected: "Three of a Kind"
- input: { ranks: [1, 1, 2, 2, 3], suits: ["a", "b", "c", "d", "a"] }
expected: "Pair"
- input: { ranks: [7, 7, 7, 7, 7], suits: ["a", "a", "a", "a", "a"] }
expected: "Flush"
- input: { ranks: [13, 13, 13, 1, 1], suits: ["b", "c", "d", "a", "b"] }
expected: "Three of a Kind"
- input: { ranks: [2, 4, 6, 8, 10], suits: ["d", "d", "d", "d", "d"] }
expected: "Flush"
description: | description: |
You are given an integer array `ranks` and a character array `suits`. You have `5` cards where the i<sup>th</sup> card has a rank of `ranks[i]` and a suit of `suits[i]`. You are given an integer array `ranks` and a character array `suits`. You have `5` cards where the i<sup>th</sup> card has a rank of `ranks[i]` and a suit of `suits[i]`.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def get_min_dist_sum(positions: list[list[int]]) -> float:"
test_cases:
visible:
- input: { positions: [[0, 1], [1, 0], [1, 2], [2, 1]] }
expected: 4.00000
- input: { positions: [[1, 1], [3, 3]] }
expected: 2.82843
hidden:
- input: { positions: [[0, 0]] }
expected: 0.00000
- input: { positions: [[0, 0], [2, 0]] }
expected: 2.00000
- input: { positions: [[0, 0], [0, 1], [1, 0], [1, 1]] }
expected: 2.82843
- input: { positions: [[1, 1], [1, 1], [1, 1]] }
expected: 0.00000
- input: { positions: [[0, 0], [50, 50], [100, 100]] }
expected: 141.42136
- input: { positions: [[1, 2], [3, 4], [5, 6], [7, 8]] }
expected: 11.31371
description: | description: |
A delivery company wants to build a new service center in a new city. The company knows the positions of all the customers in this city on a 2D-Map and wants to build the new center in a position such that **the sum of the Euclidean distances to all customers is minimum**. A delivery company wants to build a new service center in a new city. The company knows the positions of all the customers in this city on a 2D-Map and wants to build the new center in a position such that **the sum of the Euclidean distances to all customers is minimum**.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def max_score_sightseeing_pair(values: list[int]) -> int:"
test_cases:
visible:
- input: { values: [8, 1, 5, 2, 6] }
expected: 11
- input: { values: [1, 2] }
expected: 2
hidden:
- input: { values: [1, 3, 5] }
expected: 7
- input: { values: [1, 1, 1, 1] }
expected: 1
- input: { values: [10, 1, 1, 1, 1, 1, 10] }
expected: 15
- input: { values: [5, 4, 3, 2, 1] }
expected: 8
- input: { values: [1, 2, 3, 4, 5] }
expected: 8
description: | description: |
You are given an integer array `values` where `values[i]` represents the value of the i<sup>th</sup> sightseeing spot. Two sightseeing spots `i` and `j` have a **distance** `j - i` between them. You are given an integer array `values` where `values[i]` represents the value of the i<sup>th</sup> sightseeing spot. Two sightseeing spots `i` and `j` have a **distance** `j - i` between them.

View File

@@ -10,6 +10,30 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def best_team_score(scores: list[int], ages: list[int]) -> int:"
test_cases:
visible:
- input: { scores: [1, 3, 5, 10, 15], ages: [1, 2, 3, 4, 5] }
expected: 34
- input: { scores: [4, 5, 6, 5], ages: [2, 1, 2, 1] }
expected: 16
- input: { scores: [1, 2, 3, 5], ages: [8, 9, 10, 1] }
expected: 6
hidden:
- input: { scores: [1], ages: [1] }
expected: 1
- input: { scores: [5, 5], ages: [3, 3] }
expected: 10
- input: { scores: [1, 1, 1, 1, 1], ages: [5, 4, 3, 2, 1] }
expected: 5
- input: { scores: [10, 20, 5], ages: [1, 2, 3] }
expected: 35
- input: { scores: [319, 776], ages: [8, 3] }
expected: 776
- input: { scores: [9, 2, 8, 8, 2], ages: [4, 1, 3, 3, 5] }
expected: 27
description: | description: |
You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the **sum** of scores of all the players in the team. You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the **sum** of scores of all the players in the team.

View File

@@ -9,6 +9,30 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def max_profit(prices: list[int]) -> int:"
test_cases:
visible:
- input: { prices: [7, 1, 5, 3, 6, 4] }
expected: 7
- input: { prices: [1, 2, 3, 4, 5] }
expected: 4
- input: { prices: [7, 6, 4, 3, 1] }
expected: 0
hidden:
- input: { prices: [1] }
expected: 0
- input: { prices: [2, 1] }
expected: 0
- input: { prices: [1, 2] }
expected: 1
- input: { prices: [3, 3, 3, 3] }
expected: 0
- input: { prices: [1, 2, 1, 2, 1, 2] }
expected: 3
- input: { prices: [5, 1, 3, 2, 8, 4, 9] }
expected: 14
description: | description: |
You are given an integer array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day. You are given an integer array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day.

View File

@@ -9,6 +9,30 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def max_profit(prices: list[int]) -> int:"
test_cases:
visible:
- input: { prices: [3, 3, 5, 0, 0, 3, 1, 4] }
expected: 6
- input: { prices: [1, 2, 3, 4, 5] }
expected: 4
- input: { prices: [7, 6, 4, 3, 1] }
expected: 0
hidden:
- input: { prices: [1] }
expected: 0
- input: { prices: [1, 2] }
expected: 1
- input: { prices: [2, 1, 2, 0, 1] }
expected: 2
- input: { prices: [1, 2, 4, 2, 5, 7, 2, 4, 9, 0] }
expected: 13
- input: { prices: [6, 1, 3, 2, 4, 7] }
expected: 7
- input: { prices: [3, 3, 3, 3, 3] }
expected: 0
description: | description: |
You are given an array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day. You are given an array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def max_profit(k: int, prices: list[int]) -> int:"
test_cases:
visible:
- input: { k: 2, prices: [2, 4, 1] }
expected: 2
- input: { k: 2, prices: [3, 2, 6, 5, 0, 3] }
expected: 7
hidden:
- input: { k: 1, prices: [1, 2, 3, 4, 5] }
expected: 4
- input: { k: 0, prices: [1, 2, 3] }
expected: 0
- input: { k: 2, prices: [1] }
expected: 0
- input: { k: 100, prices: [1, 2, 3, 4, 5] }
expected: 4
- input: { k: 2, prices: [5, 4, 3, 2, 1] }
expected: 0
- input: { k: 3, prices: [1, 2, 4, 2, 5, 7, 2, 4, 9, 0] }
expected: 15
description: | description: |
You are given an integer array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day, and an integer `k`. You are given an integer array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day, and an integer `k`.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def max_profit(prices: list[int]) -> int:"
test_cases:
visible:
- input: { prices: [1, 2, 3, 0, 2] }
expected: 3
- input: { prices: [1] }
expected: 0
hidden:
- input: { prices: [1, 2] }
expected: 1
- input: { prices: [2, 1] }
expected: 0
- input: { prices: [1, 2, 3, 4, 5] }
expected: 4
- input: { prices: [1, 2, 4, 2, 5, 7, 2, 4, 9, 0] }
expected: 13
- input: { prices: [6, 1, 6, 4, 3, 0, 2] }
expected: 7
- input: { prices: [3, 3, 3, 3] }
expected: 0
description: | description: |
You are given an array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day. You are given an array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day.

View File

@@ -10,6 +10,28 @@ patterns:
- dynamic-programming - dynamic-programming
- greedy - greedy
function_signature: "def max_profit(prices: list[int], fee: int) -> int:"
test_cases:
visible:
- input: { prices: [1, 3, 2, 8, 4, 9], fee: 2 }
expected: 8
- input: { prices: [1, 3, 7, 5, 10, 3], fee: 3 }
expected: 6
hidden:
- input: { prices: [1], fee: 1 }
expected: 0
- input: { prices: [1, 2], fee: 1 }
expected: 0
- input: { prices: [1, 3], fee: 1 }
expected: 1
- input: { prices: [1, 5, 2, 8], fee: 2 }
expected: 6
- input: { prices: [2, 1, 4, 4, 2, 3, 2, 5, 1, 2], fee: 1 }
expected: 6
- input: { prices: [4, 5, 2, 4, 3, 3, 1, 2, 5, 4], fee: 1 }
expected: 4
description: | description: |
You are given an array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day, and an integer `fee` representing a transaction fee. You are given an array `prices` where `prices[i]` is the price of a given stock on the i<sup>th</sup> day, and an integer `fee` representing a transaction fee.

View File

@@ -8,6 +8,30 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def binary_gap(n: int) -> int:"
test_cases:
visible:
- input: { n: 22 }
expected: 2
- input: { n: 8 }
expected: 0
- input: { n: 5 }
expected: 2
hidden:
- input: { n: 1 }
expected: 0
- input: { n: 6 }
expected: 1
- input: { n: 9 }
expected: 3
- input: { n: 15 }
expected: 1
- input: { n: 17 }
expected: 4
- input: { n: 1073741824 }
expected: 0
description: | description: |
Given a positive integer `n`, find and return *the **longest distance** between any two **adjacent*** `1`*'s in the binary representation of* `n`. If there are no two adjacent `1`'s, return `0`. Given a positive integer `n`, find and return *the **longest distance** between any two **adjacent*** `1`*'s in the binary representation of* `n`. If there are no two adjacent `1`'s, return `0`.

View File

@@ -8,6 +8,32 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def has_alternating_bits(n: int) -> bool:"
test_cases:
visible:
- input: { n: 5 }
expected: true
- input: { n: 7 }
expected: false
- input: { n: 11 }
expected: false
hidden:
- input: { n: 1 }
expected: true
- input: { n: 2 }
expected: true
- input: { n: 10 }
expected: true
- input: { n: 21 }
expected: true
- input: { n: 15 }
expected: false
- input: { n: 1431655765 }
expected: true
- input: { n: 4 }
expected: false
description: | description: |
Given a positive integer `n`, check whether it has **alternating bits**: namely, if two adjacent bits will always have different values. Given a positive integer `n`, check whether it has **alternating bits**: namely, if two adjacent bits will always have different values.

View File

@@ -9,6 +9,30 @@ categories:
patterns: patterns:
- prefix-sum - prefix-sum
function_signature: "def prefixes_div_by5(nums: list[int]) -> list[bool]:"
test_cases:
visible:
- input: { nums: [0, 1, 1] }
expected: [true, false, false]
- input: { nums: [1, 1, 1] }
expected: [false, false, false]
- input: { nums: [1, 0, 1, 0] }
expected: [false, false, true, false]
hidden:
- input: { nums: [0] }
expected: [true]
- input: { nums: [1] }
expected: [false]
- input: { nums: [1, 0, 1] }
expected: [false, false, true]
- input: { nums: [0, 0, 0, 0, 0] }
expected: [true, true, true, true, true]
- input: { nums: [1, 1, 0, 0, 1] }
expected: [false, false, false, false, false]
- input: { nums: [1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1] }
expected: [false, false, false, false, true, true, true, false, false, true, true, true, true, true, true, false, false, false, false, false]
description: | description: |
You are given a binary array `nums` (**0-indexed**). You are given a binary array `nums` (**0-indexed**).

View File

@@ -10,6 +10,22 @@ patterns:
- tree-traversal - tree-traversal
- monotonic-stack - monotonic-stack
function_signature: "class BSTIterator:\n def __init__(self, root: TreeNode): ...\n def next(self) -> int: ...\n def hasNext(self) -> bool: ..."
test_cases:
visible:
- input: { operations: ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"], args: [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []] }
expected: [null, 3, 7, true, 9, true, 15, true, 20, false]
hidden:
- input: { operations: ["BSTIterator", "hasNext", "next", "hasNext"], args: [[[1]], [], [], []] }
expected: [null, true, 1, false]
- input: { operations: ["BSTIterator", "next", "next", "next", "hasNext"], args: [[[2, 1, 3]], [], [], [], []] }
expected: [null, 1, 2, 3, false]
- input: { operations: ["BSTIterator", "next", "next", "next", "next", "next"], args: [[[5, 3, 7, 2, 4, 6, 8]], [], [], [], [], []] }
expected: [null, 2, 3, 4, 5, 6]
- input: { operations: ["BSTIterator", "hasNext", "hasNext", "next", "hasNext"], args: [[[10]], [], [], [], []] }
expected: [null, true, true, 10, false]
description: | description: |
Implement the `BSTIterator` class that represents an iterator over the **in-order traversal** of a binary search tree (BST): Implement the `BSTIterator` class that represents an iterator over the **in-order traversal** of a binary search tree (BST):

View File

@@ -9,6 +9,24 @@ patterns:
- dfs - dfs
- tree-traversal - tree-traversal
function_signature: "def bst_to_gst(root: TreeNode) -> TreeNode:"
test_cases:
visible:
- input: { root: [4, 1, 6, 0, 2, 5, 7, null, null, null, 3, null, null, null, 8] }
expected: [30, 36, 21, 36, 35, 26, 15, null, null, null, 33, null, null, null, 8]
- input: { root: [0, null, 1] }
expected: [1, null, 1]
hidden:
- input: { root: [1] }
expected: [1]
- input: { root: [2, 1, 3] }
expected: [5, 6, 3]
- input: { root: [3, 2, 4, 1] }
expected: [7, 9, 4, 10]
- input: { root: [5, 3, 7, 2, 4, 6, 8] }
expected: [26, 33, 15, 35, 30, 21, 8]
description: | description: |
Given the `root` of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST. Given the `root` of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- sliding-window - sliding-window
function_signature: "def query_string(s: str, n: int) -> bool:"
test_cases:
visible:
- input: { s: "0110", n: 3 }
expected: true
- input: { s: "0110", n: 4 }
expected: false
hidden:
- input: { s: "1", n: 1 }
expected: true
- input: { s: "0", n: 1 }
expected: false
- input: { s: "110101011011000011011111000000", n: 15 }
expected: false
- input: { s: "1111000110110111011100011", n: 12 }
expected: true
- input: { s: "10101010101010", n: 7 }
expected: false
- input: { s: "1010110001010100111010011110100101110", n: 20 }
expected: false
description: | description: |
Given a binary string `s` and a positive integer `n`, return `true` *if the binary representation of all the integers in the range* `[1, n]` *are **substrings** of* `s`*, or* `false` *otherwise*. Given a binary string `s` and a positive integer `n`, return `true` *if the binary representation of all the integers in the range* `[1, n]` *are **substrings** of* `s`*, or* `false` *otherwise*.

View File

@@ -10,6 +10,28 @@ patterns:
- sliding-window - sliding-window
- prefix-sum - prefix-sum
function_signature: "def num_subarrays_with_sum(nums: list[int], goal: int) -> int:"
test_cases:
visible:
- input: { nums: [1, 0, 1, 0, 1], goal: 2 }
expected: 4
- input: { nums: [0, 0, 0, 0, 0], goal: 0 }
expected: 15
hidden:
- input: { nums: [1, 1, 1, 1, 1], goal: 3 }
expected: 3
- input: { nums: [1, 0, 0, 0, 1], goal: 1 }
expected: 8
- input: { nums: [0, 1, 0], goal: 1 }
expected: 4
- input: { nums: [1], goal: 1 }
expected: 1
- input: { nums: [0], goal: 0 }
expected: 1
- input: { nums: [1, 1, 1], goal: 5 }
expected: 0
description: | description: |
Given a binary array `nums` and an integer `goal`, return *the number of non-empty **subarrays** with a sum equal to* `goal`. Given a binary array `nums` and an integer `goal`, return *the number of non-empty **subarrays** with a sum equal to* `goal`.

View File

@@ -10,6 +10,26 @@ patterns:
- dfs - dfs
- greedy - greedy
function_signature: "def min_camera_cover(root: TreeNode) -> int:"
test_cases:
visible:
- input: { root: [0, 0, null, 0, 0] }
expected: 1
- input: { root: [0, 0, null, 0, null, 0, null, null, 0] }
expected: 2
hidden:
- input: { root: [0] }
expected: 1
- input: { root: [0, 0] }
expected: 1
- input: { root: [0, 0, 0] }
expected: 1
- input: { root: [0, 0, 0, 0, 0, 0, 0] }
expected: 2
- input: { root: [0, 0, null, 0, null, 0, null, 0] }
expected: 2
description: | description: |
You are given the `root` of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children. You are given the `root` of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.

View File

@@ -9,6 +9,28 @@ patterns:
- dfs - dfs
- tree-traversal - tree-traversal
function_signature: "def btree_game_winning_move(root: TreeNode, n: int, x: int) -> bool:"
test_cases:
visible:
- input: { root: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], n: 11, x: 3 }
expected: true
- input: { root: [1, 2, 3], n: 3, x: 1 }
expected: false
hidden:
- input: { root: [1], n: 1, x: 1 }
expected: false
- input: { root: [1, 2, 3, 4, 5, null, null], n: 5, x: 2 }
expected: true
- input: { root: [1, 2, 3, 4, 5, 6, 7], n: 7, x: 1 }
expected: true
- input: { root: [1, 2, null, 3, null, 4, null, 5], n: 5, x: 3 }
expected: false
- input: { root: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], n: 15, x: 4 }
expected: true
- input: { root: [1, 2, 3, null, null, 4, 5], n: 5, x: 3 }
expected: false
description: | description: |
Two players play a turn-based game on a binary tree. We are given the `root` of this binary tree, and the number of nodes `n` in the tree. `n` is odd, and each node has a distinct value from `1` to `n`. Two players play a turn-based game on a binary tree. We are given the `root` of this binary tree, and the number of nodes `n` in the tree. `n` is odd, and each node has a distinct value from `1` to `n`.

View File

@@ -11,6 +11,28 @@ patterns:
- tree-traversal - tree-traversal
- dfs - dfs
function_signature: "def inorder_traversal(root: TreeNode) -> list[int]:"
test_cases:
visible:
- input: { root: [1, null, 2, 3] }
expected: [1, 3, 2]
- input: { root: [] }
expected: []
- input: { root: [1] }
expected: [1]
hidden:
- input: { root: [1, 2, 3, 4, 5, null, 8, null, null, 6, 7, 9] }
expected: [4, 2, 6, 5, 7, 1, 3, 9, 8]
- input: { root: [1, 2, null, 3, null, 4] }
expected: [4, 3, 2, 1]
- input: { root: [1, null, 2, null, 3, null, 4] }
expected: [1, 2, 3, 4]
- input: { root: [5, 3, 7, 2, 4, 6, 8] }
expected: [2, 3, 4, 5, 6, 7, 8]
- input: { root: [-1, -2, -3] }
expected: [-2, -1, -3]
description: | description: |
Given the `root` of a binary tree, return *the inorder traversal of its nodes' values*. Given the `root` of a binary tree, return *the inorder traversal of its nodes' values*.

View File

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

View File

@@ -11,6 +11,28 @@ patterns:
- dfs - dfs
- dynamic-programming - dynamic-programming
function_signature: "def max_path_sum(root: TreeNode) -> int:"
test_cases:
visible:
- input: { root: [1, 2, 3] }
expected: 6
- input: { root: [-10, 9, 20, null, null, 15, 7] }
expected: 42
hidden:
- input: { root: [1] }
expected: 1
- input: { root: [-3] }
expected: -3
- input: { root: [2, -1] }
expected: 2
- input: { root: [-2, -1] }
expected: -1
- input: { root: [5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1] }
expected: 48
- input: { root: [1, -2, -3, 1, 3, -2, null, -1] }
expected: 3
description: | description: |
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root. A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.

View File

@@ -12,6 +12,24 @@ patterns:
- backtracking - backtracking
- tree-traversal - tree-traversal
function_signature: "def binary_tree_paths(root: TreeNode) -> list[str]:"
test_cases:
visible:
- input: { root: [1, 2, 3, null, 5] }
expected: ["1->2->5", "1->3"]
- input: { root: [1] }
expected: ["1"]
hidden:
- input: { root: [1, 2, 3] }
expected: ["1->2", "1->3"]
- input: { root: [1, 2, null, 3, null, 4] }
expected: ["1->2->3->4"]
- input: { root: [1, 2, 3, 4, 5, 6, 7] }
expected: ["1->2->4", "1->2->5", "1->3->6", "1->3->7"]
- input: { root: [-1, -2, -3] }
expected: ["-1->-2", "-1->-3"]
description: | description: |
Given the `root` of a binary tree, return *all root-to-leaf paths in **any order***. Given the `root` of a binary tree, return *all root-to-leaf paths in **any order***.

View File

@@ -11,6 +11,26 @@ patterns:
- dfs - dfs
- tree-traversal - tree-traversal
function_signature: "def postorder_traversal(root: TreeNode) -> list[int]:"
test_cases:
visible:
- input: { root: [1, null, 2, 3] }
expected: [3, 2, 1]
- input: { root: [] }
expected: []
- input: { root: [1] }
expected: [1]
hidden:
- input: { root: [1, 2, 3, 4, 5, null, 8, null, null, 6, 7, 9] }
expected: [4, 6, 7, 5, 2, 9, 8, 3, 1]
- input: { root: [1, 2, null, 3, null, 4] }
expected: [4, 3, 2, 1]
- input: { root: [1, null, 2, null, 3] }
expected: [3, 2, 1]
- input: { root: [5, 3, 7, 2, 4, 6, 8] }
expected: [2, 4, 3, 6, 8, 7, 5]
description: | description: |
Given the `root` of a binary tree, return *the postorder traversal of its nodes' values*. Given the `root` of a binary tree, return *the postorder traversal of its nodes' values*.

View File

@@ -11,6 +11,26 @@ patterns:
- tree-traversal - tree-traversal
- dfs - dfs
function_signature: "def preorder_traversal(root: TreeNode) -> list[int]:"
test_cases:
visible:
- input: { root: [1, null, 2, 3] }
expected: [1, 2, 3]
- input: { root: [] }
expected: []
- input: { root: [1] }
expected: [1]
hidden:
- input: { root: [1, 2, 3, 4, 5, null, 8, null, null, 6, 7, 9] }
expected: [1, 2, 4, 5, 6, 7, 3, 8, 9]
- input: { root: [1, 2, null, 3, null, 4] }
expected: [1, 2, 3, 4]
- input: { root: [1, null, 2, null, 3, null, 4] }
expected: [1, 2, 3, 4]
- input: { root: [5, 3, 7, 2, 4, 6, 8] }
expected: [5, 3, 2, 4, 7, 6, 8]
description: | description: |
Given the `root` of a binary tree, return *the preorder traversal of its nodes' values*. Given the `root` of a binary tree, return *the preorder traversal of its nodes' values*.

View File

@@ -10,6 +10,30 @@ patterns:
- dfs - dfs
- tree-traversal - tree-traversal
function_signature: "def prune_tree(root: TreeNode) -> TreeNode:"
test_cases:
visible:
- input: { root: [1, null, 0, 0, 1] }
expected: [1, null, 0, null, 1]
- input: { root: [1, 0, 1, 0, 0, 0, 1] }
expected: [1, null, 1, null, 1]
- input: { root: [1, 1, 0, 1, 1, 0, 1, 0] }
expected: [1, 1, 0, 1, 1, null, 1]
hidden:
- input: { root: [1] }
expected: [1]
- input: { root: [0] }
expected: null
- input: { root: [0, 0, 0] }
expected: null
- input: { root: [1, 1, 1, 1, 1, 1, 1] }
expected: [1, 1, 1, 1, 1, 1, 1]
- input: { root: [0, 0, 0, 1] }
expected: [0, 0, null, 1]
- input: { root: [1, 0, 0, 0, 0, 0, 1] }
expected: [1, null, 0, null, 1]
description: | description: |
Given the `root` of a binary tree, return *the same tree where every subtree (of the given tree) not containing a* `1` *has been removed*. Given the `root` of a binary tree, return *the same tree where every subtree (of the given tree) not containing a* `1` *has been removed*.

View File

@@ -10,6 +10,30 @@ patterns:
- dfs - dfs
- tree-traversal - tree-traversal
function_signature: "def right_side_view(root: TreeNode) -> list[int]:"
test_cases:
visible:
- input: { root: [1, 2, 3, null, 5, null, 4] }
expected: [1, 3, 4]
- input: { root: [1, 2, 3, 4, null, null, null, 5] }
expected: [1, 3, 4, 5]
- input: { root: [1, null, 3] }
expected: [1, 3]
hidden:
- input: { root: [] }
expected: []
- input: { root: [1] }
expected: [1]
- input: { root: [1, 2, null] }
expected: [1, 2]
- input: { root: [1, 2, 3, 4, 5, 6, 7] }
expected: [1, 3, 7]
- input: { root: [1, 2, null, 3, null, 4, null, 5] }
expected: [1, 2, 3, 4, 5]
- input: { root: [0, -1, 100] }
expected: [0, 100]
description: | description: |
Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return *the values of the nodes you can see ordered from top to bottom*. Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return *the values of the nodes you can see ordered from top to bottom*.

View File

@@ -10,6 +10,32 @@ patterns:
- dfs - dfs
- tree-traversal - tree-traversal
function_signature: "def find_tilt(root: TreeNode) -> int:"
test_cases:
visible:
- input: { root: [1, 2, 3] }
expected: 1
- input: { root: [4, 2, 9, 3, 5, null, 7] }
expected: 15
- input: { root: [21, 7, 14, 1, 1, 2, 2, 3, 3] }
expected: 9
hidden:
- input: { root: [] }
expected: 0
- input: { root: [1] }
expected: 0
- input: { root: [1, 2, null] }
expected: 2
- input: { root: [1, null, 3] }
expected: 3
- input: { root: [0, 0, 0] }
expected: 0
- input: { root: [5, 5, 5, 5, 5, 5, 5] }
expected: 0
- input: { root: [1, 2, 3, 4, null, 5, 6] }
expected: 11
description: | description: |
Given the `root` of a binary tree, return *the sum of every tree node's **tilt***. Given the `root` of a binary tree, return *the sum of every tree node's **tilt***.

View File

@@ -10,6 +10,28 @@ patterns:
- bfs - bfs
- tree-traversal - tree-traversal
function_signature: "def zigzag_level_order(root: TreeNode) -> list[list[int]]:"
test_cases:
visible:
- input: { root: [3, 9, 20, null, null, 15, 7] }
expected: [[3], [20, 9], [15, 7]]
- input: { root: [1] }
expected: [[1]]
- input: { root: [] }
expected: []
hidden:
- input: { root: [1, 2, 3, 4, 5, 6, 7] }
expected: [[1], [3, 2], [4, 5, 6, 7]]
- input: { root: [1, 2, null, 3, null, 4] }
expected: [[1], [2], [3], [4]]
- input: { root: [1, 2, 3, 4, null, null, 5] }
expected: [[1], [3, 2], [4, 5]]
- input: { root: [0, -1, 100, -50, 50, -100, 200] }
expected: [[0], [100, -1], [-50, 50, -100, 200]]
- input: { root: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] }
expected: [[1], [3, 2], [4, 5, 6, 7], [15, 14, 13, 12, 11, 10, 9, 8]]
description: | description: |
Given the `root` of a binary tree, return *the zigzag level order traversal of its nodes' values* (i.e., from left to right, then right to left for the next level and alternate between). Given the `root` of a binary tree, return *the zigzag level order traversal of its nodes' values* (i.e., from left to right, then right to left for the next level and alternate between).

View File

@@ -11,6 +11,30 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def num_factored_binary_trees(arr: list[int]) -> int:"
test_cases:
visible:
- input: { arr: [2, 4] }
expected: 3
- input: { arr: [2, 4, 5, 10] }
expected: 7
hidden:
- input: { arr: [2] }
expected: 1
- input: { arr: [2, 3, 5, 7, 11] }
expected: 5
- input: { arr: [2, 4, 8] }
expected: 7
- input: { arr: [2, 4, 8, 16] }
expected: 15
- input: { arr: [2, 3, 6, 18] }
expected: 7
- input: { arr: [18, 3, 6, 2] }
expected: 7
- input: { arr: [2, 4, 16, 8] }
expected: 15
description: | description: |
Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`. Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- backtracking - backtracking
function_signature: "def read_binary_watch(turned_on: int) -> list[str]:"
test_cases:
visible:
- input: { turned_on: 1 }
expected: ["0:01", "0:02", "0:04", "0:08", "0:16", "0:32", "1:00", "2:00", "4:00", "8:00"]
- input: { turned_on: 9 }
expected: []
hidden:
- input: { turned_on: 0 }
expected: ["0:00"]
- input: { turned_on: 2 }
expected: ["0:03", "0:05", "0:06", "0:09", "0:10", "0:12", "0:17", "0:18", "0:20", "0:24", "0:33", "0:34", "0:36", "0:40", "0:48", "1:01", "1:02", "1:04", "1:08", "1:16", "1:32", "2:01", "2:02", "2:04", "2:08", "2:16", "2:32", "3:00", "4:01", "4:02", "4:04", "4:08", "4:16", "4:32", "5:00", "6:00", "8:01", "8:02", "8:04", "8:08", "8:16", "8:32", "9:00", "10:00"]
- input: { turned_on: 10 }
expected: []
- input: { turned_on: 8 }
expected: ["7:31", "7:47", "7:55", "7:59", "11:31", "11:47", "11:55", "11:59"]
- input: { turned_on: 5 }
expected: ["0:31", "0:47", "0:55", "0:59", "1:15", "1:23", "1:27", "1:29", "1:30", "1:39", "1:43", "1:45", "1:46", "1:51", "1:53", "1:54", "1:57", "1:58", "2:15", "2:23", "2:27", "2:29", "2:30", "2:39", "2:43", "2:45", "2:46", "2:51", "2:53", "2:54", "2:57", "2:58", "3:07", "3:11", "3:13", "3:14", "3:19", "3:21", "3:22", "3:25", "3:26", "3:28", "3:35", "3:37", "3:38", "3:41", "3:42", "3:44", "3:49", "3:50", "3:52", "3:56", "4:15", "4:23", "4:27", "4:29", "4:30", "4:39", "4:43", "4:45", "4:46", "4:51", "4:53", "4:54", "4:57", "4:58", "5:07", "5:11", "5:13", "5:14", "5:19", "5:21", "5:22", "5:25", "5:26", "5:28", "5:35", "5:37", "5:38", "5:41", "5:42", "5:44", "5:49", "5:50", "5:52", "5:56", "6:07", "6:11", "6:13", "6:14", "6:19", "6:21", "6:22", "6:25", "6:26", "6:28", "6:35", "6:37", "6:38", "6:41", "6:42", "6:44", "6:49", "6:50", "6:52", "6:56", "7:03", "7:05", "7:06", "7:09", "7:10", "7:12", "7:17", "7:18", "7:20", "7:24", "7:33", "7:34", "7:36", "7:40", "7:48", "8:15", "8:23", "8:27", "8:29", "8:30", "8:39", "8:43", "8:45", "8:46", "8:51", "8:53", "8:54", "8:57", "8:58", "9:07", "9:11", "9:13", "9:14", "9:19", "9:21", "9:22", "9:25", "9:26", "9:28", "9:35", "9:37", "9:38", "9:41", "9:42", "9:44", "9:49", "9:50", "9:52", "9:56", "10:07", "10:11", "10:13", "10:14", "10:19", "10:21", "10:22", "10:25", "10:26", "10:28", "10:35", "10:37", "10:38", "10:41", "10:42", "10:44", "10:49", "10:50", "10:52", "10:56", "11:03", "11:05", "11:06", "11:09", "11:10", "11:12", "11:17", "11:18", "11:20", "11:24", "11:33", "11:34", "11:36", "11:40", "11:48"]
description: | description: |
A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right. A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.

View File

@@ -8,6 +8,32 @@ categories:
patterns: patterns:
- binary-search - binary-search
function_signature: "def range_bitwise_and(left: int, right: int) -> int:"
test_cases:
visible:
- input: { left: 5, right: 7 }
expected: 4
- input: { left: 0, right: 0 }
expected: 0
- input: { left: 1, right: 2147483647 }
expected: 0
hidden:
- input: { left: 1, right: 1 }
expected: 1
- input: { left: 10, right: 10 }
expected: 10
- input: { left: 0, right: 1 }
expected: 0
- input: { left: 12, right: 15 }
expected: 12
- input: { left: 8, right: 10 }
expected: 8
- input: { left: 1073741824, right: 2147483647 }
expected: 1073741824
- input: { left: 600000000, right: 600000001 }
expected: 600000000
description: | description: |
Given two integers `left` and `right` that represent the range `[left, right]`, return *the bitwise AND of all numbers in this range, inclusive*. Given two integers `left` and `right` that represent the range `[left, right]`, return *the bitwise AND of all numbers in this range, inclusive*.

View File

@@ -10,6 +10,30 @@ categories:
patterns: patterns:
- dynamic-programming - dynamic-programming
function_signature: "def subarray_bitwise_ors(arr: list[int]) -> int:"
test_cases:
visible:
- input: { arr: [0] }
expected: 1
- input: { arr: [1, 1, 2] }
expected: 3
- input: { arr: [1, 2, 4] }
expected: 6
hidden:
- input: { arr: [1] }
expected: 1
- input: { arr: [0, 0, 0] }
expected: 1
- input: { arr: [1, 2, 3, 4, 5] }
expected: 7
- input: { arr: [7, 7, 7] }
expected: 1
- input: { arr: [1, 3, 5, 7] }
expected: 4
- input: { arr: [8, 4, 2, 1] }
expected: 10
description: | description: |
Given an integer array `arr`, return *the number of distinct bitwise ORs of all the non-empty subarrays of* `arr`. Given an integer array `arr`, return *the number of distinct bitwise ORs of all the non-empty subarrays of* `arr`.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def xor_all_nums(nums1: list[int], nums2: list[int]) -> int:"
test_cases:
visible:
- input: { nums1: [2, 1, 3], nums2: [10, 2, 5, 0] }
expected: 13
- input: { nums1: [1, 2], nums2: [3, 4] }
expected: 0
hidden:
- input: { nums1: [1], nums2: [1] }
expected: 0
- input: { nums1: [5], nums2: [3, 7] }
expected: 0
- input: { nums1: [1, 2, 3], nums2: [4] }
expected: 4
- input: { nums1: [0], nums2: [0, 0, 0] }
expected: 0
- input: { nums1: [1, 1], nums2: [2, 2] }
expected: 0
- input: { nums1: [7, 8, 9], nums2: [1, 2, 3] }
expected: 0
description: | description: |
You are given two **0-indexed** arrays, `nums1` and `nums2`, consisting of non-negative integers. You are given two **0-indexed** arrays, `nums1` and `nums2`, consisting of non-negative integers.

View File

@@ -11,6 +11,32 @@ patterns:
- two-pointers - two-pointers
- greedy - greedy
function_signature: "def num_rescue_boats(people: list[int], limit: int) -> int:"
test_cases:
visible:
- input: { people: [1, 2], limit: 3 }
expected: 1
- input: { people: [3, 2, 2, 1], limit: 3 }
expected: 3
- input: { people: [3, 5, 3, 4], limit: 5 }
expected: 4
hidden:
- input: { people: [1], limit: 3 }
expected: 1
- input: { people: [5, 5, 5, 5], limit: 5 }
expected: 4
- input: { people: [1, 1, 1, 1], limit: 3 }
expected: 2
- input: { people: [1, 2, 3, 4, 5], limit: 5 }
expected: 3
- input: { people: [2, 2, 2, 2], limit: 4 }
expected: 2
- input: { people: [3, 8, 1, 4, 6, 5, 2, 7], limit: 9 }
expected: 5
- input: { people: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], limit: 2 }
expected: 5
description: | description: |
You are given an array `people` where `people[i]` is the weight of the i<sup>th</sup> person, and an **infinite number of boats** where each boat can carry a maximum weight of `limit`. Each boat carries **at most two people** at the same time, provided the sum of the weight of those people is at most `limit`. You are given an array `people` where `people[i]` is the weight of the i<sup>th</sup> person, and an **infinite number of boats** where each boat can carry a maximum weight of `limit`. Each boat carries **at most two people** at the same time, provided the sum of the weight of those people is at most `limit`.

View File

@@ -8,6 +8,30 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def break_palindrome(palindrome: str) -> str:"
test_cases:
visible:
- input: { palindrome: "abccba" }
expected: "aaccba"
- input: { palindrome: "a" }
expected: ""
hidden:
- input: { palindrome: "aa" }
expected: "ab"
- input: { palindrome: "aba" }
expected: "abb"
- input: { palindrome: "aaa" }
expected: "aab"
- input: { palindrome: "aaaa" }
expected: "aaab"
- input: { palindrome: "bab" }
expected: "aab"
- input: { palindrome: "abba" }
expected: "aaba"
- input: { palindrome: "zyzyz" }
expected: "ayzyz"
description: | description: |
Given a palindromic string of lowercase English letters `palindrome`, replace **exactly one** character with any lowercase English letter so that the resulting string is **not** a palindrome and that it is the **lexicographically smallest** one possible. Given a palindromic string of lowercase English letters `palindrome`, replace **exactly one** character with any lowercase English letter so that the resulting string is **not** a palindrome and that it is the **lexicographically smallest** one possible.

View File

@@ -9,6 +9,26 @@ categories:
patterns: patterns:
- prefix-sum - prefix-sum
function_signature: "def least_bricks(wall: list[list[int]]) -> int:"
test_cases:
visible:
- input: { wall: [[1, 2, 2, 1], [3, 1, 2], [1, 3, 2], [2, 4], [3, 1, 2], [1, 3, 1, 1]] }
expected: 2
- input: { wall: [[1], [1], [1]] }
expected: 3
hidden:
- input: { wall: [[1, 1], [1, 1], [1, 1]] }
expected: 0
- input: { wall: [[3, 1], [1, 3], [2, 2]] }
expected: 1
- input: { wall: [[5]] }
expected: 1
- input: { wall: [[1, 1, 1, 1], [2, 2], [1, 3], [4]] }
expected: 1
- input: { wall: [[2, 2, 2], [2, 2, 2], [2, 2, 2]] }
expected: 0
description: | description: |
There is a rectangular brick wall in front of you with `n` rows of bricks. The i<sup>th</sup> row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same. There is a rectangular brick wall in front of you with `n` rows of bricks. The i<sup>th</sup> row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same.

View File

@@ -8,6 +8,32 @@ categories:
patterns: patterns:
- greedy - greedy
function_signature: "def broken_calc(start_value: int, target: int) -> int:"
test_cases:
visible:
- input: { start_value: 2, target: 3 }
expected: 2
- input: { start_value: 5, target: 8 }
expected: 2
- input: { start_value: 3, target: 10 }
expected: 3
hidden:
- input: { start_value: 1, target: 1 }
expected: 0
- input: { start_value: 10, target: 5 }
expected: 5
- input: { start_value: 1, target: 1000000000 }
expected: 39
- input: { start_value: 1, target: 2 }
expected: 1
- input: { start_value: 1, target: 1024 }
expected: 10
- input: { start_value: 100, target: 1 }
expected: 99
- input: { start_value: 1, target: 17 }
expected: 6
description: | description: |
There is a broken calculator that has the integer `startValue` on its display initially. In one operation, you can: There is a broken calculator that has the integer `startValue` on its display initially. In one operation, you can:

View File

@@ -9,6 +9,30 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def buddy_strings(s: str, goal: str) -> bool:"
test_cases:
visible:
- input: { s: "ab", goal: "ba" }
expected: true
- input: { s: "ab", goal: "ab" }
expected: false
- input: { s: "aa", goal: "aa" }
expected: true
hidden:
- input: { s: "aaaaaaabc", goal: "aaaaaaacb" }
expected: true
- input: { s: "abcd", goal: "badc" }
expected: false
- input: { s: "a", goal: "a" }
expected: false
- input: { s: "ab", goal: "ca" }
expected: false
- input: { s: "abab", goal: "abab" }
expected: true
- input: { s: "abcdef", goal: "abcfed" }
expected: false
description: | description: |
Given two strings `s` and `goal`, return `true` *if you can swap two letters in* `s` *so the result is equal to* `goal`, *otherwise, return* `false`. Given two strings `s` and `goal`, return `true` *if you can swap two letters in* `s` *so the result is equal to* `goal`, *otherwise, return* `false`.

View File

@@ -9,6 +9,28 @@ categories:
patterns: patterns:
- two-pointers - two-pointers
function_signature: "def build_array(target: list[int], n: int) -> list[str]:"
test_cases:
visible:
- input: { target: [1, 3], n: 3 }
expected: ["Push", "Push", "Pop", "Push"]
- input: { target: [1, 2, 3], n: 3 }
expected: ["Push", "Push", "Push"]
- input: { target: [1, 2], n: 4 }
expected: ["Push", "Push"]
hidden:
- input: { target: [1], n: 1 }
expected: ["Push"]
- input: { target: [2], n: 2 }
expected: ["Push", "Pop", "Push"]
- input: { target: [3], n: 5 }
expected: ["Push", "Pop", "Push", "Pop", "Push"]
- input: { target: [1, 4], n: 5 }
expected: ["Push", "Push", "Pop", "Push", "Pop", "Push"]
- input: { target: [2, 3, 4], n: 4 }
expected: ["Push", "Pop", "Push", "Push", "Push"]
description: | description: |
You are given an integer array `target` and an integer `n`. You are given an integer array `target` and an integer `n`.

Some files were not shown because too many files have changed in this diff Show More