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_c2d050f20204 | 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": ["aaaa", "cc", "aaac", "acca", "bbcc", "aacc", "cc"]}, "output": [1, 1, 1, 2, 2]}, {"input": {"strs": ["aab", "baa", "aab"]}, "output": [3]}, {"input": {"strs": ["bba", "aba", "bbb"]}, "output": [1, 1, 1]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "85140e5f94fb9c6c"} |
vcr_d703fa5b8293 | 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": 26307}, "output": 8}, {"input": {"n": 2375}, "output": 6}, {"input": {"n": 12976}, "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": "31f02eb942d6ce48"} |
vcr_ee407a53a48f | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 4, 2, 0, 0, 0, 0, 1, 1, 0]}, "output": false}, {"input": {"nums": [1, 1, 0, 2, 0, 1, 1]}, "output": false}, {"input": {"nums": [1, 2, 0, 0, 0, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "e2ae9fee62e76fdc"} |
vcr_9f235be46aea | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 5, 6, 6, 5, 2]}, "output": [1800, 360, 300, 300, 360, 900]}, {"input": {"nums": [3, 1, 1, 4, 4]}, "output": [16, 48, 48, 12, 12]}, {"input": {"nums": [3, 4, 3, 3, 3, 2, 4]}, "output": [864, 648, 864, 864, 864, 1296, 648]}] | 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": "f36ced62895484fe"} |
vcr_e992a61c06f1 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 3, 14, 15, 23, 24, 25, 42, 43, 44, 47], "target": 7}, "output": 2}, {"input": {"nums": [10, 20, 32, 36], "target": 22}, "output": 2}, {"input": {"nums": [0, 2, 17, 22, 37], "target": 33}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "890acce183248c4b"} |
vcr_c9aedcd2f958 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-10, -13, 0, 28, 35, -3]}, "output": [-10, -23, -23, 5, 40, 37]}, {"input": {"nums": [-8, -9, 8, 6, -1, 3, -2, -6]}, "output": [-8, -17, -9, -3, -4, -1, -3, -9]}, {"input": {"nums": [6, -11, 14, -2, 12]}, "output": [6, -5, 9, 7, 19]}] | 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": "90cde680ba7c1ea6"} |
vcr_624dbee67767 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 2, 1, 7]}, "output": [14, 49, 98, 14]}, {"input": {"nums": [2, 5, 2, 5, 1]}, "output": [50, 20, 50, 20, 100]}, {"input": {"nums": [3, 1, 2]}, "output": [2, 6, 3]}] | 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": "d5c3f8b0b9b913b9"} |
vcr_e7cea1242ce9 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 1, 2, 2, 0, 2, 0]}, "output": false}, {"input": {"nums": [3, 1, 2, 2]}, "output": true}, {"input": {"nums": [3, 3, 3, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "b7a3feac9f979529"} |
vcr_903155a168ae | 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": [-27, -39, 15, 56, -24, 74, 1, 46, 78], "target": 16}, "output": [2, 6]}, {"input": {"nums": [3, -4, -27, 9, 32, -13, -30], "target": -34}, "output": [1, 6]}, {"input": {"nums": [-18, 10, 27, 47], "target": 29}, "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": "d38698b98ea4729d"} |
vcr_195bf6a32001 | 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": "cb e cdebb daec aba e a"}, "output": "a e aba daec cdebb e cb"}, {"input": {"s": "bb bbaa aaba aaab baaa"}, "output": "baaa aaab aaba bbaa bb"}, {"input": {"s": "bbb abb b b aba"}, "output": "aba b b abb bbb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "69b25a2d3c00c993"} |
vcr_e9b2c74eaffd | 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, 2, 2, 4, 2, 0, 0]}, "output": true}, {"input": {"nums": [2, 2, 2, 0, 0, 2, 2, 2, 2]}, "output": false}, {"input": {"nums": [2, 0, 1, 1, 3, 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": "960ce96a75b3217b"} |
vcr_5ca69e13bd9a | 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": [4, 10, 11, 12, 13, 13, 14, 14, 14, 15, 15, 15]}, "output": [4, 10, 11, 12, 13, 14, 15]}, {"input": {"nums": [0, 5, 6, 6]}, "output": [0, 5, 6]}, {"input": {"nums": [1, 7, 11]}, "output": [1, 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": "91de59d104ec9cb0"} |
vcr_2a906df23354 | 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": [38, 13, 16, 77, 13, 17, 17, 38, 16]}, "output": 77}, {"input": {"nums": [3, 29, 49, 14, 6, 28, 28, 14, 6, 3, 29]}, "output": 49}, {"input": {"nums": [28, 29, 6, 6, 24, 28, 24, 26, 26, 29, 59]}, "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": "5af2e9e7dbfa2ed4"} |
vcr_f8307608e0b6 | 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": "abbaaabbabcbc"}, "output": 9}, {"input": {"s": "baaaba"}, "output": 4}, {"input": {"s": "bcabc"}, "output": 5}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "e4d214137e3338b2"} |
vcr_e19a90e8b157 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-11, 28, -19, -4, 8]}, "output": [-11, 17, -2, -6, 2]}, {"input": {"nums": [13, 9]}, "output": [13, 22]}, {"input": {"nums": [-9, 7, 2, -2, -9]}, "output": [-9, -2, 0, -2, -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": "8cdbe999fc4942b6"} |
vcr_71d715bbc7d2 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [48, 9, 35, 45, 6, 48, 16, 15, 11, 50, 26, 23, 17, 21, 49, 3, 31]}, "output": true}, {"input": {"nums": [23, 15, 12, 2, 10, 6, 3, 25, 11]}, "output": false}, {"input": {"nums": [1, 12, 7, 4, 12, 13, 9, 18]}, "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": "32c0d387a7f15fc3"} |
vcr_d664a50e4b6e | 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": [3, 3, 14, 20, 25, 27]}, "output": 50}, {"input": {"nums": [14, 8, 4, 1, 15, 16, 12, 20, 9]}, "output": 54}, {"input": {"nums": [20, 19]}, "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": "0e6e8983de3b5276"} |
vcr_958de2a581ae | 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": [44, 19, 35, 22, 27, 2]}, "output": 16}, {"input": {"prices": [37, 40, 6, 10]}, "output": 4}, {"input": {"prices": [32, 26, 22, 14, 15, 20, 28, 32, 39, 5, 40]}, "output": 35}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "cff743e76dd17dc2"} |
vcr_85b4c3aa0349 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 6, 9, 11, 13, 16, 23, 24, 32, 36, 45, 46, 48], "target": 15}, "output": 5}, {"input": {"nums": [0, 3, 13, 17, 18, 33], "target": 5}, "output": 2}, {"input": {"nums": [12, 22, 31], "target": 33}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "f3acb914b0f4c8ee"} |
vcr_cc3358405537 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-23, -12, 6, 0, 2], "target": -12}, "output": [1, 3]}, {"input": {"nums": [-17, 30, 43, -15, -24, 36, 1, -16, 24], "target": 8}, "output": [7, 8]}, {"input": {"nums": [-12, -15, 1, 33], "target": 21}, "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": "42945acaa934ded7"} |
vcr_b667a53cf51d | 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": [8, 15, 0, 23, 4, 20, 4, 13, 24, 30]}, "output": 101}, {"input": {"nums": [4, 3, 5, 5, 20, 5]}, "output": 29}, {"input": {"nums": [6, 2, 8, 18, 7, 19, 19, 11, 20]}, "output": 63}] | 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": "2261c2c7b65caac1"} |
vcr_5f6d25802b5c | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-28, -16, -13, -8, -1, 12], "target": -14}, "output": [2, 4]}, {"input": {"nums": [-19, -18, 0, 16, 19, 25, 27, 33, 34], "target": 52}, "output": [4, 7]}, {"input": {"nums": [1, 6, 18, 37], "target": 24}, "output": [1, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "ab5814d89a1c12b8"} |
vcr_73e08dc09634 | 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": "aca dbab"}, "output": "dbab aca"}, {"input": {"s": "ab a a"}, "output": "a a ab"}, {"input": {"s": "a baa a b ba"}, "output": "ba b a baa 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": "7bf43dc80f137db5"} |
vcr_78352b4ab5b9 | 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": 199, "b": 217}, "output": 1}, {"input": {"a": 116, "b": 134}, "output": 2}, {"input": {"a": 157, "b": 26}, "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": "10fb578ea57a2191"} |
vcr_3c416217f198 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [7, 15, 25, 27, 27, 52, 15, 7, 25]}, "output": 52}, {"input": {"nums": [24, 46, 18, 25, 25, 24, 5, 5, 21, 21, 18]}, "output": 46}, {"input": {"nums": [16, 16, 20, 27, 9, 20, 6, 6, 42, 27, 9]}, "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": "ef5d2b7d3241e8e0"} |
vcr_0bf79dd9d148 | 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": "ddaadd cec d dabce bceebe"}, "output": "bceebe dabce d cec ddaadd"}, {"input": {"s": "ab aa b b"}, "output": "b b aa ab"}, {"input": {"s": "b bb bb bba ba"}, "output": "ba bba bb bb b"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "c278c07f92a49064"} |
vcr_09adbd0f437b | 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, 7, 46, 32, 46, 58]}, "output": 51}, {"input": {"prices": [27, 38, 15, 28]}, "output": 13}, {"input": {"prices": [4, 20, 34, 5, 11, 10, 10, 22, 21]}, "output": 30}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "d926b389449f0809"} |
vcr_930b58d8a8e7 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-28, -23, -15, 13, 15, 49], "target": -15}, "output": [0, 3]}, {"input": {"nums": [-16, 13, 15, 23, 24, 28, 33], "target": 38}, "output": [2, 3]}, {"input": {"nums": [-10, -2, 1, 9], "target": 10}, "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": "27a5c19a696d8102"} |
vcr_c654f1cef8c9 | 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": "dbab dda"}, "output": "dda dbab"}, {"input": {"s": "aa bab b abaa"}, "output": "abaa b bab aa"}, {"input": {"s": "a aabb abaa"}, "output": "abaa aabb 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": "0a939f43331eda89"} |
vcr_c5fa1bf20c68 | 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": [6, 19, 27, 29, 39], "target": 30}, "output": 4}, {"input": {"nums": [1, 2, 12, 25, 26, 27, 28, 30, 32, 36], "target": 9}, "output": 2}, {"input": {"nums": [3, 13, 18, 20, 28, 29, 32, 34], "target": 31}, "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": "424c605a0e954aa1"} |
vcr_fe3e5f2ab29a | 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, 0, 10, 10, 10, 13, 13, 13]}, "output": [0, 10, 13]}, {"input": {"nums": [1, 1, 1, 2, 4, 5, 7]}, "output": [1, 2, 4, 5, 7]}, {"input": {"nums": [3, 4, 6]}, "output": [3, 4, 6]}] | 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": "ad4c66e34ce8e865"} |
vcr_7031fedca1ea | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 5, 5, 5, 6, 8, 9, 11, 11, 14, 15]}, "output": [0, 1, 5, 6, 8, 9, 11, 14, 15]}, {"input": {"nums": [4, 4, 5, 6, 11]}, "output": [4, 5, 6, 11]}, {"input": {"nums": [0, 4, 5, 10]}, "output": [0, 4, 5, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "b755918db52b5770"} |
vcr_5740997ee456 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [18, 23, 24, 8]}, "output": 42}, {"input": {"nums": [3, 6, 13, 11, 17, 13, 16]}, "output": 49}, {"input": {"nums": [17, 0, 8, 11, 1, 14, 2, 12, 16, 5]}, "output": 59}] | 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": "11be7110fbefec42"} |
vcr_206a6c20aa1b | 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, 6, 7, 8, 26, 41, 44, 45, 47], "target": 21}, "output": 4}, {"input": {"nums": [9, 12, 16, 17, 25, 35], "target": 33}, "output": 5}, {"input": {"nums": [0, 3, 4, 12, 14, 22, 26, 27, 29, 36, 38], "target": 38}, "output": 10}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "715cb505be83a1a4"} |
vcr_9eb7aa6add10 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bcb", "ccbb", "baac", "ac", "ac", "caa"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "baa", "bbb", "baa", "aab", "aab", "aab"]}, "output": [1, 1, 5]}, {"input": {"strs": ["bbb", "bab", "bba"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "cb8a6c54fa11f523"} |
vcr_f573f00f9528 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 4, 5, 10, 15, 17, 36, 37, 42], "target": 12}, "output": 4}, {"input": {"nums": [4, 6, 13, 20, 21, 29], "target": 1}, "output": 0}, {"input": {"nums": [0, 9, 11, 13, 15, 16, 21, 24, 26, 39], "target": 8}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "4454e45031f733d1"} |
vcr_5b71a83d6639 | 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": [45, 90, 155, 104, 65]}, "output": false}, {"input": {"nums": [15, 13, 7, 20, 1, 14, 0, 12, 9, 12, 23]}, "output": true}, {"input": {"nums": [7, 25, 0, 11, 6, 19]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "8374afc8390d4f1a"} |
vcr_681ac9857fdb | 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": "adbcbdbbdbcbda"}, "output": true}, {"input": {"s": "cacaabaa"}, "output": false}, {"input": {"s": "aabb"}, "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": "e50c1a638218c0da"} |
vcr_7e7b301f911b | 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, 22, 4, -9, 4, -5, 22, 6, -7]}, "output": 44}, {"input": {"nums": [-6, 14, 14, 4, -4, -2]}, "output": 32}, {"input": {"nums": [9, -6, 5]}, "output": 9}] | 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": "30c1e22293b18367"} |
vcr_bdfa4c99cddb | 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, 14, 20, 29, 24, 24]}, "output": 86}, {"input": {"nums": [14, 18, 6, 5, 20, 18, 11, 1, 11]}, "output": 62}, {"input": {"nums": [6, 17]}, "output": 17}] | 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": "32b2917e23b05d27"} |
vcr_6266413abfdf | 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": [35, 36, 32, 20, 27, 45, 53, 23, 57, 40, 3, 13]}, "output": 37}, {"input": {"prices": [19, 20, 29, 35, 7, 22, 5]}, "output": 16}, {"input": {"prices": [7, 4, 10, 22, 34, 12, 11, 11, 2, 23, 32]}, "output": 30}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "cfd35fce85fef84e"} |
vcr_fc8d65f4ed97 | 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": [33, 3, 14, 3, 21, 21, 8, 33, 8, 55, 14]}, "output": 55}, {"input": {"nums": [19, 19, 18, 11, 11, 18, 21, 57, 21]}, "output": 57}, {"input": {"nums": [54, 5, 10, 10, 5, 1, 1]}, "output": 54}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "5335ff9998725dd1"} |
vcr_b6d7042ca711 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-28, -26, -21, -20, -9, -5, 12, 13, 20, 28, 30], "target": -7}, "output": [3, 7]}, {"input": {"nums": [-18, -12, -9, -3, 1, 7, 10, 16, 23, 26, 30], "target": -15}, "output": [1, 3]}, {"input": {"nums": [-16, -12, -7, 12, 15, 18, 20, 21, 22, 28, 38], "target": 10}, "output": [1, 8]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "557222a851aebe52"} |
vcr_5c326d8387d7 | 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": ["cbbb", "cb", "caba", "bccc", "ca", "cab", "aab", "bac"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "aaa", "aab", "aaa", "aba", "abb"]}, "output": [1, 2, 3]}, {"input": {"strs": ["bbb", "bab", "aaa", "abb", "bba"]}, "output": [1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "24aebdd16801b86f"} |
vcr_3d5f7d2152b7 | 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": [159, 127, 73, 145, 164, 121, 28, 34, 29, 68, 116, 105, 21, 5, 39, 183, 154, 22]}, "output": false}, {"input": {"nums": [21, 0, 22, 8]}, "output": false}, {"input": {"nums": [0, 18, 17, 11, 22, 3, 7, 3, 20, 7, 23]}, "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": "4623afe738401050"} |
vcr_04d4c6b7bc06 | 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": "aea acd aaba d dedcd ebdd"}, "output": "ebdd dedcd d aaba acd aea"}, {"input": {"s": "baaa b abbb"}, "output": "abbb b baaa"}, {"input": {"s": "babb a"}, "output": "a babb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "80c49ec8c00e37bc"} |
vcr_5e0209d6b5a1 | 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": [6, -25, -24, 16, 11, 14, 7, -22]}, "output": [6, -19, -43, -27, -16, -2, 5, -17]}, {"input": {"nums": [8, 9, -6, -1]}, "output": [8, 17, 11, 10]}, {"input": {"nums": [14, 8, 3, 13, -12, 14, 14]}, "output": [14, 22, 25, 38, 26, 40, 54]}] | 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": "784a40b63afa0a0b"} |
vcr_6eb147a536bf | 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": [-5, -7, 0, 30, -18, -13, -5, -19, -11, 30]}, "output": [-5, -12, -12, 18, 0, -13, -18, -37, -48, -18]}, {"input": {"nums": [5, -6, 3, -9, 9, -5, 14, -11, 9]}, "output": [5, -1, 2, -7, 2, -3, 11, 0, 9]}, {"input": {"nums": [-9, 9, 11, 6, -6, -1, -9, 5, 0, 6]}, "output": [-9, 0, 11, 17, 11, 10, 1, 6,... | 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": "2de8046894973a4e"} |
vcr_af2557c7548e | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [26, 36, 25, 36, 25, 69, 26]}, "output": 69}, {"input": {"nums": [4, 4, 57, 25, 25]}, "output": 57}, {"input": {"nums": [56, 28, 21, 21, 28, 13, 16, 13, 16]}, "output": 56}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "6c460042ba56abf4"} |
vcr_03f2f2fc0e33 | 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": [61, -29, 18, 36, 4, 13, 7, 6, -21, 28, 50], "target": 74}, "output": [0, 5]}, {"input": {"nums": [14, 15, -10, 6, -24, -26, 21, -23, -11, 9], "target": -1}, "output": [2, 9]}, {"input": {"nums": [19, 42, 12, 28, -20, 15], "target": 54}, "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": "eb8532ffef999f9d"} |
vcr_52a7cd6e3dbc | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bb", "ab", "ccc", "bcab", "cabc", "baac"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bba", "aaa"]}, "output": [1, 2]}, {"input": {"strs": ["bbb", "bba", "aaa", "aba", "bba", "aab", "abb"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "787192d6b363cc22"} |
vcr_eb556861e577 | 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": [-1, 3, 13, 14, 18, 30], "target": 31}, "output": [2, 4]}, {"input": {"nums": [-8, -5, 3, 15, 16, 18, 19, 21, 36], "target": 24}, "output": [2, 7]}, {"input": {"nums": [-19, 1, 3, 4, 5, 14, 18, 20, 39], "target": 44}, "output": [4, 8]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "f3b6026b13932a4c"} |
vcr_9cb46faa5377 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [41, 15, 4, 16, 6, 20, 22, 6]}, "output": 18}, {"input": {"prices": [34, 2, 7, 33, 8, 9, 23]}, "output": 31}, {"input": {"prices": [18, 8, 11, 22]}, "output": 14}] | 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": "6d61ed5e9b5b4fdb"} |
vcr_dcf18dfe8262 | 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, 23, 38, 46, 4, 33, 21, 56, 32, 20, 3, 43]}, "output": 52}, {"input": {"prices": [27, 27, 29, 27, 15, 21, 27, 5, 32]}, "output": 27}, {"input": {"prices": [39, 21, 1, 21, 5, 27, 16, 14, 2, 15, 12]}, "output": 26}] | 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": "927b00d12e15a21e"} |
vcr_ca8cbddee30f | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 4, 5, 7, 15, 17, 26, 28, 29, 30, 43, 46, 47, 48], "target": 7}, "output": 3}, {"input": {"nums": [13, 14, 21, 24, 25, 36, 39], "target": 0}, "output": 0}, {"input": {"nums": [1, 2, 4, 5, 12, 18, 20, 25, 27, 32, 37], "target": 28}, "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": "dae5d22616099dba"} |
vcr_261bf1247ab1 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-3, 11, 17, -7, 5, 20]}, "output": [-3, 8, 25, 18, 23, 43]}, {"input": {"nums": [-11, -12, 6, 0, 5, 3, -9, -3]}, "output": [-11, -23, -17, -17, -12, -9, -18, -21]}, {"input": {"nums": [17, -1, 3, 12, -11, 4, -6, -1, 6]}, "output": [17, 16, 19, 31, 20, 24, 18, 17, 23]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "e499a1750158406d"} |
vcr_0de4e6207498 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-18, -9, -6, -16, 8, -1, 2, 17, 4, 18, -14, -3]}, "output": 48}, {"input": {"nums": [-1, 7, 10, 5]}, "output": 22}, {"input": {"nums": [13, -7, 6, 4, -11, 13, -1, 7, 8, 14]}, "output": 46}] | 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": "74426525814b9b2e"} |
vcr_6c3f5167b325 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [34, 28, 4, 31, 8, -10, 0, -17, 3, -13, 25, 10]}, "output": [34, 62, 66, 97, 105, 95, 95, 78, 81, 68, 93, 103]}, {"input": {"nums": [16, -9, -10, 1, -12, -4, 0, 15]}, "output": [16, 7, -3, -2, -14, -18, -18, -3]}, {"input": {"nums": [2, 16, 3, 0]}, "output": [2, 18, 21, 21]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "d0448ee5d1a0505f"} |
vcr_c76d5eb157ce | 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, 16, 15, 10]}, "output": 44}, {"input": {"nums": [7, 15, 11, 14, 20, 3]}, "output": 38}, {"input": {"nums": [7, 3, 1]}, "output": 8}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "6fc70be1ca2ab13b"} |
vcr_13034f1d3454 | 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": [4, 9, 11, -3, 22, -15]}, "output": 43}, {"input": {"nums": [1, 12, 4, -8, 10, -5, 2]}, "output": 19}, {"input": {"nums": [10, -1, 0, 7, 4]}, "output": 20}] | 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": "f130f418e29a5d00"} |
vcr_5c58f37bd8c3 | 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": [17, 26, 45, 24, 37]}, "output": 28}, {"input": {"prices": [9, 30, 2, 40, 8, 35, 24, 18, 23, 13, 3, 28]}, "output": 38}, {"input": {"prices": [20, 40, 20, 16, 23, 3, 33, 38, 26, 23, 13]}, "output": 35}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "c5a95d1a92475564"} |
vcr_4954dd1f89ff | 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": [-12, -2, 22, 27, -10, 14, -23, 5, 9, 30]}, "output": [-12, -14, 8, 35, 25, 39, 16, 21, 30, 60]}, {"input": {"nums": [5, -6, 0, 10, -10, 13, 7, 17]}, "output": [5, -1, -1, 9, -1, 12, 19, 36]}, {"input": {"nums": [8, 15, -11, -5, 1, -5]}, "output": [8, 23, 12, 7, 8, 3]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "44c68149d75c4f9d"} |
vcr_123241268558 | 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": 222, "b": 189}, "output": 3}, {"input": {"a": 3, "b": 125}, "output": 1}, {"input": {"a": 101, "b": 27}, "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": "9b96d5d1db2e6b41"} |
vcr_19a4d9854389 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["aa", "ca", "cca", "aaac", "accc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["baa", "abb", "bbb"]}, "output": [1, 1, 1]}, {"input": {"strs": ["aab", "abb", "abb", "bba", "baa", "bba"]}, "output": [2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "ba862133f2c042b2"} |
vcr_223fb88b1fa2 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 4, 5, 3, 3, 5, 2]}, "output": [1800, 3150, 2520, 4200, 4200, 2520, 6300]}, {"input": {"nums": [1, 1, 5, 1, 4, 5, 2]}, "output": [200, 200, 40, 200, 50, 40, 100]}, {"input": {"nums": [3, 3, 4]}, "output": [12, 12, 9]}] | 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": "6e651fd70ff0092e"} |
vcr_e46489c160c0 | 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": "abaddbbabbddaba"}, "output": true}, {"input": {"s": "abc"}, "output": false}, {"input": {"s": "bc"}, "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": "803d37e87b0d956d"} |
vcr_f42369f5f51a | 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, 16, 11, 1, 4, -2, -2, -17], "k": 19}, "output": [-2, -2, -17, 15, 16, 11, 1, 4]}, {"input": {"nums": [-4, 4, 5, 3, 4, 0, 6], "k": 17}, "output": [4, 0, 6, -4, 4, 5, 3]}, {"input": {"nums": [-6, 3, 10, -8, 5, 8, 9], "k": 7}, "output": [-6, 3, 10, -8, 5, 8, 9]}] | 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": "1a214b34a7a3a4aa"} |
vcr_9e22640be2c7 | 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": [4, 5, 6, 11, 11, 11]}, "output": [4, 5, 6, 11]}, {"input": {"nums": [0, 1, 4, 7, 8, 9, 9, 9, 10, 11]}, "output": [0, 1, 4, 7, 8, 9, 10, 11]}, {"input": {"nums": [1, 3, 4, 7, 7, 11]}, "output": [1, 3, 4, 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": "9a41ce5b1d4691c7"} |
vcr_6226e40a976e | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [5, 26, 39, 34, 78, 39, 26, 5, 34]}, "output": 78}, {"input": {"nums": [18, 20, 11, 11, 18, 41, 20]}, "output": 41}, {"input": {"nums": [1, 24, 1, 11, 11, 24, 43]}, "output": 43}] | 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": "82257d5866427fed"} |
vcr_3ec232a223f3 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["aaab", "ab", "bbcb", "aa", "aa"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aab", "baa", "abb", "aab", "bab"]}, "output": [2, 3]}, {"input": {"strs": ["aab", "bab", "aab"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "cdfc58cb53e63b56"} |
vcr_616f05d7f16a | 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": [-13, 5, 4, 19, -9, 18, -3, -7, 2, 6, -6, 13, -17, 2]}, "output": 42}, {"input": {"nums": [-3, 3, -11, -1, -6, 12, 13, -1, 10, -1, 1]}, "output": 34}, {"input": {"nums": [-7, 8, 9, 12, -11, 3, 4, -2, 5]}, "output": 29}] | 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": "379d3c117662007b"} |
vcr_cd980730fa67 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [3, 25, 21, 27, 7, 22, 30, 21, 7, 52, 22, 25, 3, 30, 2, 27, 2]}, "output": 52}, {"input": {"nums": [55, 14, 27, 14, 22, 27, 22]}, "output": 55}, {"input": {"nums": [14, 43, 7, 23, 7, 13, 23, 14, 13]}, "output": 43}] | 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": "444b86b564989a72"} |
vcr_32953355d413 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [27, 25, 16, 5, 46, 26, 24, 40, 27, 16, 59, 39, 25, 40]}, "output": 54}, {"input": {"prices": [19, 9, 37, 39, 36, 20]}, "output": 30}, {"input": {"prices": [12, 38, 39, 15, 12, 21, 5, 35]}, "output": 30}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "9aa283dee2a20fea"} |
vcr_79194e07e6d7 | 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": [-3, -15, 3, 12, -5, -8]}, "output": 15}, {"input": {"nums": [-10, -9, 7, 5]}, "output": 12}, {"input": {"nums": [4, -3, 5, 13, -4, -9, 12, 6, 1]}, "output": 25}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "0c7061e407436c36"} |
vcr_5e4addf366ce | 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, 20, 10, 19, 19, 30]}, "output": 69}, {"input": {"nums": [1, 3, 16, 14, 19, 7, 9, 9, 16, 20]}, "output": 65}, {"input": {"nums": [5, 14, 13, 2, 8, 0, 3, 9, 20, 4]}, "output": 49}] | 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": "2ac8b317ca287e3b"} |
vcr_271207a6a681 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [18, 7, -17, -12, 12, -16, 18, -9, -12]}, "output": 25}, {"input": {"nums": [1, -3, -2, -10, 3, 2, 12, 13, -11, -8]}, "output": 30}, {"input": {"nums": [9, -6, 5, -12, -4]}, "output": 9}] | 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": "de484406ce8434f9"} |
vcr_fece1e2a8eec | 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": "dbbbabcbbdbabdcb"}, "output": false}, {"input": {"s": "babaaaacbb"}, "output": false}, {"input": {"s": "cbbacbc"}, "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": "6e186d00550ab350"} |
vcr_aaa37051577d | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [12, 20, -6, 5, 12, -17, -17, 12, 9], "k": 21}, "output": [-17, 12, 9, 12, 20, -6, 5, 12, -17]}, {"input": {"nums": [4, -2, 8, 2, 4], "k": 14}, "output": [-2, 8, 2, 4, 4]}, {"input": {"nums": [4, 0, 3, 2, 2, 3], "k": 13}, "output": [3, 4, 0, 3, 2, 2]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "ce6f415128071148"} |
vcr_a420b2f543fa | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 11, 16, 34, 43, 48], "target": 25}, "output": 3}, {"input": {"nums": [1, 2, 6, 13, 16, 17, 19, 24, 25, 38], "target": 16}, "output": 4}, {"input": {"nums": [5, 13, 39], "target": 16}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "e64261c8558bc79d"} |
vcr_7a8b6518622f | 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": 82, "b": 136}, "output": 2}, {"input": {"a": 137, "b": 105}, "output": 1}, {"input": {"a": 166, "b": 59}, "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": "86a32c3c8551104b"} |
vcr_f4eb62d554d8 | 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": [-25, 34, 3, -15, 10, 13, 9, 8]}, "output": [-25, 9, 12, -3, 7, 20, 29, 37]}, {"input": {"nums": [-8, 0, -9, -5, 3, -8]}, "output": [-8, -8, -17, -22, -19, -27]}, {"input": {"nums": [12, -3, -1, 14, -9, -10, 15, 17]}, "output": [12, 9, 8, 22, 13, 3, 18, 35]}] | 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": "5e8054260987827d"} |
vcr_98c2cf61579e | 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": "bea ddebbb cacaad ebdc caccc aea dee"}, "output": "dee aea caccc ebdc cacaad ddebbb bea"}, {"input": {"s": "ba b abab"}, "output": "abab b ba"}, {"input": {"s": "aba b aa aaab"}, "output": "aaab aa b aba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "6b247b4f12cfdca9"} |
vcr_cfbf1071aa96 | 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": "bbbbbba"}, "output": 2}, {"input": {"s": "aba"}, "output": 3}, {"input": {"s": "acababb"}, "output": 6}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "d09aed8f91cf2856"} |
vcr_bddfce70e75e | 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": 116, "b": 246}, "output": 2}, {"input": {"a": 72, "b": 70}, "output": 2}, {"input": {"a": 112, "b": 33}, "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": "1978f0eba9b47eb2"} |
vcr_e1db5e862118 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 3, 7, 6, 2, 2, 4, 1]}, "output": [2016, 672, 288, 336, 1008, 1008, 504, 2016]}, {"input": {"nums": [3, 5, 1, 1, 1, 5]}, "output": [25, 15, 75, 75, 75, 15]}, {"input": {"nums": [1, 5, 1, 3]}, "output": [15, 3, 15, 5]}] | 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": "acdf6ac38b7f793b"} |
vcr_ee5e1de1dfef | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [3, 4, 5, 5, 5, 7, 10, 12, 12, 12, 15, 15]}, "output": [3, 4, 5, 7, 10, 12, 15]}, {"input": {"nums": [3, 3, 6, 7, 12, 12]}, "output": [3, 6, 7, 12]}, {"input": {"nums": [4, 4, 5, 6, 7, 8, 12, 12]}, "output": [4, 5, 6, 7, 8, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "1a13b64930e5ca34"} |
vcr_5deab1b3f982 | 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": [58, 34, 21, 1, 1, 55, 25, 5, 5]}, "output": 54}, {"input": {"prices": [13, 29, 39, 1, 22, 23, 21]}, "output": 26}, {"input": {"prices": [20, 38, 33, 24, 38, 20, 12, 7, 37]}, "output": 30}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "0e0dee7eb32fd4db"} |
vcr_bd00ea455bc1 | 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": [31, 14, 18, 40]}, "output": 26}, {"input": {"prices": [40, 19, 37, 25, 16, 1, 40, 28, 28, 17]}, "output": 39}, {"input": {"prices": [35, 38, 32, 24]}, "output": 3}] | 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": "154f1e350a69ab7c"} |
vcr_2463f7970dcf | 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": [0, -15, -14, -4], "k": 0}, "output": [0, -15, -14, -4]}, {"input": {"nums": [-8, -6, 6, -1, 9], "k": 0}, "output": [-8, -6, 6, -1, 9]}, {"input": {"nums": [-4, -4, -6, 9, -9, 8], "k": 1}, "output": [8, -4, -4, -6, 9, -9]}] | 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": "b5d47e1c75e5dc87"} |
vcr_0f2930426556 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 3, 4, 5, 6, 6, 8, 8, 11, 11, 12, 13, 13]}, "output": [2, 3, 4, 5, 6, 8, 11, 12, 13]}, {"input": {"nums": [1, 2, 3, 5, 6, 6, 7, 9, 9, 10, 10, 11]}, "output": [1, 2, 3, 5, 6, 7, 9, 10, 11]}, {"input": {"nums": [2, 3, 10]}, "output": [2, 3, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "77a798a42d90c807"} |
vcr_a982eeeacd65 | 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, 31, 25, 49, 42, 30, 53, 28, 22, 24, 20, 51]}, "output": 43}, {"input": {"prices": [29, 4, 2, 26, 23, 4, 4, 39, 34, 26, 34]}, "output": 37}, {"input": {"prices": [24, 25, 26, 3, 33, 36, 39, 33]}, "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": "18fbd3d8c74f786b"} |
vcr_f8a3cb1922bb | 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": [10, 26, 15, 3]}, "output": 29}, {"input": {"nums": [16, 15, 10, 13, 11, 1, 0, 20, 8]}, "output": 57}, {"input": {"nums": [13, 12, 2, 6, 1, 9, 18, 3]}, "output": 37}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "6e496f8a14d11c04"} |
vcr_8841fe7a0980 | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "e bbdbad aa edddda b ecea"}, "output": "ecea b edddda aa bbdbad e"}, {"input": {"s": "bab bab ab"}, "output": "ab bab bab"}, {"input": {"s": "aaab bbaa b a bb"}, "output": "bb a b bbaa aaab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "d0f0732533309a47"} |
vcr_43ea757285c2 | 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": [64, 2, 165, 157, 27, 155, 49]}, "output": false}, {"input": {"nums": [14, 18, 7, 4]}, "output": false}, {"input": {"nums": [10, 17, 5, 13, 4, 8, 11]}, "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": "4d323bbcaefd4291"} |
vcr_320217d40e1c | 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": 101, "b": 45}, "output": 1}, {"input": {"a": 57, "b": 58}, "output": 1}, {"input": {"a": 123, "b": 130}, "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": "1e9c47cb013499de"} |
vcr_258173c92817 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 2, 7, 7, 9, 9, 14, 15]}, "output": [0, 2, 7, 9, 14, 15]}, {"input": {"nums": [1, 1, 3, 7, 7, 9, 12, 12]}, "output": [1, 3, 7, 9, 12]}, {"input": {"nums": [0, 1, 1, 4, 5, 5, 8, 9, 9, 10, 12]}, "output": [0, 1, 4, 5, 8, 9, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "75681595a319a2b5"} |
vcr_d1b261b218c4 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-19, -10, -3, 0, 7, 8, 44], "target": -19}, "output": [0, 3]}, {"input": {"nums": [-15, -9, 4, 7, 12, 27, 29, 30, 31, 33], "target": 21}, "output": [1, 7]}, {"input": {"nums": [-4, 5, 16, 27, 31, 34], "target": 12}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "bdbac6b1d152e915"} |
vcr_2b5ad4d1053a | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 2, 5, 6, 6, 8, 8, 9, 10, 11, 13, 13, 15]}, "output": [0, 1, 2, 5, 6, 8, 9, 10, 11, 13, 15]}, {"input": {"nums": [0, 2, 5, 7, 7, 9, 10, 11, 12, 12]}, "output": [0, 2, 5, 7, 9, 10, 11, 12]}, {"input": {"nums": [0, 6, 7, 8, 9, 11, 11, 12]}, "output": [0, 6, 7, 8, 9, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "4dcd127cca92796e"} |
vcr_ba7412d5fdb8 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 1, 0, 2, 4, 0]}, "output": false}, {"input": {"nums": [2, 2, 0, 2, 1, 0, 2, 2]}, "output": false}, {"input": {"nums": [0, 0, 1, 0, 0, 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": "0bfe75715a34d23e"} |
vcr_7fad4d67e30f | 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": [42, 49, 54, 39, 53, 24, 42, 51, 36]}, "output": 27}, {"input": {"prices": [34, 11, 17, 12, 32, 8, 20, 5, 25, 5]}, "output": 21}, {"input": {"prices": [22, 20, 12, 5, 15, 23, 33, 35, 38, 29, 39]}, "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": "29415038d421148e"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.