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_d027fe786ecf
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": [14, 15, 17, 32, 45], "target": 25}, "output": 3}, {"input": {"nums": [0, 14, 19, 23, 31, 37, 38], "target": 20}, "output": 3}, {"input": {"nums": [1, 4, 9, 10, 16, 20, 21, 26, 30, 35], "target": 33}, "output": 9}]
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": "d3d92126477f81d8"}
vcr_8da4ecf54965
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": ["ba", "baba", "bba", "ac", "aab", "ccc", "bbbb"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["baa", "bba", "aab", "baa", "aba"]}, "output": [1, 4]}, {"input": {"strs": ["aab", "aab", "baa", "aba", "bba", "abb"]}, "output": [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": "7aa5d50897e0c6ee"}
vcr_1bf0d5e0772e
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": [-23, 58, 39, 40, 44, 4, -26, 52, 73, 64, -3, -40, 74, -5], "target": 32}, "output": [1, 6]}, {"input": {"nums": [-2, 14, 19, 30, -17, -18, 48, 0, -19, 46], "target": 0}, "output": [2, 8]}, {"input": {"nums": [46, 3, -15, 41, -7, -12, -20, -4, 19, -9], "target": -16}, "output": [5, 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": "82bb549f04a295e4"}
vcr_9fbbe2c20c39
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": [17, 3, 9, -6, 7, -3, 12, -3], "k": 22}, "output": [9, -6, 7, -3, 12, -3, 17, 3]}, {"input": {"nums": [-1, -8, 10, -8, -2, -8, -4, -8, 10], "k": 11}, "output": [-8, 10, -1, -8, 10, -8, -2, -8, -4]}, {"input": {"nums": [-10, 4, 2, -1, -5], "k": 10}, "output": [-10, 4, 2, -1, -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": "b38fee7cf8c0bda6"}
vcr_83c06a58cf17
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": [17, -20, 3, -10, -7, 2, -10, -7, -6], "k": 4}, "output": [2, -10, -7, -6, 17, -20, 3, -10, -7]}, {"input": {"nums": [-9, 7, -4, -1, -9, -7, -10], "k": 7}, "output": [-9, 7, -4, -1, -9, -7, -10]}, {"input": {"nums": [-9, 10, -7, -4, 9, -7, 8, -6, -5, -2], "k": 7}, "output": [-4, 9, -7, 8, -6, -5, -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": "0af1bb700d70a0a3"}
vcr_aa4f653ae100
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, 5, 2, 7, 7]}, "output": [1470, 980, 588, 1470, 420, 420]}, {"input": {"nums": [4, 1, 3, 3, 1]}, "output": [9, 36, 12, 12, 36]}, {"input": {"nums": [4, 3, 3, 4, 5, 3]}, "output": [540, 720, 720, 540, 432, 720]}]
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": "f0c97c7ff857ec10"}
vcr_eb1eca6fdee8
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": "bbcabbab"}, "output": 6}, {"input": {"s": "abccbcb"}, "output": 6}, {"input": {"s": "bcaacabcba"}, "output": 9}]
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": "c060bf5eabaa9fc7"}
vcr_96f86b00952d
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": "cea cdbce ebccc"}, "output": "ebccc cdbce cea"}, {"input": {"s": "aab bba"}, "output": "bba aab"}, {"input": {"s": "aa baa"}, "output": "baa aa"}]
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": "be0926dc9a849453"}
vcr_ed55cf3a12ad
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": [-3, -1, 18, 12, 19, 18, -18, -14]}, "output": 67}, {"input": {"nums": [-6, -10, 2, -6, 1]}, "output": 2}, {"input": {"nums": [5, -6, -9, -11, 5, 3, -6, -2, -8, -6]}, "output": 8}]
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": "dff848bbda20de4f"}
vcr_69f3751479f4
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": [40, 60, 35, 46, 14, 52, 50, 28, 18, 52]}, "output": 38}, {"input": {"prices": [7, 2, 26, 25, 22, 22, 4, 30, 14, 30, 2]}, "output": 28}, {"input": {"prices": [10, 25, 33, 18, 8, 5, 29]}, "output": 24}]
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": "211835313bd8f8f1"}
vcr_0049d549bc0d
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": ["ab", "ab", "bbbb", "bca", "ba", "ac"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["bab", "aaa", "bbb", "aaa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aaa", "bab", "abb"]}, "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": "4140705cce63fc66"}
vcr_b8197c98aacc
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": [32, 17, 28, 12, 28, 69, 17, 20, 12, 20, 32, 13, 13]}, "output": 69}, {"input": {"nums": [8, 50, 20, 3, 3, 8, 20]}, "output": 50}, {"input": {"nums": [3, 20, 21, 25, 8, 20, 3, 25, 54, 21, 8]}, "output": 54}]
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": "7af4aea8617d9aa5"}
vcr_e7bcdb99ba9c
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": [38, 48, 6, 8, 38]}, "output": true}, {"input": {"nums": [18, 24, 19, 4]}, "output": false}, {"input": {"nums": [23, 19, 5, 4, 2, 23, 14]}, "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": "b9d079a59d54fb53"}
vcr_7b5765f36bd1
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": [67, 49, -28, 76, 69, 24, 28, 12, 3, 23, 71], "target": 52}, "output": [5, 6]}, {"input": {"nums": [33, -4, 44, 0, 35, 45, -20, -25, -28, 19, -21], "target": 15}, "output": [4, 6]}, {"input": {"nums": [13, 11, -26, 18, -19, 4], "target": -45}, "output": [2, 4]}]
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": "ebfdce60cfc109b8"}
vcr_ec3266b8cd89
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, -22, -9, -6, 6, 10, 31, 35], "target": -16}, "output": [1, 4]}, {"input": {"nums": [-8, 0, 1, 12, 19, 23, 37], "target": -7}, "output": [0, 2]}, {"input": {"nums": [0, 1, 6, 11, 19, 20, 29, 30], "target": 39}, "output": [4, 5]}]
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": "10a5ea4054bedd08"}
vcr_d3968ed90399
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": [-15, -11, 2, 14, 25, 44, 47], "target": 10}, "output": [0, 4]}, {"input": {"nums": [-10, -5, 0, 3, 7, 11, 20], "target": 14}, "output": [3, 5]}, {"input": {"nums": [-3, 5, 7, 29], "target": 34}, "output": [1, 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": "5a38dbfa70ae894a"}
vcr_d2a88053229e
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, 4, 9, 9, 10, 13, 14, 15]}, "output": [1, 4, 9, 10, 13, 14, 15]}, {"input": {"nums": [0, 1, 3, 3, 3, 4, 5, 10, 12]}, "output": [0, 1, 3, 4, 5, 10, 12]}, {"input": {"nums": [0, 0, 0, 4, 6, 9, 9, 11, 12, 12]}, "output": [0, 4, 6, 9, 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": "9f3c672dd970fa12"}
vcr_dad8bdffd024
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": [12, 0, -3, -6, -8]}, "output": 12}, {"input": {"nums": [12, 13, 6, -9, 8, 14]}, "output": 44}, {"input": {"nums": [-9, 9, 13, -8, 8, 11, -6, -6]}, "output": 33}]
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": "32f38f6c04afa3de"}
vcr_ab46f2b9a1a6
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": ["ccba", "bc", "aaac", "aacb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bbb", "aaa", "bba", "aba", "baa", "aab"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["baa", "aba", "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": "6e286ab50156d792"}
vcr_51f935d29773
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": 51634}, "output": 8}, {"input": {"n": 5883}, "output": 10}, {"input": {"n": 10686}, "output": 9}]
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": "aa82791b33bcf31b"}
vcr_aa724733806e
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": "acbbbcaac"}, "output": 6}, {"input": {"s": "cbccbbcba"}, "output": 7}, {"input": {"s": "accacccca"}, "output": 5}]
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": "9d8243ffd27cfd41"}
vcr_b2cff7eaab08
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": [4, 11, 22, 23, 25, 34, 39, 48], "target": 33}, "output": 5}, {"input": {"nums": [3, 4, 9, 22, 37], "target": 20}, "output": 3}, {"input": {"nums": [0, 3, 5, 16, 26, 31, 35], "target": 8}, "output": 3}]
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": "110b24d1f0957650"}
vcr_9b275cdc220b
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": [14, 26, 5, 37, 37, 17, 13, 14, 26, 13, 5, 17, 25, 25, 54]}, "output": 54}, {"input": {"nums": [16, 25, 25, 27, 27, 16, 28, 28, 7, 51, 7]}, "output": 51}, {"input": {"nums": [1, 48, 1, 29, 29]}, "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": "d1f1ae8487e101b8"}
vcr_83b827dc19ce
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, 2, 2, 0, 0, 4, 2, 0]}, "output": false}, {"input": {"nums": [1, 0, 1, 0]}, "output": false}, {"input": {"nums": [2, 3, 0, 1, 0, 2, 0, 3]}, "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": "688eb3a31f6d32c2"}
vcr_8a81b73acde9
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": [12, 13, 19, 21, 24, 29, 34, 35], "target": 34}, "output": 6}, {"input": {"nums": [0, 1, 2, 13, 15, 16, 19, 26, 30, 34, 37], "target": 1}, "output": 1}, {"input": {"nums": [0, 1, 2, 3, 9, 15, 20, 29, 31, 37], "target": 5}, "output": 4}]
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": "2d227ea417c6927b"}
vcr_06dd7f3ee325
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": 58525}, "output": 9}, {"input": {"n": 1628}, "output": 6}, {"input": {"n": 15402}, "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": "7d80178d75545c45"}
vcr_8b7dc05d8257
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": "bc cadae ed ccda dc e"}, "output": "e dc ccda ed cadae bc"}, {"input": {"s": "a aabb ba aab a"}, "output": "a aab ba aabb a"}, {"input": {"s": "a a aa"}, "output": "aa a a"}]
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": "bb4a58e1031edba1"}
vcr_60a91cc1f5d3
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": [109, 89, 196, 145, 48, 75, 90, 125, 52, 195, 49]}, "output": false}, {"input": {"nums": [2, 23, 19, 10]}, "output": false}, {"input": {"nums": [1, 1, 1, 5, 2]}, "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": "87521647ca555a8f"}
vcr_d471913f3f82
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": [73, 26, 103, 157, 196, 122, 29, 67, 99, 133, 197, 168, 136, 177, 49, 171]}, "output": false}, {"input": {"nums": [11, 24, 15, 15]}, "output": true}, {"input": {"nums": [23, 10, 5, 5, 22]}, "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": "11b47bc539b79286"}
vcr_ea02bbbdd190
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, 4, 6, 7, 10, 11, 12, 12]}, "output": [0, 2, 4, 6, 7, 10, 11, 12]}, {"input": {"nums": [2, 3, 5, 5, 6, 8, 9, 12]}, "output": [2, 3, 5, 6, 8, 9, 12]}, {"input": {"nums": [1, 1, 2, 2, 4, 6, 8, 8, 8]}, "output": [1, 2, 4, 6, 8]}]
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": "9bdc2933a00a63e8"}
vcr_937b86748516
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": [12, 2, 30, 4, 20, 11, 19, 12]}, "output": true}, {"input": {"nums": [20, 4, 6, 8, 9, 3, 14, 21]}, "output": false}, {"input": {"nums": [16, 21, 5, 10, 15, 16, 18, 3, 7, 11, 4, 19]}, "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": "bef528d1c15bb46a"}
vcr_e47a2e0843f3
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": [4, 5, 9, 10, 25, 31, 32, 33, 35, 36, 39, 40, 46], "target": 33}, "output": 7}, {"input": {"nums": [1, 5, 32], "target": 40}, "output": 3}, {"input": {"nums": [1, 12, 13, 22, 25, 36, 37], "target": 34}, "output": 5}]
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": "1a50cfc4b7a8b1eb"}
vcr_cadf80d4f232
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": [7, -33, 62, -15, -16, -5, 34], "target": 18}, "output": [4, 6]}, {"input": {"nums": [-23, 26, -12, -8, -16, -14, 21, -22, -18], "target": 4}, "output": [1, 7]}, {"input": {"nums": [-22, 43, 0, 20, -29, 35, -14, 41, 21], "target": 20}, "output": [2, 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": "d1744dd5de6b1a91"}
vcr_0a041af1e729
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": [35, 0, 50, 5, 37, 31, 16, 20, 47, 13, 39, 33, 10, 26, 35]}, "output": true}, {"input": {"nums": [11, 23, 21, 5, 23, 15, 3, 17, 25]}, "output": true}, {"input": {"nums": [9, 11, 23, 25, 19, 14, 6, 3, 4]}, "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": "938be509180f736c"}
vcr_60b87043be4a
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, 1, 4, 5, 5, 2]}, "output": [400, 1600, 800, 1600, 400, 320, 320, 800]}, {"input": {"nums": [3, 5, 3]}, "output": [15, 9, 15]}, {"input": {"nums": [4, 4, 2, 5]}, "output": [40, 40, 80, 32]}]
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": "118df4bbd372af67"}
vcr_a5d43a412d89
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": "c ddcac ce cb ec dd"}, "output": "dd ec cb ce ddcac c"}, {"input": {"s": "b ba"}, "output": "ba b"}, {"input": {"s": "a aa ab b baaa"}, "output": "baaa b ab aa a"}]
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": "2e226cd467bfa7d5"}
vcr_aacf2d94eb2b
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": [0, 0, 3, 0, 1, 2, 0]}, "output": false}, {"input": {"nums": [0, 3, 0, 3, 1, 0, 1, 1, 1]}, "output": false}, {"input": {"nums": [1, 2, 1, 1, 2, 2]}, "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": "5812e40d3bf449d6"}
vcr_d472d63f5ce2
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": [-16, 13, -19, -13, -15], "k": 5}, "output": [-16, 13, -19, -13, -15]}, {"input": {"nums": [0, -4, -2, -8], "k": 14}, "output": [-2, -8, 0, -4]}, {"input": {"nums": [3, -4, 10, -10, 1, 1, -4], "k": 0}, "output": [3, -4, 10, -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": "faf276c775d400d5"}
vcr_8b76ed761bd4
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": [8, 3, -5, -15], "k": 5}, "output": [-15, 8, 3, -5]}, {"input": {"nums": [2, -3, 1, -10, 1, 0, 6], "k": 9}, "output": [0, 6, 2, -3, 1, -10, 1]}, {"input": {"nums": [-8, -10, 1, -9, 5, -9, 1, -3, -9, -1], "k": 18}, "output": [1, -9, 5, -9, 1, -3, -9, -1, -8, -10]}]
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": "b18a8f3c727a50c4"}
vcr_190a43d4d672
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": [-21, -12, -10, -4, -1, 9, 20, 46], "target": -16}, "output": [1, 3]}, {"input": {"nums": [-17, -6, -4, -3, 2, 7, 15, 17], "target": 4}, "output": [3, 5]}, {"input": {"nums": [-6, -4, -2, -1, 0, 18, 36], "target": -7}, "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": "b3d6673715cd9168"}
vcr_30bd767b5947
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": "bcbbacaba"}, "output": 8}, {"input": {"s": "acccbcc"}, "output": 4}, {"input": {"s": "aabaccaccca"}, "output": 7}]
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": "42d7a50014b26372"}
vcr_c96c34315e75
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, -11, 20, 7], "k": 0}, "output": [-13, -11, 20, 7]}, {"input": {"nums": [0, 9, -10, -8, -10, 9, 7], "k": 12}, "output": [-10, -8, -10, 9, 7, 0, 9]}, {"input": {"nums": [8, 4, -1, -5], "k": 8}, "output": [8, 4, -1, -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": "1809de8506871ebb"}
vcr_3a9b5e076ed5
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": [3, 22, 46, 145, 196, 15]}, "output": false}, {"input": {"nums": [23, 5, 6, 14, 17, 6, 7, 9, 7, 17, 7]}, "output": true}, {"input": {"nums": [18, 11, 14, 4, 11, 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": "eb574dceae489c56"}
vcr_8435b19339a1
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, 2, 7, 3, 5, 2, 3, 4]}, "output": [5040, 2520, 720, 1680, 1008, 2520, 1680, 1260]}, {"input": {"nums": [5, 1, 2, 1]}, "output": [2, 10, 5, 10]}, {"input": {"nums": [4, 3, 5, 2, 1]}, "output": [30, 40, 24, 60, 120]}]
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": "fc0d3be8bd898dd6"}
vcr_d4faf63f858e
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": "deeb bdddc edebcb"}, "output": "edebcb bdddc deeb"}, {"input": {"s": "bbb a bbba"}, "output": "bbba a bbb"}, {"input": {"s": "ba abaa aaaa aab bbba"}, "output": "bbba aab aaaa abaa ba"}]
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": "36e47ab9fb47331c"}
vcr_d4abede8d343
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": "aaadabadaaa"}, "output": true}, {"input": {"s": "cbbbacabcc"}, "output": false}, {"input": {"s": "bbbbabbbbc"}, "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": "8687d2e6bd608fb6"}
vcr_f0a4cd8c3328
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": 132, "b": 189}, "output": 3}, {"input": {"a": 22, "b": 144}, "output": 2}, {"input": {"a": 56, "b": 65}, "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": "9702e3797d038c84"}
vcr_28dec7b59ddd
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": [17, -2, 16, -14, 2, 13, 14, -20, -5], "k": 12}, "output": [14, -20, -5, 17, -2, 16, -14, 2, 13]}, {"input": {"nums": [9, 1, 2, -10, 8], "k": 1}, "output": [8, 9, 1, 2, -10]}, {"input": {"nums": [10, -1, 2, 2, 9, -9], "k": 4}, "output": [2, 2, 9, -9, 10, -1]}]
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": "37daf1058a44ad0c"}
vcr_75c4e8c22bfb
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": [17, 8, 21, 10, 15, 23, 28, 8]}, "output": 81}, {"input": {"nums": [12, 7, 5]}, "output": 17}, {"input": {"nums": [3, 12, 8, 3]}, "output": 15}]
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": "73d3b190fa298327"}
vcr_e3fb4376cbf9
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": [137, 157, 105, 188, 152, 80, 182, 198, 170, 96, 14, 49, 134]}, "output": false}, {"input": {"nums": [20, 0, 14, 4, 11, 7, 11, 12, 5, 16, 5]}, "output": true}, {"input": {"nums": [16, 23, 0, 7, 12, 20, 2, 22, 19, 15, 17]}, "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": "e533c87dd458798d"}
vcr_4fd87ad92630
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": [29, -25, 9, 0, 32, -4, -4, 10, -3]}, "output": [29, 4, 13, 13, 45, 41, 37, 47, 44]}, {"input": {"nums": [14, 12]}, "output": [14, 26]}, {"input": {"nums": [14, -9]}, "output": [14, 5]}]
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": "73349e558a6a40b3"}
vcr_787e39486951
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": [2, 9, 14, 21, 23, 31, 33, 40, 41, 45, 47], "target": 27}, "output": 5}, {"input": {"nums": [5, 7, 12, 14, 19, 21, 32, 39], "target": 19}, "output": 4}, {"input": {"nums": [1, 4, 9, 14, 23, 28, 33, 38], "target": 38}, "output": 7}]
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": "9664f57a83679b5c"}
vcr_aabdcf4b741d
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": 10400}, "output": 4}, {"input": {"n": 8405}, "output": 6}, {"input": {"n": 16114}, "output": 10}]
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": "b5b1fa74daf9d58b"}
vcr_931ed27ed6b1
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": 59246}, "output": 11}, {"input": {"n": 12146}, "output": 9}, {"input": {"n": 13042}, "output": 8}]
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": "3530afc720f64f00"}
vcr_fa4b2ec036fc
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": [-36, 44, 27, -33, -14, 34, 75, 15, -29, 32, 38], "target": -9}, "output": [0, 2]}, {"input": {"nums": [-7, 24, -5, -11, 2, 27, 21, -21], "target": -26}, "output": [2, 7]}, {"input": {"nums": [12, -18, 37, -14, -28, -12, -1, -26, 40, 42], "target": 14}, "output": [7, 8]}]
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": "6e3e77a83266ff08"}
vcr_3d2205a49a3e
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": [7, 26, 2, 25, 13, 6, 13, 22, 26, 15, 28, 30]}, "output": 124}, {"input": {"nums": [12, 20, 13, 19, 5, 4, 10, 0, 18]}, "output": 67}, {"input": {"nums": [17, 13, 7, 7, 10, 13]}, "output": 37}]
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": "70d2b0f43d350a00"}
vcr_7f52055a963f
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, 47, 23, 48, 34, 28, 34, 16, 25, 1, 41, 12, 36, 17]}, "output": true}, {"input": {"nums": [18, 3, 21, 6, 17, 10, 0, 13, 7, 9]}, "output": false}, {"input": {"nums": [14, 22, 18, 3, 11]}, "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": "f2c10438103e77a2"}
vcr_666b3a28ea62
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, 2, 3, 3, 4, 4, 6, 8, 11, 13, 13, 14]}, "output": [1, 2, 3, 4, 6, 8, 11, 13, 14]}, {"input": {"nums": [4, 5, 12, 12]}, "output": [4, 5, 12]}, {"input": {"nums": [1, 2, 5, 7, 8, 8, 10, 10, 12]}, "output": [1, 2, 5, 7, 8, 10, 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": "0644fcff6d2c608c"}
vcr_4e33c8ebd79e
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": [11, 19, 21, 27, 36, 39, 46], "target": 16}, "output": 1}, {"input": {"nums": [0, 2, 18, 24, 25, 26, 33], "target": 17}, "output": 2}, {"input": {"nums": [3, 7, 18, 32, 36, 39], "target": 0}, "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": "d5378f26eb0ccf21"}
vcr_5a63af9538af
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, -17, -8, 10, 20, 25, 33, 34, 45, 47, 48], "target": 26}, "output": [2, 7]}, {"input": {"nums": [-20, -14, -6, -4, 10, 13, 15, 28, 35, 38], "target": 29}, "output": [2, 8]}, {"input": {"nums": [-17, -9, 6, 12, 19, 23, 24, 33], "target": 24}, "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": "92990d7c6c237f7b"}
vcr_7bf0fe1ce2f6
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": [12, -6, 19, 22, -3, 2, 6]}, "output": 52}, {"input": {"nums": [14, 0, 9, 7, -2, -12]}, "output": 30}, {"input": {"nums": [-4, 2, -11, 6, 13, -11, -12, 5, -5, 13]}, "output": 19}]
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": "ad3f71ee6be25b41"}
vcr_2d8c4d7bd265
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, 5, 14, 22, 29, 39, 40, 45], "target": 2}, "output": 0}, {"input": {"nums": [11, 14, 15, 21, 23, 25, 36, 38], "target": 33}, "output": 6}, {"input": {"nums": [2, 4, 5, 13, 15, 20, 23, 30, 35, 38, 39], "target": 34}, "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": "443d7c86fe2789a5"}
vcr_89e2f7277907
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": [-17, 0, -2, 4, 8, 12, -15, 7, 22, 19, -17, 2]}, "output": 57}, {"input": {"nums": [2, 6, -12, -2, 12]}, "output": 12}, {"input": {"nums": [-5, 1, 13, -5, -3]}, "output": 14}]
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": "123d004de64898a0"}
vcr_5913118b78fb
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, 2, 4, 6, 9, 11, 12, 13]}, "output": [1, 2, 4, 6, 9, 11, 12, 13]}, {"input": {"nums": [4, 9, 11]}, "output": [4, 9, 11]}, {"input": {"nums": [1, 2, 2, 3, 6, 9, 12, 12, 12]}, "output": [1, 2, 3, 6, 9, 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": "a48e6e7b551181b5"}
vcr_06ec46e3824a
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, 12, 19, 32, 37, 45, 47], "target": 37}, "output": 4}, {"input": {"nums": [0, 9, 11, 13, 17, 23, 25, 26, 31], "target": 31}, "output": 8}, {"input": {"nums": [1, 3, 5, 10, 15, 21, 29, 30, 32], "target": 15}, "output": 4}]
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": "034f10c3822ac31b"}
vcr_4e92ff514f60
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": [2, 12, 15, 17, 19, 25, 28, 33, 36, 39, 41, 47], "target": 32}, "output": 7}, {"input": {"nums": [2, 5, 10, 12, 14, 21, 24, 27, 29, 31, 32, 37], "target": 33}, "output": 11}, {"input": {"nums": [1, 34, 35, 38], "target": 3}, "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": "55b5f1de6e64f0ea"}
vcr_13596749cafe
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": [30, 25, 7, 7, 8, 37, 34, 1, 30, 12, 8, 34, 37, 12, 1, 66, 25]}, "output": 66}, {"input": {"nums": [55, 28, 23, 13, 28, 5, 14, 23, 5, 14, 13]}, "output": 55}, {"input": {"nums": [13, 15, 49, 5, 10, 15, 10, 13, 5]}, "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": "94aebd37a4225f1a"}
vcr_b0b17512f5b2
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": [-7, -15, -15, -18, -11, 6]}, "output": 6}, {"input": {"nums": [0, -11, -11, 6, -8, 12, -7, -3, -5]}, "output": 12}, {"input": {"nums": [6, 13, 3, 11, 8, -12, -3, -8, 13]}, "output": 41}]
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": "7074cf589725c24e"}
vcr_dbe0d6ad529d
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": 221, "b": 165}, "output": 1}, {"input": {"a": 92, "b": 123}, "output": 1}, {"input": {"a": 118, "b": 164}, "output": 2}]
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": "2e64ebf805df7a81"}
vcr_64926f639119
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": "dabc ddad de cd aacde"}, "output": "aacde cd de ddad dabc"}, {"input": {"s": "bb bab bab b"}, "output": "b bab bab bb"}, {"input": {"s": "babb b"}, "output": "b babb"}]
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": "a3758acbb9f3a410"}
vcr_8f0eb3b0771a
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": [27, 3, 3, 25, 10, 29]}, "output": 81}, {"input": {"nums": [5, 18, 2, 6, 11, 19, 12, 15]}, "output": 58}, {"input": {"nums": [12, 3, 16, 4, 4, 5, 7, 12]}, "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": "09aa596c64fc3be0"}
vcr_195e34e16578
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": [-12, -11, -11, 9, 14]}, "output": [-12, -23, -34, -25, -11]}, {"input": {"nums": [-8, 10, 14, -7, 5, -6, 12, 15, 16]}, "output": [-8, 2, 16, 9, 14, 8, 20, 35, 51]}, {"input": {"nums": [-7, 0, -3, 14, 5, -10, -10, -4, 8]}, "output": [-7, -7, -10, 4, 9, -1, -11, -15, -7]}]
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": "ef4b4945ef54bb1a"}
vcr_9ca70f04d549
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": [6, 4, 5, 3, 1, 5]}, "output": [300, 450, 360, 600, 1800, 360]}, {"input": {"nums": [3, 3, 3, 1, 1]}, "output": [9, 9, 9, 27, 27]}, {"input": {"nums": [2, 4, 2, 1, 5, 4]}, "output": [160, 80, 160, 320, 64, 80]}]
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": "3333485966107179"}
vcr_3cd2c36aaa14
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": [38, 51, 22, 21, 17, 60]}, "output": 43}, {"input": {"prices": [2, 14, 26, 7]}, "output": 24}, {"input": {"prices": [22, 25, 27, 28, 27]}, "output": 6}]
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": "07bb18b9449253e7"}
vcr_54a30c35708c
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": [56, -10, -22, 51, -21, -38, -23, 26, -13, -32, -15], "target": 18}, "output": [0, 5]}, {"input": {"nums": [28, -18, -21, -13, -29], "target": -50}, "output": [2, 4]}, {"input": {"nums": [-5, 29, 44, -13, -8, 1], "target": -18}, "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": "ba82856ddcafff1a"}
vcr_1f8da635d894
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": [26, 6, 14, 30, 16, 3, 17, 24, 15, 34, 1, 57, 25, 49, 34, 31]}, "output": 56}, {"input": {"prices": [3, 26, 31, 32, 39, 1, 1, 27, 16]}, "output": 36}, {"input": {"prices": [35, 14, 38, 20, 7, 6, 2, 35, 31]}, "output": 33}]
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": "e3c03202d0574a46"}
vcr_8a323aa3c90e
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": [38, 24, 28, 17, 17, 30, 74, 12, 12, 30, 24, 29, 29, 28, 38]}, "output": 74}, {"input": {"nums": [13, 7, 7, 1, 22, 1, 44, 13, 22]}, "output": 44}, {"input": {"nums": [26, 23, 5, 21, 21, 3, 48, 26, 6, 23, 3, 5, 6]}, "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": "85a973166168dee9"}
vcr_802174bc5f68
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": [29, 52, 60, 19, 19, 35, 19, 25, 22, 27, 33, 23, 37, 45]}, "output": 31}, {"input": {"prices": [33, 30, 38, 12, 4]}, "output": 8}, {"input": {"prices": [14, 9, 2, 19, 9, 20, 30, 23, 23]}, "output": 28}]
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": "e760200b290b1820"}
vcr_758206452cb4
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": [6, 3, 6, 5, 1, 1, 3, 1]}, "output": [270, 540, 270, 324, 1620, 1620, 540, 1620]}, {"input": {"nums": [2, 4, 1, 4, 1]}, "output": [16, 8, 32, 8, 32]}, {"input": {"nums": [5, 4, 5, 3, 3, 1, 4]}, "output": [720, 900, 720, 1200, 1200, 3600, 900]}]
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": "5152b271d6d0958f"}
vcr_cba84adde05c
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": [-15, 12, 37, 16], "target": 22}, "output": [0, 2]}, {"input": {"nums": [-9, 47, -8, -21, 5, -16, 18, 23, 38, -10, 42], "target": 30}, "output": [2, 8]}, {"input": {"nums": [3, -26, -7, -10, 43], "target": -36}, "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": "01ed580ca54ca0f0"}
vcr_fb9428b192cf
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": ["bc", "aaa", "acb", "abc", "bba", "cc", "bcab", "bb"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "bbb", "bba", "bba", "aba", "aba"]}, "output": [1, 2, 3]}, {"input": {"strs": ["baa", "aba", "aaa", "abb", "baa"]}, "output": [1, 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": "87082b7fd48affa1"}
vcr_9f77a690e6b5
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": ["bcca", "acb", "ca", "ccaa", "cbcc", "bc", "ca", "ab", "bbca"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "baa", "aba", "bab", "aaa", "bbb", "abb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["aba", "baa", "bbb"]}, "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": "dcab3a691c621a77"}
vcr_c7dcc6b0264b
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": [-20, -18, 14, -11, 3, -18, 5, -6, -20, -6, -12], "k": 18}, "output": [3, -18, 5, -6, -20, -6, -12, -20, -18, 14, -11]}, {"input": {"nums": [-2, -2, -2, -9, -6, 3, -3], "k": 5}, "output": [-2, -9, -6, 3, -3, -2, -2]}, {"input": {"nums": [1, 1, 7, 7, 7, 3, -1, 0, 3, 6], "k": 12}, "output": [3, 6, 1, ...
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": "3c382f9da01f697b"}
vcr_7c2257a53d18
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": [69, 11, 62, -32, 12, 44, -19, -33, 64], "target": 113}, "output": [0, 5]}, {"input": {"nums": [28, -5, -29, 15], "target": 43}, "output": [0, 3]}, {"input": {"nums": [39, 45, 33, 21, -20, 17, -22, 11], "target": 23}, "output": [1, 6]}]
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": "d4dca2590654e347"}
vcr_45fd3ec66020
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": [16, 17, -6, -5, 20, 12, -10, 3, -7], "k": 1}, "output": [-7, 16, 17, -6, -5, 20, 12, -10, 3]}, {"input": {"nums": [9, 5, -4, -8, -3, 1, -8, -9, 5], "k": 20}, "output": [-9, 5, 9, 5, -4, -8, -3, 1, -8]}, {"input": {"nums": [-3, 6, 5, -10, 9, 9], "k": 6}, "output": [-3, 6, 5, -10, 9, 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": "3babdb585b758ce8"}
vcr_972825a288d7
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": [2, 19, 24, 25, 31, 34, 43, 49], "target": 3}, "output": 1}, {"input": {"nums": [1, 5, 10, 15, 16, 24, 25, 26, 28, 30, 34], "target": 31}, "output": 10}, {"input": {"nums": [0, 3, 4, 11, 12, 13, 16, 27, 30, 32, 34], "target": 14}, "output": 6}]
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": "398854e28a2d21db"}
vcr_f343f6ec6525
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": [-18, 3, 32, 34, 37], "target": 19}, "output": [0, 4]}, {"input": {"nums": [-9, -7, 15, 27, 30, 31, 33, 39], "target": 22}, "output": [0, 5]}, {"input": {"nums": [-16, -1, 4, 16, 18, 23, 25, 36], "target": 40}, "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": "4051e5e94ddf9752"}
vcr_f6de000148e8
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": [163, 174, 41, 75, 17, 33, 29, 131, 133, 165, 187, 193, 19, 175, 52, 194]}, "output": false}, {"input": {"nums": [7, 13, 4]}, "output": false}, {"input": {"nums": [4, 22, 19, 0, 12, 12, 10, 8, 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": "c8cb5f51f21fdcb1"}
vcr_549e010b0eb7
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, 42, 42, 26]}, "output": 11}, {"input": {"prices": [1, 34, 28]}, "output": 33}, {"input": {"prices": [12, 32, 4, 25, 15, 40, 33, 10, 6, 14]}, "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": "ad642135f4c044e7"}
vcr_da557012150d
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": [-33, 30, 37, 52, -40, 71], "target": 89}, "output": [2, 3]}, {"input": {"nums": [-29, 37, -12, 28, -28, -6, 10, 22, 43, 44, 25], "target": -7}, "output": [0, 7]}, {"input": {"nums": [-26, 42, 30, 14, -3, -24, -7, 13, 32, 12], "target": 39}, "output": [1, 4]}]
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": "b163c21c25514b22"}
vcr_fa88a217215d
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": 22}, "output": 1}, {"input": {"a": 46, "b": 66}, "output": 2}, {"input": {"a": 162, "b": 156}, "output": 6}]
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": "df7c394ac22faaa3"}
vcr_6129afdbb550
hard
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": "bccabcdcbaccb"}, "output": true}, {"input": {"s": "baa"}, "output": false}, {"input": {"s": "cbbb"}, "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": "5999d77df9924880"}
vcr_7cdc2f3fcb73
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, 3, 8, 8, 9, 11, 12, 12, 15]}, "output": [0, 3, 8, 9, 11, 12, 15]}, {"input": {"nums": [1, 1, 4, 5, 6, 9, 10]}, "output": [1, 4, 5, 6, 9, 10]}, {"input": {"nums": [4, 4, 6, 8, 8, 9, 10]}, "output": [4, 6, 8, 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": "2ccbd98920de1a91"}
vcr_e155bd742c3b
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": 29137}, "output": 8}, {"input": {"n": 14128}, "output": 7}, {"input": {"n": 1175}, "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": "b27bff3972b143a4"}
vcr_011653e0c6bd
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": [9, 10, 11, 12, 15, 20, 21, 28, 30, 40, 42, 44], "target": 26}, "output": 7}, {"input": {"nums": [1, 2, 5, 15, 17, 18, 23, 25, 26, 31, 35, 39], "target": 16}, "output": 4}, {"input": {"nums": [8, 13, 15, 18, 22, 28, 29, 34, 36, 37], "target": 33}, "output": 7}]
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": "317d723401106bb2"}
vcr_4cc76aef64af
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": ["cacc", "aaba", "aca", "bbb", "aaca"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "aaa", "aab", "abb", "bab", "bab"]}, "output": [1, 2, 3]}, {"input": {"strs": ["baa", "aab", "abb", "bba", "abb", "bab"]}, "output": [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": "c319a4646771bb24"}
vcr_8279d6d2c102
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, -17, 2, -14, 22]}, "output": 22}, {"input": {"nums": [-1, 11, -11, -2, 6, -6, 11]}, "output": 11}, {"input": {"nums": [10, 5, 5, -4, 5, 3, 8, 14]}, "output": 46}]
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": "3673ac29ad02aa3d"}
vcr_7988c846b490
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, 3, 5, 5, 1]}, "output": [75, 100, 60, 60, 300]}, {"input": {"nums": [5, 3, 2, 4]}, "output": [24, 40, 60, 30]}, {"input": {"nums": [2, 2, 2, 4, 2, 2, 5]}, "output": [320, 320, 320, 160, 320, 320, 128]}]
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": "6a095760cc8358b0"}
vcr_aef09be6f87b
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", "bc", "ca", "aaa", "ca", "aca"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "bab", "aab", "bba", "abb", "bba", "aba"]}, "output": [3, 4]}, {"input": {"strs": ["bbb", "aaa", "aba", "baa", "bba", "bbb"]}, "output": [1, 1, 2, 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": "4f8b3ae04d2bf865"}
vcr_ed37b922782a
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": [13, -24, -19, 5, 0, 0, -15, -9, 5]}, "output": [13, -11, -30, -25, -25, -25, -40, -49, -44]}, {"input": {"nums": [-7, 4, -6, 15, 6, 8, 17, 3, -3]}, "output": [-7, -3, -9, 6, 12, 20, 37, 40, 37]}, {"input": {"nums": [8, 15, -3, -9, -11, -5]}, "output": [8, 23, 20, 11, 0, -5]}]
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": "f118702820b50feb"}