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_1c15ab07e779 | 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, 2, -15, 21, -6, 4]}, "output": 21}, {"input": {"nums": [11, -9, 7, -5, 3, -1, 4, 5, 1, -5, 6]}, "output": 17}, {"input": {"nums": [-4, -10, -9, -8]}, "output": -4}] | 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": "1915407ea0a48a10"} |
vcr_8b9dabb71d53 | 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": [168, 95, 116, 188, 124, 32, 134, 48, 93, 121, 83, 73, 55, 102]}, "output": false}, {"input": {"nums": [18, 25, 25, 24, 18, 0]}, "output": true}, {"input": {"nums": [0, 10, 4, 2, 7, 2, 19, 20]}, "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": "aaf1b14e23f72f41"} |
vcr_87778dde0a08 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 4, 11, 12, 14, 15, 15, 15, 15]}, "output": [2, 4, 11, 12, 14, 15]}, {"input": {"nums": [2, 3, 5, 6, 8, 8, 8, 9, 10, 11]}, "output": [2, 3, 5, 6, 8, 9, 10, 11]}, {"input": {"nums": [1, 2, 2, 12]}, "output": [1, 2, 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": "07b493ed89a206ab"} |
vcr_ee18399a81fb | 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": [26, 5, 2, 0, 30, 2, 20, 10]}, "output": 78}, {"input": {"nums": [14, 15, 15, 1]}, "output": 29}, {"input": {"nums": [13, 7, 7, 14, 14, 17, 11]}, "output": 45}] | 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": "7d16c61207f302b5"} |
vcr_ce35891c69e2 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [1, 4, 3, 0, 1, 3, 4, 2, 0]}, "output": true}, {"input": {"nums": [0, 2, 1, 0]}, "output": false}, {"input": {"nums": [2, 2, 2, 2, 1, 3, 2, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "b3adc8374cd1fb6a"} |
vcr_bf162f50923c | 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": ["bbba", "aaab", "caa", "bcb", "cabc", "ccc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bab", "aba", "aba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["baa", "baa", "aaa", "abb"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "8d2c0e712db77bd0"} |
vcr_977ce5071f27 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [14, 6, 3, 5]}, "output": 19}, {"input": {"nums": [7, 6, 16]}, "output": 23}, {"input": {"nums": [12, 12]}, "output": 12}] | 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": "f8269b6ccb5144ce"} |
vcr_ca989cd45d58 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [1, 30, 8, 1, 35, 8, 55, 35, 30]}, "output": 55}, {"input": {"nums": [48, 26, 22, 22, 26]}, "output": 48}, {"input": {"nums": [27, 19, 49, 17, 19, 25, 27, 17, 25]}, "output": 49}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "15a47a15ce977719"} |
vcr_e5e40efa1ef3 | 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, 1, 1, 6, 7, 8, 9, 10, 11, 11, 13, 15]}, "output": [0, 1, 6, 7, 8, 9, 10, 11, 13, 15]}, {"input": {"nums": [3, 4, 5, 6, 6, 9, 9, 10, 11, 11]}, "output": [3, 4, 5, 6, 9, 10, 11]}, {"input": {"nums": [0, 0, 2, 2, 4, 8, 11]}, "output": [0, 2, 4, 8, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "be403ea3d6f85285"} |
vcr_6812c0f1c5fa | 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, 18, 15, 12, 17, 15, 22, 10, 6, 30, 23, 28]}, "output": 130}, {"input": {"nums": [14, 16, 2]}, "output": 16}, {"input": {"nums": [15, 3, 7, 3]}, "output": 22}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "91912b31c6377858"} |
vcr_e5654ce159cd | 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": ["ac", "cbc", "bcc", "aac", "bcaa", "bcc", "cbc"]}, "output": [1, 1, 1, 4]}, {"input": {"strs": ["aab", "bba", "abb", "bba", "aab", "abb"]}, "output": [2, 4]}, {"input": {"strs": ["aaa", "bab", "aab", "abb", "baa", "bab", "bba"]}, "output": [1, 2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "0d3c72d98bb9abd5"} |
vcr_7de7d8be14c5 | 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, -8, -12, -12, -19, 20, -17, -3, 10, 2, 33, -24]}, "output": [34, 26, 14, 2, -17, 3, -14, -17, -7, -5, 28, 4]}, {"input": {"nums": [2, 3, -9, 14, 12]}, "output": [2, 5, -4, 10, 22]}, {"input": {"nums": [16, 15, 1, 0]}, "output": [16, 31, 32, 32]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "6f1ef60a4e19ac8d"} |
vcr_9819a3a20d7c | 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": [13, -14, 14, -17]}, "output": [13, -1, 13, -4]}, {"input": {"nums": [-12, -4, -2, 2, 2, 12, 5, 7, 3, 2]}, "output": [-12, -16, -18, -16, -14, -2, 3, 10, 13, 15]}, {"input": {"nums": [-4, 8]}, "output": [-4, 4]}] | 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": "eddae3f13195ee68"} |
vcr_153fe9c80d6f | 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": [2, 43, 40, 52, 14, 5, 48, 42, 21, 13, 16]}, "output": 50}, {"input": {"prices": [6, 18, 37, 29]}, "output": 31}, {"input": {"prices": [1, 18, 39, 14, 7, 27, 25, 10]}, "output": 38}] | 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": "0f05d9c2dc54dded"} |
vcr_847e9c97cbaf | 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": ["abbb", "bcbb", "bac", "baab", "ccaa"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "aba", "abb"]}, "output": [1, 2]}, {"input": {"strs": ["aba", "bbb", "bbb", "bbb", "bba", "baa"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "512dfebbb73936f5"} |
vcr_87355e96eb6d | 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": [-13, -5, 3, 7, 8, 29, 40, 43], "target": -18}, "output": [0, 1]}, {"input": {"nums": [-18, -7, -5, -3, 14, 27, 32, 37], "target": 9}, "output": [0, 5]}, {"input": {"nums": [-18, -16, -7, 3, 6, 14, 34, 38], "target": -34}, "output": [0, 1]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "b7bd2fa6536b9914"} |
vcr_d042eba7e0da | 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": ["bac", "ba", "ba", "abcc", "cbc", "ac", "aaa", "aca", "abcc"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["baa", "bba", "aba", "aab", "bab", "aba"]}, "output": [2, 4]}, {"input": {"strs": ["bba", "aba", "abb", "bab", "baa", "baa"]}, "output": [3, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "3c0a0f1f2b57bb5c"} |
vcr_f585fe6e7988 | 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": [16, 16, 4, 26]}, "output": 42}, {"input": {"nums": [16, 7, 11]}, "output": 27}, {"input": {"nums": [12, 20, 0, 9, 6, 20]}, "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": "e8e53f134fae595f"} |
vcr_873f4cc418c2 | 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": [6, 5, 5, 5, 7]}, "output": [875, 1050, 1050, 1050, 750]}, {"input": {"nums": [1, 3, 1, 1, 2, 3]}, "output": [18, 6, 18, 18, 9, 6]}, {"input": {"nums": [4, 2, 3, 5]}, "output": [30, 60, 40, 24]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "ea178f70e43e0e73"} |
vcr_15e209ce6ef7 | 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": [21, 17, 23, 16, 43, 6, 51, 51, 39, 21]}, "output": 45}, {"input": {"prices": [37, 2, 4]}, "output": 2}, {"input": {"prices": [30, 8, 38, 11, 14]}, "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": "2020e8979cb6773c"} |
vcr_59593a54bbed | 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, 12, 15, 21, 23, 30, 35, 41], "target": 18}, "output": 3}, {"input": {"nums": [6, 21, 28, 32, 34], "target": 7}, "output": 1}, {"input": {"nums": [3, 7, 13, 14, 15, 25, 27, 29, 31], "target": 38}, "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": "49edd7ee85d4fd7d"} |
vcr_06ce84631b8d | 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": "abccbbabbbba"}, "output": 7}, {"input": {"s": "bbaabbcccaaa"}, "output": 5}, {"input": {"s": "cabbbcaabb"}, "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": "3746e00468c6c508"} |
vcr_0b8acd7c69d3 | 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, -18, 8, -8, 6]}, "output": [-10, -28, -20, -28, -22]}, {"input": {"nums": [-1, -12, -3, -9]}, "output": [-1, -13, -16, -25]}, {"input": {"nums": [-11, 7, -1]}, "output": [-11, -4, -5]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "3624a7156cc64cdc"} |
vcr_7f7aeb36cdc5 | 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, 18, -15, -7, 0, -4, 21, 9, 11, 7, -11, 18, 1, -9]}, "output": 56}, {"input": {"nums": [-8, 7, 8, 11, -10, 12, 13]}, "output": 41}, {"input": {"nums": [-7, 5, 10, -3, 10, -9, 2, 2, -10, -9, 13]}, "output": 22}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "1970e6cb1a0b7206"} |
vcr_40ba959bc76b | 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": 59311}, "output": 12}, {"input": {"n": 738}, "output": 5}, {"input": {"n": 4421}, "output": 5}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "6a343f5ef4228795"} |
vcr_4f4137ae14ae | 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": [18, -16, 21, -33, 51, 58, -18, 72, -22, -4, 3], "target": 40}, "output": [5, 6]}, {"input": {"nums": [-3, -22, -13, -21, -7, 49, 10, 32, 31], "target": -12}, "output": [1, 6]}, {"input": {"nums": [16, -17, -22, -29, -7, 23, 38, -30], "target": -47}, "output": [1, 7]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "376fde937dce7bf9"} |
vcr_8de13262fdf1 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["aaba", "bcaa", "cab", "bcc", "bb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["abb", "baa", "aab", "abb", "aba", "aaa"]}, "output": [1, 2, 3]}, {"input": {"strs": ["bab", "aaa", "abb", "bbb", "aaa", "aaa", "aaa"]}, "output": [1, 2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "73c12a63ec43c2d5"} |
vcr_e75632fc9431 | 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": ["cccc", "aaa", "aa", "ac"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bab", "baa", "aaa", "aba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bba", "aba", "aaa", "abb", "bba", "baa"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "efe1503972602b2d"} |
vcr_ff0b343954ad | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, 6, 9, 16, 23, 25, 31], "target": 47}, "output": [3, 6]}, {"input": {"nums": [-20, -6, 15, 20, 25, 28], "target": -5}, "output": [0, 2]}, {"input": {"nums": [-15, -11, -7, 5, 8, 14], "target": 22}, "output": [4, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "c0bd787eac47903d"} |
vcr_21fcf047092c | 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": "dbeade bdaa b e bdad dcaaad eabce"}, "output": "eabce dcaaad bdad e b bdaa dbeade"}, {"input": {"s": "aa a b"}, "output": "b a aa"}, {"input": {"s": "ab abb"}, "output": "abb ab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "ab91e0247f6ca928"} |
vcr_e3d27d012384 | 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": [18, 20, -16, 20, 6, 10, 12, 6, 16, -2, -5], "k": 25}, "output": [16, -2, -5, 18, 20, -16, 20, 6, 10, 12, 6]}, {"input": {"nums": [-3, -7, 3], "k": 12}, "output": [-3, -7, 3]}, {"input": {"nums": [-6, 1, -1], "k": 4}, "output": [-1, -6, 1]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "be6c737653916bc8"} |
vcr_cc461e1507d2 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [1, 1, 0, 9, 15, 0, 30, 21, 2, 21, 25]}, "output": 73}, {"input": {"nums": [4, 18, 12, 2, 2, 4, 7, 4]}, "output": 28}, {"input": {"nums": [9, 3, 11, 9, 16]}, "output": 36}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "512e22270c9dcee2"} |
vcr_16afcf97cff1 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, 2, 13, 29, 30, 36, 39, 46, 48], "target": 10}, "output": [0, 6]}, {"input": {"nums": [-12, -5, -4, -3, -2, 3, 10, 19, 31, 33], "target": -2}, "output": [0, 6]}, {"input": {"nums": [-11, -10, -2, 2, 16, 29], "target": 18}, "output": [0, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "fd0bc643458279e8"} |
vcr_bfaa0df3ac80 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [20, 27, 5, 24, 54, 36, 21, 36, 5, 27, 21, 14, 20, 24, 14]}, "output": 54}, {"input": {"nums": [9, 9, 14, 11, 11, 14, 44]}, "output": 44}, {"input": {"nums": [9, 55, 5, 9, 5]}, "output": 55}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "39e786627dd20975"} |
vcr_da5f72e63a36 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, -7, 7, 11, 15, 20, 23, 36], "target": -37}, "output": [0, 1]}, {"input": {"nums": [-17, -16, -5, 2, 22, 25, 27], "target": -22}, "output": [0, 2]}, {"input": {"nums": [-18, -1, 0, 3, 8, 19, 20, 31, 35, 37], "target": 19}, "output": [0, 9]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "ab84338f13168a75"} |
vcr_43edc68dabf3 | 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": [6, 18, 6, 0, 11, 50, 30, 40, 3, 21, 3, 14, 37]}, "output": true}, {"input": {"nums": [4, 5, 19, 3, 19, 9, 10, 1, 7]}, "output": true}, {"input": {"nums": [13, 15, 2, 5, 19, 20, 21, 20, 6]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "553c236843e4190c"} |
vcr_79f8a36fb832 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [12, 29, 47, 17]}, "output": 35}, {"input": {"prices": [26, 21, 12, 10, 26, 13, 8, 40, 13, 9, 5]}, "output": 32}, {"input": {"prices": [36, 29, 5, 39, 23, 28, 18, 35, 12]}, "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": "5a424e36e56b4b6e"} |
vcr_21b9a312c5e1 | 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": [15, 10, 28, 27, 13, 21, 6, 30, 12, 17, 29]}, "output": 123}, {"input": {"nums": [10, 17, 19, 19, 2, 4, 14, 5, 1, 19]}, "output": 69}, {"input": {"nums": [9, 18, 17]}, "output": 26}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "03e8358eb8586f22"} |
vcr_fa20bda49491 | 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, 9, 11, 12, 14]}, "output": [4, 9, 11, 12, 14]}, {"input": {"nums": [0, 6, 7]}, "output": [0, 6, 7]}, {"input": {"nums": [1, 1, 2, 2, 3, 7, 9, 11]}, "output": [1, 2, 3, 7, 9, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "b6dd71099f124367"} |
vcr_1a42200da553 | 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": [57, 50, 44, 57, 46, 28, 54, 22, 46, 34, 19, 14]}, "output": 26}, {"input": {"prices": [7, 31, 9, 6, 38, 7, 18]}, "output": 32}, {"input": {"prices": [3, 40, 4]}, "output": 37}] | 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": "2d0207c4099665ef"} |
vcr_ec3f703be206 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 9, 10, 13, 23, 25, 27, 31, 39, 43], "target": 19}, "output": 4}, {"input": {"nums": [10, 20, 22, 34], "target": 23}, "output": 3}, {"input": {"nums": [2, 6, 13, 17, 18, 19, 23, 31, 32, 33, 34, 36], "target": 29}, "output": 7}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "ec5ec5b55ed0056a"} |
vcr_42990f363b02 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-14, 2, 13, -8, -6, 11, -17, 0], "k": 24}, "output": [-14, 2, 13, -8, -6, 11, -17, 0]}, {"input": {"nums": [-1, 4, -2, -3, 6, 6, 1, -7], "k": 9}, "output": [-7, -1, 4, -2, -3, 6, 6, 1]}, {"input": {"nums": [-10, -10, -9, 6, 10], "k": 9}, "output": [-10, -9, 6, 10, -10]}] | 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": "8a075319c5f0f5a0"} |
vcr_78ef6a9f50d5 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-24, -3, 2, 9, 11, 18, 22, 27, 35, 36, 37, 41], "target": 54}, "output": [5, 9]}, {"input": {"nums": [-3, -2, 11, 12, 18, 21, 29, 30, 32], "target": 47}, "output": [4, 6]}, {"input": {"nums": [-18, -15, -10, 9, 18, 20, 26, 35, 37], "target": 19}, "output": [0, 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": "2f9e33a78162d8c3"} |
vcr_9fdc3958ec06 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 7, 5, 4]}, "output": [140, 100, 140, 175]}, {"input": {"nums": [3, 1, 4, 5, 3]}, "output": [60, 180, 45, 36, 60]}, {"input": {"nums": [3, 1, 2, 1, 3]}, "output": [6, 18, 9, 18, 6]}] | 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": "4d1e7ea2ddcca084"} |
vcr_79c85eb68e17 | 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": 41831}, "output": 9}, {"input": {"n": 3544}, "output": 7}, {"input": {"n": 5277}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "2e7fc25e84a2a21c"} |
vcr_b1f38372db74 | 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": "abcdcd e"}, "output": "e abcdcd"}, {"input": {"s": "a b b b"}, "output": "b b b a"}, {"input": {"s": "bbb bba a aa baaa"}, "output": "baaa aa a bba 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": "53088ef0ac0c7566"} |
vcr_931ca2486f85 | 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": [6, 5, 0, 1, 7, 4, 5]}, "output": [0, 0, 4200, 0, 0, 0, 0]}, {"input": {"nums": [4, 1, 3]}, "output": [3, 12, 4]}, {"input": {"nums": [5, 3, 3, 4, 2, 2]}, "output": [144, 240, 240, 180, 360, 360]}] | 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": "85b620b872986558"} |
vcr_dc625af5d1e6 | 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, 16, 20, 24, 30, 32, 36, 43], "target": 42}, "output": 8}, {"input": {"nums": [1, 11, 17, 24, 30], "target": 8}, "output": 1}, {"input": {"nums": [1, 4, 18, 28, 36, 39], "target": 32}, "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": "a6e4bddd54f61353"} |
vcr_9f4ee301fa5d | 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": ["aca", "ca", "bc", "cc", "bb", "caa", "ccc", "ab"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "aab", "bbb", "aaa", "bab"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aba", "aaa", "aba", "bbb", "baa"]}, "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": "6838e7ad84df483c"} |
vcr_41211e6a754a | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-16, -16, 10, -6, 6, -2, 6, 18, 11], "k": 12}, "output": [6, 18, 11, -16, -16, 10, -6, 6, -2]}, {"input": {"nums": [1, -8, 10, -5, 0, 2, -4, 6, 5, -9], "k": 10}, "output": [1, -8, 10, -5, 0, 2, -4, 6, 5, -9]}, {"input": {"nums": [1, 1, 10, -6, 3, 1, 6], "k": 6}, "output": [1, 10, -6, 3, 1, 6, 1]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "e568dc507d4d9711"} |
vcr_429e42131805 | 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": [35, 5, 70, 38, 5, 35, 38, 15, 15]}, "output": 70}, {"input": {"nums": [15, 4, 4, 11, 11, 19, 19, 55, 15]}, "output": 55}, {"input": {"nums": [10, 56, 22, 10, 22]}, "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": "3ce7ca8c93f075a6"} |
vcr_c0a2446dd11a | 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": [20, 0, 20, 1, 21]}, "output": 61}, {"input": {"nums": [0, 0, 1, 3, 1, 13, 18]}, "output": 21}, {"input": {"nums": [16, 2]}, "output": 16}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "154a447fa2471f50"} |
vcr_a4f7caaf06e6 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-26, -20, -15, -9, -3, 8, 27, 46, 48], "target": 20}, "output": [0, 7]}, {"input": {"nums": [-15, -3, -1, 2, 16, 32, 37], "target": 53}, "output": [4, 6]}, {"input": {"nums": [-14, -4, 4, 18, 20, 21, 22, 23, 32, 38], "target": 61}, "output": [7, 9]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "4d1e63c39a2e2756"} |
vcr_a838e402879b | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-2, 14, -8, 7, -15], "k": 0}, "output": [-2, 14, -8, 7, -15]}, {"input": {"nums": [-5, 6, -10, 3, -1, -2], "k": 16}, "output": [-10, 3, -1, -2, -5, 6]}, {"input": {"nums": [-1, -2, 6], "k": 18}, "output": [-1, -2, 6]}] | 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": "1d1ae8c99ef46a9d"} |
vcr_f8750a1eb271 | 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 cbc becce aeaee"}, "output": "aeaee becce cbc e"}, {"input": {"s": "baab ba"}, "output": "ba baab"}, {"input": {"s": "a bbb aaaa aaba"}, "output": "aaba aaaa bbb 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": "dfc7745fc8645517"} |
vcr_4c65bc4c5826 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [2, 4, 7, 5, 1, 2, 5]}, "output": [1400, 700, 400, 560, 2800, 1400, 560]}, {"input": {"nums": [2, 3, 5, 3, 4, 3, 4]}, "output": [2160, 1440, 864, 1440, 1080, 1440, 1080]}, {"input": {"nums": [3, 3, 1]}, "output": [3, 3, 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": "edb94b498296d64f"} |
vcr_386ab38bd2be | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [6, 1, -5, 9, 9, 20, -2, -3]}, "output": 40}, {"input": {"nums": [2, 10, -11, -3]}, "output": 12}, {"input": {"nums": [8, 3, -1, -2, 4, 1, -6, 5]}, "output": 13}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "182f261dca33c70d"} |
vcr_0caf2537099a | 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, 4, 7, 8, 9, 10, 11, 12, 14, 14, 15, 15]}, "output": [0, 2, 4, 7, 8, 9, 10, 11, 12, 14, 15]}, {"input": {"nums": [0, 0, 0, 2, 2, 4, 4, 5, 9, 9]}, "output": [0, 2, 4, 5, 9]}, {"input": {"nums": [0, 0, 5, 6, 8, 10, 11]}, "output": [0, 5, 6, 8, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "8c27e5e057fc57a0"} |
vcr_628f2428848b | 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": "dcaec ecd dc"}, "output": "dc ecd dcaec"}, {"input": {"s": "aaa aaa aab"}, "output": "aab aaa aaa"}, {"input": {"s": "ab a"}, "output": "a ab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "3fd2d91e066833ce"} |
vcr_fb11b928f9af | 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": [16, 4, 4, 28, 12, 0, 5, 29, 21, 23, 20]}, "output": 96}, {"input": {"nums": [16, 8, 7, 15, 5, 5, 11, 12]}, "output": 48}, {"input": {"nums": [3, 13, 19, 11, 20, 18]}, "output": 42}] | 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": "3715405b2501f5a3"} |
vcr_b713799525db | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 20, 33, 41], "target": 10}, "output": 1}, {"input": {"nums": [2, 11, 24, 27, 30, 32, 36, 38], "target": 30}, "output": 4}, {"input": {"nums": [1, 2, 3, 6, 7, 9, 11, 13, 17, 26, 29], "target": 33}, "output": 11}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "28785b9026704234"} |
vcr_a2cf32c7560f | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "b bacee dbcbee ccabbb bbccb cbb"}, "output": "cbb bbccb ccabbb dbcbee bacee b"}, {"input": {"s": "aaab b"}, "output": "b aaab"}, {"input": {"s": "babb bb b"}, "output": "b bb 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": "5853abccc2d5ae86"} |
vcr_ebfeaa9449fc | 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": [76, 114, 133, 17, 18, 141, 115, 66, 195, 69, 72, 108, 59, 169, 85]}, "output": false}, {"input": {"nums": [24, 10, 7, 16]}, "output": false}, {"input": {"nums": [4, 7, 12, 7, 24, 4, 0]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "2d3120db05386160"} |
vcr_79a1be139722 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [24, 2, 13, 14, 0]}, "output": 38}, {"input": {"nums": [11, 19, 8, 8, 2, 18, 17, 2, 9, 18]}, "output": 65}, {"input": {"nums": [6, 3, 19, 4, 9]}, "output": 34}] | 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": "9aa1f257a5d181d0"} |
vcr_39924bb21dc1 | 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, 14, -6, -15, -3, -6, 10, 15, -8], "k": 0}, "output": [-12, 14, -6, -15, -3, -6, 10, 15, -8]}, {"input": {"nums": [8, -8, 0, -9, -6, -10, -2, 0], "k": 1}, "output": [0, 8, -8, 0, -9, -6, -10, -2]}, {"input": {"nums": [2, 6, 3, -10, 6, 8, -10, 4], "k": 3}, "output": [8, -10, 4, 2, 6, 3, -10, 6]}... | 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": "7a1c68ea527d5de4"} |
vcr_cec63d0a6f6a | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [27, 191, 130, 11, 198, 66, 142, 141, 72, 137, 65, 37, 107, 46]}, "output": false}, {"input": {"nums": [14, 16, 10, 2, 23]}, "output": false}, {"input": {"nums": [0, 24, 0, 22, 20]}, "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": "06183d3e03aad89a"} |
vcr_2682c8e78d81 | 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": [21, 7, 52, 23, 42, 51, 25]}, "output": 45}, {"input": {"prices": [26, 18, 9, 36, 9, 24, 29, 12, 25, 32]}, "output": 27}, {"input": {"prices": [4, 5, 8, 32, 16]}, "output": 28}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "b4d87e820a2c3790"} |
vcr_5925017b315f | 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": [35, 1, 33, 4, 33, 35, 4, 1, 20, 20, 65]}, "output": 65}, {"input": {"nums": [8, 1, 23, 23, 56, 8, 12, 1, 12]}, "output": 56}, {"input": {"nums": [27, 27, 48, 16, 7, 16, 29, 7, 29]}, "output": 48}] | 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": "d321460ee04efd6d"} |
vcr_b8b1d51aa512 | 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, 3, 3, 4, 7, 11, 12, 14]}, "output": [0, 2, 3, 4, 7, 11, 12, 14]}, {"input": {"nums": [0, 0, 0, 2, 2, 3, 3, 3, 3, 9, 9, 11]}, "output": [0, 2, 3, 9, 11]}, {"input": {"nums": [3, 4, 5, 7, 11]}, "output": [3, 4, 5, 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": "816de1f68606f314"} |
vcr_5938a33ecce1 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-2, -15, -20, -7, 18, -13, -9, 18, -14, -1], "k": 22}, "output": [-14, -1, -2, -15, -20, -7, 18, -13, -9, 18]}, {"input": {"nums": [-6, 6, 2, -1, 7, 9], "k": 16}, "output": [2, -1, 7, 9, -6, 6]}, {"input": {"nums": [8, 0, -10, 1], "k": 10}, "output": [-10, 1, 8, 0]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "3c057d64ba6d22c9"} |
vcr_e830f84d0823 | 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": [18, 15, 24, 37, 7, 5, 37, 5, 32, 9, 7, 79, 9, 32, 18, 24, 15]}, "output": 79}, {"input": {"nums": [19, 4, 4, 40, 20, 11, 19, 20, 11]}, "output": 40}, {"input": {"nums": [23, 41, 25, 23, 25]}, "output": 41}] | 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": "c2a9f29ea3095598"} |
vcr_9440b8350710 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [24, 32, -8, 23]}, "output": [24, 56, 48, 71]}, {"input": {"nums": [14, 7, 6, 6, 9, -11, 11, -11, -12]}, "output": [14, 21, 27, 33, 42, 31, 42, 31, 19]}, {"input": {"nums": [9, 17, 1, -5, -12]}, "output": [9, 26, 27, 22, 10]}] | 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": "9a6b7c01571d3d22"} |
vcr_0fbd49b54d35 | 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": 188, "b": 54}, "output": 2}, {"input": {"a": 70, "b": 12}, "output": 2}, {"input": {"a": 12, "b": 130}, "output": 2}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "86c229d2e33e5aab"} |
vcr_ff9ab6da6a19 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [8, 7, 20, -13, 16, -10, 17, 12, -13, -17]}, "output": 57}, {"input": {"nums": [-11, 12, -6, -4, -10]}, "output": 12}, {"input": {"nums": [8, -2, 0, 7, 13, 9, -11]}, "output": 35}] | 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": "cf471f7480c5c5f2"} |
vcr_b101fac825cb | 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": 218, "b": 3}, "output": 1}, {"input": {"a": 37, "b": 42}, "output": 1}, {"input": {"a": 63, "b": 95}, "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": "b2a6db38d1d0e763"} |
vcr_f6adf14c6610 | 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": ["abcc", "bba", "cbb", "aaa", "cbc", "aa", "abb", "cbb"]}, "output": [1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["bab", "aaa", "abb"]}, "output": [1, 2]}, {"input": {"strs": ["aaa", "aab", "abb", "abb", "aba", "baa"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "ea87e974349b2246"} |
vcr_8d03c454d7db | 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": [36, 14, 35, 7, 4, 24, 10, 36]}, "output": true}, {"input": {"nums": [12, 2, 1, 4, 19, 0, 6, 8, 17, 3, 5]}, "output": false}, {"input": {"nums": [5, 25, 19, 6, 10, 14]}, "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": "cc47834aa8616501"} |
vcr_b08f4287276c | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [30, 4, 19, 3, 13, 26]}, "output": 75}, {"input": {"nums": [13, 1]}, "output": 13}, {"input": {"nums": [11, 13, 12, 12, 8, 14, 16]}, "output": 47}] | 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": "e4d00f9f4c26c361"} |
vcr_935c56f432ab | 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": "dcaacacabadcbdcc"}, "output": false}, {"input": {"s": "cbabcca"}, "output": false}, {"input": {"s": "cbbbacba"}, "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": "eec8ed9a529d081e"} |
vcr_f524e4dbf9fa | 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, 17, -20]}, "output": [-3, 14, -6]}, {"input": {"nums": [15, 6]}, "output": [15, 21]}, {"input": {"nums": [-11, -9, 16, 8, 15, 3, -11]}, "output": [-11, -20, -4, 4, 19, 22, 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": "3772198e83d5bc4f"} |
vcr_3914dc0ee2ab | 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": 75, "b": 161}, "output": 1}, {"input": {"a": 153, "b": 109}, "output": 1}, {"input": {"a": 152, "b": 114}, "output": 38}] | 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": "8e2b24129efa9921"} |
vcr_fe7a33245fb1 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [4, 4, 4, 2, 2, 5]}, "output": [320, 320, 320, 640, 640, 256]}, {"input": {"nums": [2, 2, 5, 2]}, "output": [20, 20, 8, 20]}, {"input": {"nums": [2, 1, 4]}, "output": [4, 8, 2]}] | 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": "2c9e56e98c3219cd"} |
vcr_7b15ef231569 | 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": ["ab", "acaa", "bbbc", "abc", "bcb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "aba", "bba"]}, "output": [1, 2]}, {"input": {"strs": ["aaa", "aaa", "abb", "bba", "baa", "aaa", "abb"]}, "output": [1, 3, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "a46c45db4c9e5950"} |
vcr_9aecf274f8cc | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [52, 16, 57, 24, 6, 0, -7, -39, 74, -12, -38, -9, 2, -36], "target": 54}, "output": [0, 12]}, {"input": {"nums": [-3, 18, 36, -21, 1, -16, -19, 37, -29], "target": 16}, "output": [3, 7]}, {"input": {"nums": [-12, 10, 29, -17, -4, 49, 3, -1, -18, 26], "target": -30}, "output": [0, 8]}] | 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": "101a83bfc899deaf"} |
vcr_79d4c89027e4 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 3, 3, 4, 6, 9, 10, 10, 12, 12, 12]}, "output": [0, 3, 4, 6, 9, 10, 12]}, {"input": {"nums": [0, 1, 1, 4, 4, 6, 7, 8, 9, 11]}, "output": [0, 1, 4, 6, 7, 8, 9, 11]}, {"input": {"nums": [0, 2, 3, 3, 4, 5, 7]}, "output": [0, 2, 3, 4, 5, 7]}] | 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": "8da687886d829a76"} |
vcr_6465c368e0ec | 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, 3, -20, -15, 1, 10, 8], "k": 19}, "output": [-20, -15, 1, 10, 8, 0, 3]}, {"input": {"nums": [5, -5, 3, 2, -8, -9, -2, -3, 9], "k": 7}, "output": [3, 2, -8, -9, -2, -3, 9, 5, -5]}, {"input": {"nums": [2, -7, -6, 1, 1, -3, -4, 0, 4], "k": 1}, "output": [4, 2, -7, -6, 1, 1, -3, -4, 0]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "912fe53c929be3ae"} |
vcr_8a1f8e995342 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 7, 3, 5, 2]}, "output": [210, 150, 350, 210, 525]}, {"input": {"nums": [4, 4, 4]}, "output": [16, 16, 16]}, {"input": {"nums": [1, 1, 3, 1]}, "output": [3, 3, 1, 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": "01bc0a42c891bba9"} |
vcr_6fd3d15def03 | 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 acda"}, "output": "acda e"}, {"input": {"s": "aa abbb aab abb b"}, "output": "b abb aab abbb aa"}, {"input": {"s": "a b aa"}, "output": "aa b 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": "3a597603ee822258"} |
vcr_2bc35e552da4 | 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, 2, 5, 27, 29, 32, 36, 39, 42, 46], "target": 5}, "output": 2}, {"input": {"nums": [2, 3, 4, 10, 11, 20, 27, 32], "target": 28}, "output": 7}, {"input": {"nums": [16, 23, 31], "target": 7}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "b61b2c1832d5396f"} |
vcr_485979924f03 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [3, 6, 5, 1, 7, 7, 2, 3]}, "output": [8820, 4410, 5292, 26460, 3780, 3780, 13230, 8820]}, {"input": {"nums": [1, 2, 5, 5, 2]}, "output": [100, 50, 20, 20, 50]}, {"input": {"nums": [1, 4, 3, 5, 4]}, "output": [240, 60, 80, 48, 60]}] | 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": "dd6d9d718e7a9a74"} |
vcr_348cc1ba3757 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [6, -18, -3, 2, -19, 2, 3], "k": 14}, "output": [6, -18, -3, 2, -19, 2, 3]}, {"input": {"nums": [5, 9, 5], "k": 1}, "output": [5, 5, 9]}, {"input": {"nums": [-6, 0, 9, -4, -10, -8, -3, -8, -4], "k": 3}, "output": [-3, -8, -4, -6, 0, 9, -4, -10, -8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "15f21eebbf945a2f"} |
vcr_d4409d39f6eb | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["acb", "cc", "bbcc", "aaba"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "baa", "aaa", "aaa", "baa", "baa", "abb"]}, "output": [2, 2, 3]}, {"input": {"strs": ["bab", "aaa", "aaa", "aba", "bab", "abb"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "88bbe5836c122140"} |
vcr_bd1eeb5150f7 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [17, 14, -11, -3, -3, 5, -10, 12, -1, 6]}, "output": 31}, {"input": {"nums": [9, -8, 6, 6, 9, 5]}, "output": 27}, {"input": {"nums": [3, -8, -5]}, "output": 3}] | 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": "6afc41a0451f3a3f"} |
vcr_4537a309438d | 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, 16, 17, 18, 22, 32], "target": 11}, "output": 2}, {"input": {"nums": [13, 17, 26, 37], "target": 26}, "output": 2}, {"input": {"nums": [3, 20, 36, 38], "target": 5}, "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": "666de226657097d6"} |
vcr_d32706d04d2f | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [24, 8, 5, 1, 27]}, "output": 56}, {"input": {"nums": [17, 11, 12, 16, 0]}, "output": 33}, {"input": {"nums": [1, 5, 1, 0, 7, 3, 13]}, "output": 25}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "1e6d113ab873cc88"} |
vcr_44cc30550a74 | 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, 6, 6, 9, 11, 11, 14, 15]}, "output": [0, 1, 6, 9, 11, 14, 15]}, {"input": {"nums": [2, 3, 8, 10, 12]}, "output": [2, 3, 8, 10, 12]}, {"input": {"nums": [1, 6, 8, 10, 10, 10]}, "output": [1, 6, 8, 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": "a56e904d77b6c85f"} |
vcr_f51326a4f30d | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-13, 18, -3, 17, -9, 0, 18, 18, 16, 19, -20, 5], "k": 7}, "output": [0, 18, 18, 16, 19, -20, 5, -13, 18, -3, 17, -9]}, {"input": {"nums": [1, 7, 1, -8, 3, -9, -3], "k": 12}, "output": [1, -8, 3, -9, -3, 1, 7]}, {"input": {"nums": [-10, 7, -10, 10, -5, 8], "k": 14}, "output": [-5, 8, -10, 7, -10, 10... | 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": "42ce3c09fa2228dd"} |
vcr_2e53e4834f9e | 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": [0, 20, 29, 37, 47], "target": 47}, "output": [0, 4]}, {"input": {"nums": [1, 3, 6, 12, 22], "target": 34}, "output": [3, 4]}, {"input": {"nums": [-9, -3, 0, 1, 2, 15, 18, 22, 23, 28, 36], "target": 23}, "output": [2, 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": "1dfef552e32da21b"} |
vcr_63c151efe7d6 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -13, -6, 1, 9, 12, 30, 31, 36, 39, 41], "target": -4}, "output": [1, 4]}, {"input": {"nums": [-7, -6, -2, 3, 6, 31], "target": -9}, "output": [0, 2]}, {"input": {"nums": [-15, -7, -5, -2, -1, 1, 6, 10, 24, 28, 38], "target": 23}, "output": [0, 10]}] | 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": "9fb9b3327f65e800"} |
vcr_60255e64e7fc | 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, 3, 0, 2, 1, 0, 4, 3, 4, 0]}, "output": false}, {"input": {"nums": [0, 1, 1, 2, 1]}, "output": false}, {"input": {"nums": [0, 0, 1, 0, 2, 1, 2, 1, 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": "051f4e46d2c328a5"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.