id
stringlengths
16
16
difficulty
stringclasses
3 values
category
stringclasses
11 values
problem
stringclasses
25 values
signature
stringclasses
25 values
reasoning
stringclasses
25 values
code
stringclasses
25 values
tests
stringlengths
106
815
verified
bool
1 class
language
stringclasses
1 value
code_hash
stringclasses
25 values
sft_text
stringclasses
25 values
meta
stringlengths
11
47
vcr_d72aa5ae8af9
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-18, 3, -19, 8, 8], "k": 6}, "output": [8, -18, 3, -19, 8]}, {"input": {"nums": [-4, -1, 3, -8, 0, 10], "k": 2}, "output": [0, 10, -4, -1, 3, -8]}, {"input": {"nums": [8, -4, -4, 9, 7, 0, -7, 5, -6], "k": 1}, "output": [-6, 8, -4, -4, 9, 7, 0, -7, 5]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "ef5eadb50f1f5fbd"}
vcr_20909b50dcf9
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["aab", "aa", "bcbb", "acbb", "aa", "cbac", "cac", "ccc", "aacb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "abb", "aab"]}, "output": [1, 1, 1]}, {"input": {"strs": ["abb", "aab", "bab"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "67a6b2076aaefda4"}
vcr_a31002b536be
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [11, 0, 22, 0, 7, 11, 3, 4, -1, 0, 1, 17, 9, 9, 11]}, "output": 104}, {"input": {"nums": [6, 14, 12, -8, 4]}, "output": 32}, {"input": {"nums": [6, -10, 3, 12, -11]}, "output": 15}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "10b271a5f125488f"}
vcr_40608092811a
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-1, -6, 9, -16, -2, -5, 3, 20, 15, 17], "k": 11}, "output": [17, -1, -6, 9, -16, -2, -5, 3, 20, 15]}, {"input": {"nums": [3, -3, 9, -7, 9, -5, 7, 6, -8, 8], "k": 19}, "output": [-3, 9, -7, 9, -5, 7, 6, -8, 8, 3]}, {"input": {"nums": [-9, 1, 9, -6, 1, 3, -8, -5], "k": 11}, "output": [3, -8, -5, -9, ...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "245bf7b88670283f"}
vcr_8de8d92027d0
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 0, 0, 2, 0, 0, 0, 0]}, "output": false}, {"input": {"nums": [1, 0, 3, 0, 3, 1, 2, 3, 3]}, "output": false}, {"input": {"nums": [3, 3, 0, 3, 3, 3, 3, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "99d24e2683d47fce"}
vcr_61250aa496bc
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 2, 3, 3, 0, 2, 4, 3, 0]}, "output": true}, {"input": {"nums": [3, 0, 0, 1]}, "output": true}, {"input": {"nums": [1, 3, 2, 2, 1, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "8b724a112f24a138"}
vcr_6abfd3c5c2c7
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, -25, -19, -3, -1, 15, 18, 21, 23, 40, 44, 48], "target": 17}, "output": [0, 10]}, {"input": {"nums": [-14, 5, 9, 34], "target": 39}, "output": [1, 3]}, {"input": {"nums": [-15, -14, 9, 27, 28], "target": 12}, "output": [0, 3]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "5182d4e702766011"}
vcr_f9466e92b848
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 4, 2, 3, 0, 0, 2, 0]}, "output": true}, {"input": {"nums": [3, 3, 1, 2, 3]}, "output": true}, {"input": {"nums": [0, 2, 2, 3, 0, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "bcba618092051c59"}
vcr_ecf458428aa7
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [15, 5, -19, 3, -10, -10, 11, -12, 0, 6, 12, -8, 10, 31]}, "output": [15, 20, 1, 4, -6, -16, -5, -17, -17, -11, 1, -7, 3, 34]}, {"input": {"nums": [13, 15, 0]}, "output": [13, 28, 28]}, {"input": {"nums": [14, 0, 3]}, "output": [14, 14, 17]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "02507bac7f6cdce6"}
vcr_712f3a66eb23
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-24, -18, -7, -4, -3, 1, 6, 11, 39, 47], "target": 50}, "output": [7, 8]}, {"input": {"nums": [-8, -7, 5, 20, 31], "target": -2}, "output": [1, 2]}, {"input": {"nums": [-13, -6, 6, 12, 24, 27, 33, 37, 39], "target": 31}, "output": [1, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "7324f0255e2d9113"}
vcr_fcda5898cb5d
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-28, -10, -1, 13, 36, 40], "target": 26}, "output": [1, 4]}, {"input": {"nums": [-9, -1, 2, 7, 16], "target": 6}, "output": [1, 3]}, {"input": {"nums": [-17, -15, 0, 3, 19, 21, 23], "target": 19}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "2cc49a3b7a44d081"}
vcr_03a39fa7ab30
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [18, 3, -11, 30, 34, 23, 9, 24, -11, 20, 7, 27]}, "output": [18, 21, 10, 40, 74, 97, 106, 130, 119, 139, 146, 173]}, {"input": {"nums": [14, -10, 4, -2, 13, 16, -4]}, "output": [14, 4, 8, 6, 19, 35, 31]}, {"input": {"nums": [9, -7, 7, 17, 17, -3, 13, 14]}, "output": [9, 2, 9, 26, 43, 40, 53, 67]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "848cc33ee4cebef1"}
vcr_680ad1727a72
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "d baee edce acccba"}, "output": "acccba edce baee d"}, {"input": {"s": "b baa baaa babb aabb"}, "output": "aabb babb baaa baa b"}, {"input": {"s": "bb b abab"}, "output": "abab b bb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "3d88f51d40916bcd"}
vcr_aa3f3b1d7130
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [65, 22, 38, 6, 78, 41, 46, 15, 18, 43], "target": 24}, "output": [3, 8]}, {"input": {"nums": [38, -10, -11, 22, 0, 43, -21, -5, 26], "target": 12}, "output": [1, 3]}, {"input": {"nums": [46, 33, 17, 27, -29, -21, 0], "target": 60}, "output": [1, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "7180f7475937e67d"}
vcr_2a44a61a0d4a
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [4, 1, 2, 0, 3]}, "output": [0, 0, 0, 24, 0]}, {"input": {"nums": [4, 4, 5, 5, 2, 4, 5]}, "output": [4000, 4000, 3200, 3200, 8000, 4000, 3200]}, {"input": {"nums": [2, 2, 1, 5, 2]}, "output": [20, 20, 40, 8, 20]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "4c373ca0016fb162"}
vcr_bb8fb026435c
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-18, 66, 36, 51, 25, 78, -4, -39, -29, 43, 18, -40], "target": -4}, "output": [4, 8]}, {"input": {"nums": [7, 33, -14, 46, 1, -10], "target": 53}, "output": [0, 3]}, {"input": {"nums": [41, 14, -20, -19], "target": -5}, "output": [1, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "bc02e362f2d1a466"}
vcr_e9208466913f
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [5, 6, 12, 14, 21, 25, 26, 31, 35, 36, 41, 42, 46, 49], "target": 2}, "output": 0}, {"input": {"nums": [8, 11, 25, 32, 34, 39], "target": 11}, "output": 1}, {"input": {"nums": [7, 16, 17, 23, 38], "target": 12}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "34211e8968dd16f5"}
vcr_754510b80abb
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 2, 3, 3, 5, 5, 5, 6, 6, 7, 8, 11, 14]}, "output": [0, 1, 2, 3, 5, 6, 7, 8, 11, 14]}, {"input": {"nums": [0, 3, 4, 4, 5, 8, 9, 9, 10, 11]}, "output": [0, 3, 4, 5, 8, 9, 10, 11]}, {"input": {"nums": [0, 0, 1, 1, 1, 3, 4, 5, 6, 9]}, "output": [0, 1, 3, 4, 5, 6, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "8fcd70d97cfe5432"}
vcr_4dd82294673f
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [8, 67, 180, 105, 151, 24, 197, 66, 161, 135, 166, 81, 158]}, "output": false}, {"input": {"nums": [15, 1, 20, 4, 19, 13, 19, 13]}, "output": true}, {"input": {"nums": [1, 0, 3]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "e64b5ab30f8baee3"}
vcr_54a641175051
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [1, -3, -18, -9, -5, 3, -13, -11, -19, 15, -14, 11], "k": 18}, "output": [-13, -11, -19, 15, -14, 11, 1, -3, -18, -9, -5, 3]}, {"input": {"nums": [-4, 1, -9, 9, 2, 7, 8, 4, 1], "k": 3}, "output": [8, 4, 1, -4, 1, -9, 9, 2, 7]}, {"input": {"nums": [3, 10, 0, -9, -9], "k": 19}, "output": [10, 0, -9, -...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "cc92abdd0335cb69"}
vcr_2dbd0c01d4a7
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-7, 16, -15, 3, 0, -12, 10], "k": 4}, "output": [3, 0, -12, 10, -7, 16, -15]}, {"input": {"nums": [-9, -10, -4, 10, -10, 1, -2], "k": 13}, "output": [-10, -4, 10, -10, 1, -2, -9]}, {"input": {"nums": [7, -8, 10, 4, 2], "k": 10}, "output": [7, -8, 10, 4, 2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "aee73f3a378ce468"}
vcr_4afbc9439f42
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [24, 27, 16, 12, -11, -11, 17, -19, 28, 28]}, "output": [24, 51, 67, 79, 68, 57, 74, 55, 83, 111]}, {"input": {"nums": [-7, 2]}, "output": [-7, -5]}, {"input": {"nums": [-2, 17, 12, -11, 13, -7, -3, 1, -1]}, "output": [-2, 15, 27, 16, 29, 22, 19, 20, 19]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "b6ae5f89751e3aec"}
vcr_2d0ddcf55130
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [11, 11, 16, -8, -10, -14, 19, -14, -14, -4, -2, 2, -17, 9]}, "output": 38}, {"input": {"nums": [-3, -5, 13, -9, -2]}, "output": 13}, {"input": {"nums": [-11, 9, 10, 5, 10, -9, 6, 6, 12, 14]}, "output": 63}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "f863977fa23930fc"}
vcr_061718055b2b
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [15, 15, 2, 4, 35, 14, 35, 38, 5, 5, 2, 4, 56, 38, 13, 13, 14]}, "output": 56}, {"input": {"nums": [5, 5, 21, 28, 3, 56, 11, 28, 3, 21, 11]}, "output": 56}, {"input": {"nums": [18, 18, 6, 49, 1, 1, 6]}, "output": 49}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "5c68038dc185a21b"}
vcr_d0aee8ea3df3
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [2, 27, 22, 6, 13, 14, 23]}, "output": 63}, {"input": {"nums": [11, 14, 13, 12, 14, 19]}, "output": 45}, {"input": {"nums": [14, 10, 19, 20, 9, 2, 18, 16, 1, 19]}, "output": 79}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "50f3ce52ff3bee10"}
vcr_307f899652d3
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-8, 24, 73, -16, 35, 21, 75, 54, 72], "target": 108}, "output": [2, 4]}, {"input": {"nums": [5, 23, -11, 39], "target": 62}, "output": [1, 3]}, {"input": {"nums": [19, -25, 40, -26, -12, 15, -29, 38, 14], "target": 12}, "output": [3, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "50d20dcd992a4378"}
vcr_082468bb0e81
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [26, 5, 8, 5]}, "output": 34}, {"input": {"nums": [8, 4, 7, 15, 4, 11, 10, 12, 19, 4]}, "output": 53}, {"input": {"nums": [13, 4, 6, 2, 9]}, "output": 28}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "64c0ae9565d8e985"}
vcr_61fc9c4f5ceb
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 3, 7, 9, 11, 11, 13, 13, 14, 14, 15]}, "output": [3, 7, 9, 11, 13, 14, 15]}, {"input": {"nums": [5, 9, 11]}, "output": [5, 9, 11]}, {"input": {"nums": [0, 2, 4, 5]}, "output": [0, 2, 4, 5]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "59031d86bf9b88f2"}
vcr_df49e5b84cd4
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 141, "b": 87}, "output": 3}, {"input": {"a": 54, "b": 125}, "output": 1}, {"input": {"a": 28, "b": 140}, "output": 28}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "b494d01336a277bd"}
vcr_84b1ec894757
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 1, 1, 2, 2, 6, 13]}, "output": [1, 2, 6, 13]}, {"input": {"nums": [0, 1, 2, 3, 3, 8]}, "output": [0, 1, 2, 3, 8]}, {"input": {"nums": [0, 0, 1, 3, 4, 4, 9, 10]}, "output": [0, 1, 3, 4, 9, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "3953b2fcd37d13b2"}
vcr_67702c202b53
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [1, 29, 55, -22], "target": 56}, "output": [0, 2]}, {"input": {"nums": [26, -8, -28, -4, 28, 12], "target": 0}, "output": [2, 4]}, {"input": {"nums": [2, 24, -22, -13], "target": 2}, "output": [1, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "dd4f38c06b75dbab"}
vcr_49d7a323afeb
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "bdbadaadabdb"}, "output": true}, {"input": {"s": "bbbbbbbaba"}, "output": false}, {"input": {"s": "cbacccbaa"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "8374fb9cdd0d707b"}
vcr_a740c07039a7
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 2, 3, 6, 7, 11, 17, 20, 21, 23, 28, 33, 40], "target": 7}, "output": 4}, {"input": {"nums": [3, 5, 6, 8, 10, 19, 21, 24, 28, 35], "target": 35}, "output": 9}, {"input": {"nums": [3, 7, 8, 22, 32, 37], "target": 2}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "9882d15558ffd059"}
vcr_8ed9546a2e9f
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 74, "b": 48}, "output": 2}, {"input": {"a": 93, "b": 27}, "output": 3}, {"input": {"a": 5, "b": 31}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "702f0d9be1699c0e"}
vcr_4c1b7232d8a6
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [7, 6, 7, 0, 3, 2, 7, 6]}, "output": [0, 0, 0, 74088, 0, 0, 0, 0]}, {"input": {"nums": [1, 5, 3, 2, 3, 1, 2]}, "output": [180, 36, 60, 90, 60, 180, 90]}, {"input": {"nums": [1, 2, 5]}, "output": [10, 5, 2]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "6f83bbd76bd22a6c"}
vcr_d7b1d4d6cbae
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [9, -12, -7, -9, 18, -18, 7, 4]}, "output": 18}, {"input": {"nums": [3, -1, 13, -9, -10, 10, 7, 11, -11, 13]}, "output": 30}, {"input": {"nums": [9, 1, -5, 8, -10, -9, 13, -5]}, "output": 13}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "e96dbea9cbcea4e9"}
vcr_fb212f211b0b
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [35, 7, 78, 7, 5, 12, 37, 35, 12, 37, 5, 16, 16]}, "output": 78}, {"input": {"nums": [55, 26, 13, 26, 13]}, "output": 55}, {"input": {"nums": [11, 27, 1, 11, 24, 14, 21, 24, 21, 48, 1, 14, 27]}, "output": 48}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "b4034267af9da08f"}
vcr_c6346613144e
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 4, 7, 9, 13, 14, 34, 36, 39], "target": 2}, "output": 0}, {"input": {"nums": [7, 8, 11, 30, 32, 33], "target": 9}, "output": 2}, {"input": {"nums": [1, 2, 6, 8, 10, 17, 23, 24, 31], "target": 30}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "b2704eaa1e6b5b1b"}
vcr_b8860b3c3740
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "a cb a ceaaac e dbce aecbab"}, "output": "aecbab dbce e ceaaac a cb a"}, {"input": {"s": "ba baaa a abbb abb"}, "output": "abb abbb a baaa ba"}, {"input": {"s": "baa b bbb bbb"}, "output": "bbb bbb b baa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "160c05203fb8c6b0"}
vcr_0d00ac2450f6
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [20, 5, 19, 26, 19, 6, 27, 24, 23]}, "output": 108}, {"input": {"nums": [18, 12, 6, 19, 2, 15]}, "output": 52}, {"input": {"nums": [2, 11, 18, 8, 20, 19, 18, 17, 4]}, "output": 62}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "02fbd5dcb2cac4c5"}
vcr_78e21844fced
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [16, 22, 67, 14, 14, 22, 16]}, "output": 67}, {"input": {"nums": [19, 29, 12, 57, 12, 29, 19, 1, 1]}, "output": 57}, {"input": {"nums": [23, 22, 22, 23, 51]}, "output": 51}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "9c1a9be08fb28d19"}
vcr_1ac0c0c91a4a
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [53, 16, 16, 28, 28, 9, 9]}, "output": 53}, {"input": {"nums": [23, 3, 23, 3, 57, 13, 13, 16, 16, 10, 10]}, "output": 57}, {"input": {"nums": [46, 13, 7, 11, 28, 11, 16, 12, 13, 7, 28, 16, 12]}, "output": 46}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "8f340d15a279da43"}
vcr_458643f6ae9e
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [11, 3, 26, 49, 53, 33]}, "output": 50}, {"input": {"prices": [7, 10, 11, 5, 11, 17, 27, 13, 32, 14]}, "output": 27}, {"input": {"prices": [13, 15, 28, 1, 27, 19, 6, 37, 30]}, "output": 36}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "73f642bb50f1008e"}
vcr_0e36405aa8a6
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 0, 1, 2, 2, 3, 1, 4, 2, 0]}, "output": true}, {"input": {"nums": [3, 0, 0, 0]}, "output": true}, {"input": {"nums": [3, 1, 2, 0, 2, 3, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "ebcf47f80d27e547"}
vcr_f77c72fb3be7
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 187, "b": 193}, "output": 1}, {"input": {"a": 121, "b": 166}, "output": 1}, {"input": {"a": 53, "b": 123}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "3162aab30ecc1e50"}
vcr_989ac854601d
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [10, 0, 15]}, "output": 25}, {"input": {"nums": [18, 8, 13, 1, 14, 2, 14, 1, 12]}, "output": 71}, {"input": {"nums": [6, 3, 14, 5, 9, 12, 15, 5, 11, 16]}, "output": 60}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "ee7b3152a909d106"}
vcr_02408a201f45
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "aabbbbbaaaca"}, "output": 5}, {"input": {"s": "cacacbaccac"}, "output": 10}, {"input": {"s": "abc"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "81a8e7a88106779b"}
vcr_aae1cf3b61f3
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-15, 12, 1, -8, -2, 6, 13, 8, -18, -3]}, "output": 30}, {"input": {"nums": [-7, -4, 0, 10, 2, 13]}, "output": 25}, {"input": {"nums": [8, 6, -3, 12, 10, 11, 6, 10, -10, 0, 9]}, "output": 60}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "be1e01ce7ab3755b"}
vcr_11216a7fdea3
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [11, 11, 27, 3, 1, 50, 3, 27, 1]}, "output": 50}, {"input": {"nums": [16, 12, 12, 16, 13, 26, 19, 26, 44, 13, 19]}, "output": 44}, {"input": {"nums": [50, 15, 21, 10, 20, 10, 15, 21, 20]}, "output": 50}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "8a3905c246c8dc9e"}
vcr_31819d5c83bf
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [31, 44, 22, 40, 32, 7, 58, 32, 45, 5, 36]}, "output": 51}, {"input": {"prices": [8, 36, 40, 14]}, "output": 32}, {"input": {"prices": [21, 2, 6, 30, 22, 29, 19, 39, 33, 40, 3]}, "output": 38}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "190c629bc808bbba"}
vcr_a40558965e58
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "bbacbaacba"}, "output": 8}, {"input": {"s": "cabab"}, "output": 5}, {"input": {"s": "aacabbcb"}, "output": 6}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "d203f2d7cd2bddcf"}
vcr_24790d509ada
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [92, 9, 3, 96, 196, 113, 16, 188, 137, 154, 191, 97, 43, 6, 134, 50, 158, 44]}, "output": false}, {"input": {"nums": [25, 18, 1, 2, 5, 13]}, "output": false}, {"input": {"nums": [6, 21, 2, 5, 13, 13, 5, 15, 1]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "326cd1952968bf6a"}
vcr_3901deb1a0bf
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 3, 1, 4, 3, 2]}, "output": [72, 48, 144, 36, 48, 72]}, {"input": {"nums": [1, 4, 1, 2, 4, 1]}, "output": [32, 8, 32, 16, 8, 32]}, {"input": {"nums": [1, 1, 4]}, "output": [4, 4, 1]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "9f8e8a770bf9bac4"}
vcr_16268abb394c
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [42, 53, 57, -31, 34, 10, 50, -28, 72, 59, 28], "target": 109}, "output": [6, 9]}, {"input": {"nums": [-26, 41, 2, -7, 45, 4], "target": 6}, "output": [2, 5]}, {"input": {"nums": [15, 49, -3, 17, -22], "target": 12}, "output": [0, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "e056c642e419af11"}
vcr_2f19d33dd125
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-32, 71, 27, 42, 53, -27, 47, 30, 67], "target": 97}, "output": [7, 8]}, {"input": {"nums": [-8, -10, -6, 23, 10], "target": 17}, "output": [2, 3]}, {"input": {"nums": [39, -28, 0, -19, 41, 9, 17, -17], "target": 39}, "output": [0, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "80b2f4e347b8ff55"}
vcr_5a83096bd24e
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 89, "b": 24}, "output": 1}, {"input": {"a": 44, "b": 76}, "output": 4}, {"input": {"a": 107, "b": 57}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "49a99a0adecf9659"}
vcr_5330c7863112
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [3, -13, -7, 5, 16, -15, 19, 6, -4], "k": 1}, "output": [-4, 3, -13, -7, 5, 16, -15, 19, 6]}, {"input": {"nums": [4, 6, -10, -6], "k": 2}, "output": [-10, -6, 4, 6]}, {"input": {"nums": [3, -10, 0], "k": 18}, "output": [3, -10, 0]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "45d74e459d1a515b"}
vcr_1f5e6f7e055b
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 1, 3, 6, 1, 1]}, "output": [18, 18, 6, 3, 18, 18]}, {"input": {"nums": [5, 1, 4, 3]}, "output": [12, 60, 15, 20]}, {"input": {"nums": [5, 4, 4, 2, 1, 4]}, "output": [128, 160, 160, 320, 640, 160]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "cbee7ba773a2cfb1"}
vcr_2b1b5600fa06
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-9, 13, -5, 9, 5, -2, -2, 7, 19, -7, 13]}, "output": 50}, {"input": {"nums": [6, -2, -10, 8, 14, -11, -9]}, "output": 22}, {"input": {"nums": [2, -10, -6, -10, -10]}, "output": 2}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "a414b609d1486b09"}
vcr_59ccd9eaf90b
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 1, 1, 2, 5, 6, 6, 6, 8, 9, 10, 11, 12, 13]}, "output": [1, 2, 5, 6, 8, 9, 10, 11, 12, 13]}, {"input": {"nums": [1, 3, 5, 5, 8, 9, 9, 12]}, "output": [1, 3, 5, 8, 9, 12]}, {"input": {"nums": [0, 0, 1, 2, 5, 7, 7, 7, 7, 9, 10, 11]}, "output": [0, 1, 2, 5, 7, 9, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "bdacfa8d5bbde6f4"}
vcr_c73d756c47c5
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["cc", "abc", "aca", "abc"]}, "output": [1, 1, 2]}, {"input": {"strs": ["baa", "aba", "aaa", "aba", "bbb", "aba", "aaa"]}, "output": [1, 2, 4]}, {"input": {"strs": ["bab", "baa", "bbb", "aba", "bba", "bba", "abb"]}, "output": [1, 2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "668d067597923228"}
vcr_0fecdcc8c0f7
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [32, -3, 22]}, "output": [32, 29, 51]}, {"input": {"nums": [16, 18, -11, 10]}, "output": [16, 34, 23, 33]}, {"input": {"nums": [2, -3, 5, -10, -10, 8, -5, 8, 16, -9]}, "output": [2, -1, 4, -6, -16, -8, -13, -5, 11, 2]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "507ccfb7609450f8"}
vcr_b368bfa12697
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-11, -9, -2, 14, 48], "target": 46}, "output": [2, 4]}, {"input": {"nums": [5, 24, 29, 31], "target": 60}, "output": [2, 3]}, {"input": {"nums": [-15, -12, 9, 21, 23, 26], "target": -3}, "output": [1, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "23a08caa7ee50ac6"}
vcr_706bc5aaebaf
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [7, 23, 23, 52, 39, 7, 39]}, "output": 52}, {"input": {"nums": [17, 28, 26, 28, 17, 13, 44, 13, 26]}, "output": 44}, {"input": {"nums": [3, 27, 7, 7, 27, 3, 41]}, "output": 41}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "74025ea1f98e714f"}
vcr_3ce4611cfa5e
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [25, 15, 20, 9, 27, 2, 19, 17]}, "output": 91}, {"input": {"nums": [8, 14, 13]}, "output": 21}, {"input": {"nums": [8, 18, 1]}, "output": 18}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "b963291de332cf10"}
vcr_4baace62eea2
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "bcaaabbacb"}, "output": 7}, {"input": {"s": "acaccbcaa"}, "output": 7}, {"input": {"s": "cabbcc"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "0069927e19842f18"}
vcr_04cc083de21a
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [22, 145, 34, 57, 156, 120, 172, 94, 132, 51]}, "output": false}, {"input": {"nums": [9, 21, 13, 10, 24, 25, 19, 4, 14, 3, 17]}, "output": false}, {"input": {"nums": [11, 7, 15, 6, 12, 13, 1]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "2fe6b0efdb846194"}
vcr_862aa91f36cb
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [24, 6, 21, 9, 12, 8, 15, 22, 17, 20]}, "output": 99}, {"input": {"nums": [20, 2, 13, 7, 17, 19, 11]}, "output": 61}, {"input": {"nums": [6, 11, 5, 4, 14, 20, 20]}, "output": 45}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "a4503266169bc125"}
vcr_2df6bba24613
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "baadd aacc"}, "output": "aacc baadd"}, {"input": {"s": "bbb b"}, "output": "b bbb"}, {"input": {"s": "aabb ba"}, "output": "ba aabb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "d0ff8bcc7f405850"}
vcr_252bdb56bb6a
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 1, 1, 2, 1, 0, 1, 4, 0]}, "output": false}, {"input": {"nums": [0, 3, 1, 0, 0, 2, 2, 3]}, "output": false}, {"input": {"nums": [3, 0, 3, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "563ea777d4fdcf42"}
vcr_e73d66faf557
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [1, -5, -8, 12, 6, 7, 9, 20], "k": 9}, "output": [20, 1, -5, -8, 12, 6, 7, 9]}, {"input": {"nums": [1, -9, -6, 5, -4, -9], "k": 19}, "output": [-9, 1, -9, -6, 5, -4]}, {"input": {"nums": [6, -9, 4, 8, 9, 6, -9], "k": 3}, "output": [9, 6, -9, 6, -9, 4, 8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "83f42b48981d6a83"}
vcr_3e620863ac38
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-13, 22, 14, -17, 3]}, "output": 36}, {"input": {"nums": [9, -3, 3, -7]}, "output": 9}, {"input": {"nums": [1, 12, -6, 9, 4, -4, -4, 3]}, "output": 20}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "bf5c1063b39325ba"}
vcr_323d1b3c936a
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["caca", "bcb", "acbc", "bb", "bbcb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["abb", "bba", "bbb", "bbb", "bab", "aaa", "baa"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["bba", "abb", "bba", "aba"]}, "output": [1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "1834674dc1e09924"}
vcr_3638f147d966
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [41, 7, 19, 34, 38, 46, 20, 37, 16, 13, 37, 16, 1, 6, 32, 10]}, "output": true}, {"input": {"nums": [20, 6, 12, 1, 22, 14, 7, 16, 12, 15, 10]}, "output": true}, {"input": {"nums": [18, 18, 22, 10, 15, 12, 15, 5, 0, 3, 17, 11]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "883a8bc4bf6b22a0"}
vcr_e783b747db76
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [6, 7, 10, 11, 21, 44], "target": 5}, "output": 0}, {"input": {"nums": [8, 10, 14], "target": 4}, "output": 0}, {"input": {"nums": [4, 5, 9, 10, 13, 18, 22, 25, 27, 28, 33], "target": 37}, "output": 11}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "af0c898d954259fe"}
vcr_c49d73a2ee8d
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [33, -13, -24, 35, -9, -8]}, "output": [33, 20, -4, 31, 22, 14]}, {"input": {"nums": [10, 3, 12, 5, -5, -9, 10, 0, 5]}, "output": [10, 13, 25, 30, 25, 16, 26, 26, 31]}, {"input": {"nums": [16, -9, -2, 8, 7]}, "output": [16, 7, 5, 13, 20]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "8e27b2b79d845c19"}
vcr_23d1e75d5760
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [13, 0, 1, -3, -7, -2, -19], "k": 7}, "output": [13, 0, 1, -3, -7, -2, -19]}, {"input": {"nums": [-6, 6, -9, -2], "k": 14}, "output": [-9, -2, -6, 6]}, {"input": {"nums": [10, -7, 7, 3, 6], "k": 13}, "output": [7, 3, 6, 10, -7]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "0ceca75028259035"}
vcr_82ef8ec68c01
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 6, 2, 3, 0, 3, 4, 5]}, "output": [0, 0, 0, 0, 6480, 0, 0, 0]}, {"input": {"nums": [1, 3, 2, 4, 1, 4, 3]}, "output": [288, 96, 144, 72, 288, 72, 96]}, {"input": {"nums": [5, 1, 1, 2, 1, 1]}, "output": [2, 10, 10, 5, 10, 10]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "729d1a383828fc99"}
vcr_d358ab98647d
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [0, 7, 4, 4, 1]}, "output": [112, 0, 0, 0, 0]}, {"input": {"nums": [5, 2, 2, 2, 2]}, "output": [16, 40, 40, 40, 40]}, {"input": {"nums": [3, 1, 2, 1, 3]}, "output": [6, 18, 9, 18, 6]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "79bf5f6c2a229d73"}
vcr_914d3fee96da
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 4, 9, 9, 10, 14, 15]}, "output": [0, 2, 4, 9, 10, 14, 15]}, {"input": {"nums": [4, 6, 7]}, "output": [4, 6, 7]}, {"input": {"nums": [0, 2, 4, 6, 7, 8, 11, 12, 12, 12]}, "output": [0, 2, 4, 6, 7, 8, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "ccbca2aa9953b159"}
vcr_0feb37ad06d3
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [8, 31, 55, 40, 23, 3, 7]}, "output": 47}, {"input": {"prices": [21, 19, 7, 2, 11, 40, 15, 22, 26, 12, 7]}, "output": 38}, {"input": {"prices": [3, 26, 25, 30, 5, 27, 2, 28]}, "output": 27}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "c79bfe03d00371aa"}
vcr_23ce159135c5
easy
bit
Return the number of set bits in non-negative integer n (Hamming weight).
def hamming_weight(n: int) -> int:
Count the least bit with n & 1, then shift right until n is 0.
def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c
[{"input": {"n": 35580}, "output": 9}, {"input": {"n": 998}, "output": 7}, {"input": {"n": 12860}, "output": 7}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "4c5304677b04b0e7"}
vcr_2d6348b1c036
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [29, 15, 39, 11, 54, 29, 16, 39, 16, 11, 15]}, "output": 54}, {"input": {"nums": [53, 8, 8, 25, 25, 27, 27]}, "output": 53}, {"input": {"nums": [3, 19, 17, 6, 9, 28, 9, 28, 17, 19, 3, 51, 6]}, "output": 51}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "af09e5d50b5a7d0c"}
vcr_33fe88296e55
easy
bit
Return the number of set bits in non-negative integer n (Hamming weight).
def hamming_weight(n: int) -> int:
Count the least bit with n & 1, then shift right until n is 0.
def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c
[{"input": {"n": 52619}, "output": 9}, {"input": {"n": 5615}, "output": 10}, {"input": {"n": 7313}, "output": 6}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "eea9dcb729390d9f"}
vcr_4d5e48733886
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-16, -14, -13, -9, 8, 10, 25, 47], "target": 12}, "output": [2, 6]}, {"input": {"nums": [-5, -4, 4, 9, 13, 27, 34], "target": 31}, "output": [2, 5]}, {"input": {"nums": [4, 20, 21, 33], "target": 41}, "output": [1, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "e033e4b378e6f525"}
vcr_43a55eb80f1e
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [20, 9, 20, 40, 22, 32, 46, 11, 40]}, "output": true}, {"input": {"nums": [14, 11, 6, 0]}, "output": false}, {"input": {"nums": [23, 2, 7, 17, 11, 0, 8, 1, 22]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "74ba273c9eef7cd1"}
vcr_4d0d3f6b3015
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "caeac abaa ebbc"}, "output": "ebbc abaa caeac"}, {"input": {"s": "aaa a b aba"}, "output": "aba b a aaa"}, {"input": {"s": "b ab bba b aaba"}, "output": "aaba b bba ab b"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "70471a8a99c6a1d6"}
vcr_ca494d31093d
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -11, -7, -5, 30, 33], "target": 23}, "output": [2, 4]}, {"input": {"nums": [-12, -7, 0, 8, 9, 18, 22], "target": -7}, "output": [1, 2]}, {"input": {"nums": [-17, -16, -7, -3, 4, 6, 15, 19, 23, 36, 38], "target": 12}, "output": [2, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "6e8762909cf78ae4"}
vcr_27d1382e79b2
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [0, 28, -17, 12, 9, -5, -20]}, "output": [0, 28, 11, 23, 32, 27, 7]}, {"input": {"nums": [4, 2, 8, -11, 7, 13, -3]}, "output": [4, 6, 14, 3, 10, 23, 20]}, {"input": {"nums": [8, 17]}, "output": [8, 25]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "a0e6105616ea86fb"}
vcr_5f556e2a63eb
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["cbcc", "ccc", "cb", "cbba", "baba", "bac", "ab", "cacc", "cb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bbb", "baa", "bba", "bba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bab", "baa", "aab", "aba", "aba", "bbb"]}, "output": [1, 1, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "d3d0de8d1f3f6b4d"}
vcr_cb76e727e935
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 242, "b": 74}, "output": 2}, {"input": {"a": 29, "b": 92}, "output": 1}, {"input": {"a": 53, "b": 161}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "68f634e44a08f987"}
vcr_25ad82016e82
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 3, 4, 7, 6, 5, 4]}, "output": [10080, 3360, 2520, 1440, 1680, 2016, 2520]}, {"input": {"nums": [4, 5, 3, 4, 2, 3, 5]}, "output": [1800, 1440, 2400, 1800, 3600, 2400, 1440]}, {"input": {"nums": [3, 4, 5, 4, 3, 3, 5]}, "output": [3600, 2700, 2160, 2700, 3600, 3600, 2160]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "daa6151eb70eb030"}
vcr_a1eb7c9254e7
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [44, 34, 10, 36, 44, 37, 58, 28, 56, 9, 41, 38, 27]}, "output": 48}, {"input": {"prices": [2, 11, 28, 19, 21, 20, 14, 31, 34, 27, 21, 14]}, "output": 32}, {"input": {"prices": [38, 29, 25, 8, 10]}, "output": 2}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "212fb271f349e33d"}
vcr_3b628f2cd7d2
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [37, 54, 14, 22, 61, 40, -39, -11, 18, -14, -13, 74, 73], "target": -52}, "output": [6, 10]}, {"input": {"nums": [10, 31, 29, 23], "target": 54}, "output": [1, 3]}, {"input": {"nums": [11, 24, 2, 39], "target": 50}, "output": [0, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "c3a3d7da9b38ec59"}
vcr_26f7d09dd02d
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [56, 45, 46, 18, 19, 25, 15, 54, 13, 49]}, "output": 39}, {"input": {"prices": [17, 12, 2, 9, 30, 7, 10, 18, 4, 20, 24, 38]}, "output": 36}, {"input": {"prices": [16, 11, 19, 13]}, "output": 8}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "3a8557423de77364"}
vcr_2877ad678d7c
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [33, 32, -7, 1, 23, 14, -15]}, "output": [33, 65, 58, 59, 82, 96, 81]}, {"input": {"nums": [3, 6, -6, 15, -11, -2, 17]}, "output": [3, 9, 3, 18, 7, 5, 22]}, {"input": {"nums": [4, 18, -10, 8, -12]}, "output": [4, 22, 12, 20, 8]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "00e378226e6db44d"}
vcr_d9ecf5d0ec15
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-15, 15, 3, -10, 15, 11, 4, -17, -11, -4], "k": 26}, "output": [15, 11, 4, -17, -11, -4, -15, 15, 3, -10]}, {"input": {"nums": [10, -9, 9, -8, -7], "k": 16}, "output": [-7, 10, -9, 9, -8]}, {"input": {"nums": [-2, -10, 1, 1, 4], "k": 20}, "output": [-2, -10, 1, 1, 4]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "1a2d99f3e2cafd4d"}
vcr_05c19f948e35
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [22, 15, 16, -13, -10, 14, -6, 15, 14, -2, 13, 16]}, "output": 94}, {"input": {"nums": [-3, 10, 11, 3, 4, -8, -4, 0, -7, -2]}, "output": 28}, {"input": {"nums": [11, -6, 11, 13, -6, 13, 14, 8, -5, -2, -11]}, "output": 58}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "6b7566f7fd643af4"}
vcr_e34d23a97eba
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "babbdbdbbab"}, "output": true}, {"input": {"s": "ababbbb"}, "output": false}, {"input": {"s": "caabbba"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "91c90a3a95d5eb9f"}
vcr_9c39edb9e762
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [60, 16, 16, 38, 31, 38, 31, 14, 14]}, "output": 60}, {"input": {"nums": [13, 24, 22, 11, 24, 22, 29, 52, 11, 13, 9, 29, 9]}, "output": 52}, {"input": {"nums": [20, 4, 4, 11, 14, 11, 14, 20, 47]}, "output": 47}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "17f610d91cb5af6c"}