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_4a94878887f2
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": [2, 4, 6, 7, 9, 9, 13, 14, 14, 15, 15]}, "output": [2, 4, 6, 7, 9, 13, 14, 15]}, {"input": {"nums": [0, 3, 3, 5, 5, 5, 10]}, "output": [0, 3, 5, 10]}, {"input": {"nums": [3, 4, 6, 11, 11, 11]}, "output": [3, 4, 6, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "86d123734a362912"}
vcr_ef24eccb82a8
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": [11, -30, -23, 58, -13, 25, 51, -28, 3, 66, 41, -18, -16], "target": -29}, "output": [4, 12]}, {"input": {"nums": [11, 36, 6, 22, 14, -22, 23], "target": 25}, "output": [0, 4]}, {"input": {"nums": [17, 29, 19, 12], "target": 46}, "output": [0, 1]}]
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": "88cb6fdc96728b9e"}
vcr_28803b080566
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": [28, 29, 12, 12, 31, 31, 61, 7, 38, 38, 29, 7, 28]}, "output": 61}, {"input": {"nums": [21, 23, 9, 9, 50, 14, 14, 21, 3, 8, 8, 23, 3]}, "output": 50}, {"input": {"nums": [22, 10, 22, 2, 1, 1, 7, 40, 7, 10, 2]}, "output": 40}]
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": "2bc9f9a27ade2ae0"}
vcr_8f554202dfaf
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, 5, 6, 6, 6, 7, 9, 10, 10, 13, 14, 15]}, "output": [1, 4, 5, 6, 7, 9, 10, 13, 14, 15]}, {"input": {"nums": [0, 0, 1, 1, 2, 4, 6, 8, 9, 9, 11, 12]}, "output": [0, 1, 2, 4, 6, 8, 9, 11, 12]}, {"input": {"nums": [0, 2, 2, 4, 4, 5, 5, 6, 7, 7, 8, 9]}, "output": [0, 2, 4, 5, 6, 7, 8, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "f9ba4ac9443722d0"}
vcr_2196ebb35acd
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": [-30, -21, -14, -1, 9, 24, 34, 42, 43], "target": 13}, "output": [0, 8]}, {"input": {"nums": [-18, -10, 22, 35], "target": 25}, "output": [1, 3]}, {"input": {"nums": [1, 11, 16, 23], "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": "2b482fb9bf59b3e0"}
vcr_07906d4088e8
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": "bede cddadb cbbeb a bebe d cccbd"}, "output": "cccbd d bebe a cbbeb cddadb bede"}, {"input": {"s": "aa a ab aba"}, "output": "aba ab a aa"}, {"input": {"s": "b abaa b"}, "output": "b abaa b"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "3c604516850b333a"}
vcr_529ead1eadaa
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": [10, 28, 5, 5, 16, 31, 19, 28, 65, 10, 16, 36, 31, 36, 19]}, "output": 65}, {"input": {"nums": [50, 12, 11, 11, 12]}, "output": 50}, {"input": {"nums": [28, 7, 2, 5, 3, 24, 5, 28, 2, 7, 46, 24, 3]}, "output": 46}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "301ffe1190141e2a"}
vcr_0740bb6ee9ed
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": [49, 32, 35, 37, 46, 30, 21, 42, 30, 34, 49, 13, 37]}, "output": true}, {"input": {"nums": [21, 17, 4, 19, 8, 12, 0, 20, 11, 22, 17]}, "output": true}, {"input": {"nums": [21, 15, 17, 23, 5]}, "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": "05ecf0c096f5ab3f"}
vcr_cf51225e7e0a
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": [-22, -12, -7, -5, 6, 11, 14, 19, 37, 47], "target": -3}, "output": [0, 7]}, {"input": {"nums": [-13, -10, -8, -5, 7, 16, 19, 27], "target": 11}, "output": [2, 6]}, {"input": {"nums": [-15, -10, 5, 10, 14, 16, 18, 23, 29, 30, 39], "target": 47}, "output": [6, 8]}]
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": "43ac4efe694fbb03"}
vcr_f859f16a899f
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 6, 6, 0, 6]}, "output": [0, 0, 0, 648, 0]}, {"input": {"nums": [3, 1, 4, 4]}, "output": [16, 48, 12, 12]}, {"input": {"nums": [2, 1, 1, 5, 5, 3]}, "output": [75, 150, 150, 30, 30, 50]}]
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": "712c25b6c3b26b26"}
vcr_137256913d23
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 aebc bbba bedc"}, "output": "bedc bbba aebc cea"}, {"input": {"s": "ba a"}, "output": "a ba"}, {"input": {"s": "abbb a"}, "output": "a abbb"}]
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": "323d165e47889417"}
vcr_18843de2f82a
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": ["bac", "baaa", "baab", "cabb", "cbac", "aacb", "bcbb", "cb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "aba", "abb", "bbb", "baa"]}, "output": [1, 1, 3]}, {"input": {"strs": ["bab", "bbb", "bab", "baa", "aab"]}, "output": [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": "5cb0a7684206bc53"}
vcr_5fd882929f57
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, 4, 6, 5, 6, 5]}, "output": [3600, 3600, 2400, 2880, 2400, 2880]}, {"input": {"nums": [1, 1, 5, 2, 2]}, "output": [20, 20, 4, 10, 10]}, {"input": {"nums": [5, 4, 5, 2]}, "output": [40, 50, 40, 100]}]
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": "760ff1e1285fe322"}
vcr_047fa542f80c
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": ["ccab", "cab", "acbc", "cbcb", "acaa"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aba", "aab", "aba", "baa", "abb"]}, "output": [1, 4]}, {"input": {"strs": ["aab", "bab", "bbb"]}, "output": [1, 1, 1]}]
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": "e0030e9f92cf0bd4"}
vcr_e46a349b858a
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": [0, 6, 7, 8, 9, 12, 19, 36, 39, 46], "target": 33}, "output": 7}, {"input": {"nums": [6, 11, 23], "target": 21}, "output": 2}, {"input": {"nums": [0, 1, 5, 10, 15, 26, 28, 32, 33, 37], "target": 40}, "output": 10}]
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": "4ac97b2b507aa285"}
vcr_148648e222de
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": [-8, 8, 20, -18, 13, 13, 14]}, "output": 50}, {"input": {"nums": [2, 11, 4, -10, 1, 3, 8, -7]}, "output": 19}, {"input": {"nums": [5, 9, -7, 6, 10, 12, -5, 0, -7]}, "output": 35}]
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": "efb19fa0f50e6507"}
vcr_c7ac251a5b26
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, 4, 8, 21, 36], "target": 57}, "output": [3, 4]}, {"input": {"nums": [-8, -5, 8, 14, 20, 21, 27, 37], "target": 19}, "output": [0, 6]}, {"input": {"nums": [2, 4, 5, 8, 12, 32, 34], "target": 17}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "0a80cf4b2d6e0938"}
vcr_0308b48fc807
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": [-39, 58, -22, -9, 62], "target": -48}, "output": [0, 3]}, {"input": {"nums": [-24, -17, -14, 0, -7, 35, 26, -20, -21, 45], "target": 12}, "output": [2, 6]}, {"input": {"nums": [-7, -10, -8, 32, 14, 48], "target": 41}, "output": [0, 5]}]
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": "24a317ec70173106"}
vcr_ee54f9eda46d
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, 23, 37, 39, 41, 49], "target": 46}, "output": 5}, {"input": {"nums": [4, 5, 7, 13, 14, 21, 23, 25], "target": 16}, "output": 5}, {"input": {"nums": [6, 19, 20, 23, 31], "target": 22}, "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": "8733d4d0737fcd24"}
vcr_e3845b34036d
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, 3, 4, 10, 15, 17, 19, 20, 21, 32, 33, 42], "target": 27}, "output": 9}, {"input": {"nums": [1, 4, 9, 14, 18, 23, 26, 29], "target": 5}, "output": 2}, {"input": {"nums": [4, 10, 21, 23, 35, 39], "target": 24}, "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": "226db7f19a0cc73e"}
vcr_d3c7f8d4faf6
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": [18, 36, 18, 36, 39, 37, 39, 30, 26, 57, 26, 30, 37]}, "output": 57}, {"input": {"nums": [11, 4, 11, 12, 25, 25, 4, 48, 15, 12, 15]}, "output": 48}, {"input": {"nums": [29, 27, 56, 29, 27]}, "output": 56}]
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": "1edcd5b53a87faa9"}
vcr_a53169db1c60
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": [17, 14, -21, 22, -8, -14, 27, -18, 33, -12, -6, -21]}, "output": [17, 31, 10, 32, 24, 10, 37, 19, 52, 40, 34, 13]}, {"input": {"nums": [16, -5, -1, 14, -1, 17, -5, -8]}, "output": [16, 11, 10, 24, 23, 40, 35, 27]}, {"input": {"nums": [13, 18, -10, 14, 1, 8, -10]}, "output": [13, 31, 21, 35, 36, 44,...
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": "587f0fa529613f12"}
vcr_4673effb0976
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, -24, -17, 3, 28, 33, 40, 45], "target": 73}, "output": [4, 7]}, {"input": {"nums": [-17, -11, 1, 7, 15, 24, 25, 29, 32, 35, 38], "target": 33}, "output": [2, 8]}, {"input": {"nums": [-17, -11, 0, 26], "target": -28}, "output": [0, 1]}]
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": "7b72e7a4f4b5b2df"}
vcr_b1ed9c1a2401
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": [-10, 5, 16, 28, 31], "target": 6}, "output": [0, 2]}, {"input": {"nums": [-13, -4, -2, -1, 23], "target": 19}, "output": [1, 4]}, {"input": {"nums": [-9, -8, 7, 10, 11, 13, 27, 29, 36], "target": -1}, "output": [1, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "264b78b9499287ef"}
vcr_172836a0d27a
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": [0, 5, 24, 4, 21, 20, 4]}, "output": 49}, {"input": {"nums": [8, 8, 18, 20, 12, 12, 15, 17]}, "output": 57}, {"input": {"nums": [15, 8, 1, 15, 3]}, "output": 30}]
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": "9193d93f310a978d"}
vcr_123a59455364
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": 64828}, "output": 11}, {"input": {"n": 3811}, "output": 8}, {"input": {"n": 7106}, "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": "9e3cb6f0ccd6ea5e"}
vcr_c1b454841186
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": 60434}, "output": 7}, {"input": {"n": 3756}, "output": 7}, {"input": {"n": 4413}, "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": "22e14e8347dbaa54"}
vcr_dc7c6ee6fd9d
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": [0, 20, 23, 25, 22, 23, 3, 25, 16, 26]}, "output": 119}, {"input": {"nums": [1, 8, 14, 20, 20, 3]}, "output": 35}, {"input": {"nums": [18, 8, 16, 7, 3, 14]}, "output": 48}]
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": "a2fc194a65b23f6b"}
vcr_30f3275d2099
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": [-22, -18, -12, -5, 2, 8, 10, 11, 31, 32], "target": 13}, "output": [1, 8]}, {"input": {"nums": [-19, -14, -8, -5, -3, 10, 13, 14, 24], "target": 23}, "output": [5, 6]}, {"input": {"nums": [-20, -15, -4, -2, 37], "target": -22}, "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": "8cc4fd4372c108c1"}
vcr_c8a3cfa7cb51
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": ["abb", "ba", "abab", "bbac", "cabb", "baac", "cbba"]}, "output": [1, 1, 1, 1, 3]}, {"input": {"strs": ["baa", "baa", "aaa", "abb", "baa", "bbb", "aab"]}, "output": [1, 1, 1, 4]}, {"input": {"strs": ["bbb", "baa", "bab", "aba", "aba", "bba"]}, "output": [1, 2, 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": "1808f0efa29aa4d2"}
vcr_58c6aff0c858
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, -15, -7, -4, -3, 3, 7, 16, 32, 34], "target": 50}, "output": [7, 9]}, {"input": {"nums": [-20, -14, -11, -10, 2, 31, 32, 39], "target": -8}, "output": [3, 4]}, {"input": {"nums": [-11, -5, -2, 7, 11, 28, 35], "target": 63}, "output": [5, 6]}]
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": "1844ad3d399d2048"}
vcr_ad803d6d95b6
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 2, 1, 4, 1, 7, 3, 1]}, "output": [168, 252, 504, 126, 504, 72, 168, 504]}, {"input": {"nums": [2, 1, 2, 2, 4, 2]}, "output": [32, 64, 32, 32, 16, 32]}, {"input": {"nums": [2, 3, 3, 1, 4]}, "output": [36, 24, 24, 72, 18]}]
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": "cb0b073ff88fb365"}
vcr_1b5304f9938b
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": [13, 8, 17, 23, 3, 2, 17, 29, 28, 17, 9]}, "output": 90}, {"input": {"nums": [2, 20, 7, 13, 5, 11, 2, 1]}, "output": 45}, {"input": {"nums": [19, 10, 16, 12, 15, 6, 1]}, "output": 51}]
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": "48dc33d10eac0d89"}
vcr_a0d8517ba67f
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, 23, 24, 32, 38], "target": 11}, "output": 0}, {"input": {"nums": [0, 4, 7, 14, 17, 22, 23, 29, 31, 32, 34], "target": 5}, "output": 2}, {"input": {"nums": [1, 4, 11, 13, 14, 16, 19, 20, 22, 25, 26, 39], "target": 10}, "output": 2}]
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": "8032ed59fe196df1"}
vcr_cbfec0280601
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": [17, -14, 33, 26, -16, -22, -7]}, "output": [17, 3, 36, 62, 46, 24, 17]}, {"input": {"nums": [2, 1, -10, 5, 14, -12, -10, 7, 1]}, "output": [2, 3, -7, -2, 12, 0, -10, -3, -2]}, {"input": {"nums": [-11, -10, -9, 3, 1, -7, 0]}, "output": [-11, -21, -30, -27, -26, -33, -33]}]
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": "bed68b0ffeafe099"}
vcr_5339ed4d9c09
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": "baaabccbaaab"}, "output": true}, {"input": {"s": "bcacbaca"}, "output": false}, {"input": {"s": "aabccab"}, "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": "c699d50d321a0666"}
vcr_f511878d66d1
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 4, 8, 9, 10, 11, 21, 24, 34, 42, 46, 47, 48, 49], "target": 18}, "output": 6}, {"input": {"nums": [9, 14, 23, 28, 32], "target": 17}, "output": 2}, {"input": {"nums": [0, 6, 7, 8, 16, 20, 25, 28, 35, 36, 37, 39], "target": 12}, "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": "6d3efc4e3e191f2e"}
vcr_8eca87ee2020
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 4, 6, 6, 7, 7, 9, 10, 12]}, "output": [0, 1, 4, 6, 7, 9, 10, 12]}, {"input": {"nums": [0, 1, 3, 3, 4, 6, 8, 10, 12, 12]}, "output": [0, 1, 3, 4, 6, 8, 10, 12]}, {"input": {"nums": [5, 6, 7, 9, 10]}, "output": [5, 6, 7, 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": "8175761921fa8188"}
vcr_f410b7d6a410
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": [23, 11, 35, 45, 6, 30, 30, 38, 39, 4, 4, 18, 27, 33, 11]}, "output": true}, {"input": {"nums": [1, 17, 19, 17, 24, 1, 16]}, "output": true}, {"input": {"nums": [7, 21, 1, 16]}, "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": "2f58592c16cd386e"}
vcr_531706ce398b
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, 7, 7, 5, 5, 1, 1, 7]}, "output": [8575, 2450, 2450, 3430, 3430, 17150, 17150, 2450]}, {"input": {"nums": [4, 5, 4]}, "output": [20, 16, 20]}, {"input": {"nums": [1, 2, 3]}, "output": [6, 3, 2]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "081f1e029f6ae429"}
vcr_12cbe3803785
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, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 11, 11]}, "output": [1, 4, 5, 6, 7, 8, 9, 10, 11]}, {"input": {"nums": [0, 1, 3, 5, 8, 9, 9, 11]}, "output": [0, 1, 3, 5, 8, 9, 11]}, {"input": {"nums": [1, 2, 7, 8]}, "output": [1, 2, 7, 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": "199df572690d62ad"}
vcr_01553f4bbda1
medium
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": "cabdabcba"}, "output": false}, {"input": {"s": "cbcbabc"}, "output": false}, {"input": {"s": "aac"}, "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": "5e940c3cdc734543"}
vcr_d6e53c4541f0
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 4, 6, 22, 29, 42, 49], "target": 10}, "output": 3}, {"input": {"nums": [5, 12, 15, 31, 33, 35, 36, 37], "target": 35}, "output": 5}, {"input": {"nums": [0, 3, 7, 10, 11, 17, 24, 28, 29, 31, 32], "target": 25}, "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": "eb952abad34aebfe"}
vcr_0c11b3490ee1
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": 3008}, "output": 5}, {"input": {"n": 1056}, "output": 2}, {"input": {"n": 1336}, "output": 5}]
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": "e97f883653847cd2"}
vcr_043d5051e7c1
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": "dca bdbdeb"}, "output": "bdbdeb dca"}, {"input": {"s": "a abab abbb ba b"}, "output": "b ba abbb abab a"}, {"input": {"s": "babb ab ba b"}, "output": "b ba ab 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": "078c5282b2e07d3d"}
vcr_a226dd376d37
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": [54, 35, 32, 60, 27, 54]}, "output": 28}, {"input": {"prices": [38, 12, 13, 38, 1, 20, 19, 37, 23, 4, 39]}, "output": 38}, {"input": {"prices": [4, 35, 27, 39, 11, 19, 3, 23, 38, 37, 14, 23]}, "output": 35}]
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": "291510db52175b76"}
vcr_24994aac9d62
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, 121, 157, 173, 118, 59, 53, 98, 6, 93, 125, 35, 69, 180]}, "output": false}, {"input": {"nums": [23, 3, 15, 5, 18, 9, 25, 22, 14, 17, 13]}, "output": false}, {"input": {"nums": [20, 11, 16, 24]}, "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": "d46e5320eec3abde"}
vcr_89902543a918
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": [18, 24, 27, 28, 29, 31, 37, 38, 46], "target": 46}, "output": 8}, {"input": {"nums": [4, 6, 7, 12, 16, 24, 26, 34, 36], "target": 25}, "output": 6}, {"input": {"nums": [0, 2, 5, 11, 13, 19, 27, 29, 34, 35, 37], "target": 37}, "output": 10}]
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": "242ca2726ff60955"}
vcr_95daf32d34e5
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": [21, 2, 49, 19, 31, 29, 41, 40, 21, 20, 13, 12, 12]}, "output": 47}, {"input": {"prices": [19, 33, 37, 40, 19]}, "output": 21}, {"input": {"prices": [16, 18, 31, 12, 14]}, "output": 15}]
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": "148ecc00cd11b0eb"}
vcr_c0c77354c3f9
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, 4, 5, 6, 8, 9, 14]}, "output": [0, 3, 4, 5, 6, 8, 9, 14]}, {"input": {"nums": [4, 5, 11]}, "output": [4, 5, 11]}, {"input": {"nums": [3, 7, 8, 9, 9, 12]}, "output": [3, 7, 8, 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": "565f1700a2d6af11"}
vcr_6deb06f7cd58
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 4, 7, 11, 15]}, "output": [3, 4, 7, 11, 15]}, {"input": {"nums": [3, 4, 10]}, "output": [3, 4, 10]}, {"input": {"nums": [0, 1, 2, 3, 5, 7, 8, 9, 9, 12]}, "output": [0, 1, 2, 3, 5, 7, 8, 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": "1b4c15fff4a92698"}
vcr_9fcabd0b36c3
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": [19, 15, 8, 8, 30, 2, 8, 25, 16, 7, 30, 28]}, "output": 117}, {"input": {"nums": [17, 3, 11, 9]}, "output": 28}, {"input": {"nums": [20, 5, 6, 13, 13, 0, 19]}, "output": 58}]
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": "53b604ea6e4e1943"}
vcr_62ee57efdfbd
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": [6, 6, 9, 10, 12, 14, 14, 15, 15]}, "output": [6, 9, 10, 12, 14, 15]}, {"input": {"nums": [0, 2, 7, 10, 11]}, "output": [0, 2, 7, 10, 11]}, {"input": {"nums": [0, 3, 4, 10]}, "output": [0, 3, 4, 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": "57da20562bcf6f1f"}
vcr_15cf58e40f85
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": [-11, -24, 33, 17, 20, -23, -6, 4, -24, -11, 11, 26, -5]}, "output": [-11, -35, -2, 15, 35, 12, 6, 10, -14, -25, -14, 12, 7]}, {"input": {"nums": [-8, 14, 10, 17, -4, 0, -7, -2, 15]}, "output": [-8, 6, 16, 33, 29, 29, 22, 20, 35]}, {"input": {"nums": [8, 0, -10]}, "output": [8, 8, -2]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "674f83e7ebdcf957"}
vcr_bb749f685708
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, 13, 20, 21, 22, 27, 35, 46, 47], "target": 14}, "output": 3}, {"input": {"nums": [2, 12, 16, 20, 30, 39], "target": 13}, "output": 2}, {"input": {"nums": [7, 9, 22, 35, 38], "target": 4}, "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": "0425ed3fc49a5f18"}
vcr_b22c3965be51
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": [18, 6, 30, 19, 16, 7]}, "output": 64}, {"input": {"nums": [19, 10, 6, 15]}, "output": 34}, {"input": {"nums": [5, 17, 15, 6, 18, 11]}, "output": 38}]
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": "ab3ee92148230032"}
vcr_009952ffe57f
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": [10, 33, 3, 4, 2, 4]}, "output": [10, 43, 46, 50, 52, 56]}, {"input": {"nums": [-2, -11, 5, -1, 0]}, "output": [-2, -13, -8, -9, -9]}, {"input": {"nums": [5, 5, 16, -3, 12, -9, -4, 9, -7, 17]}, "output": [5, 10, 26, 23, 35, 26, 22, 31, 24, 41]}]
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": "601d8d77adf41770"}
vcr_a7dca6d5df50
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": [68, -34, 55, 12, -4, -8, 28, 52, 50, -10, 16, -37, 4, -32], "target": 56}, "output": [7, 12]}, {"input": {"nums": [22, 16, 23, 29, 5], "target": 38}, "output": [0, 1]}, {"input": {"nums": [3, 23, -28, 12, 39, 37, 30, 1, 20, 26, -24], "target": 33}, "output": [0, 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": "b93a2a53731d264a"}
vcr_423120086d7c
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": [-20, -15, 0, 27, 28, 30, 46], "target": 7}, "output": [0, 3]}, {"input": {"nums": [-18, -10, -9, 9, 10, 17, 20, 22, 25, 35, 37], "target": 57}, "output": [6, 10]}, {"input": {"nums": [-11, 10, 18, 28, 31], "target": 46}, "output": [2, 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": "ab5f1b0b41ffeb12"}
vcr_6ca7941c7550
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, 0, 0, -9, -1, 13, -11, -4, 11, 18], "k": 7}, "output": [-9, -1, 13, -11, -4, 11, 18, -17, 0, 0]}, {"input": {"nums": [3, 7, -3, 7, 8, 6, -5], "k": 12}, "output": [-3, 7, 8, 6, -5, 3, 7]}, {"input": {"nums": [4, 4, 5, 6, -2, 7], "k": 2}, "output": [-2, 7, 4, 4, 5, 6]}]
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": "f1c7fabc5b37deda"}
vcr_5b68faf1a9b4
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": "edbcb cde"}, "output": "cde edbcb"}, {"input": {"s": "a bab"}, "output": "bab a"}, {"input": {"s": "abb a bb"}, "output": "bb a abb"}]
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": "ace0d2c90df6f2dc"}
vcr_eaea4b29f70f
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": [28, 21, 38, 39, 9, 7]}, "output": 18}, {"input": {"prices": [10, 16, 34, 31, 33, 18, 10, 32, 27, 24, 17]}, "output": 24}, {"input": {"prices": [8, 3, 17, 12, 32, 5, 7]}, "output": 29}]
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": "8ac49724419471d8"}
vcr_58a19c6cecb0
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": "ceca eccc eabaed bceaeb eaddbb a"}, "output": "a eaddbb bceaeb eabaed eccc ceca"}, {"input": {"s": "ab bab bbab ab abaa"}, "output": "abaa ab bbab bab ab"}, {"input": {"s": "bb aa aaaa bbab"}, "output": "bbab aaaa aa bb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "6017d12a2fd82f75"}
vcr_f99b31b6b072
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, -16, 13, -2, 1, 14, 15, -8, -17], "k": 13}, "output": [14, 15, -8, -17, 13, -16, 13, -2, 1]}, {"input": {"nums": [-9, -8, 5, -5, 0, 8, -2, 2, 2, -1], "k": 13}, "output": [2, 2, -1, -9, -8, 5, -5, 0, 8, -2]}, {"input": {"nums": [-2, 7, 5], "k": 13}, "output": [5, -2, 7]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "e18a7d138b6b818b"}
vcr_d5b45d8eeefa
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": [7, 1, 44, 42]}, "output": 43}, {"input": {"prices": [35, 24, 22, 23, 21, 18, 27, 38, 25, 24]}, "output": 20}, {"input": {"prices": [19, 2, 12, 2, 26, 4]}, "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": "57fc02b95307c478"}
vcr_748e14e7f63e
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [20, 5, 0, 15, 20, 11, 24, 30, 18, 9, 22, 17]}, "output": 104}, {"input": {"nums": [16, 19, 2, 2, 5, 17, 7, 5, 1, 11]}, "output": 54}, {"input": {"nums": [11, 0, 15, 18, 12]}, "output": 38}]
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": "56243be7b2a4f03e"}
vcr_a463dddc0438
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, 4, 3, 3, 2, 3, 0]}, "output": true}, {"input": {"nums": [3, 1, 3, 2, 1, 3, 0]}, "output": true}, {"input": {"nums": [2, 1, 0, 2, 1, 1, 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": "f67e8cf8d2fa9ab6"}
vcr_e129e2c3ec2d
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": [5, 7, 58, 57, 31, 52, 57, 28]}, "output": 53}, {"input": {"prices": [17, 21, 26, 27, 29, 9, 9]}, "output": 12}, {"input": {"prices": [25, 10, 37, 3, 31, 25, 35, 26, 13, 32]}, "output": 32}]
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": "7d8460b86b3aa46e"}
vcr_0f6805ed8d35
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [18, -19, -17, -10, 28, -3, 5, -9]}, "output": [18, -1, -18, -28, 0, -3, 2, -7]}, {"input": {"nums": [-5, 14]}, "output": [-5, 9]}, {"input": {"nums": [14, 16, -7, 1, 13, -4]}, "output": [14, 30, 23, 24, 37, 33]}]
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": "47a07869dbf5d765"}
vcr_09df24cb6310
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": "bbaadbbdcbacbaa"}, "output": false}, {"input": {"s": "ccccbcaa"}, "output": false}, {"input": {"s": "babbcc"}, "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": "3c380b4f2d3d4e47"}
vcr_a96f0a0cd1a5
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": [40, 147, 138, 149, 181, 143, 173, 39, 196, 72, 29, 15, 41, 31]}, "output": false}, {"input": {"nums": [11, 9, 1, 9]}, "output": true}, {"input": {"nums": [19, 2, 8, 3, 9, 2, 15, 24, 11, 14, 24]}, "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": "70f44c805ba3adca"}
vcr_34f74902ea6f
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": [5, 7, 4, 4, 7, 7, 1]}, "output": [5488, 3920, 6860, 6860, 3920, 3920, 27440]}, {"input": {"nums": [2, 2, 3, 2, 4, 3, 4]}, "output": [576, 576, 384, 576, 288, 384, 288]}, {"input": {"nums": [5, 5, 1, 2, 3]}, "output": [30, 30, 150, 75, 50]}]
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": "aa9347f86d2b172a"}
vcr_77c7a8e707d4
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 0, 1, 0, 4, 2, 1, 1, 2, 0]}, "output": true}, {"input": {"nums": [1, 2, 2, 2, 1, 2, 2, 2, 0]}, "output": true}, {"input": {"nums": [3, 1, 0, 2, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "d5fa1241ad898f60"}
vcr_7c4d67f491c8
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": ["bbb", "cb", "cabc", "acc", "aa", "caac", "baa", "baba", "ac"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "aba", "bab", "bbb", "abb", "baa"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["baa", "bba", "aba"]}, "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": "1ec13181f645e721"}
vcr_00e81b7fdbdf
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": [13, 49, 41, 52, 46, 46]}, "output": 39}, {"input": {"prices": [6, 14, 37, 23]}, "output": 31}, {"input": {"prices": [21, 38, 13, 24, 14, 20, 40, 19, 12]}, "output": 27}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "73fd288a628def95"}
vcr_83fc2b7d8bae
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": [5, 3, 4, 1, 6, 5, 1, 4]}, "output": [1440, 2400, 1800, 7200, 1200, 1440, 7200, 1800]}, {"input": {"nums": [3, 4, 4, 1]}, "output": [16, 12, 12, 48]}, {"input": {"nums": [4, 3, 3, 1]}, "output": [9, 12, 12, 36]}]
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": "8e0725bc049adc1e"}
vcr_a9176a46a7db
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": [39, 18, 12, 6, 31, 39, 69, 6, 33, 31, 18, 33, 12]}, "output": 69}, {"input": {"nums": [24, 1, 53, 24, 4, 1, 4]}, "output": 53}, {"input": {"nums": [5, 23, 12, 23, 15, 58, 12, 15, 5]}, "output": 58}]
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": "af67d55ccdfd4fef"}
vcr_e2b7487c5221
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": [5, 12, 36, 53, 1, 35, 23, 35, 5, 32, 37, 37, 32, 1, 12, 36, 23]}, "output": 53}, {"input": {"nums": [28, 7, 3, 42, 3, 7, 28]}, "output": 42}, {"input": {"nums": [4, 14, 27, 14, 11, 2, 27, 2, 4, 58, 11]}, "output": 58}]
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": "e6214c7eccce2614"}
vcr_09fe07cfcbfa
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": [8, 16, 20, 27, 39], "target": 50}, "output": 5}, {"input": {"nums": [0, 2, 11, 13, 17, 21, 28, 32, 34, 36, 37], "target": 22}, "output": 6}, {"input": {"nums": [6, 11, 17, 26, 30, 37, 38], "target": 11}, "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": "833cbf04fa877f45"}
vcr_320b2dc98a8b
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": "adeaaa ea"}, "output": "ea adeaaa"}, {"input": {"s": "aaab baaa"}, "output": "baaa aaab"}, {"input": {"s": "abba a aa bbbb bbb"}, "output": "bbb bbbb aa a abba"}]
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": "76ad10ca3856d4b1"}
vcr_42aef00a174c
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 0, 0, 0, 3, 4, 0]}, "output": true}, {"input": {"nums": [1, 2, 2]}, "output": true}, {"input": {"nums": [3, 2, 1, 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": "3130fb693e2c8ea3"}
vcr_f56f3055b249
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": "dbad cac c cb b"}, "output": "b cb c cac dbad"}, {"input": {"s": "abba ba"}, "output": "ba abba"}, {"input": {"s": "b a aa"}, "output": "aa a b"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "6aa86583c2f05c9b"}
vcr_116ffd18f030
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": [24, 24, 35, 35, 67, 2, 2, 18, 18]}, "output": 67}, {"input": {"nums": [13, 46, 17, 17, 13]}, "output": 46}, {"input": {"nums": [3, 13, 3, 13, 46]}, "output": 46}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "7658289938314f6c"}
vcr_8d038fb8dd33
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": [35, 10, 26, 12, 28, -16]}, "output": [35, 45, 71, 83, 111, 95]}, {"input": {"nums": [-4, 18, -12, -6, -7, 16, -1, -11, 14]}, "output": [-4, 14, 2, -4, -11, 5, 4, -7, 7]}, {"input": {"nums": [-1, 6, 1, 0, -12, 3, -6]}, "output": [-1, 5, 6, 6, -6, -3, -9]}]
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": "b1eb25767c120c93"}
vcr_e302d600bd79
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": [30, 12, 20, 20, 28, 12, 59, 30, 50]}, "output": 47}, {"input": {"prices": [12, 40, 21, 9, 3, 18, 5, 25, 23, 16, 21]}, "output": 28}, {"input": {"prices": [40, 7, 14, 7, 11, 39, 5, 23, 37, 13]}, "output": 32}]
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": "f864cb9dec5acae9"}
vcr_5bff39f6184d
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": [37, 25, 47, 37, 4, 46, 15, 5, 30, 9, 46, 8, 19, 1, 12]}, "output": true}, {"input": {"nums": [18, 9, 20, 21, 11]}, "output": false}, {"input": {"nums": [10, 25, 0, 22, 23, 11, 10]}, "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": "f56378229a300b73"}
vcr_30b1100ec5c7
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 11, 13, 14, 16, 17, 19, 26, 32, 35, 48], "target": 33}, "output": 9}, {"input": {"nums": [6, 8, 9, 10, 15, 16, 17, 21, 23, 26, 39], "target": 19}, "output": 7}, {"input": {"nums": [9, 11, 14, 20, 38], "target": 15}, "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": "cc8aeabddefd9c57"}
vcr_9c882f59034b
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [29, 29, 78, 22, 32, 27, 22, 32, 27]}, "output": 78}, {"input": {"nums": [27, 27, 54, 28, 28]}, "output": 54}, {"input": {"nums": [41, 12, 25, 12, 25]}, "output": 41}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "b08afdc6d118ac57"}
vcr_2f2400770711
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": [149, 114, 82, 174, 138, 88, 106, 113]}, "output": false}, {"input": {"nums": [2, 0, 5, 11, 15, 18]}, "output": false}, {"input": {"nums": [12, 14, 24, 11, 18]}, "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": "c763286ffd04554d"}
vcr_81e9f1b9b12d
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": ["aac", "ccb", "aaca", "bcb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bab", "aaa", "abb", "bab", "abb"]}, "output": [2, 4]}, {"input": {"strs": ["bba", "aaa", "bbb", "aba", "bab", "bab"]}, "output": [1, 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": "394fc97fed022366"}
vcr_8d18e6cff14a
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": [28, 19, 9, 49, 34, 17, 18, 24, 28]}, "output": true}, {"input": {"nums": [25, 8, 11, 9, 15, 11, 0, 21, 24, 17, 20]}, "output": true}, {"input": {"nums": [13, 13, 1, 20, 7, 11, 18, 6, 24, 5, 14, 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": "18b1891679bf7d5c"}
vcr_73c154554af3
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [2, 11, 11, 24, 13, 17, 22, 1, 0, 15, 29, 9]}, "output": 86}, {"input": {"nums": [15, 10, 0, 1, 1, 2, 9]}, "output": 25}, {"input": {"nums": [17, 20, 9, 12]}, "output": 32}]
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": "7bb0c4be79a3287a"}
vcr_d76dbe48c7d8
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": 46, "b": 71}, "output": 1}, {"input": {"a": 142, "b": 129}, "output": 1}, {"input": {"a": 84, "b": 91}, "output": 7}]
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": "597672090c14e889"}
vcr_4e174fe87a22
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": [3, 27, 11, 11, 27, 3, 8, 9, 9, 61, 8]}, "output": 61}, {"input": {"nums": [50, 12, 12, 10, 10]}, "output": 50}, {"input": {"nums": [10, 17, 10, 5, 7, 7, 5, 45, 17]}, "output": 45}]
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": "c357b4d210e4772d"}
vcr_4fbc6b772d6e
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": "cacecc ebade ecc"}, "output": "ecc ebade cacecc"}, {"input": {"s": "bb a"}, "output": "a bb"}, {"input": {"s": "a aaa bab"}, "output": "bab aaa 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": "d7be0620c99af229"}
vcr_c127d146454c
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": ["aba", "aba", "cacc", "abc", "cbba", "cacc", "bc"]}, "output": [1, 1, 1, 2, 2]}, {"input": {"strs": ["aba", "bba", "aab", "bbb", "bab", "baa", "bbb"]}, "output": [2, 2, 3]}, {"input": {"strs": ["bbb", "abb", "aab"]}, "output": [1, 1, 1]}]
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": "a42263e79fe6802a"}
vcr_df72d4c07f25
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": [7, 9, 37, 44, 38, 27, 55, 32, 42, 56, 32, 38, 17, 37, 5]}, "output": 49}, {"input": {"prices": [22, 1, 13, 15, 1, 14, 29, 20, 9, 33]}, "output": 32}, {"input": {"prices": [15, 19, 13, 2, 36, 3, 15]}, "output": 34}]
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": "d25cafe4fcacb779"}
vcr_90ee06ea76aa
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": ["acaa", "ac", "bbac", "cbc", "cac", "caac", "cc"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "baa", "aab", "baa", "bba", "abb"]}, "output": [2, 4]}, {"input": {"strs": ["bbb", "bab", "aaa", "abb"]}, "output": [1, 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": "253b6b08339a4aa3"}
vcr_aec7e1692761
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": [-29, -22, -19, -8, 0, 15, 21, 25, 33, 38, 47], "target": -51}, "output": [0, 1]}, {"input": {"nums": [-18, -17, -14, -11, 0, 11, 15, 18, 20, 25, 39], "target": 35}, "output": [6, 8]}, {"input": {"nums": [-3, -1, 0, 2, 3, 6, 8, 22, 24, 32], "target": -1}, "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": "7db1f75ce380ad84"}
vcr_91d543b11601
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": [18, 30, 32, 33, 41, 42], "target": 20}, "output": 1}, {"input": {"nums": [5, 24, 29, 33, 39], "target": 24}, "output": 1}, {"input": {"nums": [10, 11, 21, 37], "target": 32}, "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": "625516858e38862e"}