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_3554623d08f1
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 6, 9, 14, 19, 20, 27, 30, 32, 38, 46, 48], "target": 26}, "output": 6}, {"input": {"nums": [3, 17, 18, 23, 27, 33, 34, 36, 37], "target": 18}, "output": 2}, {"input": {"nums": [2, 5, 14, 17, 25, 30, 33, 37, 38, 39], "target": 39}, "output": 9}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "961166eb05c37eff"}
vcr_c4ae2d157253
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": [0, 18, -17, -18, -11]}, "output": 18}, {"input": {"nums": [-4, -1, 9, 2, -7, -1, 4, 0, -3]}, "output": 11}, {"input": {"nums": [-10, -10, 9, 7, 8, 5, 13, -8, 2, 13]}, "output": 49}]
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": "22e7184f9e40011c"}
vcr_bc341bae7221
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 3, 0, 7, 2]}, "output": [0, 0, 42, 0, 0]}, {"input": {"nums": [5, 2, 3, 3]}, "output": [18, 45, 30, 30]}, {"input": {"nums": [3, 4, 4, 3, 3]}, "output": [144, 108, 108, 144, 144]}]
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": "0977f42c6d9a6583"}
vcr_d4b2fe4854fb
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "c cebd"}, "output": "cebd c"}, {"input": {"s": "bbaa ab bb"}, "output": "bb ab bbaa"}, {"input": {"s": "a a"}, "output": "a a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "45ad956676421a72"}
vcr_2694e480e774
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": [-16, -11, 11, -8, -3, 0, -15, -12, -6, 14]}, "output": 14}, {"input": {"nums": [3, -10, 11, -10, 8, 12]}, "output": 21}, {"input": {"nums": [9, 10, 9]}, "output": 28}]
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": "0c3688ad780188a7"}
vcr_9bdb81382854
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": [28, 10, 13, 4, 28, 13, 24, 23]}, "output": 93}, {"input": {"nums": [3, 8, 17, 17, 13]}, "output": 33}, {"input": {"nums": [10, 16, 11, 9]}, "output": 25}]
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": "56ddaf8413fdd2b2"}
vcr_67a2e991fb08
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": [26, 39, 26, 6, 39, 31, 29, 70, 31, 6, 29]}, "output": 70}, {"input": {"nums": [27, 3, 3, 10, 10, 53, 27]}, "output": 53}, {"input": {"nums": [22, 15, 57, 20, 16, 13, 16, 13, 20, 15, 22]}, "output": 57}]
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": "434c9d0a0101d473"}
vcr_4e08379cacbd
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, 10, 11, 13, 15, 16, 21, 22, 23, 40], "target": 31}, "output": 9}, {"input": {"nums": [2, 9, 12, 14, 34, 36], "target": 26}, "output": 4}, {"input": {"nums": [2, 7, 12, 15, 17, 20, 21, 22, 25, 34, 38], "target": 4}, "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": "3a7f1b0b41337a2c"}
vcr_eca192c714c2
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": [16, 0, 21, 45, 47, 6, 21, 33, 33, 7, 18, 50, 21, 20, 2, 17, 46]}, "output": true}, {"input": {"nums": [13, 18, 4, 9, 8, 16, 4, 8, 5, 11, 11]}, "output": true}, {"input": {"nums": [13, 15, 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": "71c4851806213aa2"}
vcr_63701157198f
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 3, 4, 7, 7, 9, 10, 11, 12, 13, 14, 15]}, "output": [1, 2, 3, 4, 7, 9, 10, 11, 12, 13, 14, 15]}, {"input": {"nums": [0, 4, 8, 12]}, "output": [0, 4, 8, 12]}, {"input": {"nums": [1, 3, 4, 10, 11]}, "output": [1, 3, 4, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "0fe66bce81c096a0"}
vcr_6236f42040e4
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, 4, 4, 1, 2, 5, 4]}, "output": [640, 480, 480, 1920, 960, 384, 480]}, {"input": {"nums": [1, 1, 5, 1, 5]}, "output": [25, 25, 5, 25, 5]}, {"input": {"nums": [4, 4, 4, 5]}, "output": [80, 80, 80, 64]}]
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": "0c66f3e53c862b76"}
vcr_9350f04073f7
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": [170, 3, 135, 180, 174, 129, 106, 103, 162, 35, 8, 185, 59, 5, 62, 114, 154]}, "output": false}, {"input": {"nums": [3, 16, 0, 8, 13, 5]}, "output": false}, {"input": {"nums": [14, 23, 7, 23, 25, 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": "82829b906d7613b0"}
vcr_aa6466eca22a
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": [51, 86, 127, 16, 87, 196, 164, 24, 74, 159, 121, 73, 7, 70, 48, 1]}, "output": false}, {"input": {"nums": [14, 8, 23, 20, 19, 24, 21, 2, 5, 23, 7]}, "output": true}, {"input": {"nums": [8, 5, 15, 15, 22, 4, 13]}, "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": "77db58db5b916b86"}
vcr_259afa4f0f10
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": [22, 3, 16, 10, 0, 14, 29]}, "output": 67}, {"input": {"nums": [3, 13, 19, 9, 2, 16, 13, 8]}, "output": 46}, {"input": {"nums": [4, 9, 1, 6, 10]}, "output": 19}]
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": "d5a616d8f883176e"}
vcr_c2b2ab702198
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, 5, 6, 7, 8, 8, 12, 14, 15]}, "output": [0, 1, 5, 6, 7, 8, 12, 14, 15]}, {"input": {"nums": [1, 4, 5, 9]}, "output": [1, 4, 5, 9]}, {"input": {"nums": [0, 1, 3, 4, 7, 8, 9, 11]}, "output": [0, 1, 3, 4, 7, 8, 9, 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": "2bba9d207428effc"}
vcr_d6186d0265f8
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, -19, -4, 1, 2, 3, 17, 21, 31, 38, 45, 47], "target": 48}, "output": [3, 11]}, {"input": {"nums": [-16, -14, -8, 13, 14, 24, 25, 31, 32, 39], "target": 27}, "output": [3, 4]}, {"input": {"nums": [-17, -8, -1, 15, 19, 25, 32, 38], "target": 57}, "output": [4, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "76224d4592db6107"}
vcr_0ae2c009da4c
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["bc", "bb", "ca", "baaa", "caaa", "cbac", "cc"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bab", "bbb", "baa", "bab", "abb"]}, "output": [1, 1, 3]}, {"input": {"strs": ["abb", "abb", "baa", "bbb"]}, "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": "f23f6678a8954b37"}
vcr_ab1620beed5a
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": [7, 10, 24, 34, 36, 40, 45], "target": 12}, "output": 2}, {"input": {"nums": [0, 7, 14, 16, 19, 38], "target": 26}, "output": 5}, {"input": {"nums": [6, 8, 9, 14, 15, 18, 25, 37, 39], "target": 8}, "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": "64a212b698eb67db"}
vcr_e1a886715abf
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": "ddcacda"}, "output": false}, {"input": {"s": "cb"}, "output": false}, {"input": {"s": "cacb"}, "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": "916b0a157f5042bc"}
vcr_a4269a3e9113
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, 17, 19, 3, 38, 17, 19, 22, 58, 38, 22]}, "output": 58}, {"input": {"nums": [16, 4, 12, 12, 22, 9, 4, 22, 20, 47, 20, 9, 16]}, "output": 47}, {"input": {"nums": [13, 13, 17, 51, 8, 17, 8]}, "output": 51}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "767b66368fe51d1e"}
vcr_f7d60d03efaf
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": [4, 14, -1, -6, -18, -3, -13, 16], "k": 12}, "output": [-18, -3, -13, 16, 4, 14, -1, -6]}, {"input": {"nums": [2, -4, -1, -6, 4], "k": 1}, "output": [4, 2, -4, -1, -6]}, {"input": {"nums": [-4, 6, 7, 2], "k": 7}, "output": [6, 7, 2, -4]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "2f46e86b0f64ac70"}
vcr_78b7588a938a
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, 3, 4, 5, 10, 12, 15]}, "output": [2, 3, 4, 5, 10, 12, 15]}, {"input": {"nums": [4, 8, 12]}, "output": [4, 8, 12]}, {"input": {"nums": [0, 0, 1, 2, 3, 4, 5, 5, 7, 12]}, "output": [0, 1, 2, 3, 4, 5, 7, 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": "0c0353fe1742dcae"}
vcr_7b87c6463916
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": "aebace db ccdaad ebaaa d"}, "output": "d ebaaa ccdaad db aebace"}, {"input": {"s": "ab aa ba b aaba"}, "output": "aaba b ba aa ab"}, {"input": {"s": "b bab aaa aaaa"}, "output": "aaaa aaa bab 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": "5ac34bdc4110c118"}
vcr_e872d75accdc
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [11, -7, -18, -6, 19, -14, 4, 22]}, "output": 31}, {"input": {"nums": [2, 4, 9]}, "output": 15}, {"input": {"nums": [0, 0, 8, -6, -4, -6, 6, 5, 14, -3, -5]}, "output": 25}]
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": "5b5f44efe2751a15"}
vcr_cc9172f50b7b
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, 35, 7, 21, 22, 17, 7, 21, 50, 27, 37, 36, 27, 17, 6, 41, 35]}, "output": true}, {"input": {"nums": [10, 4, 12]}, "output": false}, {"input": {"nums": [6, 23, 12, 6, 10, 21, 25, 25, 12, 16, 1]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "d41777deb0f69fc8"}
vcr_a9eec4d277bb
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, 14, 20, 21, 25, 27, 31, 48, 49], "target": 18}, "output": 2}, {"input": {"nums": [1, 2, 7, 8, 24, 27, 33, 35], "target": 17}, "output": 4}, {"input": {"nums": [6, 14, 30], "target": 10}, "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": "b7719d480813b3c2"}
vcr_8b58d5dfeee1
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-7, 10, -6, 6, 19, 4, -18, 12, 2, -9], "k": 9}, "output": [10, -6, 6, 19, 4, -18, 12, 2, -9, -7]}, {"input": {"nums": [10, -9, -10, -9, 2, -2, -5, -9, -2], "k": 17}, "output": [-9, -10, -9, 2, -2, -5, -9, -2, 10]}, {"input": {"nums": [1, 5, -7, 2, 7, 4, 7, 9, -7], "k": 3}, "output": [7, 9, -7, 1, 5...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "989427c6a3f22819"}
vcr_d73bff5a7686
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, 2, 8, 10, 11, 21, 25, 28, 32, 40, 43, 44, 46], "target": 49}, "output": 13}, {"input": {"nums": [5, 8, 10, 15, 17, 18, 22, 27, 31, 34], "target": 34}, "output": 9}, {"input": {"nums": [10, 31, 32], "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": "e751cfe576f63721"}
vcr_2a5f4b61a8ff
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "baaabbb"}, "output": 3}, {"input": {"s": "bcac"}, "output": 4}, {"input": {"s": "caaacbc"}, "output": 5}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "07b98ac5f1e28fa5"}
vcr_61367ab1c82b
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, 21, 31, 37, 43, 49], "target": 8}, "output": 2}, {"input": {"nums": [5, 10, 12, 16, 17, 19, 22, 29, 30, 31, 36, 37], "target": 29}, "output": 7}, {"input": {"nums": [2, 12, 27, 32], "target": 14}, "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": "2f66ed860532a9e3"}
vcr_3f40ae95b61f
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": [4, -27, -7, -36, 48, 14, 7], "target": -23}, "output": [0, 1]}, {"input": {"nums": [-6, 31, -10, -27, 22, 46, -30, 4, -15, -14], "target": -42}, "output": [3, 8]}, {"input": {"nums": [-5, -23, 48, -22, -13, 20, 34], "target": 68}, "output": [2, 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": "d2481352acfb0bf6"}
vcr_963a3eeecc07
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": [9, 4, 24, 28, 13, 1, 27, 16]}, "output": 73}, {"input": {"nums": [19, 18, 2, 18, 17, 14, 1, 20]}, "output": 71}, {"input": {"nums": [17, 4, 16, 16]}, "output": 33}]
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": "d8bac5ecb11968e7"}
vcr_fbe5aa5f99c9
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, 4, 6, 7, 8, 8, 9, 13, 15, 15]}, "output": [2, 4, 6, 7, 8, 9, 13, 15]}, {"input": {"nums": [2, 2, 6, 8, 8, 9, 10, 10, 12]}, "output": [2, 6, 8, 9, 10, 12]}, {"input": {"nums": [0, 3, 7, 8, 8, 10, 12]}, "output": [0, 3, 7, 8, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "edf7c8008aaab523"}
vcr_e7469c156aef
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": 3163}, "output": 7}, {"input": {"n": 11369}, "output": 7}, {"input": {"n": 11784}, "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": "9b8dc5d0d8338de8"}
vcr_6fcd5ebc9d57
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "abaccbbbc"}, "output": 6}, {"input": {"s": "acabbccbcca"}, "output": 8}, {"input": {"s": "cacbabbabbab"}, "output": 10}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "4917f89160214b8c"}
vcr_852299cd4d39
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": [24, 25, 58, 54]}, "output": 34}, {"input": {"prices": [7, 3, 18, 11, 29, 29]}, "output": 26}, {"input": {"prices": [11, 32, 35, 22, 2, 24, 35, 23, 10, 21, 20, 8]}, "output": 33}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "6bde76fdafdda611"}
vcr_bd4d40ad334a
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [24, -7, 12, -11, 5, 20, -17]}, "output": [24, 17, 29, 18, 23, 43, 26]}, {"input": {"nums": [1, -8, 16, 17, -5, 12]}, "output": [1, -7, 9, 26, 21, 33]}, {"input": {"nums": [-6, 10, 12, 17, 2, 9, 11, 9, 3, -8]}, "output": [-6, 4, 16, 33, 35, 44, 55, 64, 67, 59]}]
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": "6c46f4c87f75ad91"}
vcr_aa035350b21b
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 3, 7, 5, 6]}, "output": [630, 210, 90, 126, 105]}, {"input": {"nums": [5, 1, 3, 4]}, "output": [12, 60, 20, 15]}, {"input": {"nums": [2, 4, 2, 2, 1, 4]}, "output": [64, 32, 64, 64, 128, 32]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "d4ae76a15747bcb6"}
vcr_ba9fcb6a3111
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, -21, -16, -9, -5, -4, 21, 25, 26, 30, 34, 48], "target": -42}, "output": [0, 2]}, {"input": {"nums": [-19, -12, 6, 24, 28, 29], "target": 30}, "output": [2, 3]}, {"input": {"nums": [-19, -17, -15, -7, -6, -5, 0, 3, 4, 9, 27], "target": -25}, "output": [0, 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": "683dfe079535d7d0"}
vcr_b42addaf7679
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": "dd eaaaea dd aa adbaa ad bdc"}, "output": "bdc ad adbaa aa dd eaaaea dd"}, {"input": {"s": "ba bb"}, "output": "bb ba"}, {"input": {"s": "ab b bbaa"}, "output": "bbaa b ab"}]
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": "5152b551894e5063"}
vcr_6339b5a452ce
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": "dcce e eeac adb bcdbdd bdc"}, "output": "bdc bcdbdd adb eeac e dcce"}, {"input": {"s": "bbb ba abb"}, "output": "abb ba bbb"}, {"input": {"s": "aa aa"}, "output": "aa aa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "a2ae2792a4f4264b"}
vcr_0b4b439a8593
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": 31342}, "output": 10}, {"input": {"n": 10446}, "output": 7}, {"input": {"n": 6649}, "output": 9}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "1c4cb2a728f8de07"}
vcr_94fee4a0606c
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": [60, 18, 47, 39, 55, 37, 6, 18, 6, 18, 18]}, "output": 37}, {"input": {"prices": [36, 12, 4, 35, 3, 36, 33, 20, 30, 17]}, "output": 33}, {"input": {"prices": [14, 4, 19, 21, 35, 2, 7, 28, 23, 7, 18]}, "output": 31}]
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": "4f23757ef935f4f0"}
vcr_9352c07bfa34
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 3, 3, 4, 7, 15]}, "output": [3, 4, 7, 15]}, {"input": {"nums": [4, 7, 9, 9, 12, 12, 12]}, "output": [4, 7, 9, 12]}, {"input": {"nums": [0, 1, 3, 3, 3, 4, 7, 7, 8, 8, 12]}, "output": [0, 1, 3, 4, 7, 8, 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": "afe1668ab5e9f6bb"}
vcr_eb9e6cd7e8e0
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", "baa", "ab", "bb", "cc", "cabc", "aab", "aaac", "ca"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["aaa", "aaa", "bbb"]}, "output": [1, 2]}, {"input": {"strs": ["bba", "bab", "aba", "bab", "aba"]}, "output": [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": "a5786ea7e408af17"}
vcr_7a6393163114
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "abacabcabbabbb"}, "output": 11}, {"input": {"s": "caccbbaba"}, "output": 7}, {"input": {"s": "accabccccb"}, "output": 6}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "8cf699371fe4b15e"}
vcr_7aac176e88e7
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [4, 1, 0, 6, 1, 7, 2]}, "output": [0, 0, 336, 0, 0, 0, 0]}, {"input": {"nums": [2, 1, 3, 3, 2]}, "output": [18, 36, 12, 12, 18]}, {"input": {"nums": [3, 2, 1, 4, 2]}, "output": [16, 24, 48, 12, 24]}]
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": "0088257130284f9a"}
vcr_b5c11bae2fd2
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": ["ccca", "ccba", "aa", "aca", "ac", "cc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bab", "aab", "aab", "abb"]}, "output": [2, 2]}, {"input": {"strs": ["aaa", "bab", "baa", "bab", "bba"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "eaee323d7bcf6dd6"}
vcr_8d90090905b3
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-17, -13, -5, 3, -11, -9, -3, -13, -5, 6, 20, 15, -17, 0, 2]}, "output": 41}, {"input": {"nums": [-7, 7, 0, -11, 10, -8, 12, 1]}, "output": 15}, {"input": {"nums": [-11, 13, -12, -2, -4, 10, -1, 11, 9, 2, 1]}, "output": 32}]
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": "63bbaaeefc749c31"}
vcr_d925edad6bc3
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, 20, 9, 23, 7, 3, 11, 25, 27, 11]}, "output": 82}, {"input": {"nums": [20, 4, 15, 11, 18, 12, 8, 12, 2, 5]}, "output": 70}, {"input": {"nums": [8, 14, 7, 8, 4]}, "output": 22}]
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": "67a78300ea265be2"}
vcr_4283251df34e
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": "acadd dcaceb becada"}, "output": "becada dcaceb acadd"}, {"input": {"s": "bbaa b a aab abba"}, "output": "abba aab a b bbaa"}, {"input": {"s": "aaab aabb ba"}, "output": "ba aabb aaab"}]
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": "78bf9aa6803afe3b"}
vcr_8ce86701fc40
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": [1, -14, -12, -18, -14, -1, -11, -9, -8, 1, 9, 17, -10]}, "output": 27}, {"input": {"nums": [7, -1, 6, 2, 7, -3, 14, -1, 14]}, "output": 45}, {"input": {"nums": [0, 2, 0, 9, 0, 0, 1, -7, -7]}, "output": 12}]
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": "f6ef2f3e53937dd8"}
vcr_1e5d371bf0e7
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": [37, 10, 56, 23, 54, 34, 3, 29, 2]}, "output": 46}, {"input": {"prices": [22, 25, 11, 29, 40, 7, 4, 21, 2, 3, 22, 25]}, "output": 29}, {"input": {"prices": [9, 28, 13, 30, 33]}, "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": "352c66ed863c47bb"}
vcr_f81927b79c37
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": [9, -15, -17, -9, 2, -10], "k": 10}, "output": [-17, -9, 2, -10, 9, -15]}, {"input": {"nums": [4, 10, -8, -10, -8], "k": 15}, "output": [4, 10, -8, -10, -8]}, {"input": {"nums": [5, 3, -1, 0, 0, -8, -10, 2], "k": 2}, "output": [-10, 2, 5, 3, -1, 0, 0, -8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "0c3131d294909a7d"}
vcr_47068198173c
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": [19, 26, 2, 25, -25]}, "output": [19, 45, 47, 72, 47]}, {"input": {"nums": [-6, 12, 15, -7, -2, 4, -4]}, "output": [-6, 6, 21, 14, 12, 16, 12]}, {"input": {"nums": [-7, 4, 15, -7, 11, 17, -1]}, "output": [-7, -3, 12, 5, 16, 33, 32]}]
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": "7f9546f989478941"}
vcr_5780eaabc608
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, -15, -6, 22, -1, 22, 25, 30]}, "output": [-17, -32, -38, -16, -17, 5, 30, 60]}, {"input": {"nums": [-5, -6, 6, 0, -2, 8]}, "output": [-5, -11, -5, -5, -7, 1]}, {"input": {"nums": [4, 3, -9, 14, 11]}, "output": [4, 7, -2, 12, 23]}]
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": "f0048d61e1c3748d"}
vcr_3f0748348ac3
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": ["ca", "cba", "aaab", "cb", "cb", "cacc", "cacb", "aa"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "bab", "bab", "aab"]}, "output": [2, 2]}, {"input": {"strs": ["bba", "aba", "abb"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "de337e431bcc4cc9"}
vcr_ea07bf1fed95
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [24, 17, 19, 9, 11, 23, 26, 20]}, "output": 86}, {"input": {"nums": [3, 15]}, "output": 15}, {"input": {"nums": [7, 14, 7, 13]}, "output": 27}]
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": "f808e4bf1c1db071"}
vcr_a792c80861e2
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, 3, 9, 48, 49], "target": 51}, "output": [3, 5]}, {"input": {"nums": [-10, -6, -4, 8, 9, 12, 14, 22, 30, 38], "target": 44}, "output": [6, 8]}, {"input": {"nums": [-14, -10, 16, 17, 20, 29], "target": 45}, "output": [2, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "a0cb5ee68c79ebdd"}
vcr_9b72535c027b
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": [7, 16, 23, 27, 31, 32, 38, 46, 49], "target": 1}, "output": 0}, {"input": {"nums": [0, 2, 4, 6, 10, 13, 31, 34, 38], "target": 12}, "output": 5}, {"input": {"nums": [5, 8, 9, 19, 23, 28, 36, 39], "target": 11}, "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": "bf665c34b8d7f5f5"}
vcr_b16a94d9ff96
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, 4, 2, 1]}, "output": [8, 6, 12, 24]}, {"input": {"nums": [1, 2, 4, 3, 4, 1, 1]}, "output": [96, 48, 24, 32, 24, 96, 96]}, {"input": {"nums": [4, 3, 5, 2, 5, 1]}, "output": [150, 200, 120, 300, 120, 600]}]
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": "826bf7094a18bb4c"}
vcr_0eeb30cb24f2
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, 11, 12, 15, 17, 18, 19, 20, 36, 48, 49], "target": 8}, "output": 2}, {"input": {"nums": [0, 8, 18, 23], "target": 21}, "output": 3}, {"input": {"nums": [4, 10, 17, 19, 22, 26, 30, 32, 33, 36, 38], "target": 9}, "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": "08abd52c54f92490"}
vcr_daa56b0009af
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": 70, "b": 231}, "output": 7}, {"input": {"a": 39, "b": 32}, "output": 1}, {"input": {"a": 82, "b": 29}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "5562f351e964f8e7"}
vcr_765d1fd78a47
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, 26, 35, 35, 26, 78, 2, 10, 2, 24, 36, 10, 36]}, "output": 78}, {"input": {"nums": [17, 40, 17, 20, 8, 1, 1, 20, 8]}, "output": 40}, {"input": {"nums": [24, 2, 24, 9, 55, 1, 13, 17, 1, 17, 13, 9, 2]}, "output": 55}]
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": "5506e79aafe5d908"}
vcr_9761cd578a5c
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": [34, 1, -17, -13]}, "output": [34, 35, 18, 5]}, {"input": {"nums": [-8, -3, 14, 0, 18, -1, 3, 0]}, "output": [-8, -11, 3, 3, 21, 20, 23, 23]}, {"input": {"nums": [-12, -7, 18, 6, 8, 1, 11, 9]}, "output": [-12, -19, -1, 5, 13, 14, 25, 34]}]
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": "4bd0624488da233b"}
vcr_6bbaaf4e857a
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": [22, 18, 4, 17, 22, 10, 22]}, "output": 70}, {"input": {"nums": [1, 18, 6]}, "output": 18}, {"input": {"nums": [4, 6, 19, 17, 8, 3, 20]}, "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": "2d8e2aead0779e1a"}
vcr_4271f8787a23
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": [36, -3, 44, 55, -26], "target": 91}, "output": [0, 3]}, {"input": {"nums": [1, 14, 25, 17, -12, -16, 8, -20, -13], "target": 1}, "output": [3, 5]}, {"input": {"nums": [32, 1, -26, -21, 28, -4, 33], "target": 6}, "output": [0, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "9e42302387868388"}
vcr_03af4b75d322
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": 63, "b": 186}, "output": 3}, {"input": {"a": 79, "b": 55}, "output": 1}, {"input": {"a": 3, "b": 44}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "5ba33f1f62aa8c44"}
vcr_fcae00c83bae
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": [21, 23, -9, 9, 3, 32, 14, -2, 33, -13, -25, 25, 7, -15]}, "output": [21, 44, 35, 44, 47, 79, 93, 91, 124, 111, 86, 111, 118, 103]}, {"input": {"nums": [14, 14, 1, 13, -1, 8]}, "output": [14, 28, 29, 42, 41, 49]}, {"input": {"nums": [-5, 4, -7, 1, 17, -1, -2, -11, -11]}, "output": [-5, -1, -8, -7, 1...
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": "7802f4698d0dfe6a"}
vcr_102125e64dd1
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 4, 0, 3, 3, 0, 0]}, "output": true}, {"input": {"nums": [2, 2, 2, 0, 2, 0, 2, 1, 3]}, "output": true}, {"input": {"nums": [1, 0, 3, 2, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "ead3bdb8518d5d1a"}
vcr_e20ff5994602
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": "dccdb"}, "output": false}, {"input": {"s": "ccacca"}, "output": false}, {"input": {"s": "bcbccbccb"}, "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": "21273910e80497bd"}
vcr_5bd419417504
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": [70, 88, 14, 21, 82, 155]}, "output": false}, {"input": {"nums": [5, 18, 21, 10, 17]}, "output": false}, {"input": {"nums": [25, 9, 19, 19, 22, 5, 10, 20, 22, 1, 6]}, "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": "c68e57afb8cf2177"}
vcr_da2ad9ac8273
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": [4, -11, 17, 23, -4, 21, -15, -6]}, "output": [4, -7, 10, 33, 29, 50, 35, 29]}, {"input": {"nums": [8, 1, 7, 1]}, "output": [8, 9, 16, 17]}, {"input": {"nums": [-2, -1]}, "output": [-2, -3]}]
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": "a086ed0ebb656b82"}
vcr_6e1e0878e81f
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, 30, 48, 49], "target": 8}, "output": 1}, {"input": {"nums": [6, 35, 37], "target": 37}, "output": 2}, {"input": {"nums": [6, 13, 20, 30, 33], "target": 24}, "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": "485ba9860ed18e63"}
vcr_2b2b6f9ae5b7
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, 3, 17, 42], "target": 33}, "output": 3}, {"input": {"nums": [9, 17, 30], "target": 14}, "output": 1}, {"input": {"nums": [8, 9, 11, 14, 17, 18, 21, 22, 27, 28, 33], "target": 31}, "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": "f7a63e23d400bd89"}
vcr_b75fa4455df9
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": [0, -5, 16, -10, 4, -10]}, "output": 16}, {"input": {"nums": [14, 6, 11, -8, -6, -11]}, "output": 31}, {"input": {"nums": [-8, 4, 7, -9, -4, 8, 6, 7]}, "output": 21}]
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": "a0b52a92cd28f80d"}
vcr_7c24c0b4a1bf
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": "ddee ecddd d a"}, "output": "a d ecddd ddee"}, {"input": {"s": "b aaba"}, "output": "aaba b"}, {"input": {"s": "baaa bb aa bbaa"}, "output": "bbaa aa bb baaa"}]
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": "70c061c3b0637793"}
vcr_41b3ac0cdabe
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": [36, 49, 38, 48, 19, 47, 20, 25, 37, 27, 13, 11, 11]}, "output": 28}, {"input": {"prices": [25, 17, 39, 20]}, "output": 22}, {"input": {"prices": [37, 32, 8]}, "output": 0}]
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": "93a27a411c974239"}
vcr_0941633ce9a4
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, -28, -3, 6, 34, 40, 44], "target": 40}, "output": [3, 4]}, {"input": {"nums": [-14, 8, 12, 15, 22, 23, 26, 28, 36, 39], "target": -6}, "output": [0, 1]}, {"input": {"nums": [-19, -12, 2, 7, 16, 26], "target": -10}, "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": "fe094bdf24603cc9"}
vcr_1dabb2c889b6
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": 3, "b": 46}, "output": 1}, {"input": {"a": 23, "b": 151}, "output": 1}, {"input": {"a": 156, "b": 129}, "output": 3}]
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": "ec41443447070d20"}
vcr_704ed0a1a517
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [20, 38, 7, 7, 46, 7, 17, 20, 7, 41, 26, 12, 13, 24, 3, 13, 25]}, "output": true}, {"input": {"nums": [14, 10, 14, 14]}, "output": true}, {"input": {"nums": [16, 13, 10]}, "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": "587eb2bdd34f942e"}
vcr_4f9fd949851d
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": 174, "b": 86}, "output": 2}, {"input": {"a": 7, "b": 56}, "output": 7}, {"input": {"a": 43, "b": 155}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "21a54851238d4590"}
vcr_64433cfb6ed6
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [16, 29, 13, 12, 39, 29, 12, 70, 13, 39, 16]}, "output": 70}, {"input": {"nums": [14, 1, 1, 44, 14]}, "output": 44}, {"input": {"nums": [11, 46, 21, 21, 11, 19, 19]}, "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": "8a3ed3534697ef6b"}
vcr_f65e953e75f0
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": [31, 18, 2, 1, 37, 50, 30, 2, 46, 34, 11, 37, 24, 40, 27, 21, 48]}, "output": true}, {"input": {"nums": [3, 10, 22]}, "output": false}, {"input": {"nums": [8, 1, 12]}, "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": "df476f1ab72a50cf"}
vcr_915bfb468725
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": [-14, -2, 3, 24, 31, 47], "target": 29}, "output": [1, 4]}, {"input": {"nums": [-20, -12, -10, -9, -3, 5, 11, 14, 15, 18, 36], "target": 24}, "output": [1, 10]}, {"input": {"nums": [-18, -12, -6, -3, 18, 21, 28, 36, 38], "target": 18}, "output": [0, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "f3fad03b25a25ec4"}
vcr_8bacfc1f771b
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": [26, -4, 17, 4, 27, 7, -9, -8, 4, 6, 28, -14, -14, 23]}, "output": [26, 22, 39, 43, 70, 77, 68, 60, 64, 70, 98, 84, 70, 93]}, {"input": {"nums": [2, -7, 12, -3, -5, -1, -9, -7]}, "output": [2, -5, 7, 4, -1, -2, -11, -18]}, {"input": {"nums": [16, 15, 5, -8, -11]}, "output": [16, 31, 36, 28, 17]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "bdab230065fa7313"}
vcr_5ac8d496733c
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "baacabcbaab"}, "output": 9}, {"input": {"s": "abc"}, "output": 3}, {"input": {"s": "caac"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "81a15f9ce0e452c5"}
vcr_0c0e6554131e
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, 6, 7, 8, 9]}, "output": [3, 6, 7, 8, 9]}, {"input": {"nums": [1, 1, 2, 4, 5, 7, 10, 10]}, "output": [1, 2, 4, 5, 7, 10]}, {"input": {"nums": [4, 7, 8, 10, 10, 11, 11]}, "output": [4, 7, 8, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "b858527d571a61f1"}
vcr_1778c74c5f39
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": ["bca", "ac", "cb", "cc"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aab", "abb", "aab", "bab", "bab", "aba"]}, "output": [3, 3]}, {"input": {"strs": ["bab", "aaa", "baa", "aab", "bbb", "baa", "aaa"]}, "output": [1, 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": "36a4111a8c5566a2"}
vcr_f8d8f7fd9f5c
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": [50, 42, 4, 57, 58, 5, 29, 48, 46, 33, 10]}, "output": 54}, {"input": {"prices": [35, 21, 22, 34, 23, 31, 4, 8, 40, 36]}, "output": 36}, {"input": {"prices": [7, 25, 12, 1, 11, 2, 34, 36]}, "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": "ca5f5b9944b92163"}
vcr_787420fb18c9
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": [28, 16, 0, 18, 28, 0, 17, 13, 14]}, "output": 87}, {"input": {"nums": [11, 12, 13, 1, 14, 19]}, "output": 43}, {"input": {"nums": [9, 6, 10, 2, 7]}, "output": 26}]
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": "ac29b1af4e9088d4"}
vcr_a48a31eed3c4
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": [30, 6, 179, 183, 158, 57, 114, 79, 184, 164, 10, 78, 62, 161, 134, 59, 65]}, "output": false}, {"input": {"nums": [14, 11, 23, 23, 6]}, "output": true}, {"input": {"nums": [16, 17, 14, 18, 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": "8307466c0d45e0cc"}
vcr_b41717748e92
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, -21, 24, 13, 21, 30, -3, -6, 31, 19, -25, 5, 18]}, "output": [35, 14, 38, 51, 72, 102, 99, 93, 124, 143, 118, 123, 141]}, {"input": {"nums": [2, 16, 6, 9, 9, 11, -7, -5, -5, 2]}, "output": [2, 18, 24, 33, 42, 53, 46, 41, 36, 38]}, {"input": {"nums": [15, 6, 17, -1, -6, 0, 3, 18, 17, -7]}, "outp...
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": "a8847fb624d79a62"}
vcr_371ca550e0d8
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": [55, 34, 32, 28, 5]}, "output": 0}, {"input": {"prices": [13, 14, 2]}, "output": 1}, {"input": {"prices": [8, 36, 38, 33, 4, 10, 28, 16, 9, 19]}, "output": 30}]
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": "434c60b780652950"}
vcr_4605d8ea1b95
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": [-17, 8, 55, 44, -36, -13, 42, 39, 38], "target": 47}, "output": [1, 7]}, {"input": {"nums": [10, -16, 28, 15, -4, 33, 12, -20, -9, -28, 29], "target": 0}, "output": [2, 9]}, {"input": {"nums": [-1, 29, 32, 48, 42, 18, 3, 45, 9, 10, 44], "target": 87}, "output": [4, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "15b612bd4084fe74"}
vcr_e1ce8c9d4a6d
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, 7, 13, 14, 18, 27, 32, 34, 35, 43, 44], "target": 19}, "output": 7}, {"input": {"nums": [8, 13, 14, 23, 33, 35, 39], "target": 33}, "output": 4}, {"input": {"nums": [0, 1, 3, 5, 7, 9, 13, 20, 23, 24, 34, 35], "target": 21}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "601855820ac3b46f"}
vcr_660af79e51a7
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [7, 24, 71, 7, 14, 22, 38, 38, 14, 24, 22]}, "output": 71}, {"input": {"nums": [20, 4, 6, 25, 52, 6, 4, 20, 25]}, "output": 52}, {"input": {"nums": [12, 9, 12, 46, 22, 22, 9]}, "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": "75840f5f732717b4"}
vcr_39119895e118
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 2, 2, 3, 5, 7, 8, 8]}, "output": [0, 1, 2, 3, 5, 7, 8]}, {"input": {"nums": [3, 3, 4, 5, 6, 8, 8, 9, 11, 11, 11]}, "output": [3, 4, 5, 6, 8, 9, 11]}, {"input": {"nums": [1, 1, 2, 6, 7, 10, 12]}, "output": [1, 2, 6, 7, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "a7ad336e5f79128b"}
vcr_27857c591b01
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 2, 3, 3, 5, 6, 7, 9, 11, 14, 15, 15]}, "output": [0, 2, 3, 5, 6, 7, 9, 11, 14, 15]}, {"input": {"nums": [0, 0, 1, 5, 5, 7, 10, 11]}, "output": [0, 1, 5, 7, 10, 11]}, {"input": {"nums": [0, 1, 4, 6, 9, 10, 12]}, "output": [0, 1, 4, 6, 9, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "626804a1c9d96d31"}
vcr_2367958a8469
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-24, -21, -10, -6, -4, 9, 40, 41], "target": 3}, "output": [3, 5]}, {"input": {"nums": [-15, -14, -9, -4, 3, 17, 19, 29, 30, 38], "target": -13}, "output": [2, 3]}, {"input": {"nums": [-15, -13, -5, 11, 13, 16, 29], "target": 1}, "output": [0, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "dcbb0ea5d9c1adc2"}