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_2437f2f68e48 | 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": [30, 34, 27, -2]}, "output": [30, 64, 91, 89]}, {"input": {"nums": [13, 5, 6, -10, 9, 4, 17, 11]}, "output": [13, 18, 24, 14, 23, 27, 44, 55]}, {"input": {"nums": [13, -9, 2, 8, -5, -1, 15, -2]}, "output": [13, 4, 6, 14, 9, 8, 23, 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": "cd9a8443dc91fd40"} |
vcr_71ee46f70c7a | 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": "dbdaddbbaaacacab"}, "output": false}, {"input": {"s": "ab"}, "output": false}, {"input": {"s": "aabcacbb"}, "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": "309b0f3a726334e9"} |
vcr_1863eeb8d778 | 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": [23, 48, 74, -5, -1, 10, 25, 28, 50, 31], "target": 59}, "output": [7, 9]}, {"input": {"nums": [24, -25, -17, 5], "target": -1}, "output": [0, 1]}, {"input": {"nums": [40, -18, 17, -8, 12, -16, -5, 10], "target": -13}, "output": [3, 6]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "78e99bc6615b3983"} |
vcr_998661bfb555 | 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, 4, 0, 3, 1, 2, 1, 3, 0]}, "output": true}, {"input": {"nums": [3, 2, 1, 1, 3, 0, 2]}, "output": true}, {"input": {"nums": [0, 2, 0, 1]}, "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": "02163a716c178482"} |
vcr_b0f171430683 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [15, 26, 68, 17, 12, 17, 22, 26, 15, 12, 22]}, "output": 68}, {"input": {"nums": [2, 24, 14, 58, 24, 28, 2, 14, 16, 28, 16]}, "output": 58}, {"input": {"nums": [16, 4, 12, 10, 4, 14, 8, 10, 12, 50, 8, 14, 16]}, "output": 50}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "0a902d86d5f83798"} |
vcr_d96259fcec09 | 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": [1, 13, 29, 27, 27, 38, 30, 30, 1, 13, 38, 64, 29]}, "output": 64}, {"input": {"nums": [12, 7, 7, 29, 15, 15, 12, 29, 13, 13, 14, 14, 50]}, "output": 50}, {"input": {"nums": [6, 19, 6, 21, 15, 15, 21, 19, 54]}, "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": "4ecad26287565195"} |
vcr_e8418a7d59b5 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [7, 18, 24, 28, 31, 38, 40, 48], "target": 49}, "output": 8}, {"input": {"nums": [7, 8, 20, 21, 28, 35, 38], "target": 35}, "output": 5}, {"input": {"nums": [0, 2, 5, 6, 7, 9, 11, 14, 15, 30, 36], "target": 6}, "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": "fb416eaaba6b474a"} |
vcr_008b0acbcc59 | 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": [29, 3, 67, 39, 8], "target": 70}, "output": [1, 2]}, {"input": {"nums": [-24, 21, -27, 32, 20, -19, 43, -4], "target": 1}, "output": [4, 5]}, {"input": {"nums": [23, 39, 12, -14], "target": -2}, "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": "cb21c904d86fbb9c"} |
vcr_f1f1bcb03211 | 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": [51, 9, 18, 56, 29, 9]}, "output": 47}, {"input": {"prices": [27, 31, 13, 15, 29, 2, 9, 30, 20, 30, 34]}, "output": 32}, {"input": {"prices": [26, 17, 40]}, "output": 23}] | 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": "0b785a89fb4e3f53"} |
vcr_d493fbc09cd9 | 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, 14, 77, 2, 2, 21, 11, 37, 21, 37, 4, 25, 11, 25, 14]}, "output": 77}, {"input": {"nums": [16, 16, 28, 21, 22, 21, 12, 28, 18, 59, 12, 18, 22]}, "output": 59}, {"input": {"nums": [1, 54, 8, 10, 20, 8, 25, 1, 20, 10, 25]}, "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": "f60a6dd34cc8f825"} |
vcr_ad25eb37054a | 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": "babdbaccccabdbab"}, "output": true}, {"input": {"s": "cbccbca"}, "output": false}, {"input": {"s": "ca"}, "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": "799e19ef6333f496"} |
vcr_7bf7029241e8 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [16, 8, -17, -4, -4, 0, -13], "k": 2}, "output": [0, -13, 16, 8, -17, -4, -4]}, {"input": {"nums": [0, -10, 9, -8, -8, 5, -3, -3], "k": 18}, "output": [-3, -3, 0, -10, 9, -8, -8, 5]}, {"input": {"nums": [8, 10, 5, -8, -2, -10], "k": 20}, "output": [-2, -10, 8, 10, 5, -8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "3ddb326317e4b8ea"} |
vcr_106a746c07d4 | 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": [14, 18, 13, 10, 3]}, "output": 58}, {"input": {"nums": [2, -3, -5, 3, 13, 14, 12, -4, 3, -11, 0]}, "output": 42}, {"input": {"nums": [5, 12, 13, 10, -6, 11, 14, -9, -9]}, "output": 59}] | 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": "a7539bb2eefe67b1"} |
vcr_a080e6053c61 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [2, 5, 2, 5]}, "output": [50, 20, 50, 20]}, {"input": {"nums": [2, 1, 5]}, "output": [5, 10, 2]}, {"input": {"nums": [3, 4, 5, 3, 1]}, "output": [60, 45, 36, 60, 180]}] | 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": "6f28fafe2cf27199"} |
vcr_b23b48afc2dd | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["ba", "aab", "ac", "cac", "cbba", "bb"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bab", "abb", "aab"]}, "output": [1, 2]}, {"input": {"strs": ["aab", "aaa", "abb", "abb", "bab", "aba"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "3d614531cf0d4976"} |
vcr_3391876da71a | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-4, 5, 11, 1, -19, -18, 17, -8, -9, -15, -15], "k": 12}, "output": [-15, -4, 5, 11, 1, -19, -18, 17, -8, -9, -15]}, {"input": {"nums": [-7, -9, 5], "k": 0}, "output": [-7, -9, 5]}, {"input": {"nums": [6, -8, 7, 8, 3, 10], "k": 16}, "output": [7, 8, 3, 10, 6, -8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "0685dc48b4a5d0b5"} |
vcr_bdbf0d15f0aa | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 4, 7, 7, 8, 10, 10, 11, 11, 12, 12, 13, 14, 15]}, "output": [1, 4, 7, 8, 10, 11, 12, 13, 14, 15]}, {"input": {"nums": [2, 4, 4, 4, 5, 6, 6, 7, 12, 12]}, "output": [2, 4, 5, 6, 7, 12]}, {"input": {"nums": [2, 3, 3, 4]}, "output": [2, 3, 4]}] | 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": "94f6109eb407c44a"} |
vcr_e47bab6456a8 | 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": ["aaba", "ccaa", "cb", "bb", "bbbc", "ccba"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "abb", "aaa", "bba", "abb"]}, "output": [2, 3]}, {"input": {"strs": ["bba", "aaa", "aba"]}, "output": [1, 1, 1]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "6fefb9ae41105d9c"} |
vcr_1d1758dacc86 | 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, 9, 11, 12, 18, 19, 23, 24, 27, 29, 44, 49], "target": 42}, "output": 10}, {"input": {"nums": [1, 3, 8, 11, 21, 30], "target": 22}, "output": 5}, {"input": {"nums": [4, 9, 12, 21, 30, 35, 38], "target": 18}, "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": "3bdc99424f1463c4"} |
vcr_5184d0110cd6 | 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, -25, -15, -1, 10, 12, 19, 30, 31, 37, 41, 44], "target": -6}, "output": [1, 6]}, {"input": {"nums": [-5, 3, 8, 9, 10, 12, 13, 36], "target": 5}, "output": [0, 4]}, {"input": {"nums": [-12, -5, -3, 2, 12, 18, 23, 28, 29, 30], "target": 30}, "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": "39271f6a5bcc7084"} |
vcr_efad936915be | 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": 205, "b": 9}, "output": 1}, {"input": {"a": 158, "b": 155}, "output": 1}, {"input": {"a": 81, "b": 53}, "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": "8cc41becea459411"} |
vcr_af3bcd02c869 | 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, 5, 8, 10, 12, 12, 15]}, "output": [0, 5, 8, 10, 12, 15]}, {"input": {"nums": [2, 6, 7, 8, 10]}, "output": [2, 6, 7, 8, 10]}, {"input": {"nums": [0, 5, 6, 6, 6, 6, 8, 8, 8, 11, 11, 11]}, "output": [0, 5, 6, 8, 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": "c90bc43dc0ea5b98"} |
vcr_09878752b0f2 | 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": 126, "b": 245}, "output": 7}, {"input": {"a": 43, "b": 72}, "output": 1}, {"input": {"a": 81, "b": 12}, "output": 3}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "5529a03d9354a5b6"} |
vcr_6b75f6c1390d | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 3, 4, 6, 7, 10, 10, 11, 11, 13, 13, 15, 15]}, "output": [0, 3, 4, 6, 7, 10, 11, 13, 15]}, {"input": {"nums": [3, 3, 4, 12]}, "output": [3, 4, 12]}, {"input": {"nums": [4, 4, 9, 10]}, "output": [4, 9, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "49f2b9894e496365"} |
vcr_eba6a08f1c15 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [7, 5, -7, -16, -8, 18]}, "output": 18}, {"input": {"nums": [-11, 10, 9, -5, -4, 4]}, "output": 19}, {"input": {"nums": [9, 1, -10, -2, 7, 6, -1, -7, 0]}, "output": 13}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "a4c66b2125993a47"} |
vcr_f6ec2ab274a0 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 1, 2, 6, 7, 8, 9, 9, 10, 10, 11, 12, 12, 13]}, "output": [1, 2, 6, 7, 8, 9, 10, 11, 12, 13]}, {"input": {"nums": [2, 3, 6, 8, 10]}, "output": [2, 3, 6, 8, 10]}, {"input": {"nums": [2, 3, 3, 9]}, "output": [2, 3, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "1908308d2f228549"} |
vcr_c5a291d2743c | 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, -2, 35]}, "output": [15, 13, 48]}, {"input": {"nums": [-9, 12, 14]}, "output": [-9, 3, 17]}, {"input": {"nums": [11, -2, 4, -11, -8, 2, 7, -3, -4, -4]}, "output": [11, 9, 13, 2, -6, -4, 3, 0, -4, -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": "7852f3c205be8482"} |
vcr_b5ca37d55cdf | 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": [27, 47, 36, 6, 23, 4, 28, 25, 21, 8, 12, 33, 47, 13, 30, 21, 32, 23]}, "output": true}, {"input": {"nums": [12, 15, 5, 12, 0]}, "output": true}, {"input": {"nums": [10, 20, 16, 9]}, "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": "78af26360f27524b"} |
vcr_16d560ca6f1e | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [28, 33, 1, 32, 17, 39, 41, 26, 5, 13, 36]}, "output": 40}, {"input": {"prices": [31, 17, 20, 12, 39, 39, 12, 7, 28]}, "output": 27}, {"input": {"prices": [19, 1, 5, 11, 2, 2, 27, 16, 29, 14]}, "output": 28}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "2dba58d76e87187c"} |
vcr_7394b3e043f5 | 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, 3, 14, 23, 26, 29, 33, 40, 43, 47], "target": 28}, "output": 5}, {"input": {"nums": [9, 26, 33], "target": 22}, "output": 1}, {"input": {"nums": [1, 2, 29, 33], "target": 21}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "ce50902a8fd68005"} |
vcr_2f87f36e57ae | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [17, 21, 8, 15, 3, 27]}, "output": 63}, {"input": {"nums": [13, 12, 18, 19, 16, 20, 15, 13, 9, 6]}, "output": 71}, {"input": {"nums": [19, 3, 2, 2, 1, 15, 13]}, "output": 36}] | 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": "0123e932ea25edec"} |
vcr_e56888a8e1d2 | 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": [189, 3, 101, 157, 31, 61, 62, 22]}, "output": false}, {"input": {"nums": [2, 11, 23, 11, 19, 3, 10, 6]}, "output": true}, {"input": {"nums": [17, 6, 22, 16, 8, 10, 19]}, "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": "9325f8aa26045098"} |
vcr_82e33367e733 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [16, 3, 36, 3, 5, 5, 36, 54, 16]}, "output": 54}, {"input": {"nums": [27, 53, 27, 16, 12, 16, 12]}, "output": 53}, {"input": {"nums": [23, 11, 51, 5, 5, 23, 6, 11, 6]}, "output": 51}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "fbfa65dbd943856a"} |
vcr_33c5c5e381ea | 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": [3, 8, 41, 42, 49], "target": 52}, "output": [0, 4]}, {"input": {"nums": [-14, -13, -9, -8, -4, 7, 11, 27, 29, 30, 34], "target": 41}, "output": [5, 10]}, {"input": {"nums": [-13, -9, -2, 21, 27], "target": 19}, "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": "9d957662c49ecb38"} |
vcr_364b93d33559 | 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, 17, 20, 27, 38, 40, 41, 44], "target": 25}, "output": 3}, {"input": {"nums": [5, 25, 30, 38], "target": 38}, "output": 3}, {"input": {"nums": [2, 22, 28], "target": 25}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "d9eb17922dcc25b2"} |
vcr_90fc8aa9813f | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 3, 4, 5, 7, 8, 13, 14]}, "output": [0, 3, 4, 5, 7, 8, 13, 14]}, {"input": {"nums": [2, 2, 6, 7, 11, 11]}, "output": [2, 6, 7, 11]}, {"input": {"nums": [1, 1, 3, 4, 6, 7, 8, 9, 10, 11, 11]}, "output": [1, 3, 4, 6, 7, 8, 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": "9e7d91bdd9d24b17"} |
vcr_bf025448e607 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 5, 10, 11, 15]}, "output": [2, 5, 10, 11, 15]}, {"input": {"nums": [0, 0, 7, 9, 10, 10, 10]}, "output": [0, 7, 9, 10]}, {"input": {"nums": [1, 1, 2, 3, 6, 9, 12]}, "output": [1, 2, 3, 6, 9, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "b9e3a4864b5f546f"} |
vcr_a5dcb1042e1c | 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": [14, 26, 43, 36]}, "output": 29}, {"input": {"prices": [29, 40, 39, 9, 29, 18, 25, 28]}, "output": 20}, {"input": {"prices": [26, 17, 16, 18, 10, 7, 20]}, "output": 13}] | 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": "10b09bd2d4a665e7"} |
vcr_b1010069004e | 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": [15, 25, 26, 29, 38], "target": 2}, "output": 0}, {"input": {"nums": [0, 1, 3, 5, 17, 21, 23, 25, 35], "target": 6}, "output": 4}, {"input": {"nums": [3, 9, 12, 19, 20, 27, 30], "target": 11}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "8a1aa00f3ab3ed8e"} |
vcr_57c3987cdee0 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-21, -4, 8, 11, 33], "target": 29}, "output": [1, 4]}, {"input": {"nums": [-19, -9, 5, 14, 33, 34], "target": 24}, "output": [1, 4]}, {"input": {"nums": [-13, -11, 13, 15, 18, 33, 39], "target": 22}, "output": [1, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "85c874c511911153"} |
vcr_a556870f7f50 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [28, 27, 28, 13, 18]}, "output": 74}, {"input": {"nums": [3, 9, 5, 12, 0, 1, 0, 18]}, "output": 40}, {"input": {"nums": [8, 3]}, "output": 8}] | 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": "b1a7b3ab37c96af4"} |
vcr_1697b7dd638d | 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": [20, 11, 11, 13, 38, 39, 39, 13, 35, 38, 16, 67, 16, 20, 18, 35, 18]}, "output": 67}, {"input": {"nums": [9, 49, 21, 9, 21]}, "output": 49}, {"input": {"nums": [7, 6, 25, 6, 51, 25, 15, 15, 7]}, "output": 51}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "f7cadcd43198627f"} |
vcr_07eb8ec511b3 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bc", "abba", "cbba", "acaa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "aaa", "bbb", "baa", "aba"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["bbb", "baa", "bbb", "bab", "bbb", "aaa"]}, "output": [1, 1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "44afe1d93ad9f140"} |
vcr_d6f9ffcf54dd | 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": [52, 34, -33, 30, 58, 25, 10, 15, 62, 70], "target": 122}, "output": [0, 9]}, {"input": {"nums": [45, 49, 43, -16, -24, -4, 30, -5], "target": 29}, "output": [0, 3]}, {"input": {"nums": [45, -25, 26, 40], "target": 1}, "output": [1, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "387f04a4527889ae"} |
vcr_82b5c19ce719 | 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": "cdba edbdac dadbad e b"}, "output": "b e dadbad edbdac cdba"}, {"input": {"s": "b a abb ab"}, "output": "ab abb a b"}, {"input": {"s": "abaa bab"}, "output": "bab abaa"}] | 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": "ae0e06c1a68170ba"} |
vcr_26039ad77428 | 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, -13, 4, 21, 19]}, "output": 44}, {"input": {"nums": [-12, -6, 11]}, "output": 11}, {"input": {"nums": [9, 5, 7, 4, -2, -11]}, "output": 25}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "5a1a2ca9755dc154"} |
vcr_12ae616395b1 | 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": "acbbb bb eda d aa ecdb"}, "output": "ecdb aa d eda bb acbbb"}, {"input": {"s": "a a baab"}, "output": "baab a a"}, {"input": {"s": "aba baa bab ab bb"}, "output": "bb ab bab baa 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": "1426cb71105c605d"} |
vcr_30c2277fdc77 | 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, 1, 4, 6, 11, 14, 18, 31, 36, 38, 48], "target": 21}, "output": 7}, {"input": {"nums": [0, 1, 3, 14, 19, 23, 31, 34, 35, 36, 39], "target": 5}, "output": 3}, {"input": {"nums": [1, 2, 5, 13, 15, 24, 28, 29, 38, 39], "target": 15}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "7f2261bdacb68814"} |
vcr_4e45c7923c3f | 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, 5, 3, 6]}, "output": [450, 90, 90, 150, 75]}, {"input": {"nums": [1, 4, 4, 4, 2]}, "output": [128, 32, 32, 32, 64]}, {"input": {"nums": [2, 5, 4, 5]}, "output": [100, 40, 50, 40]}] | 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": "2f368c9136d4692f"} |
vcr_1c8d369f8913 | 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": [54, 24, 175, 180, 143, 113, 119, 88]}, "output": false}, {"input": {"nums": [9, 15, 22, 11, 15, 16, 11, 3, 9, 3]}, "output": true}, {"input": {"nums": [14, 6, 24, 15, 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": "11d4a0b69766b57f"} |
vcr_823dfa99c0e4 | 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": ["acab", "bb", "cb", "ccac", "aac", "aca", "ccac", "cc", "ab"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["bba", "aab", "aab", "bbb", "aba"]}, "output": [1, 1, 3]}, {"input": {"strs": ["bab", "bba", "abb", "bbb", "abb", "baa"]}, "output": [1, 1, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "31408a9f86d2805d"} |
vcr_8ae7f17c2ca4 | 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, 16, 0, -16, 11, 14]}, "output": 25}, {"input": {"nums": [6, 7, 11, 1, 1, -2, 2, 3]}, "output": 29}, {"input": {"nums": [-8, -11, -11, 13, 11, 3, 5]}, "output": 32}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "2db393a3c911e772"} |
vcr_ca8840dc710b | 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": [176, 107, 111, 71, 43, 161, 170, 11, 44, 45, 96, 83, 95, 130, 17, 99]}, "output": false}, {"input": {"nums": [15, 17, 25, 10, 15, 5, 16, 22, 18, 6]}, "output": true}, {"input": {"nums": [19, 3, 24, 21, 25, 14, 10, 11, 7, 5, 10, 14]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "a65f55ddf28e4abe"} |
vcr_01cf398cef12 | 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": "caacbbcaac"}, "output": true}, {"input": {"s": "bbbcbbcca"}, "output": false}, {"input": {"s": "bbaabbb"}, "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": "fdb302e66bd320a7"} |
vcr_bee8b3697f41 | 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": [-10, 7, -13, 4], "k": 11}, "output": [7, -13, 4, -10]}, {"input": {"nums": [-9, -7, -5, -2, 10, 7, 7, -8], "k": 7}, "output": [-7, -5, -2, 10, 7, 7, -8, -9]}, {"input": {"nums": [7, -9, 1, 2, -7], "k": 12}, "output": [2, -7, 7, -9, 1]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "d9237d5f10ac7ebc"} |
vcr_3465832a5a32 | 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": [-5, 77, 5, 53, 79], "target": 0}, "output": [0, 2]}, {"input": {"nums": [12, -25, 10, -20, 43, -3, -28, 6], "target": -28}, "output": [1, 5]}, {"input": {"nums": [39, 5, 42, -29, 32], "target": 37}, "output": [1, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "42378cee0e0840b6"} |
vcr_bdbbe8c4aaa5 | 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, 22, 31, 39, 40, 46, 49], "target": 48}, "output": 6}, {"input": {"nums": [1, 2, 7, 8, 16, 29, 31, 32, 37, 39], "target": 25}, "output": 5}, {"input": {"nums": [3, 7, 19, 28], "target": 10}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "7465f0906d3c6e58"} |
vcr_f4c6e247080a | 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": "baccdbdcddad"}, "output": false}, {"input": {"s": "ca"}, "output": false}, {"input": {"s": "cbaaaaccca"}, "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": "620ad3a5317587eb"} |
vcr_9929f4efe0e7 | 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": [6, -14, 4, -12, 14, 18, 15, 9]}, "output": 56}, {"input": {"nums": [9, 0, -5, 4, 7]}, "output": 15}, {"input": {"nums": [-11, -11, 1]}, "output": 1}] | 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": "de2e99541f84dbd4"} |
vcr_21d7f83929e5 | 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": "dadcd a"}, "output": "a dadcd"}, {"input": {"s": "bba bb aab bbb"}, "output": "bbb aab bb bba"}, {"input": {"s": "ab bbba ab a ba"}, "output": "ba a ab 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": "c08245b4f1f50a1a"} |
vcr_5b47c2405269 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 1, 3, 6, 6, 6]}, "output": [648, 4536, 1512, 756, 756, 756]}, {"input": {"nums": [4, 2, 2, 2]}, "output": [8, 16, 16, 16]}, {"input": {"nums": [5, 2, 4, 2, 3, 5]}, "output": [240, 600, 300, 600, 400, 240]}] | 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": "35a27cff5ff16ea0"} |
vcr_a429cf4d6a33 | 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": ["baba", "ab", "cba", "acb", "aba", "cbac", "bb"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bab", "aba", "baa", "baa", "abb", "aba"]}, "output": [3, 4]}, {"input": {"strs": ["aab", "baa", "baa"]}, "output": [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": "ae6d92cbb0269c1d"} |
vcr_f358594730db | 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": 41, "b": 147}, "output": 1}, {"input": {"a": 143, "b": 147}, "output": 1}, {"input": {"a": 23, "b": 133}, "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": "1db07bbbc04c5859"} |
vcr_9c371e5e73cc | 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": 38067}, "output": 8}, {"input": {"n": 5842}, "output": 7}, {"input": {"n": 6311}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "8c9f2b7c0613e4f3"} |
vcr_26b67ca3a096 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [11, 14, 30, 39, 49], "target": 39}, "output": 3}, {"input": {"nums": [18, 23, 30, 35, 37], "target": 22}, "output": 1}, {"input": {"nums": [11, 17, 18, 20, 22, 24, 30, 31, 32, 39], "target": 17}, "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": "13896e4341931140"} |
vcr_031cc3924ced | 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 dacbd ae"}, "output": "ae dacbd b"}, {"input": {"s": "b ab b"}, "output": "b ab b"}, {"input": {"s": "bbab ab abba b"}, "output": "b abba ab bbab"}] | 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": "e04556084530bb3a"} |
vcr_5eca64173d9f | 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": [44, 27, 49, 22, 19, 20, 4, 23, 17, 3, 9, 28, 47, 21, 2, 44]}, "output": true}, {"input": {"nums": [12, 1, 10, 0, 18, 20, 25, 23, 11]}, "output": false}, {"input": {"nums": [3, 23, 20, 17]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "5516e0edba32a271"} |
vcr_c50df8225a4b | 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": [12, -6, 3, 15, 8], "k": 1}, "output": [8, 12, -6, 3, 15]}, {"input": {"nums": [-6, 8, -6], "k": 11}, "output": [8, -6, -6]}, {"input": {"nums": [7, 2, 0, -7, 7, -1, 4, -10, 10], "k": 6}, "output": [-7, 7, -1, 4, -10, 10, 7, 2, 0]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "1fefeed6536044dd"} |
vcr_9f80e8f1d9db | 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": [-14, -4, 19, -4, 19, -16, -15, 12, -5, 16, 13, -18]}, "output": 39}, {"input": {"nums": [9, -2, -6, 10, 12]}, "output": 23}, {"input": {"nums": [9, 7, -6, -5, 1, 1, 4, 13]}, "output": 24}] | 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": "0e34d87e02228c2d"} |
vcr_7219dd7f44a3 | 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": ["acb", "ba", "aaa", "cbaa", "ca", "cba", "bcba", "ba"]}, "output": [1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["aab", "bab", "aba"]}, "output": [1, 2]}, {"input": {"strs": ["bba", "aab", "aaa", "aab"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "a6bc8398dcdde00b"} |
vcr_183efc249660 | 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": ["bb", "cb", "bcc", "aa", "bcc", "bcc"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["bba", "bab", "abb"]}, "output": [3]}, {"input": {"strs": ["bab", "abb", "bba", "bbb", "baa", "bba", "aba"]}, "output": [1, 2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "9a5bb76186754dc8"} |
vcr_7fb2a7f846d5 | 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, 11, 15, 25, 30, 32, 38], "target": 26}, "output": 5}, {"input": {"nums": [2, 4, 18, 36, 37], "target": 34}, "output": 3}, {"input": {"nums": [3, 9, 10, 12, 30, 33], "target": 31}, "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": "89f0fb572ea8300a"} |
vcr_3e563d083aa1 | 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, -9, 4, 10, 13, -1]}, "output": 27}, {"input": {"nums": [-12, -9, 5, -2, -3, -7]}, "output": 5}, {"input": {"nums": [13, -8, 5, 9, 11, 1, 14, 7, 3]}, "output": 55}] | 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": "5bc4e286597c664b"} |
vcr_c101ea616f48 | 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": "bcaabccacaabb"}, "output": 9}, {"input": {"s": "aaccccbbc"}, "output": 4}, {"input": {"s": "aaaa"}, "output": 1}] | 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": "f9014320136eec01"} |
vcr_a49c529cf701 | 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": [39, 41, 16, 3, 26, 27, 14, 25, 26, 20, 35, 33, 6, 40, 48]}, "output": true}, {"input": {"nums": [24, 20, 20, 20]}, "output": true}, {"input": {"nums": [3, 22, 19, 2, 17, 6, 17, 17]}, "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": "eeaee2b6de174d9e"} |
vcr_9cac08e406c5 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [9, -15, -2, 18, 13], "k": 14}, "output": [-15, -2, 18, 13, 9]}, {"input": {"nums": [-1, -4, 10, 7, 6], "k": 5}, "output": [-1, -4, 10, 7, 6]}, {"input": {"nums": [8, -5, 1, 2, -6, -3, -4], "k": 13}, "output": [-5, 1, 2, -6, -3, -4, 8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "76e596e52feb9bbd"} |
vcr_888c42ff7280 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 1, 4, 0, 4, 4, 0, 0, 0, 0]}, "output": true}, {"input": {"nums": [3, 3, 0, 2, 2]}, "output": true}, {"input": {"nums": [2, 2, 3, 0]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "ce306e032340b615"} |
vcr_4b8ba6cfc4a9 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [37, 20, 8, 0, 25, 30, 11, 48, 37]}, "output": true}, {"input": {"nums": [24, 10, 22, 25, 5, 0, 21, 8, 9, 10, 23, 4]}, "output": true}, {"input": {"nums": [5, 22, 23, 5, 21, 19, 11, 24, 19, 6, 16, 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": "99afb9640b9a4235"} |
vcr_dd696cf068f6 | 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": "adcbdbdaacb"}, "output": false}, {"input": {"s": "abbccaa"}, "output": false}, {"input": {"s": "cacc"}, "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": "b60873ecce6cb8de"} |
vcr_247aa505f116 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 1, 6, 4, 1, 6]}, "output": [144, 144, 24, 36, 144, 24]}, {"input": {"nums": [3, 2, 1, 5, 1, 5, 5]}, "output": [250, 375, 750, 150, 750, 150, 150]}, {"input": {"nums": [4, 3, 2, 4, 3, 3, 3]}, "output": [648, 864, 1296, 648, 864, 864, 864]}] | 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": "5fc3b69c9835b6bd"} |
vcr_ff857c24a1c0 | 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": [6, 18, -11, 5, -3, -19, -7], "k": 6}, "output": [18, -11, 5, -3, -19, -7, 6]}, {"input": {"nums": [6, 2, -3, 10, 3, -8, -10, -4, 1, -1], "k": 4}, "output": [-10, -4, 1, -1, 6, 2, -3, 10, 3, -8]}, {"input": {"nums": [3, 8, -5, 10, 9, -6, -7], "k": 0}, "output": [3, 8, -5, 10, 9, -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": "e9e37eed269eb3df"} |
vcr_d036bbd21325 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 1, 2, 3, 7, 7, 8, 8, 9, 9, 10]}, "output": [1, 2, 3, 7, 8, 9, 10]}, {"input": {"nums": [0, 5, 9, 11]}, "output": [0, 5, 9, 11]}, {"input": {"nums": [0, 0, 3, 3, 3, 4, 5, 8, 10, 12, 12]}, "output": [0, 3, 4, 5, 8, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "3c9b26d5b06df865"} |
vcr_7cdf82e10fb6 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [28, 23, 12, 55, 13, 13, 23, 22, 28, 12, 22]}, "output": 55}, {"input": {"nums": [15, 51, 15, 24, 24, 11, 11]}, "output": 51}, {"input": {"nums": [4, 52, 20, 20, 23, 23, 11, 4, 11]}, "output": 52}] | 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": "35dfd4a65c89eb34"} |
vcr_fb44868ed166 | 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, 4, 20, 6]}, "output": 39}, {"input": {"nums": [7, 20, 16, 12, 0, 6, 10, 3, 18, 16]}, "output": 60}, {"input": {"nums": [13, 3, 16, 19, 8]}, "output": 37}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "2391b6dcc6c6e015"} |
vcr_a6f12757b652 | 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": [10, 48, 20, 9, 11, 46, 10]}, "output": true}, {"input": {"nums": [14, 17, 23, 2, 25, 15, 24, 0, 22, 6]}, "output": false}, {"input": {"nums": [18, 19, 12]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "aba34f0c118ee7dc"} |
vcr_c6e773ab8f2b | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [27, 6, 6, 28, 8, 24, 9, 11, 7, 14, 4, 5]}, "output": 109}, {"input": {"nums": [8, 6, 6, 20, 2, 2, 10, 16]}, "output": 46}, {"input": {"nums": [19, 16, 5, 4, 14, 2, 2, 15, 10, 3]}, "output": 56}] | 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": "28bdcd990484c064"} |
vcr_5e946decfaed | 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": [-19, -13, -12, -10, -7, -5, 18, 20, 29, 35, 38], "target": 25}, "output": [1, 10]}, {"input": {"nums": [-15, 2, 5, 29, 30], "target": 35}, "output": [2, 4]}, {"input": {"nums": [-9, 9, 23, 32], "target": 0}, "output": [0, 1]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "aa82f3134cca2852"} |
vcr_90fb2f0dab48 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [9, 14, 16, 19, 29, 30, 32, 35, 38, 42, 43, 47, 49], "target": 22}, "output": 4}, {"input": {"nums": [0, 5, 6, 7, 28, 38], "target": 39}, "output": 6}, {"input": {"nums": [1, 6, 18, 37, 38, 39], "target": 15}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "180c292ac63d0fec"} |
vcr_df08f5938136 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-22, -8, -4, -2, 2, 5, 8, 25, 32, 33, 38, 39], "target": 71}, "output": [8, 11]}, {"input": {"nums": [-10, 9, 10, 19], "target": 28}, "output": [1, 3]}, {"input": {"nums": [-18, -8, 2, 3, 7, 10, 21, 27, 36, 37], "target": 46}, "output": [5, 8]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "24492eedb8618083"} |
vcr_2313bf80dcda | 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": [20, 6, 32, 59, 6]}, "output": 53}, {"input": {"prices": [21, 34, 30, 18, 39, 1, 29, 21]}, "output": 28}, {"input": {"prices": [27, 6, 38, 35, 34, 3, 39, 32, 30, 7, 39]}, "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": "28438337b66965bc"} |
vcr_0058fc57037a | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 2, 7, 9, 10, 13, 14, 14, 15]}, "output": [0, 1, 2, 7, 9, 10, 13, 14, 15]}, {"input": {"nums": [3, 7, 10]}, "output": [3, 7, 10]}, {"input": {"nums": [1, 3, 6, 8, 8, 9, 12]}, "output": [1, 3, 6, 8, 9, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "19851c5b11fb84c4"} |
vcr_0bdc725b745a | 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": ["aaab", "abab", "bc", "cbb", "bbc", "ac"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "bbb", "bba", "bab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aaa", "aaa", "bbb", "aab", "bba", "baa", "baa"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "1a3fdf83055210b7"} |
vcr_4f0340c61350 | 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": [13, 49, 23, -40, -22, 17, -12, -31, 19, 79], "target": 48}, "output": [7, 9]}, {"input": {"nums": [7, 45, 26, -12, 0, 34, 27, -13, -27], "target": -25}, "output": [3, 7]}, {"input": {"nums": [-13, 9, -9, 20, -4, 14, 19, 31], "target": 34}, "output": [3, 5]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "ae380a72f9300afd"} |
vcr_68ce6ae5e7b9 | 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, 23, 33, 47, 49], "target": 24}, "output": 2}, {"input": {"nums": [13, 14, 15, 28, 30, 38, 39], "target": 7}, "output": 0}, {"input": {"nums": [5, 12, 17, 25, 34], "target": 27}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "75440ff854a0331c"} |
vcr_1a0d92aa6855 | 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, 20, 15, 8]}, "output": 34}, {"input": {"nums": [0, 3, 7, 15, 1, 4, 15, 6]}, "output": 33}, {"input": {"nums": [6, 8, 7, 16, 5, 14, 19, 6, 10]}, "output": 53}] | 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": "9cc9f7073eab30a3"} |
vcr_2cbf5ae69b01 | 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": "ccbbbbacbbbca"}, "output": 7}, {"input": {"s": "ccbbaaacacbc"}, "output": 8}, {"input": {"s": "caacaacccbac"}, "output": 8}] | 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": "dbdac22b7e371353"} |
vcr_4dbee7e05e2c | 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": [172, 196, 45, 49, 23, 130, 156, 123, 77, 138, 173, 68, 42, 140, 170]}, "output": false}, {"input": {"nums": [4, 16, 8, 0, 25, 4, 16, 0, 7, 6]}, "output": true}, {"input": {"nums": [5, 9, 13, 16, 10, 13, 19, 15, 1, 4, 12]}, "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": "235dcb882c0aacb4"} |
vcr_662405c1276c | 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": "aabcdbbbbbbdcbaa"}, "output": true}, {"input": {"s": "acccba"}, "output": false}, {"input": {"s": "cbaac"}, "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": "bb035ecf7c833558"} |
vcr_56e03816d8fc | 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": [1, 5, 17, 1, 27]}, "output": 45}, {"input": {"nums": [14, 18, 18, 11, 4]}, "output": 36}, {"input": {"nums": [19, 15, 20, 20, 7]}, "output": 46}] | 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": "b5be07a65d0d8397"} |
vcr_069107fe8e70 | 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, -11, 29, 26, 23, 9, 0]}, "output": [3, -8, 21, 47, 70, 79, 79]}, {"input": {"nums": [-1, 11, -7, 2, 1, 0]}, "output": [-1, 10, 3, 5, 6, 6]}, {"input": {"nums": [-12, -10, 11, 3, -11, -4, -6]}, "output": [-12, -22, -11, -8, -19, -23, -29]}] | 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": "97b453107f36af26"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.