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_5fb651a8e630 | 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": ["cbaa", "cac", "cbbc", "cb", "ccbb", "abc"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "bab", "aab", "baa", "baa", "bba", "aab"]}, "output": [2, 5]}, {"input": {"strs": ["aab", "aab", "bbb", "aba", "bab"]}, "output": [1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "aef46936aaa65600"} |
vcr_30631d9ed6f8 | 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": [4, 46, 30, 50, 6, 49, 17, 46, 24, 47, 47, 19]}, "output": true}, {"input": {"nums": [7, 18, 10, 21, 20, 14, 19, 21, 11, 11, 10]}, "output": true}, {"input": {"nums": [23, 18, 6, 5, 4, 10, 17, 6, 16, 13, 23, 22]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "1028b04d2e31fd75"} |
vcr_6af1485cef29 | 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, 29, 9, 22, 24, 24, 11, 10, 15, 7, 13]}, "output": 103}, {"input": {"nums": [9, 18, 14, 3, 18]}, "output": 41}, {"input": {"nums": [19, 4, 16, 9]}, "output": 35}] | 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": "55afaba38555313f"} |
vcr_b5ed17a79ebf | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [16, 13, -9, -5, -16, 11, 21, -9, 1, -15, 3, 12, 0, 14]}, "output": 38}, {"input": {"nums": [-2, 10, -1, -6, 14, 12, 8, 4, 14, 11, 3]}, "output": 69}, {"input": {"nums": [10, -5, -11, 2, -3, 10, 11, -9, -4]}, "output": 21}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "33069622b1787af5"} |
vcr_a59ce5cbe926 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 10, 21, 28, 43], "target": 42}, "output": 4}, {"input": {"nums": [4, 10, 25, 29, 35], "target": 0}, "output": 0}, {"input": {"nums": [4, 11, 15, 17, 21, 27, 28, 32, 33, 34, 39], "target": 23}, "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": "42a7750e4295af95"} |
vcr_d58fef1e2f37 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [2, 16, 28, 10, 24, 18, 10, 23, 4, 6, 14, 4]}, "output": 91}, {"input": {"nums": [0, 19]}, "output": 19}, {"input": {"nums": [0, 4, 2, 1, 17, 20, 0, 17, 12, 16]}, "output": 58}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "8ac0d4b74ac73b43"} |
vcr_a11906b17269 | 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": [-21, 70, -36, -10, -30, 44, -15, -35, 3, 52], "target": 122}, "output": [1, 9]}, {"input": {"nums": [-17, 35, -5, 43, -25, 32], "target": 75}, "output": [3, 5]}, {"input": {"nums": [-19, 33, 9, -24, 29, 25], "target": -10}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "52da3affcd1fd126"} |
vcr_d663d96edff6 | 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": "caa acabda deddb deb dcc"}, "output": "dcc deb deddb acabda caa"}, {"input": {"s": "b aa baba"}, "output": "baba aa b"}, {"input": {"s": "a bb"}, "output": "bb a"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "156f6df1c3008862"} |
vcr_9b16e723c816 | 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": ["baab", "bb", "bb", "ca", "acac"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["bba", "aaa", "aaa", "bbb", "aaa", "aba", "bbb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["bab", "aab", "aba", "bab", "bbb"]}, "output": [1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "f5e3b016ee25a38c"} |
vcr_0b21343d3066 | 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": [7, 22, -1, 30, 33, 32, 6, -20, -24, -4, -15, -12, 9]}, "output": [7, 29, 28, 58, 91, 123, 129, 109, 85, 81, 66, 54, 63]}, {"input": {"nums": [-9, -3, 11, -8, 14]}, "output": [-9, -12, -1, -9, 5]}, {"input": {"nums": [-9, -10, 6, 15, 1, 9, -9, 17, -12]}, "output": [-9, -19, -13, 2, 3, 12, 3, 20, 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": "1159ade8bfbe377f"} |
vcr_65045fbb49ee | 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": [29, 26, 22, 22]}, "output": 51}, {"input": {"nums": [4, 0, 5, 16, 19, 6, 10, 9, 2, 7]}, "output": 45}, {"input": {"nums": [17, 8, 10, 16, 2, 14, 17, 11]}, "output": 58}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "4ea43a6ca7543a2b"} |
vcr_89b0289882b3 | 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": ["caac", "baaa", "cb", "ccba", "bbb", "acc", "ccab", "cab"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "aba", "bba", "bab", "baa"]}, "output": [1, 2, 2]}, {"input": {"strs": ["baa", "baa", "baa", "aba", "baa", "aaa"]}, "output": [1, 5]}] | 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": "321fb6bc146c50a6"} |
vcr_4e38f1f6ec47 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [14, 25, 5, 31, 9, 14, 10, 9, 25, 5, 31, 10, 38, 34, 34, 38, 74]}, "output": 74}, {"input": {"nums": [18, 9, 49, 10, 10, 18, 9]}, "output": 49}, {"input": {"nums": [18, 16, 16, 1, 18, 44, 1]}, "output": 44}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "207581986a7a43d7"} |
vcr_2cb048fa3574 | 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": [5, 6, 7, 9, 11, 11, 11, 14]}, "output": [5, 6, 7, 9, 11, 14]}, {"input": {"nums": [1, 3, 4, 7, 7, 8, 10, 10, 11]}, "output": [1, 3, 4, 7, 8, 10, 11]}, {"input": {"nums": [0, 5, 6, 8, 8, 12, 12]}, "output": [0, 5, 6, 8, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "a818df1eb62285b3"} |
vcr_ea5eaf195b15 | 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": [65, 22, 14, 13, 22, 13, 39, 39, 14]}, "output": 65}, {"input": {"nums": [17, 17, 16, 22, 22, 40, 16]}, "output": 40}, {"input": {"nums": [11, 7, 23, 7, 28, 23, 28, 59, 11]}, "output": 59}] | 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": "196ebdb3834826d1"} |
vcr_59e959a32a66 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 1, 3, 4, 4, 2, 0]}, "output": true}, {"input": {"nums": [0, 3, 2, 3, 0]}, "output": false}, {"input": {"nums": [3, 2, 1, 2, 1, 1, 1, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "783bcb5f5ed315d4"} |
vcr_2abdb8eda19d | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [14, 24, 28, 25, 25, 14, 28, 24, 26, 26, 69]}, "output": 69}, {"input": {"nums": [6, 9, 24, 18, 9, 6, 28, 28, 26, 24, 52, 26, 18]}, "output": 52}, {"input": {"nums": [18, 52, 21, 21, 1, 1, 18]}, "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": "2978fd485aeef07a"} |
vcr_95359e6cd8c2 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 2, 3, 10, 11, 14, 14]}, "output": [1, 2, 3, 10, 11, 14]}, {"input": {"nums": [0, 3, 5, 7, 8, 9, 10, 10, 12, 12]}, "output": [0, 3, 5, 7, 8, 9, 10, 12]}, {"input": {"nums": [0, 2, 5, 10, 11, 12, 12, 12]}, "output": [0, 2, 5, 10, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "b26346c25fcf7f1a"} |
vcr_f98e086f8dcc | 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": "bacbbbabbcbb"}, "output": 8}, {"input": {"s": "bcbbb"}, "output": 3}, {"input": {"s": "acaaaacc"}, "output": 4}] | 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": "3582d7bdea27601d"} |
vcr_a68715e11e94 | 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": [45, 17, 31, 22, 42, 31, 8, 48, 39, 56]}, "output": 48}, {"input": {"prices": [19, 12, 30, 34, 13, 40, 1, 40, 20, 24, 5]}, "output": 39}, {"input": {"prices": [31, 15, 36, 32, 40, 13, 27, 25]}, "output": 25}] | 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": "bf104ce4f08440cd"} |
vcr_c4a2d99d5354 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 7, 0, 3, 3, 1]}, "output": [0, 0, 315, 0, 0, 0]}, {"input": {"nums": [2, 1, 3]}, "output": [3, 6, 2]}, {"input": {"nums": [1, 4, 1, 4]}, "output": [16, 4, 16, 4]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "6476ce80424832c2"} |
vcr_a8ab92f6a612 | 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", "cab", "bca", "bccb", "acc", "aaa", "ca", "ccb"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "aab", "bab"]}, "output": [1, 1, 1]}, {"input": {"strs": ["abb", "abb", "bab"]}, "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": "09b19033672b2a39"} |
vcr_d9f62cef3006 | 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": 64, "b": 43}, "output": 1}, {"input": {"a": 53, "b": 52}, "output": 1}, {"input": {"a": 108, "b": 142}, "output": 2}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "d50e9b0254e900b6"} |
vcr_6bcc58f01793 | 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, 4, 5, 5, 5, 8, 8, 13]}, "output": [1, 2, 3, 4, 5, 8, 13]}, {"input": {"nums": [2, 3, 5, 5, 12]}, "output": [2, 3, 5, 12]}, {"input": {"nums": [1, 2, 7, 11]}, "output": [1, 2, 7, 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": "a49131238833ae94"} |
vcr_a28ba674cb41 | 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": [8, 59, 55, 25, 2, 47, 25, 18, 54, 11, 6, 41, 57, 27]}, "output": 55}, {"input": {"prices": [35, 4, 12, 23, 13, 31, 16]}, "output": 27}, {"input": {"prices": [39, 13, 40, 32, 25, 16]}, "output": 27}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "84f3f5022e17ed70"} |
vcr_3c6c113705b8 | 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": [43, 12, 50, 36, 40, 31, 20, 31, 35, 11, 24, 21, 29, 47]}, "output": true}, {"input": {"nums": [12, 24, 9, 0, 6, 14, 3, 15, 5, 10, 24]}, "output": true}, {"input": {"nums": [13, 19, 2, 5, 19, 4]}, "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": "dbfe5b71ea9337da"} |
vcr_f738cda23b5b | 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": 15, "b": 184}, "output": 1}, {"input": {"a": 124, "b": 50}, "output": 2}, {"input": {"a": 116, "b": 151}, "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": "f9ae344c75894fe6"} |
vcr_19d5cc8872e9 | 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, 20, 25, 33, 35, 38, 40], "target": 12}, "output": 1}, {"input": {"nums": [2, 5, 13, 17, 20, 21, 24, 31], "target": 2}, "output": 0}, {"input": {"nums": [4, 6, 7, 9, 14, 19, 21, 22, 23, 28, 37], "target": 26}, "output": 9}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "154dac26502d9ddd"} |
vcr_1c93871a96e3 | 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": [14, 15, 18, 6, 27, 14, 0]}, "output": 59}, {"input": {"nums": [1, 3, 12, 12, 0, 7, 20, 20, 10]}, "output": 45}, {"input": {"nums": [12, 2, 0, 5, 6]}, "output": 18}] | 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": "84807f2c4ff1081c"} |
vcr_a45b1995ba8b | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 1, 5, 4, 3, 3, 3, 3]}, "output": [1620, 8100, 1620, 2025, 2700, 2700, 2700, 2700]}, {"input": {"nums": [5, 1, 4, 1]}, "output": [4, 20, 5, 20]}, {"input": {"nums": [5, 3, 3, 2, 3]}, "output": [54, 90, 90, 135, 90]}] | 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": "b432df097b9efb1e"} |
vcr_405256ceb1b2 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [18, 27, 14]}, "output": [18, 45, 59]}, {"input": {"nums": [-5, 16, -7]}, "output": [-5, 11, 4]}, {"input": {"nums": [-8, -12, -10, -11, 2, 4, -1, 2, -2, 8]}, "output": [-8, -20, -30, -41, -39, -35, -36, -34, -36, -28]}] | 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": "2cc74c7ccb224dbd"} |
vcr_ba9343929cc0 | 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": [43, 188, 77, 70, 139, 9, 85, 83, 116, 176, 25, 21, 30, 67, 27, 154, 122]}, "output": false}, {"input": {"nums": [12, 2, 5]}, "output": false}, {"input": {"nums": [14, 23, 20, 25, 13, 21, 15, 1, 8]}, "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": "6c7fb15144448f85"} |
vcr_0e998ab92c41 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-16, 22, -9, 11, 31, 34, -23, 27, 19, -14, 23, -14, 15, -23]}, "output": [-16, 6, -3, 8, 39, 73, 50, 77, 96, 82, 105, 91, 106, 83]}, {"input": {"nums": [9, -7, -6]}, "output": [9, 2, -4]}, {"input": {"nums": [12, 13, 10, 4, 18, -6, 16, 8]}, "output": [12, 25, 35, 39, 57, 51, 67, 75]}] | 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": "17dd096e61f125de"} |
vcr_da799f8c2208 | 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": 62073}, "output": 10}, {"input": {"n": 10679}, "output": 9}, {"input": {"n": 4373}, "output": 5}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "bb97d433bb25ffb3"} |
vcr_8c6d44855d33 | 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, 25, 31, 37, 40], "target": 30}, "output": 2}, {"input": {"nums": [0, 20, 21, 36], "target": 34}, "output": 3}, {"input": {"nums": [1, 3, 10, 16, 29, 33], "target": 38}, "output": 6}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "bcfed080c608c75d"} |
vcr_de3fd8a372e4 | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [40, 25, -1, 44, 26, 7, 64, -9], "target": 35}, "output": [3, 7]}, {"input": {"nums": [27, 5, -26, 48, 38], "target": 65}, "output": [0, 4]}, {"input": {"nums": [48, 27, 10, 25, 32, 12], "target": 37}, "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": "fa3b881c76523ffc"} |
vcr_f6c224ba6e36 | 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": "bbabacbbabca"}, "output": 10}, {"input": {"s": "ccaaabbb"}, "output": 3}, {"input": {"s": "bbbcb"}, "output": 3}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "ab525052647ab521"} |
vcr_4444e0f4f368 | 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": [53, 55, 24, 59, 31, 45, 50, 32]}, "output": 35}, {"input": {"prices": [10, 13, 39, 8, 35, 17, 32, 2, 13]}, "output": 29}, {"input": {"prices": [19, 36, 29, 26, 1, 19, 4, 37, 16, 3]}, "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": "71845f9ff5b73baa"} |
vcr_b32362fe36d7 | 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, 22, 27, 21, 0, 2, 6]}, "output": 52}, {"input": {"nums": [18, 0, 6, 5, 12, 7, 5, 7, 2]}, "output": 43}, {"input": {"nums": [15, 6, 7, 15, 19]}, "output": 41}] | 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": "c9e8776e8c36df04"} |
vcr_64efc291621d | 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": [23, 3, 10, 15, 9, 21, 27, 25]}, "output": 84}, {"input": {"nums": [16, 9, 19, 13, 6, 15, 13]}, "output": 54}, {"input": {"nums": [1, 13, 19, 4]}, "output": 20}] | 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": "ee4c9f727c2aaf7c"} |
vcr_90d54b5ecef9 | 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, 12]}, "output": [1, 2, 6, 7, 8, 12]}, {"input": {"nums": [4, 6, 11]}, "output": [4, 6, 11]}, {"input": {"nums": [6, 6, 10, 11]}, "output": [6, 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": "6e7df67108877958"} |
vcr_4a726d7c3435 | 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": 142, "b": 77}, "output": 1}, {"input": {"a": 46, "b": 137}, "output": 1}, {"input": {"a": 16, "b": 79}, "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": "c7f1355b58b15b33"} |
vcr_76ec9f96082d | 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": [12, 11, 0, 24, 23, 7, 24, 22, 8, 15, 9]}, "output": 80}, {"input": {"nums": [16, 14]}, "output": 16}, {"input": {"nums": [11, 18, 15, 18, 17, 7, 13, 4, 16, 10]}, "output": 72}] | 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": "aff223975f30d750"} |
vcr_c395862dee29 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 5, 16, 25, 28, 29, 38, 40, 41, 48], "target": 28}, "output": 4}, {"input": {"nums": [2, 6, 12, 18, 23, 25, 30, 32, 33], "target": 15}, "output": 3}, {"input": {"nums": [4, 7, 16, 20, 26, 28], "target": 0}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "fd7911f89f4eb7e2"} |
vcr_27151df3b296 | 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": "acbbbabbac"}, "output": 7}, {"input": {"s": "bbcacb"}, "output": 5}, {"input": {"s": "aabbbaac"}, "output": 4}] | 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": "dcbd5c8cc17d4869"} |
vcr_6974390c68a1 | 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, 5, 7, 7, 10, 10, 11, 12, 15, 15, 15]}, "output": [1, 2, 3, 5, 7, 10, 11, 12, 15]}, {"input": {"nums": [4, 5, 6, 8, 8, 9, 10, 10, 10, 12]}, "output": [4, 5, 6, 8, 9, 10, 12]}, {"input": {"nums": [10, 11, 11]}, "output": [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": "298574c75a1a0c2d"} |
vcr_7eccaaa35108 | 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, 4, 8, 11, 15, 15]}, "output": [0, 3, 4, 8, 11, 15]}, {"input": {"nums": [1, 1, 2, 3, 5, 7, 10, 11, 12, 12, 12, 12]}, "output": [1, 2, 3, 5, 7, 10, 11, 12]}, {"input": {"nums": [0, 0, 4, 11]}, "output": [0, 4, 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": "656ae14ea8b2b98a"} |
vcr_0ea5b6fd5dee | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 6, 14, 32, 47], "target": 23}, "output": 3}, {"input": {"nums": [3, 9, 10, 13, 24, 25, 29, 34], "target": 0}, "output": 0}, {"input": {"nums": [5, 9, 15, 27, 30, 32, 33, 36], "target": 22}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "77364e539867cc32"} |
vcr_7f6dad5fbf1a | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 8, 9, 10, 13, 14, 15, 23, 32, 33, 38, 43, 45, 49], "target": 36}, "output": 10}, {"input": {"nums": [3, 4, 8, 11, 15, 22], "target": 31}, "output": 6}, {"input": {"nums": [7, 10, 12, 13, 15, 19, 24, 31, 32, 34, 36, 37], "target": 33}, "output": 9}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "945a338c109eba7a"} |
vcr_e8fd31b8abe8 | 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, 4, -8, -9, 13, 6, -15, 1, -9, -11, 9, -10, 8, -11]}, "output": 19}, {"input": {"nums": [11, 13, -7, -12, -9, -6, -4, 12, -8, 2]}, "output": 24}, {"input": {"nums": [-10, -11, 8, 6, 10, -5, -11, -6, -5]}, "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": "6834f2111c9fecf8"} |
vcr_e8288accf385 | 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": ["cabb", "bcac", "bccb", "caa", "acab", "acc", "abcb"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bbb", "bab", "aba", "bbb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["abb", "bbb", "bba", "baa", "baa", "baa", "bbb"]}, "output": [2, 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": "048639b5b6f3c879"} |
vcr_d32cdfba0f3a | 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": 64965}, "output": 11}, {"input": {"n": 2779}, "output": 8}, {"input": {"n": 2968}, "output": 6}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "caf6c86cf53928ce"} |
vcr_680973f310de | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [56, 11, 68, 64, -33, 32], "target": 120}, "output": [0, 3]}, {"input": {"nums": [1, 30, 38, 49, -27, 29, 7, 35, -15, -24], "target": 11}, "output": [2, 4]}, {"input": {"nums": [-17, -22, -9, -27], "target": -44}, "output": [0, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "5a4c08e53e085b54"} |
vcr_580175c8f427 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [13, 10, 34, 40, 40, 33, 39, 29, 4, 30, 14, 24, 2]}, "output": 30}, {"input": {"prices": [18, 29, 17, 12, 31, 11, 13, 24, 26, 27, 14, 5]}, "output": 19}, {"input": {"prices": [32, 8, 31, 11, 4, 36]}, "output": 32}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "99dc8bdeb796496e"} |
vcr_e2c230f2db0f | 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": "caee b eeae"}, "output": "eeae b caee"}, {"input": {"s": "a aa ab"}, "output": "ab aa a"}, {"input": {"s": "ba bbaa baa bba baaa"}, "output": "baaa bba baa bbaa ba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "6c401f532a16a5f7"} |
vcr_bd6404f1fee0 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [7, 13, 35, 23, 46, 7, 2]}, "output": 39}, {"input": {"prices": [26, 19, 39, 38, 26, 4, 15]}, "output": 20}, {"input": {"prices": [3, 32, 22, 15, 6, 37, 17, 32, 34, 20, 6, 34]}, "output": 34}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "b105303112674edc"} |
vcr_c49c10057f6e | 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": [1, 27, 37, 39, 29, 7, 34]}, "output": 38}, {"input": {"prices": [11, 9, 21, 29, 5, 4, 31]}, "output": 27}, {"input": {"prices": [13, 11, 23, 6, 27, 18, 26, 31]}, "output": 25}] | 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": "83734ea5ce5d4eec"} |
vcr_0cd24d2bfa99 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [13, 28, 21, 27, 1, 19, 30, 12]}, "output": 86}, {"input": {"nums": [4, 7]}, "output": 7}, {"input": {"nums": [18, 18, 11, 20]}, "output": 38}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "7539185efc418c52"} |
vcr_81b95d573c3f | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-15, -16, 42, -2, 47, 21, -21, 5, 49, -37, 39], "target": -10}, "output": [0, 7]}, {"input": {"nums": [-4, -22, 40, 35, 16, -8, 28, 7, -18], "target": 8}, "output": [4, 5]}, {"input": {"nums": [21, 43, 14, -21, 40], "target": 83}, "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": "3465a7e594379c18"} |
vcr_73fcf25a4046 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 0, 2, 0, 1, 3, 3, 3, 0]}, "output": true}, {"input": {"nums": [3, 1, 3]}, "output": true}, {"input": {"nums": [3, 0, 2, 3, 0, 0, 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": "c3202cc7d0d1d456"} |
vcr_95b8b2d8e9ad | 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": "baabac acc acdbad dbaccc bdce"}, "output": "bdce dbaccc acdbad acc baabac"}, {"input": {"s": "a aaaa b ab aabb"}, "output": "aabb ab b aaaa a"}, {"input": {"s": "bb aaa b b"}, "output": "b b aaa bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "39bf0268fb45e56a"} |
vcr_7e993a8188d6 | 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, -1, 8, 35, 44, 47, 48], "target": 34}, "output": [1, 3]}, {"input": {"nums": [-12, -7, -5, -3, -1, 4, 13, 19, 34, 38], "target": 47}, "output": [6, 8]}, {"input": {"nums": [-10, -6, 3, 8, 31, 34, 38, 39], "target": 29}, "output": [0, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "b64b19346e97e665"} |
vcr_a73c69360fbd | 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": [79, 10, 11, 11, 10, 24, 24]}, "output": 79}, {"input": {"nums": [48, 7, 2, 10, 23, 29, 13, 7, 10, 23, 29, 2, 13]}, "output": 48}, {"input": {"nums": [25, 23, 42, 6, 25, 6, 23, 10, 10]}, "output": 42}] | 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": "a57a156ae2eaa536"} |
vcr_0e549e4758c5 | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [79, 34, 73, 56, 65, 63, -12], "target": 61}, "output": [2, 6]}, {"input": {"nums": [37, 18, -15, -8, -24, -29, 9, -25], "target": -54}, "output": [5, 7]}, {"input": {"nums": [-16, -19, 24, 23, -1, 28, -4, -8, -29], "target": 9}, "output": [1, 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": "ef0668a778163501"} |
vcr_daedb4f9b380 | 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": [10, 56, 17, 29, 13, 17, 12, 43, 53, 36, 31, 45]}, "output": 46}, {"input": {"prices": [16, 4, 26, 26, 8, 12]}, "output": 22}, {"input": {"prices": [21, 22, 6, 6, 4]}, "output": 1}] | 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": "73501bc1f63db286"} |
vcr_8cfc0de6048a | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-26, -15, -9, -5, -4, 3, 38, 41, 45], "target": 41}, "output": [4, 8]}, {"input": {"nums": [-20, 10, 28, 33, 35, 39], "target": 13}, "output": [0, 3]}, {"input": {"nums": [-1, 0, 1, 4, 13, 19, 21, 25], "target": 13}, "output": [1, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "e4e1ad6835bd8ff3"} |
vcr_0b1da8f4da67 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -27, -26, -19, -15, -8, -5, 5, 19, 27], "target": -23}, "output": [4, 5]}, {"input": {"nums": [-17, -2, 10, 32, 38], "target": 42}, "output": [2, 3]}, {"input": {"nums": [-20, -18, -7, 2, 3, 9, 13, 16], "target": -4}, "output": [0, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "37d5fa8dbc2e76ed"} |
vcr_3acbad29c4fa | 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, -20, -16, -12, -4, 0, 5, 13, 14, 34, 39, 41], "target": -3}, "output": [2, 7]}, {"input": {"nums": [-20, -11, -9, -5, -2, 12, 25, 28, 30], "target": -29}, "output": [0, 2]}, {"input": {"nums": [-10, -2, 0, 10, 19, 20, 21, 22, 39], "target": 41}, "output": [4, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "f32051a98f4d846b"} |
vcr_f3b1ca2806bd | 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, -12, -11, -4, 5, 8, 12, 15, 35, 38, 41, 46], "target": -38}, "output": [0, 1]}, {"input": {"nums": [-10, -9, -8, -2, 0, 2, 7, 12, 19, 20, 34], "target": 32}, "output": [3, 10]}, {"input": {"nums": [-11, -7, -6, 1, 2, 22, 24, 27], "target": 3}, "output": [3, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "725871169aecd4a4"} |
vcr_70be8e89cdcc | 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": 53, "b": 126}, "output": 1}, {"input": {"a": 111, "b": 86}, "output": 1}, {"input": {"a": 94, "b": 23}, "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": "22cfc3c5256b0023"} |
vcr_d956acdf1cf2 | 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": 19542}, "output": 7}, {"input": {"n": 4053}, "output": 9}, {"input": {"n": 6703}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "33a9586e3c0767d7"} |
vcr_f2f487d5c4d4 | 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, 14, -4, -16, -14, 16]}, "output": 21}, {"input": {"nums": [6, -12, -1, 11, 3, -4, 6, -10, 4, 9, 12]}, "output": 31}, {"input": {"nums": [7, -12, 14, 4, -4, -9]}, "output": 18}] | 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": "60435941ddcd6ef7"} |
vcr_300169467646 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -23, -19, -18, -17, -6, -4, 15, 35, 38, 40, 47], "target": 82}, "output": [8, 11]}, {"input": {"nums": [-7, -5, 10, 35], "target": 3}, "output": [0, 2]}, {"input": {"nums": [-13, 7, 30, 31, 35], "target": 61}, "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": "17ec814add8d16f3"} |
vcr_eb9f8a4f01a0 | 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": [34, -23, 54, -6, 36, -25, 48, -24, -26, -2, -9, 56, 65, 70], "target": -48}, "output": [1, 5]}, {"input": {"nums": [-21, 3, -25, -6, 19], "target": -6}, "output": [2, 4]}, {"input": {"nums": [-6, -17, 34, 45, 3, 19, -10, 38, -19, 11], "target": 45}, "output": [2, 9]}] | 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": "2ea948894d0ac0f8"} |
vcr_5e4ec13b9bf3 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [5, 13, 19, 24, 4, 30, 14, 26, 9, 20, 8]}, "output": 113}, {"input": {"nums": [18, 15, 18, 9, 6, 6, 4, 0]}, "output": 46}, {"input": {"nums": [8, 1, 3, 16, 18, 16, 0, 8, 0, 13]}, "output": 61}] | 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": "dfb2871bcf9d9ba2"} |
vcr_aeed8fc0bd30 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 1, 2, 4, 6, 9, 15]}, "output": [0, 1, 2, 4, 6, 9, 15]}, {"input": {"nums": [2, 5, 5, 5, 7, 9, 9, 9, 10, 12, 12]}, "output": [2, 5, 7, 9, 10, 12]}, {"input": {"nums": [0, 2, 2, 5, 8, 8, 8, 9, 9]}, "output": [0, 2, 5, 8, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "c30ea9d47acc9dc2"} |
vcr_bbfbeee56631 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 2, 4, 10, 11, 17, 24, 28, 35, 41], "target": 40}, "output": 9}, {"input": {"nums": [8, 9, 19, 24, 27, 38], "target": 38}, "output": 5}, {"input": {"nums": [4, 14, 17, 23, 26, 28, 36], "target": 13}, "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": "0b198c817da2b6f2"} |
vcr_e4cac90003fa | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [17, -12, -2]}, "output": [17, 5, 3]}, {"input": {"nums": [14, 14, -8, 17, 16, 3, 8, 1, 8, -1]}, "output": [14, 28, 20, 37, 53, 56, 64, 65, 73, 72]}, {"input": {"nums": [-4, 15]}, "output": [-4, 11]}] | 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": "0f6477793343099f"} |
vcr_7d3eddb602b0 | 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, -10, 0, 11, -8, -4]}, "output": 11}, {"input": {"nums": [7, 3, 2, -9, 14, 12, 2, -3, -7, 9, -4]}, "output": 31}, {"input": {"nums": [5, 3, 12, 3, -9]}, "output": 23}] | 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": "3d1ca25e099d38f5"} |
vcr_b680c333f5a0 | 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, 19, 11, 1, 4, 13, 20, 21, 28, 1, 3, 21]}, "output": 101}, {"input": {"nums": [15, 5, 15, 0]}, "output": 30}, {"input": {"nums": [3, 19, 8, 11]}, "output": 30}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "34f64f05a9198915"} |
vcr_b269d42470e8 | 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": "aadbadacb"}, "output": false}, {"input": {"s": "ab"}, "output": false}, {"input": {"s": "abcabbaa"}, "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": "21675a3452f79cad"} |
vcr_2543a6f0034e | 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, 7, 7, 10, 11, 12, 14, 14]}, "output": [2, 7, 10, 11, 12, 14]}, {"input": {"nums": [0, 1, 2, 2, 4, 5, 8, 12]}, "output": [0, 1, 2, 4, 5, 8, 12]}, {"input": {"nums": [0, 1, 1, 3, 4, 6, 11, 11]}, "output": [0, 1, 3, 4, 6, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "55bf2c0b94299ed6"} |
vcr_fa452e303fda | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [9, 12, 4, 17, 8]}, "output": 29}, {"input": {"nums": [10, 5]}, "output": 10}, {"input": {"nums": [16, 1]}, "output": 16}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "936620ff9216bc7d"} |
vcr_bf5d50752e76 | 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": ["cbca", "ab", "bb", "accc", "cacc", "ac", "ac", "abcc", "bbbb"]}, "output": [1, 1, 1, 2, 2, 2]}, {"input": {"strs": ["baa", "bba", "aba", "aaa", "aba"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aaa", "bab", "abb", "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": "b782cf8262735f46"} |
vcr_fb1118d57c01 | 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": "bddc e ebad"}, "output": "ebad e bddc"}, {"input": {"s": "bbb ba a bbaa baab"}, "output": "baab bbaa a ba bbb"}, {"input": {"s": "bbab ba"}, "output": "ba 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": "2edc182c4979e719"} |
vcr_af9583d69161 | 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": "baeda aabbc ba ae ddeab e"}, "output": "e ddeab ae ba aabbc baeda"}, {"input": {"s": "aaaa bb"}, "output": "bb aaaa"}, {"input": {"s": "aabb bbbb ab"}, "output": "ab bbbb aabb"}] | 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": "12c2eab8df4e10f6"} |
vcr_8ec4a5def90c | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [30, 16, 9, 5, 19, 27, 7]}, "output": 66}, {"input": {"nums": [5, 4, 13, 6, 14, 11]}, "output": 32}, {"input": {"nums": [9, 3, 9, 7, 9, 2, 16, 2, 9, 17]}, "output": 60}] | 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": "029870453b91a5d8"} |
vcr_79659f83c506 | 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": [57, -33, -11, 46, 56, 67, -19, -13], "target": 46}, "output": [0, 2]}, {"input": {"nums": [-18, 1, 33, -19, 2, 15, -8, 19, 42], "target": -17}, "output": [0, 1]}, {"input": {"nums": [-15, -27, -23, 18, 31, 5], "target": -38}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "403505ea914c56e6"} |
vcr_168a8e7bc4c5 | 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": [77, 6, 30, -19, 50, 60, 61, -1, -38, 72, -29, -12, 70, -37], "target": 120}, "output": [4, 12]}, {"input": {"nums": [-27, -5, -10, 45, 4, 41, -7, 0, 28], "target": 49}, "output": [3, 4]}, {"input": {"nums": [11, 5, 39, -10, -30, 2, 33], "target": 44}, "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": "a8e1ad15f7da808c"} |
vcr_faff18f91dd8 | 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": [11, 22, 6, 9, 19, 16, 7, 25, 6, 30, 6]}, "output": 102}, {"input": {"nums": [10, 18, 18, 17, 6, 12, 14, 1, 3]}, "output": 52}, {"input": {"nums": [17, 17, 16, 8, 18, 7, 3, 2, 7, 4]}, "output": 61}] | 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": "23b33e7767c2da1d"} |
vcr_071ad389852d | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-18, -15, 4, 10, 13, 20, 23, 24, 27, 31], "target": 58}, "output": [8, 9]}, {"input": {"nums": [-16, 12, 20, 22, 28, 34, 38], "target": 22}, "output": [0, 6]}, {"input": {"nums": [-20, -18, -9, -7, -1, 13, 17, 20, 27, 37, 39], "target": 64}, "output": [8, 9]}] | 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": "a3c20bb512fe9c90"} |
vcr_b94cc670b369 | 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": [15, 2, -3, -8, 4, -2, 4, -18, 18, -18, -3, 22, 1]}, "output": 23}, {"input": {"nums": [5, 6, -11]}, "output": 11}, {"input": {"nums": [-12, 1, 1, -2, 7, -12, 4, -11, 8, 8, 6]}, "output": 22}] | 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": "61f420285dfbb659"} |
vcr_6cec5b07add6 | 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": [-15, -18, -15, 20, -11, -15], "k": 10}, "output": [-15, 20, -11, -15, -15, -18]}, {"input": {"nums": [2, -10, 9], "k": 17}, "output": [-10, 9, 2]}, {"input": {"nums": [2, -3, 10, -5, -5, -9], "k": 4}, "output": [10, -5, -5, -9, 2, -3]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "e993c13d6560413c"} |
vcr_bc4e57928804 | 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": "abc cb ddba ddee"}, "output": "ddee ddba cb abc"}, {"input": {"s": "bbb aa"}, "output": "aa bbb"}, {"input": {"s": "abab ba baab bbba abb"}, "output": "abb bbba baab ba abab"}] | 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": "6963b7f2d146890b"} |
vcr_a6e98f304699 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [1, 1, 4, 4, 1, 1, 1, 3, 0]}, "output": true}, {"input": {"nums": [3, 0, 2, 1, 3, 2, 2, 2, 1]}, "output": true}, {"input": {"nums": [2, 1, 0, 3, 0, 2, 3, 2]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "ffe4207dd6b68d3b"} |
vcr_ca4604fb11a3 | 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": "ca bc"}, "output": "bc ca"}, {"input": {"s": "ab aab"}, "output": "aab ab"}, {"input": {"s": "ab bb bbab"}, "output": "bbab bb 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": "b0d7850e8b39c939"} |
vcr_45f0eaa33842 | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "caadabdd"}, "output": false}, {"input": {"s": "aacccc"}, "output": false}, {"input": {"s": "baac"}, "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": "2271d2998171705f"} |
vcr_2752851779c4 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [13, -8, 7, 10, -13], "k": 2}, "output": [10, -13, 13, -8, 7]}, {"input": {"nums": [2, 8, 6, -3, -8, -10], "k": 12}, "output": [2, 8, 6, -3, -8, -10]}, {"input": {"nums": [-8, 9, -1, 9, 0, 3, 8, 8, -7, -10], "k": 15}, "output": [3, 8, 8, -7, -10, -8, 9, -1, 9, 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": "9562b290fe67b936"} |
vcr_496c8cc68d5b | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [60, 30, 32, 11]}, "output": 2}, {"input": {"prices": [9, 10, 26, 33, 31, 32]}, "output": 24}, {"input": {"prices": [35, 1, 20, 32, 7]}, "output": 31}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "3e7b9b114ff4d834"} |
vcr_635c0f485e78 | 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": [1, -24, -21, 17, -9, 1, -24, 17]}, "output": [1, -23, -44, -27, -36, -35, -59, -42]}, {"input": {"nums": [8, 1, -5, -2, 11, 3, -10, -3]}, "output": [8, 9, 4, 2, 13, 16, 6, 3]}, {"input": {"nums": [3, 16, 13, -9, -10, 17, 5, 16, 11]}, "output": [3, 19, 32, 23, 13, 30, 35, 51, 62]}] | 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": "be1eaa34f03d2a2f"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.