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_10091b043d55 | 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": [48, 41, 21, 10, 47, 5, 48]}, "output": true}, {"input": {"nums": [17, 13, 4, 19]}, "output": false}, {"input": {"nums": [0, 10, 21]}, "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": "e8fe26d7e58de11e"} |
vcr_4edd7b56bed9 | 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": [29, 29, 17, 13, 23, 25, 0, 3, 11, 21, 8, 6, 39]}, "output": true}, {"input": {"nums": [17, 10, 10, 2, 15, 15, 22, 9, 14, 21, 17, 23]}, "output": true}, {"input": {"nums": [12, 12, 4, 4, 13, 0]}, "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": "b2461b8e2fb0ce2a"} |
vcr_b99d371bcb27 | 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, 5, 6, 6, 7, 7, 8, 9, 10]}, "output": [3, 5, 6, 7, 8, 9, 10]}, {"input": {"nums": [3, 6, 6, 7, 11, 11]}, "output": [3, 6, 7, 11]}, {"input": {"nums": [0, 3, 3, 7, 7, 9, 10, 10, 10, 11]}, "output": [0, 3, 7, 9, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "ef02ee7d29c8264b"} |
vcr_6e2d8d9b22ce | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "acbddaadadd"}, "output": false}, {"input": {"s": "bcbbbcaba"}, "output": false}, {"input": {"s": "bbcaaaba"}, "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": "207a8fb00c565ddd"} |
vcr_53a3ecca6fb0 | 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": [-28, -11, 27, 74, -4, 6, -7, 36, 68, -21, 13], "target": 74}, "output": [5, 8]}, {"input": {"nums": [-8, 29, -30, -27, 3, 1, 9, -24], "target": -35}, "output": [0, 3]}, {"input": {"nums": [6, 27, -16, 36, -2, 37], "target": 63}, "output": [1, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "94536f0fa885c957"} |
vcr_5b289dda8bf7 | 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": "ebae eccec bdbadd a"}, "output": "a bdbadd eccec ebae"}, {"input": {"s": "aa ab ab"}, "output": "ab ab aa"}, {"input": {"s": "ab ba"}, "output": "ba 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": "66d4247bf0be36ba"} |
vcr_fedff6649d41 | 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": [8, 9, 10, 12, 13]}, "output": [8, 9, 10, 12, 13]}, {"input": {"nums": [1, 7, 9]}, "output": [1, 7, 9]}, {"input": {"nums": [0, 2, 3, 4, 6, 7, 7, 10]}, "output": [0, 2, 3, 4, 6, 7, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "17890747bca30d36"} |
vcr_77d4a20fcad7 | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "a dd ee dccda ae eeca"}, "output": "eeca ae dccda ee dd a"}, {"input": {"s": "bba aab b"}, "output": "b aab bba"}, {"input": {"s": "bab aab baaa bbbb"}, "output": "bbbb baaa aab bab"}] | 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": "cfc30c92092bc507"} |
vcr_234302efd039 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-28, -20, -19, -3, 5, 28, 30, 34], "target": 2}, "output": [0, 6]}, {"input": {"nums": [-17, -8, 1, 7, 16, 38], "target": -1}, "output": [0, 4]}, {"input": {"nums": [-12, -6, 6, 34], "target": 40}, "output": [2, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "62526ef05173d1a3"} |
vcr_68894a74dc50 | 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": "e eeedbd abcd ed aaba cab bbbc"}, "output": "bbbc cab aaba ed abcd eeedbd e"}, {"input": {"s": "abab aabb aa abaa"}, "output": "abaa aa aabb abab"}, {"input": {"s": "bbbb aa"}, "output": "aa bbbb"}] | 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": "7887179b1d3f2cd5"} |
vcr_61a4ff648859 | 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": ["bcb", "abab", "acaa", "cc", "bcb", "caac"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bbb", "baa"]}, "output": [1, 1, 1]}, {"input": {"strs": ["aaa", "bab", "aba", "bbb", "bbb", "aab"]}, "output": [1, 1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "7b2e9508a7f35962"} |
vcr_07fa23c0fb6e | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 4, 3, 4, 2, 2, 0, 2, 0, 0]}, "output": false}, {"input": {"nums": [2, 1, 1, 1, 3, 2, 0, 0]}, "output": true}, {"input": {"nums": [2, 2, 3]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "c5fef3a7224c4a98"} |
vcr_50b316f39bb5 | 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": "ddcbcaddacbcdd"}, "output": true}, {"input": {"s": "aabcb"}, "output": false}, {"input": {"s": "cabaccaaa"}, "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": "35ace8a2e52c5866"} |
vcr_9131c8eb9526 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [15, 15, 0, 6, 32, -13, 25, -9, -1, -11]}, "output": [15, 30, 30, 36, 68, 55, 80, 71, 70, 59]}, {"input": {"nums": [-4, -11, 7, 5, -5, -12]}, "output": [-4, -15, -8, -3, -8, -20]}, {"input": {"nums": [10, 0, -2, 3, 3, 5, -8, -5, 15]}, "output": [10, 10, 8, 11, 14, 19, 11, 6, 21]}] | 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": "d13c4421b5a5887c"} |
vcr_541a38c62e94 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [1, -8, -11, -6, -19, -19, -12, 12, -14, 17], "k": 14}, "output": [-12, 12, -14, 17, 1, -8, -11, -6, -19, -19]}, {"input": {"nums": [-7, 3, 2], "k": 13}, "output": [2, -7, 3]}, {"input": {"nums": [-10, -3, 0, -1], "k": 6}, "output": [0, -1, -10, -3]}] | 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": "a97d9da6d18db733"} |
vcr_2d71d8ae4289 | 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, 7, 11, 14, 17, 22, 26, 27, 30, 34, 37, 42, 44], "target": 16}, "output": 4}, {"input": {"nums": [3, 5, 7, 12, 13, 20, 23, 32, 33], "target": 7}, "output": 2}, {"input": {"nums": [13, 14, 16, 18, 21, 23, 24, 25, 28, 29, 35], "target": 36}, "output": 11}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "5502ced20fb78f02"} |
vcr_8b135ac5146f | 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, 4, 12, 13, 23, 24, 41], "target": 35}, "output": 6}, {"input": {"nums": [1, 8, 10, 11, 20, 21, 23, 24, 31, 32, 38], "target": 33}, "output": 10}, {"input": {"nums": [4, 14, 17, 21, 31], "target": 14}, "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": "59cd63a7dea680e2"} |
vcr_7bdbf06cf829 | 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, 5, 6, 11, 12, 15, 22, 26, 31, 33, 36, 48], "target": 7}, "output": 4}, {"input": {"nums": [3, 12, 13, 15, 17, 19, 21, 24, 26, 35], "target": 3}, "output": 0}, {"input": {"nums": [20, 23, 27], "target": 32}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "d9756e22e49c6d93"} |
vcr_f76a66e20a40 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 12, 13, 15, 30, 35, 44, 47], "target": 16}, "output": 4}, {"input": {"nums": [10, 19, 22, 27], "target": 37}, "output": 4}, {"input": {"nums": [9, 11, 16, 17, 22, 26, 27, 31, 33, 38, 39], "target": 37}, "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": "6f9308da413863e6"} |
vcr_dcc5242e21e8 | 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": [5, 18, 24, 3]}, "output": 29}, {"input": {"nums": [10, 18, 4, 11, 10]}, "output": 29}, {"input": {"nums": [1, 12, 5, 6, 10, 8, 15, 18]}, "output": 44}] | 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": "cc3259e2a0223622"} |
vcr_401a120e316e | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [4, 3, 3, 1, 5, 1, 1]}, "output": [45, 60, 60, 180, 36, 180, 180]}, {"input": {"nums": [3, 5, 3, 5, 3]}, "output": [225, 135, 225, 135, 225]}, {"input": {"nums": [5, 5, 3, 4, 2, 3]}, "output": [360, 360, 600, 450, 900, 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": "515769e75ea2f5f9"} |
vcr_8a07ed948730 | 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, 1, 1, 2, 2, 4, 5, 6, 8, 9, 12, 15]}, "output": [0, 1, 2, 4, 5, 6, 8, 9, 12, 15]}, {"input": {"nums": [1, 2, 2, 3, 3, 4, 4, 6, 10, 11]}, "output": [1, 2, 3, 4, 6, 10, 11]}, {"input": {"nums": [0, 6, 8, 9, 9, 11, 11, 12, 12]}, "output": [0, 6, 8, 9, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "496e1d7c7aeba894"} |
vcr_bede5571d424 | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "a dcbbd dddda ddb abdda eacadc dbadbc"}, "output": "dbadbc eacadc abdda ddb dddda dcbbd a"}, {"input": {"s": "abba bbb"}, "output": "bbb abba"}, {"input": {"s": "baaa aab aba"}, "output": "aba aab 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": "4b1ef99a5d51ed3c"} |
vcr_0e27bd6df940 | 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": [-17, -16, -11, -6, -3, 37, 38, 45], "target": 26}, "output": [2, 5]}, {"input": {"nums": [-13, -10, -8, 10, 25, 33, 39], "target": 49}, "output": [3, 6]}, {"input": {"nums": [-16, -13, 22, 25, 30, 35, 39], "target": 52}, "output": [2, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "bcc20463020a9871"} |
vcr_afe8d643bc14 | 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": "aabbabbcbba"}, "output": 7}, {"input": {"s": "abbbb"}, "output": 2}, {"input": {"s": "ccaac"}, "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": "80333fa361d13f75"} |
vcr_a4b9b1124521 | 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": [3, -14, 0, 1, 0, 33]}, "output": [3, -11, -11, -10, -10, 23]}, {"input": {"nums": [-6, -1, 7, 13, 8, -7, 10, -9]}, "output": [-6, -7, 0, 13, 21, 14, 24, 15]}, {"input": {"nums": [-10, 5]}, "output": [-10, -5]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "53c963d7618c6a7a"} |
vcr_7a9c4dcd3ba7 | 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, 1, -2, -2, 4]}, "output": 12}, {"input": {"nums": [9, 10, -6, 9, 9, 1, 10, -12]}, "output": 42}, {"input": {"nums": [11, 8, -10, -7, 2, -9]}, "output": 19}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "acb460d4e255d17f"} |
vcr_77b71f9dcbe7 | 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": [27, 42, 37, 9, 14, 13, 19, 31, 11, 57, 50]}, "output": 48}, {"input": {"prices": [9, 2, 21, 1, 31, 22, 3, 33, 6, 3, 40, 31]}, "output": 39}, {"input": {"prices": [5, 28, 10, 35, 15, 1, 4, 23, 29]}, "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": "75d6a251a8533459"} |
vcr_a91079975fb2 | 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, 7, 0, 1, 7]}, "output": [0, 0, 196, 0, 0]}, {"input": {"nums": [5, 1, 2]}, "output": [2, 10, 5]}, {"input": {"nums": [5, 1, 2, 4]}, "output": [8, 40, 20, 10]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "22ee60874de7b3bf"} |
vcr_93ef7039cca8 | 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": [15, 36, 5, 44, 60, 30, 11, 24, 37, 30, 58]}, "output": 55}, {"input": {"prices": [30, 14, 39, 32, 21, 16, 4, 1, 22, 20]}, "output": 25}, {"input": {"prices": [8, 22, 11, 30]}, "output": 22}] | 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": "07b39e35bbfbd737"} |
vcr_5de55171785a | 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, 4, 4, 4, 4, 5, 6, 6, 7, 9, 10, 14]}, "output": [0, 4, 5, 6, 7, 9, 10, 14]}, {"input": {"nums": [1, 2, 2, 3, 5, 6, 6, 7, 8, 9, 12, 12]}, "output": [1, 2, 3, 5, 6, 7, 8, 9, 12]}, {"input": {"nums": [1, 2, 6, 9, 10]}, "output": [1, 2, 6, 9, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "ff4e5e6e9850c0b2"} |
vcr_c2050604a130 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [11, 16, 16, 24, 6, 24, 6, 72, 11]}, "output": 72}, {"input": {"nums": [42, 9, 9, 12, 18, 25, 25, 12, 18, 10, 17, 17, 10]}, "output": 42}, {"input": {"nums": [4, 24, 3, 44, 4, 3, 24]}, "output": 44}] | 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": "568e19b863acec5e"} |
vcr_a58efd542b2f | 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": 3072}, "output": 2}, {"input": {"n": 2158}, "output": 6}, {"input": {"n": 1257}, "output": 6}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "d7beec85bdebf031"} |
vcr_74b3b22f0bc0 | 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, 7, 7, 8, 9, 10, 12, 12, 12, 13, 13, 14, 14, 15]}, "output": [3, 7, 8, 9, 10, 12, 13, 14, 15]}, {"input": {"nums": [0, 1, 4, 7, 8, 9, 9, 10, 12]}, "output": [0, 1, 4, 7, 8, 9, 10, 12]}, {"input": {"nums": [6, 6, 8]}, "output": [6, 8]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "706f4e928eb85886"} |
vcr_dda67d92f9a8 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-13, 16, 8, 3, -14, -5], "k": 11}, "output": [16, 8, 3, -14, -5, -13]}, {"input": {"nums": [10, 4, -1, 9, -5, -8, -2, 4], "k": 2}, "output": [-2, 4, 10, 4, -1, 9, -5, -8]}, {"input": {"nums": [2, -8, -7, -5], "k": 7}, "output": [-8, -7, -5, 2]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "79bd05358bbf85ff"} |
vcr_e0dd07a5cf58 | 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, 3, 2, 2, 1, 3, 4, 0]}, "output": true}, {"input": {"nums": [0, 2, 1, 3, 0, 1, 3]}, "output": false}, {"input": {"nums": [2, 1, 2, 1, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "4de0b719123648fa"} |
vcr_ab334a4417cc | 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": [6, 18, 11, 0, 6, 0, 27, 13, 4, 24, 21]}, "output": 76}, {"input": {"nums": [8, 13, 6, 2]}, "output": 15}, {"input": {"nums": [10, 4, 17, 15, 18, 0, 14, 19, 19]}, "output": 78}] | 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": "59ef4dfadbad246e"} |
vcr_08a5251da638 | 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, 4, 1, 1, 2, 0, 0, 0]}, "output": false}, {"input": {"nums": [3, 0, 3, 1, 3, 0, 0, 0, 2]}, "output": false}, {"input": {"nums": [3, 1, 0, 1, 0, 2, 2, 3, 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": "3a794f4a4c7d1485"} |
vcr_ade2804715f6 | 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": ["cca", "aa", "caac", "ca", "bc", "aba", "ccab", "aba", "ac"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["bba", "baa", "aaa", "aab", "bab", "abb"]}, "output": [1, 2, 3]}, {"input": {"strs": ["abb", "abb", "aab", "aba", "abb", "aaa", "aab"]}, "output": [1, 3, 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": "672efc391aae2bbb"} |
vcr_c93cdcbab472 | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-33, -30, 22, 64, 77, -14, 34, 43, 79], "target": 34}, "output": [1, 3]}, {"input": {"nums": [43, -18, -19, 12, -29, 29, 18, -9], "target": -27}, "output": [1, 7]}, {"input": {"nums": [42, -16, -18, 46, 12, 23, 1, 18], "target": 24}, "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": "2b5f22d5cb3c0cc0"} |
vcr_f628030d0417 | 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, 1, 1, 7, 2, 4, 7, 6]}, "output": [2352, 7056, 7056, 1008, 3528, 1764, 1008, 1176]}, {"input": {"nums": [1, 5, 1, 2, 3, 3, 5]}, "output": [450, 90, 450, 225, 150, 150, 90]}, {"input": {"nums": [2, 2, 1]}, "output": [2, 2, 4]}] | 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": "3f9286adf46341a8"} |
vcr_968ed136fe35 | 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": [77, 34, 9, 13, 13, 36, 17, 9, 22, 23, 19, 34, 36, 23, 22, 19, 17]}, "output": 77}, {"input": {"nums": [27, 2, 11, 21, 3, 11, 12, 49, 27, 21, 2, 12, 3]}, "output": 49}, {"input": {"nums": [24, 3, 3, 24, 46]}, "output": 46}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "d30ececbf627f91b"} |
vcr_f4b4344c8a5a | 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, -23, -4, -3, 7, 10, 31, 43], "target": 50}, "output": [4, 7]}, {"input": {"nums": [8, 11, 19, 20, 27, 33, 36], "target": 47}, "output": [1, 6]}, {"input": {"nums": [-13, -3, 1, 6, 13, 16, 22, 32], "target": 38}, "output": [3, 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": "3c5db8fba9e71523"} |
vcr_38ca973a6ca6 | 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": [33, 34, 38, 42, 44], "target": 1}, "output": 0}, {"input": {"nums": [0, 3, 4, 15, 16, 19, 28, 29, 32, 33, 39], "target": 11}, "output": 3}, {"input": {"nums": [7, 16, 22], "target": 33}, "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": "ecb39a240c3e9e2a"} |
vcr_a41659330b9d | 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": [4, 35, 3, 38, 34, 2, 10, 2, 61, 10, 34, 4, 38, 3, 35]}, "output": 61}, {"input": {"nums": [29, 17, 17, 56, 29]}, "output": 56}, {"input": {"nums": [20, 20, 53, 21, 23, 21, 23]}, "output": 53}] | 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": "5a25b6c638696f2d"} |
vcr_97e8811e045c | 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": ["aa", "aa", "cbc", "bcb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bab", "aaa", "bab", "bbb", "aaa", "baa"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["bbb", "bbb", "bbb", "baa", "aaa"]}, "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": "c06be42aeb97dcb6"} |
vcr_3edc58ce3b38 | 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": "aacdba dde db"}, "output": "db dde aacdba"}, {"input": {"s": "bba a b ba"}, "output": "ba b a bba"}, {"input": {"s": "ab bbba abbb"}, "output": "abbb bbba 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": "c555501412938668"} |
vcr_d7244db290d3 | 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": [-16, 32, 29, 23, -21, 20, 6, 32, 8]}, "output": [-16, 16, 45, 68, 47, 67, 73, 105, 113]}, {"input": {"nums": [-2, 11, 9, 16, -8, 7, 12]}, "output": [-2, 9, 18, 34, 26, 33, 45]}, {"input": {"nums": [7, 18, 18, 13, -2, 6, 4, 14]}, "output": [7, 25, 43, 56, 54, 60, 64, 78]}] | 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": "024bce5dc0733018"} |
vcr_c0bd22f7f39a | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-28, 0, 22, 23, 28, 29, 31, 32, 40, 45], "target": 57}, "output": [4, 5]}, {"input": {"nums": [4, 14, 23, 28], "target": 32}, "output": [0, 3]}, {"input": {"nums": [-14, -4, 26, 30], "target": 22}, "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": "fd1a508906620e2a"} |
vcr_6b3a6c0f1ea1 | 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": [-2, 3, -5, 15, -5, -1], "k": 12}, "output": [-2, 3, -5, 15, -5, -1]}, {"input": {"nums": [10, -4, 7, -7], "k": 6}, "output": [7, -7, 10, -4]}, {"input": {"nums": [0, -5, -3, -4, 7, 10, -3], "k": 5}, "output": [-3, -4, 7, 10, -3, 0, -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": "b9dd688fb004c602"} |
vcr_08d0575bf6e6 | 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": [-23, -15, -9, 6, 14, 16, 27, 31, 40], "target": 22}, "output": [2, 7]}, {"input": {"nums": [-20, -14, -7, 5, 7, 15, 18, 21, 31, 33], "target": -15}, "output": [0, 3]}, {"input": {"nums": [-17, -8, -7, -1, 9, 17, 25, 37, 39], "target": 54}, "output": [5, 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": "c785b0e11c73b5bc"} |
vcr_0207eb74165c | 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, 13, 22, 24, 28, 29, 31, 33], "target": 33}, "output": 7}, {"input": {"nums": [5, 21, 24, 32, 34], "target": 23}, "output": 2}, {"input": {"nums": [1, 3, 5, 11, 15, 19, 22, 34, 35, 36], "target": 39}, "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": "c0980199694e058b"} |
vcr_6002bae83040 | 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": 76, "b": 132}, "output": 4}, {"input": {"a": 150, "b": 93}, "output": 3}, {"input": {"a": 83, "b": 136}, "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": "cc4ca9e7902ac954"} |
vcr_9b5de44e4b05 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 10, 19, 21, 26, 31, 34, 36, 40, 41, 42, 45, 46], "target": 2}, "output": 0}, {"input": {"nums": [5, 8, 13, 24, 31, 37], "target": 10}, "output": 2}, {"input": {"nums": [0, 24, 27], "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": "34bd7520d8c76c3b"} |
vcr_5df1a07ec33f | 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, 7, 30, 34, 30, 3, 23, 3, 27, 23, 79, 27, 8, 9, 9, 34, 8]}, "output": 79}, {"input": {"nums": [24, 22, 53, 24, 22]}, "output": 53}, {"input": {"nums": [29, 1, 25, 23, 25, 17, 19, 49, 23, 19, 17, 1, 29]}, "output": 49}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "64b46333ab9574c1"} |
vcr_304996ed2e26 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-8, -4, 12, 4, -11, -1, 15, 0, -5, 5, 11], "k": 2}, "output": [5, 11, -8, -4, 12, 4, -11, -1, 15, 0, -5]}, {"input": {"nums": [-8, -2, 8, 5, -5], "k": 1}, "output": [-5, -8, -2, 8, 5]}, {"input": {"nums": [2, -7, 8, 1, -8, -3], "k": 16}, "output": [8, 1, -8, -3, 2, -7]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "d79a90dd2d21b29b"} |
vcr_4f9df0ddc892 | 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": [2, 20, 11, 14, -6, 3, -12]}, "output": 47}, {"input": {"nums": [-10, -6, -10, 2, 5, 2, -3, 14, -12, 4, 12]}, "output": 24}, {"input": {"nums": [12, 14, -2, 6, 4, -7, -3, -9, -6, 11, -4]}, "output": 34}] | 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": "cea7cc877da9dd5a"} |
vcr_ce7255ea46b5 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-8, -10, 13, -10, 7]}, "output": 13}, {"input": {"nums": [10, -8, 4, -2, -11, -1]}, "output": 10}, {"input": {"nums": [-9, 10, -9, 10, 6, 13]}, "output": 30}] | 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": "d9261d0d26abe509"} |
vcr_09bf57df80b5 | 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": ["bbc", "cbc", "cccb", "cca", "ab", "bbab", "ca", "aca"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bab", "bbb", "bba", "aaa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "baa", "bbb"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "57f251dcdeb4a632"} |
vcr_664b956da637 | 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": [3, 53, 9, 46, 46, 7, 54]}, "output": 51}, {"input": {"prices": [20, 20, 8, 31, 22]}, "output": 23}, {"input": {"prices": [12, 11, 32]}, "output": 21}] | 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": "17533441cc0a6d13"} |
vcr_cb1c6e0f7538 | 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": ["aabb", "caba", "aacc", "aaa", "aa"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["baa", "aba", "baa", "baa", "bab", "bba", "baa"]}, "output": [2, 5]}, {"input": {"strs": ["bab", "baa", "abb", "bba"]}, "output": [1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "649fb3a852f7ccdc"} |
vcr_ece30ddaed5b | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "d eceec"}, "output": "eceec d"}, {"input": {"s": "bb bbab aa"}, "output": "aa bbab bb"}, {"input": {"s": "aba aa aa"}, "output": "aa aa aba"}] | 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": "1b80c7828d3c6949"} |
vcr_74a3cd7c7a95 | 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": [117, 42, 55, 185, 95, 77, 9, 66, 131, 197, 190, 38]}, "output": false}, {"input": {"nums": [20, 13, 24, 4, 16, 12, 3, 8, 5]}, "output": false}, {"input": {"nums": [6, 0, 11, 10, 22, 2]}, "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": "3b2f23bc6719008b"} |
vcr_dd623718a981 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [32, 63, 23, 9, 32, 9, 8, 15, 15, 23, 8, 11, 11]}, "output": 63}, {"input": {"nums": [28, 20, 20, 28, 29, 29, 45]}, "output": 45}, {"input": {"nums": [10, 10, 54, 25, 25, 3, 14, 14, 3]}, "output": 54}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "23743956ee90d1ab"} |
vcr_2296a5b1b4d0 | 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": [62, 59, -35, 47, -4, -15, 12, 3], "target": 8}, "output": [4, 6]}, {"input": {"nums": [6, 40, 36, -2, 49, 9], "target": 55}, "output": [0, 4]}, {"input": {"nums": [-29, -20, -22, -11, 36, 15, 40, 27, 30], "target": 14}, "output": [2, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "4b28611610638dfc"} |
vcr_0894a37972c6 | 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": "b decbd e caceda cad aadec cdbb"}, "output": "cdbb aadec cad caceda e decbd b"}, {"input": {"s": "ba bab a ba bbba"}, "output": "bbba ba a bab ba"}, {"input": {"s": "bbb a abab ba bbbb"}, "output": "bbbb ba abab a bbb"}] | 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": "6ccfa1e673049fa9"} |
vcr_74ad6c1bfd23 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [7, 28, 26, 9, 16]}, "output": 49}, {"input": {"nums": [14, 15, 0, 4, 12, 19]}, "output": 38}, {"input": {"nums": [6, 10, 0, 1]}, "output": 11}] | 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": "230e511836004243"} |
vcr_e16fb123a2ea | 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": [34, 30, 27, 37, 9, 10, 37, 34, 69, 27, 30, 10, 9]}, "output": 69}, {"input": {"nums": [16, 26, 2, 22, 16, 14, 59, 22, 26, 14, 2]}, "output": 59}, {"input": {"nums": [6, 57, 6, 21, 4, 21, 4]}, "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": "a9806b1e8ec08e21"} |
vcr_dd172dba48ed | 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": [30, 22, 12, 30, 25, 15]}, "output": 75}, {"input": {"nums": [4, 1, 1, 15, 17]}, "output": 22}, {"input": {"nums": [13, 9, 10, 14, 6, 9, 11, 11, 5, 15]}, "output": 62}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "a348a21212aa396d"} |
vcr_124f5a1201a2 | 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": [19, 28, 22, 33, 12, 19, 22, 28, 12, 33, 65, 13, 13]}, "output": 65}, {"input": {"nums": [24, 24, 18, 18, 48]}, "output": 48}, {"input": {"nums": [14, 26, 3, 28, 28, 14, 40, 13, 13, 3, 26, 20, 20]}, "output": 40}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "59ea28ed93998836"} |
vcr_824e8a0b464f | 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": [12, 39, 46, 13, 22, 18, 20, 13, 52, 47, 40, 49, 41]}, "output": 40}, {"input": {"prices": [20, 31, 8, 14, 39, 29, 11, 35, 35, 6, 1, 26]}, "output": 31}, {"input": {"prices": [4, 27, 40, 38]}, "output": 36}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "5cd95361b900b839"} |
vcr_d71548aac6e0 | 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, 7, 11, 17, 18, 20, 21, 30, 31, 35, 39, 44, 45, 47], "target": 23}, "output": 7}, {"input": {"nums": [14, 18, 34], "target": 31}, "output": 2}, {"input": {"nums": [1, 6, 7, 9, 10, 16, 21, 25, 28, 34, 38], "target": 30}, "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": "60e3d3691c406565"} |
vcr_5a3800bbcc22 | 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, 36, 3, 11, 24, 13, 27, 22, 3, 19, 31, 22, 49]}, "output": 46}, {"input": {"prices": [20, 5, 32, 33, 36, 27, 1, 1]}, "output": 31}, {"input": {"prices": [35, 16, 21, 3, 18, 27]}, "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": "24aa956e88bc8758"} |
vcr_2d9ddbe7e1dd | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-16, 7, -18, -36, 33, -34, 10, -33, -17, 31, 73], "target": -6}, "output": [0, 6]}, {"input": {"nums": [40, -26, 11, 14], "target": 25}, "output": [2, 3]}, {"input": {"nums": [13, -23, -29, -9, -14, 35, 36], "target": -23}, "output": [3, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "d1c27fd5e0e36738"} |
vcr_d1d1e5c8df75 | 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": [34, 51, 28, 36, 29, 59, 12, 30, 42, 15, 34, 47, 29]}, "output": 35}, {"input": {"prices": [37, 20, 4, 5, 40, 3, 27, 27, 21, 24, 25]}, "output": 36}, {"input": {"prices": [26, 34, 16, 7, 4, 12]}, "output": 8}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "267024f1859ee9d7"} |
vcr_bb1d87fce21a | 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": [41, 3, 50, 26, 34, 8, 1, 41, 3, 58, 44, 58, 56]}, "output": 57}, {"input": {"prices": [18, 17, 23, 37, 20, 25, 26]}, "output": 20}, {"input": {"prices": [28, 39, 6, 25, 12, 11, 11, 16, 16, 38, 17]}, "output": 32}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "104409e3667d1620"} |
vcr_1c5b7c322bfc | 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, 5, 1, 1, 4, 7]}, "output": [140, 28, 140, 140, 35, 20]}, {"input": {"nums": [3, 2, 2, 2]}, "output": [8, 12, 12, 12]}, {"input": {"nums": [3, 3, 1, 5, 5]}, "output": [75, 75, 225, 45, 45]}] | 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": "414a82f802748d3b"} |
vcr_fc87c04c0890 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 1, 0, 3, 0, 2, 3, 1, 0]}, "output": true}, {"input": {"nums": [0, 1, 1, 1, 2, 0, 1, 3, 1]}, "output": false}, {"input": {"nums": [3, 0, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "55cef86e532f3a9b"} |
vcr_b2a551c979a3 | 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": [18, -16, 19, 17, 22, 0, -12]}, "output": 60}, {"input": {"nums": [-7, 4, 7, 8, 3, 0, 7, 1, 7, -1, -4]}, "output": 37}, {"input": {"nums": [5, 13, 4, 8, -6, 1, -2]}, "output": 30}] | 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": "5a8b9056a332af4b"} |
vcr_d8bbe741b200 | 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": [6, 49, 50, 4, 26, 32, 21, 21, 27, 36, 17]}, "output": 44}, {"input": {"prices": [2, 4, 28, 36, 23, 31, 9, 30, 29, 19, 39, 23]}, "output": 37}, {"input": {"prices": [16, 1, 25]}, "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": "442d8a98dfaaf679"} |
vcr_f29fb4cf0fd3 | 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": 195, "b": 97}, "output": 1}, {"input": {"a": 157, "b": 60}, "output": 1}, {"input": {"a": 18, "b": 104}, "output": 2}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "db491b9cbee1763a"} |
vcr_68fd4c3b5bea | 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": [16, 18, 21, 24, 25, 31, 47], "target": 49}, "output": 7}, {"input": {"nums": [10, 13, 39], "target": 12}, "output": 1}, {"input": {"nums": [5, 6, 10, 12, 16, 26], "target": 25}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "144d30545e98822c"} |
vcr_f73ce2f8c7ec | 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": [19, 18, 19, -1], "k": 8}, "output": [19, 18, 19, -1]}, {"input": {"nums": [-9, 0, 5, -10, -3, 5, -6, 4, -7], "k": 3}, "output": [-6, 4, -7, -9, 0, 5, -10, -3, 5]}, {"input": {"nums": [-6, -7, -2], "k": 7}, "output": [-2, -6, -7]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "c8b6dc623de0fb9a"} |
vcr_1b26403693f4 | 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": [47, 9, 18, 12, 10, 32, 22, 0, 4, 18, 18, 18, 15, 13, 21, 0, 33]}, "output": true}, {"input": {"nums": [2, 2, 22]}, "output": true}, {"input": {"nums": [18, 18, 25, 20, 8]}, "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": "3d74d76726d1c08b"} |
vcr_cc6e1933aa36 | 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, 19, 38, 44, 49], "target": 4}, "output": 0}, {"input": {"nums": [5, 16, 19, 21, 25, 27, 29, 37], "target": 28}, "output": 6}, {"input": {"nums": [0, 2, 13, 14, 16, 22, 25, 36, 38, 39], "target": 35}, "output": 7}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "5eb48344a2b62a35"} |
vcr_58fe9fd4a493 | 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": [66, 42, 22, -14, 78, -26, 5, -40, 17], "target": 108}, "output": [0, 1]}, {"input": {"nums": [12, 47, -14, 23], "target": 35}, "output": [0, 3]}, {"input": {"nums": [4, -3, 18, 0, -19], "target": 18}, "output": [2, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "ee5ecf10f2f2bb85"} |
vcr_8adbaa266cf0 | 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": [58, -16, 9, 44, 21], "target": 79}, "output": [0, 4]}, {"input": {"nums": [11, 0, 14, -22], "target": -22}, "output": [1, 3]}, {"input": {"nums": [-15, 11, 37, 3, 20, 0, 4, -27, -12, -23, 40], "target": 17}, "output": [9, 10]}] | 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": "29cddfec9e60e00b"} |
vcr_4e4e4c8a0e6c | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 4, 1, 3, 1, 3, 0, 0]}, "output": false}, {"input": {"nums": [2, 0, 1, 1]}, "output": true}, {"input": {"nums": [1, 2, 3]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "85ddb0bda5704da3"} |
vcr_a019181dfa6b | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [0, 28, 4, 20]}, "output": 48}, {"input": {"nums": [12, 17, 5, 3, 20, 6, 6, 19, 13, 10]}, "output": 66}, {"input": {"nums": [14, 8, 2]}, "output": 16}] | 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": "335a62d7920a873d"} |
vcr_6828b3bd7e4d | 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": "cdd c acceb e debe abac bd"}, "output": "bd abac debe e acceb c cdd"}, {"input": {"s": "baaa aaaa bbba"}, "output": "bbba aaaa baaa"}, {"input": {"s": "bb a b a baa"}, "output": "baa a b a bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "02789950081fbbc3"} |
vcr_5f4e333ef59d | 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, -13, -2, 15, 26, 35, 40], "target": -39}, "output": [0, 1]}, {"input": {"nums": [-19, -15, -13, 7, 12, 14, 20, 23, 31, 32], "target": 54}, "output": [7, 8]}, {"input": {"nums": [-14, -12, 1, 9, 18, 32, 33, 35], "target": 19}, "output": [0, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "f871eb624af7ce3c"} |
vcr_058f9ac65c2a | 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, 9, 12, -22, 30, 10, -12, 4, -25, -24, -17]}, "output": [26, 35, 47, 25, 55, 65, 53, 57, 32, 8, -9]}, {"input": {"nums": [14, 17, 14]}, "output": [14, 31, 45]}, {"input": {"nums": [-5, 9, 15, 17]}, "output": [-5, 4, 19, 36]}] | 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": "4e0cb7ff05d2f9b4"} |
vcr_a13d71525391 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [19, 21, 6, 25, 13, 10, 26, 13, 29, 5]}, "output": 101}, {"input": {"nums": [20, 0, 9, 1, 5, 10]}, "output": 39}, {"input": {"nums": [9, 2, 18, 6]}, "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": "7143916592b3c608"} |
vcr_546cbd2cfaeb | 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": 135, "b": 161}, "output": 1}, {"input": {"a": 157, "b": 70}, "output": 1}, {"input": {"a": 86, "b": 76}, "output": 2}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "26820c92500fe438"} |
vcr_371be4d6cc79 | 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": [14, 28, 16, -21, 6, -15, 28, 25, -5, 3, 29]}, "output": [14, 42, 58, 37, 43, 28, 56, 81, 76, 79, 108]}, {"input": {"nums": [-8, 14, 3, 9, 13, 0, 6, -2, 10]}, "output": [-8, 6, 9, 18, 31, 31, 37, 35, 45]}, {"input": {"nums": [18, -5, 11, -10, -11, -9, 14, -11]}, "output": [18, 13, 24, 14, 3, -6, 8, ... | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "a8d8cee7b1a84c8c"} |
vcr_e13263d0b44e | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-9, -17, 12, -1, 16, -17]}, "output": 27}, {"input": {"nums": [5, -4, 10]}, "output": 11}, {"input": {"nums": [-12, 4, 4, -9, -4, -11, -2, 4]}, "output": 8}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "c1fb7d60a79af238"} |
vcr_e6de4b000435 | 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": [-14, -8, -1, 2, 16, -7, 10, 17, -11, -2, -20], "k": 18}, "output": [16, -7, 10, 17, -11, -2, -20, -14, -8, -1, 2]}, {"input": {"nums": [-8, -4, 5, 10, 1, 4, -3, -10, -4, -9], "k": 0}, "output": [-8, -4, 5, 10, 1, 4, -3, -10, -4, -9]}, {"input": {"nums": [8, 2, 9, -6, -5, 4, 1, -8, 1, 8], "k": 19}, ... | 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": "8e4992f36b7dada3"} |
vcr_9d537d764ba5 | 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": 92, "b": 110}, "output": 2}, {"input": {"a": 85, "b": 130}, "output": 5}, {"input": {"a": 53, "b": 169}, "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": "62d04bc144ba8138"} |
vcr_aac833935bdc | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [23, 31, 14, 13, 23]}, "output": true}, {"input": {"nums": [5, 20, 1, 3]}, "output": false}, {"input": {"nums": [20, 10, 0, 11, 21, 22, 11, 20, 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": "a03f35ef34067550"} |
vcr_25712f70eebd | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [32, 32, 1, 1, 23, 37, 37, 73, 23]}, "output": 73}, {"input": {"nums": [10, 11, 12, 11, 44, 8, 12, 8, 10]}, "output": 44}, {"input": {"nums": [3, 14, 28, 28, 14, 3, 55]}, "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": "cf52d2f30ca1b4c0"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.