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_e2627345494f | 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": [6, 36, 1, 3, 10, 3, 1, 10, 79, 23, 36, 6, 23]}, "output": 79}, {"input": {"nums": [11, 11, 27, 50, 27]}, "output": 50}, {"input": {"nums": [25, 11, 25, 11, 27, 27, 14, 14, 53]}, "output": 53}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "5c78feea357d96a2"} |
vcr_b58afb41919b | 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": [-25, -3, 4, 16, 24, 26, 31, 36], "target": 1}, "output": [0, 5]}, {"input": {"nums": [-20, -14, 1, 3, 7, 18, 26, 36], "target": 12}, "output": [1, 6]}, {"input": {"nums": [-18, -14, -1, 3, 6, 15, 17, 27, 36], "target": 35}, "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": "eb0e51189dcdfd23"} |
vcr_f544c632794f | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["ba", "ccaa", "baac", "ab", "bcb", "baa"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "aab", "bab"]}, "output": [1, 2]}, {"input": {"strs": ["bab", "baa", "aab", "baa"]}, "output": [1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "27c148c9554bba93"} |
vcr_ab0fa8f66174 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 5, 5, 5, 9, 9]}, "output": [1, 5, 9]}, {"input": {"nums": [0, 0, 0, 2, 3, 5, 5, 8, 8, 9, 11, 12]}, "output": [0, 2, 3, 5, 8, 9, 11, 12]}, {"input": {"nums": [0, 4, 6, 10, 10, 11, 11]}, "output": [0, 4, 6, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "fbb104b03ed37c30"} |
vcr_a9404f6ce327 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 4, 17, 21, 25, 31, 33, 34, 49], "target": 39}, "output": 8}, {"input": {"nums": [1, 3, 5, 13, 14, 22, 31, 35, 36], "target": 5}, "output": 2}, {"input": {"nums": [7, 14, 16, 19, 22, 30, 31, 33, 36, 38], "target": 26}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "8fcacace3caf4866"} |
vcr_3f97a83b232e | 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, 17, 20]}, "output": 44}, {"input": {"nums": [0, 12, 1, 0]}, "output": 12}, {"input": {"nums": [12, 16, 5, 2, 10]}, "output": 27}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "4e4d35a7ee422d94"} |
vcr_90350ccab682 | 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": "caccaccacc"}, "output": 7}, {"input": {"s": "bbcacbaacc"}, "output": 7}, {"input": {"s": "ccaaaacb"}, "output": 4}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "aa9b265f29e2c066"} |
vcr_2a891a518f8f | 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, 2, 3, 4, 5, 6, 11, 11, 14, 14]}, "output": [2, 3, 4, 5, 6, 11, 14]}, {"input": {"nums": [0, 0, 1, 3, 5, 5, 6]}, "output": [0, 1, 3, 5, 6]}, {"input": {"nums": [2, 4, 6, 7]}, "output": [2, 4, 6, 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": "5661eaf589eccf49"} |
vcr_a35c59e5fabc | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [6, 23, 25, 9, 3, 14, 23, 19, 14, 30, 11, 29]}, "output": 124}, {"input": {"nums": [19, 3, 7, 13, 16, 11]}, "output": 43}, {"input": {"nums": [0, 1]}, "output": 1}] | 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": "10ccce4c9c0e40cd"} |
vcr_6bce78873656 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [15, 59, 21, 22, 21, 15, 16, 22, 16]}, "output": 59}, {"input": {"nums": [9, 14, 14, 57, 9]}, "output": 57}, {"input": {"nums": [1, 19, 13, 1, 42, 13, 19, 25, 25]}, "output": 42}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "2919b917f70d72a5"} |
vcr_9c7e094332f2 | 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": 22, "b": 38}, "output": 2}, {"input": {"a": 74, "b": 86}, "output": 2}, {"input": {"a": 96, "b": 170}, "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": "3a7fb1c839fb506f"} |
vcr_5d957c685fae | 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": [30, 28, 76, 28, 23, 30, 23]}, "output": 76}, {"input": {"nums": [26, 56, 7, 7, 21, 26, 24, 21, 24]}, "output": 56}, {"input": {"nums": [6, 24, 47, 6, 24]}, "output": 47}] | 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": "f11a6efcf9ef80cb"} |
vcr_bbb64bec6bc3 | 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": 219, "b": 238}, "output": 1}, {"input": {"a": 27, "b": 126}, "output": 9}, {"input": {"a": 102, "b": 110}, "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": "fc4acb3d9ae6fecc"} |
vcr_c984462e183f | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-28, -4, -2, 8, 20, 25, 27, 30, 48], "target": 68}, "output": [4, 8]}, {"input": {"nums": [-8, 0, 5, 6, 15, 20, 32], "target": 12}, "output": [0, 5]}, {"input": {"nums": [-8, 5, 6, 12, 21, 22, 28, 32], "target": 13}, "output": [0, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "66631b25fc2940c2"} |
vcr_93dbdf556d8c | 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": ["cc", "cac", "abbc", "aa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aab", "bbb", "bbb"]}, "output": [1, 2]}, {"input": {"strs": ["baa", "aba", "bab", "baa", "bab", "bba", "bbb"]}, "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": "c02bafdc1703dc8a"} |
vcr_106c7fe78d21 | 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, 2, 4, 3, 4, 4, 0]}, "output": true}, {"input": {"nums": [1, 3, 1, 2, 1, 3, 3, 1]}, "output": true}, {"input": {"nums": [3, 0, 1, 3, 2, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "661ed6a37ae260e7"} |
vcr_2adc30b5ce25 | 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": 184, "b": 132}, "output": 4}, {"input": {"a": 141, "b": 131}, "output": 1}, {"input": {"a": 168, "b": 38}, "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": "d5229a92985abbe9"} |
vcr_e7b2c3dc4046 | 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": "eaa bdd"}, "output": "bdd eaa"}, {"input": {"s": "baba ab b aa"}, "output": "aa b ab baba"}, {"input": {"s": "aa bb baa"}, "output": "baa bb aa"}] | 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": "05bf57291fb20597"} |
vcr_fd8fa5865c22 | 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": 10113}, "output": 6}, {"input": {"n": 5877}, "output": 9}, {"input": {"n": 16287}, "output": 12}] | 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": "8232c8be0484a979"} |
vcr_fe6f89814ff9 | 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": [37, 40, 26, 21, 35, 23, 57, 58, 27, 30, 50, 45, 22, 6]}, "output": 37}, {"input": {"prices": [28, 19, 13]}, "output": 0}, {"input": {"prices": [2, 10, 24, 11, 30, 1, 18, 6, 29]}, "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": "9c7f5351d682fe69"} |
vcr_c4dbd02008e9 | 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, 19, 21, 22, 23, 31, 34, 40], "target": 50}, "output": 8}, {"input": {"nums": [2, 20, 26, 28], "target": 0}, "output": 0}, {"input": {"nums": [1, 5, 15, 17, 32, 35, 39], "target": 35}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "18b7e0a60551f4d9"} |
vcr_88989ce089f9 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [16, -5, 18, -3, -8, 2, 13, -1, -11, 19]}, "output": 40}, {"input": {"nums": [6, 14, -10]}, "output": 20}, {"input": {"nums": [13, 6, -10, 13, -7]}, "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": "c5dc77602293509e"} |
vcr_b383a1cb83ba | 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, 2, -3, 1, -19, 17, 9, -1, 10, 7], "k": 15}, "output": [17, 9, -1, 10, 7, -13, 2, -3, 1, -19]}, {"input": {"nums": [-7, -6, -6, -5, -7, 10], "k": 17}, "output": [-6, -6, -5, -7, 10, -7]}, {"input": {"nums": [-5, 4, 9], "k": 4}, "output": [9, -5, 4]}] | 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": "3ab9c0ba3f49e28a"} |
vcr_8a8abfef9912 | 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": 240, "b": 203}, "output": 1}, {"input": {"a": 151, "b": 56}, "output": 1}, {"input": {"a": 154, "b": 21}, "output": 7}] | 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": "6c3296e4e2a7b53b"} |
vcr_37370991ec5e | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 4, 6, 8, 10, 11, 14]}, "output": [0, 4, 6, 8, 10, 11, 14]}, {"input": {"nums": [0, 2, 3, 3, 4, 6, 6, 7, 10, 11, 11, 12]}, "output": [0, 2, 3, 4, 6, 7, 10, 11, 12]}, {"input": {"nums": [0, 1, 2, 3, 5, 5, 5, 6, 8, 9, 11]}, "output": [0, 1, 2, 3, 5, 6, 8, 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": "9ac03a669ac89688"} |
vcr_8ab873a4ed98 | 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, 26, 6]}, "output": 26}, {"input": {"nums": [16, 9, 7, 13, 1, 7]}, "output": 36}, {"input": {"nums": [11, 7, 17, 16, 14, 3, 11]}, "output": 53}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "db51f8b5c37cc456"} |
vcr_c5150f8981e3 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [5, 18, 29, 5, 13, 5, 0, 10, 1, 20, 28]}, "output": 85}, {"input": {"nums": [5, 15, 13, 8]}, "output": 23}, {"input": {"nums": [2, 20, 9, 16, 0, 18, 8, 6, 11]}, "output": 65}] | 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": "fcca03a3befc433d"} |
vcr_9acfcc04f3d5 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 1, 2, 2, 3, 7, 9, 14, 15]}, "output": [1, 2, 3, 7, 9, 14, 15]}, {"input": {"nums": [2, 4, 5, 5, 5, 6, 7, 11, 11, 12, 12, 12]}, "output": [2, 4, 5, 6, 7, 11, 12]}, {"input": {"nums": [1, 3, 4, 5, 6, 6, 8, 8, 10, 11, 12]}, "output": [1, 3, 4, 5, 6, 8, 10, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "6f0ba8a8e16fc598"} |
vcr_8599bf961305 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-7, 12, -5, -14, 14, -2, -12, 3, 19, 3, 17, 2, -2, -4]}, "output": 44}, {"input": {"nums": [5, 13, 10, 2, -12, 14, 4, -11, -2, 3, 9]}, "output": 36}, {"input": {"nums": [-8, 3, 7]}, "output": 10}] | 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": "0331a6dc96ce8113"} |
vcr_5516257833d3 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [8, 20, 22, 26, 42], "target": 4}, "output": 0}, {"input": {"nums": [1, 17, 20, 21, 22, 38], "target": 6}, "output": 1}, {"input": {"nums": [0, 2, 4, 5, 10, 33], "target": 29}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "a9bfb734407e1e2d"} |
vcr_443d23480a16 | 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, 0, 0, 4, 2, 3, 1, 0]}, "output": true}, {"input": {"nums": [1, 2, 3, 2, 2, 1]}, "output": true}, {"input": {"nums": [0, 2, 2, 2, 1]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "871a6bca945262bf"} |
vcr_4f2ccd1d96df | 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, -12, 14, -20, -11, -14, 3, 9, -7, 15, 18, -18], "k": 15}, "output": [15, 18, -18, -2, -12, 14, -20, -11, -14, 3, 9, -7]}, {"input": {"nums": [-4, 1, 6, -7, -10, -7, -7, 10, 8], "k": 15}, "output": [-7, -10, -7, -7, 10, 8, -4, 1, 6]}, {"input": {"nums": [9, 0, 6], "k": 2}, "output": [0, 6, 9]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "47e0203206b6c3de"} |
vcr_dee1725bd2ed | 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": ["caa", "bc", "ccab", "bcca", "bb", "bb", "ba"]}, "output": [1, 1, 1, 2, 2]}, {"input": {"strs": ["aab", "aab", "bbb", "bab", "aba"]}, "output": [1, 1, 3]}, {"input": {"strs": ["bab", "aab", "aba"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "882966967d6439d8"} |
vcr_125aab621470 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [8, 15, 20, 24, 28, 32, 38, 43], "target": 0}, "output": 0}, {"input": {"nums": [0, 4, 5, 8, 12, 20, 23, 25, 26, 30, 33, 37], "target": 16}, "output": 5}, {"input": {"nums": [11, 17, 24, 31], "target": 14}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "1e17ca3011858c54"} |
vcr_2271624385b7 | 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, 7, 3, 30, 47, 13, 20, 6]}, "output": true}, {"input": {"nums": [11, 8, 4]}, "output": false}, {"input": {"nums": [1, 12, 15, 4, 4, 19, 18, 22, 7]}, "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": "ed5daf92f144349f"} |
vcr_34502b4666e1 | 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": 230, "b": 243}, "output": 1}, {"input": {"a": 103, "b": 52}, "output": 1}, {"input": {"a": 103, "b": 130}, "output": 1}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "4155d00b138b598d"} |
vcr_852dce2c4910 | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [53, 56, 61, -17, 23, 12, 20, 75, 5, -20, 47, -2, 64], "target": 100}, "output": [0, 10]}, {"input": {"nums": [12, 18, -14, -8, 2, -28, -23], "target": -42}, "output": [2, 5]}, {"input": {"nums": [-5, 17, 12, 2, -9, 30, 42, -22, 21], "target": 23}, "output": [3, 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": "b34a63e2a012a452"} |
vcr_b670c65f892c | 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": "bcbaaabcca"}, "output": 7}, {"input": {"s": "cbcbaaacbcc"}, "output": 8}, {"input": {"s": "bcbbbbbbcca"}, "output": 5}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "949522a20e8c9c7c"} |
vcr_4856701fb464 | 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": [21, -14, -4, -7, -9, 4, -18, -7, 11, 14, -6, -2, 1, 19]}, "output": 37}, {"input": {"nums": [11, 8, -3, -4]}, "output": 19}, {"input": {"nums": [-5, 10, -6, -5, 3]}, "output": 10}] | 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": "07ef6de948355065"} |
vcr_4bb35a222e21 | 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 a ccebbc d edadbd"}, "output": "edadbd d ccebbc a e"}, {"input": {"s": "ab b abaa baa"}, "output": "baa abaa b ab"}, {"input": {"s": "b b"}, "output": "b b"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "0b574d07d1d5ae5a"} |
vcr_acd89281b6cb | 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": [174, 68, 157, 101, 69, 171, 28, 5, 180, 24, 1, 11, 33, 70, 173, 186, 44]}, "output": false}, {"input": {"nums": [0, 18, 19, 6, 18, 8, 7, 13, 25, 14]}, "output": true}, {"input": {"nums": [23, 20, 4, 6, 15, 24, 16, 23, 10, 7, 8]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "d6af46b86608b025"} |
vcr_2600c9e385ff | 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": [25, 12, 19, 8, 24, 11, 2, 12, 21]}, "output": 91}, {"input": {"nums": [17, 18, 10, 2, 14, 20, 14, 8, 12, 10]}, "output": 67}, {"input": {"nums": [11, 3]}, "output": 11}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "29b62d7eb1262043"} |
vcr_dceaf0f57700 | 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": "acbcbababcbb"}, "output": 11}, {"input": {"s": "bba"}, "output": 2}, {"input": {"s": "bcab"}, "output": 4}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "d9805b77a4c12273"} |
vcr_2f5943b0c487 | 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, 6, 16, 17, 18, 19, 20, 23, 24, 26, 36, 38, 43], "target": 14}, "output": 2}, {"input": {"nums": [19, 31, 35], "target": 25}, "output": 1}, {"input": {"nums": [4, 10, 21], "target": 30}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "6e3be79cf7a26aa5"} |
vcr_a4d5f943501a | 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": [22, 13, 25, 23, 18, 2, 25, 26, 30, 8, 25, 3]}, "output": 145}, {"input": {"nums": [16, 2, 0, 6, 7, 20, 14, 6, 15]}, "output": 57}, {"input": {"nums": [20, 14, 6, 6, 17, 1, 5]}, "output": 48}] | 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": "b1e14035f2d65fcb"} |
vcr_9f8f617e8e54 | 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": "cecc dbbeed bbbcc ddb"}, "output": "ddb bbbcc dbbeed cecc"}, {"input": {"s": "bbab baa a"}, "output": "a baa bbab"}, {"input": {"s": "b baab b ba"}, "output": "ba b baab b"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "2f7b0b90da68fd31"} |
vcr_6b8bdb0433ba | 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": [173, 47, 107, 98, 171]}, "output": false}, {"input": {"nums": [0, 22, 24, 12, 6, 8, 0, 5]}, "output": true}, {"input": {"nums": [18, 9, 6, 11, 6, 1, 3, 3, 16, 14, 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": "016f7131807dd153"} |
vcr_666a0f936700 | 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": [11, -2, -6, -14, -3, 16, -10, -6, -16, -15], "k": 1}, "output": [-15, 11, -2, -6, -14, -3, 16, -10, -6, -16]}, {"input": {"nums": [8, -6, 10], "k": 9}, "output": [8, -6, 10]}, {"input": {"nums": [-5, 5, -8, -2, -5, 1, -10, 5], "k": 3}, "output": [1, -10, 5, -5, 5, -8, -2, -5]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "5ce04568a4527238"} |
vcr_b8e7239a21f5 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-15, -4, -11, -13], "k": 9}, "output": [-13, -15, -4, -11]}, {"input": {"nums": [-3, 4, 0, 2], "k": 6}, "output": [0, 2, -3, 4]}, {"input": {"nums": [-7, 10, -3, -6, -7, 1, 10, -8, 2, -8], "k": 18}, "output": [-3, -6, -7, 1, 10, -8, 2, -8, -7, 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": "fa6042e39179f324"} |
vcr_5fa30a87482c | 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, -6, -5, 7, 13, 34, 37, 49], "target": 62}, "output": [4, 7]}, {"input": {"nums": [-20, -10, -6, -1, 2, 9, 15, 17, 21, 33, 34], "target": 15}, "output": [2, 8]}, {"input": {"nums": [-13, 8, 13, 26, 28, 39], "target": 47}, "output": [1, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "aad22be05d96867c"} |
vcr_d684998ee71c | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 4, 2, 5, 3, 1, 1]}, "output": [120, 210, 420, 168, 280, 840, 840]}, {"input": {"nums": [3, 1, 1, 3]}, "output": [3, 9, 9, 3]}, {"input": {"nums": [2, 3, 2, 1, 1]}, "output": [6, 4, 6, 12, 12]}] | 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": "25fc80b476588143"} |
vcr_586f15907255 | 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": [25, 0, 50, 10, 39, 12, 25]}, "output": true}, {"input": {"nums": [12, 9, 18]}, "output": false}, {"input": {"nums": [21, 8, 9]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "2701ae198a20cfb2"} |
vcr_979c2168a9a1 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-3, 1, 21, -7, 9, -11, -9, -3, -18, 5, 18, -4, 22, -18]}, "output": 41}, {"input": {"nums": [13, 14, -8]}, "output": 27}, {"input": {"nums": [-5, 4, 0, -2, -7]}, "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": "2c398d2209548664"} |
vcr_e1d1964c0040 | 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, 111, 16, 100, 147, 31, 33, 188, 117, 83, 99, 194, 20, 157, 122, 95, 184]}, "output": false}, {"input": {"nums": [10, 19, 17, 11, 1, 25, 22]}, "output": false}, {"input": {"nums": [17, 12, 20, 16, 19, 10]}, "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": "dcd90e837400d6e2"} |
vcr_01f08a4d378d | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 1, 10, 18, 24, 28, 29, 35, 37, 40, 48], "target": 23}, "output": 4}, {"input": {"nums": [1, 2, 4, 6, 9, 12, 13, 16, 36], "target": 6}, "output": 3}, {"input": {"nums": [30, 31, 37, 39], "target": 28}, "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": "12b65eb581054553"} |
vcr_d3111e4e4003 | 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": [47, 19, 55, 55, 44, 32, 27, 59, 4, 9, 53, 26, 22, 29]}, "output": 49}, {"input": {"prices": [16, 31, 33, 24]}, "output": 17}, {"input": {"prices": [23, 24, 20, 23, 24, 5, 28, 33, 4, 20]}, "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": "452ed99936b95ca4"} |
vcr_4613395db0ba | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [5, 17, 29, 30, 22]}, "output": 56}, {"input": {"nums": [2, 18, 18, 4, 6, 19, 12]}, "output": 41}, {"input": {"nums": [11, 12, 12, 1, 14, 4]}, "output": 37}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "aa80f029abf26948"} |
vcr_3ce4584ab884 | 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": [36, 36, 13, 22, 13, 59, 22]}, "output": 59}, {"input": {"nums": [7, 16, 25, 16, 25, 26, 26, 7, 46]}, "output": 46}, {"input": {"nums": [17, 16, 17, 51, 16]}, "output": 51}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "a739b1d71c1839b7"} |
vcr_27c41a937a75 | 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": [1, 2, -17, 3, 19, 7, -4, 21, -11, 8, 20, 5, 11, 11, 3]}, "output": 93}, {"input": {"nums": [6, 1, 7]}, "output": 14}, {"input": {"nums": [3, 10, 8, -11, -3, 6, 14]}, "output": 27}] | 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": "0bc2b81e7cf6f52b"} |
vcr_1855092d413c | 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, -5, -14, -1, -18, -8, -9], "k": 10}, "output": [-18, -8, -9, 14, -5, -14, -1]}, {"input": {"nums": [9, -2, 10, -9, 7, -1], "k": 6}, "output": [9, -2, 10, -9, 7, -1]}, {"input": {"nums": [-3, 5, 0, -1, 0, -5], "k": 16}, "output": [0, -1, 0, -5, -3, 5]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "6ea04566a695f667"} |
vcr_b6a13bb4d6ee | 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": "bbbabbbccba"}, "output": 6}, {"input": {"s": "cbcba"}, "output": 5}, {"input": {"s": "abababbac"}, "output": 8}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "c5cb736d955a15a6"} |
vcr_c881d99e1c8d | 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": [-22, 2, 34, 15, -20, 12, 13, 21, -12]}, "output": [-22, -20, 14, 29, 9, 21, 34, 55, 43]}, {"input": {"nums": [-11, 8]}, "output": [-11, -3]}, {"input": {"nums": [-9, 4, 18, 5, 6, -1, -8, 7, -3]}, "output": [-9, -5, 13, 18, 24, 23, 15, 22, 19]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "5eda1f8d50a30f35"} |
vcr_0daa4ef43beb | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-2, 20, 1, -16, -11, 12, 14, 21, -14, -18, -5, 4, 12, -9]}, "output": 47}, {"input": {"nums": [7, 13, 13, 13, -3, 5, 14, -9, 3]}, "output": 62}, {"input": {"nums": [6, 1, 4, 3, 12, -12, 9, 1, 3, 11, 3]}, "output": 41}] | 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": "75c534896b419db4"} |
vcr_b4efe12ca39b | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 3, 4, 6, 11, 11]}, "output": [1, 3, 4, 6, 11]}, {"input": {"nums": [8, 11, 11]}, "output": [8, 11]}, {"input": {"nums": [0, 0, 1, 3, 5, 11, 11]}, "output": [0, 1, 3, 5, 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": "b08f527bbce5ccbb"} |
vcr_cba752dc2a3d | 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": ["abc", "caa", "ccc", "aa", "aaa"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bab", "baa", "baa", "bab"]}, "output": [2, 2]}, {"input": {"strs": ["abb", "aab", "bab", "bba", "bab", "aab"]}, "output": [2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "0db1db58b835d42c"} |
vcr_a6a40c0e4271 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [8, 9, 1, 20, 5, 16]}, "output": 45}, {"input": {"nums": [20, 19, 15, 1]}, "output": 35}, {"input": {"nums": [18, 19, 5, 4]}, "output": 23}] | 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": "a115fd0ce31c1da3"} |
vcr_6519320baeee | 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": ["caa", "cbab", "ccbb", "ccca", "aaab", "aa", "aa", "aaa", "ccc"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "abb", "aaa", "baa", "aaa"]}, "output": [1, 2, 2]}, {"input": {"strs": ["bbb", "bab", "aba", "bbb", "baa", "bba", "aab"]}, "output": [2, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "0ff49ed8571d6535"} |
vcr_436fe9a7e354 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [1, -5, 21, 32, 31, 6, 11, 31, -17]}, "output": [1, -4, 17, 49, 80, 86, 97, 128, 111]}, {"input": {"nums": [5, 2, 14, -1, -4, -8, 5]}, "output": [5, 7, 21, 20, 16, 8, 13]}, {"input": {"nums": [-4, 11, 13, 5, -4, -3, -10, 6, -1, 13]}, "output": [-4, 7, 20, 25, 21, 18, 8, 14, 13, 26]}] | 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": "45ff0086f5ca035e"} |
vcr_e2cb13939e34 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [32, 3, 29, 32, 3, 5, 19, 20, 34, 34, 19, 5, 52, 29, 20, 21, 21]}, "output": 52}, {"input": {"nums": [18, 25, 45, 25, 18]}, "output": 45}, {"input": {"nums": [41, 6, 16, 2, 6, 16, 2]}, "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": "cbb1e4e8cd3df18e"} |
vcr_198928b9d9d1 | 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": [56, 13, 5, 37, 5, 37, 13]}, "output": 56}, {"input": {"nums": [28, 29, 29, 53, 23, 28, 23]}, "output": 53}, {"input": {"nums": [26, 21, 1, 26, 11, 21, 50, 9, 9, 11, 3, 1, 3]}, "output": 50}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "a4ad34c3982d4b57"} |
vcr_760b69f6c35e | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-16, -9, -16, 19, 5, 21]}, "output": 45}, {"input": {"nums": [-12, -12, 11, 3, 3, -12, 5, -1, 12, 12, -10]}, "output": 33}, {"input": {"nums": [-3, 12, 6, 5, 7, 10, -6, -8, 6, -9]}, "output": 40}] | 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": "73d66e7891845ea2"} |
vcr_29484b9a4a46 | 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, 6, 8, 10, 10, 11, 12, 13]}, "output": [0, 2, 3, 4, 6, 8, 10, 11, 12, 13]}, {"input": {"nums": [2, 2, 3, 5, 6, 6, 6, 7, 7, 9, 9, 11]}, "output": [2, 3, 5, 6, 7, 9, 11]}, {"input": {"nums": [2, 3, 6, 7, 7, 8, 10, 12, 12]}, "output": [2, 3, 6, 7, 8, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "8a39d52368b5da61"} |
vcr_62092d34c096 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 5, 6, 7, 7, 8, 8, 10, 10, 13, 13, 14, 14]}, "output": [1, 5, 6, 7, 8, 10, 13, 14]}, {"input": {"nums": [0, 5, 6, 11, 12]}, "output": [0, 5, 6, 11, 12]}, {"input": {"nums": [0, 1, 4, 4, 5, 8, 9, 11]}, "output": [0, 1, 4, 5, 8, 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": "d8e0bc327b3b0da9"} |
vcr_06994f860dc8 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [11, -17, -2, -1, -13, 22, 10, 2, 13, -5, 6, 8]}, "output": 56}, {"input": {"nums": [-6, -5, -4]}, "output": -4}, {"input": {"nums": [3, -6, 0, 14, 4, -1, 4]}, "output": 21}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "8c684d31cef4dd42"} |
vcr_bfbf9d27f8d2 | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [25, 48, 17, -8, -5, 37, 16], "target": 85}, "output": [1, 5]}, {"input": {"nums": [19, 35, 30, 10, -3, 42], "target": 16}, "output": [0, 4]}, {"input": {"nums": [40, 31, 35, 37], "target": 66}, "output": [1, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "e155ae409b94bcfc"} |
vcr_82b6b31e9573 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [32, 2, 18, 1, 37, 2, 1, 73, 36, 37, 18, 32, 9, 36, 9]}, "output": 73}, {"input": {"nums": [8, 53, 20, 11, 8, 4, 4, 27, 11, 20, 27]}, "output": 53}, {"input": {"nums": [24, 14, 56, 14, 24]}, "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": "7c72c8610f9964b0"} |
vcr_ac8122ea82d4 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 4, 3, 0, 4, 0, 0]}, "output": true}, {"input": {"nums": [1, 1, 3, 3, 1, 0, 2, 2]}, "output": true}, {"input": {"nums": [2, 1, 2, 2, 2, 0, 1, 1, 0]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "229427dc85ae29ff"} |
vcr_009b206fd5b7 | 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": "bbbaacababbabb"}, "output": 9}, {"input": {"s": "cabcacccbc"}, "output": 8}, {"input": {"s": "aaacaa"}, "output": 3}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "d189398b47c88693"} |
vcr_ca80042eb38e | 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, -20, -5, 12, 5, 5, 10, 18], "k": 1}, "output": [18, 14, -20, -5, 12, 5, 5, 10]}, {"input": {"nums": [-7, -1, -1, -4, -5, -7], "k": 19}, "output": [-7, -7, -1, -1, -4, -5]}, {"input": {"nums": [1, 7, 6, 4, -4, 6, 5], "k": 10}, "output": [-4, 6, 5, 1, 7, 6, 4]}] | 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": "30c4d62bc5bb9cbd"} |
vcr_a01eed1752a9 | 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": [22, 25, 11, 30, 22, 8, 19, 4, 11, 6, 22, 26]}, "output": 111}, {"input": {"nums": [6, 7, 7, 20, 6, 16, 10]}, "output": 43}, {"input": {"nums": [1, 2, 18]}, "output": 19}] | 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": "49a3fdb3ba117a67"} |
vcr_9ad63ea558ca | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [12, 12, 13, 14, 26]}, "output": 51}, {"input": {"nums": [12, 1]}, "output": 12}, {"input": {"nums": [4, 0, 9, 12, 10, 12, 8, 0, 3, 2]}, "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": "19f475edfd8ffe14"} |
vcr_33d90df00333 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-21, -20, -6, 15, 23, 31, 40], "target": 38}, "output": [3, 4]}, {"input": {"nums": [-9, -4, 8, 9, 10, 17, 27, 34], "target": 35}, "output": [2, 6]}, {"input": {"nums": [-6, 1, 20, 26], "target": -5}, "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": "893145d0a0cbf92c"} |
vcr_5add8b8e8041 | 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": ["cacb", "bcca", "abb", "baba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aba", "aaa", "bbb"]}, "output": [1, 1, 1]}, {"input": {"strs": ["bab", "baa", "baa", "bbb", "baa", "bab"]}, "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": "f4c1665aac17a475"} |
vcr_1d32c3dca4a2 | 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": [75, 6, 6, 16, 2, 18, 2, 16, 18]}, "output": 75}, {"input": {"nums": [1, 12, 56, 1, 12]}, "output": 56}, {"input": {"nums": [1, 17, 12, 1, 17, 12, 47]}, "output": 47}] | 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": "a553bc81388af4ac"} |
vcr_4a283babe77b | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [4, 5, 5, 7, 11, 12, 14]}, "output": [4, 5, 7, 11, 12, 14]}, {"input": {"nums": [0, 2, 3, 4, 7, 7, 8, 8, 8, 11]}, "output": [0, 2, 3, 4, 7, 8, 11]}, {"input": {"nums": [2, 2, 6, 6, 10, 12, 12]}, "output": [2, 6, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "424e5552c1bb9a82"} |
vcr_cf785fed1a52 | 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": [12, 14, 6, 3, -11, -10, -4, 22]}, "output": 35}, {"input": {"nums": [-9, 13, 13, -10, 4, 4, 14, 14, -11, -11, 2]}, "output": 52}, {"input": {"nums": [-7, 5, 10, -7, 5]}, "output": 15}] | 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": "fe5a6abb51a60420"} |
vcr_5baaa6fe01bf | 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": [5, 12, 14, 21, 25, 29, 31, 42, 43, 45], "target": 49}, "output": 10}, {"input": {"nums": [21, 22, 29], "target": 33}, "output": 3}, {"input": {"nums": [6, 27, 35], "target": 8}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "93a0a1363752272e"} |
vcr_a30c7a111f2b | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "cccacccddcccaccc"}, "output": true}, {"input": {"s": "baccb"}, "output": false}, {"input": {"s": "caabcacba"}, "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": "196a7b655ea21781"} |
vcr_0bf45097a2a0 | 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, 0, 4, 5, 5, 6, 11, 12, 15]}, "output": [0, 4, 5, 6, 11, 12, 15]}, {"input": {"nums": [0, 0, 1, 6, 7, 8, 8, 9, 9, 10]}, "output": [0, 1, 6, 7, 8, 9, 10]}, {"input": {"nums": [2, 7, 8, 8, 10, 11]}, "output": [2, 7, 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": "d9b5cdd7aab38a93"} |
vcr_f652ad3f2ad6 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-1, 14, -14, -1], "k": 0}, "output": [-1, 14, -14, -1]}, {"input": {"nums": [-9, 6, 1, 0], "k": 11}, "output": [6, 1, 0, -9]}, {"input": {"nums": [8, 8, 7, -6, -2, -1, -5, 6, 0], "k": 15}, "output": [-6, -2, -1, -5, 6, 0, 8, 8, 7]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "823d53a52deb918f"} |
vcr_793ceadf4918 | 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": [39, 39, 43, 15]}, "output": 4}, {"input": {"prices": [16, 4, 6, 38, 12, 35, 16, 6, 9]}, "output": 34}, {"input": {"prices": [40, 25, 33]}, "output": 8}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "aa0541747c7bfba5"} |
vcr_bacfdba38b76 | 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, 4, 18, 25, 25, 64, 4]}, "output": 64}, {"input": {"nums": [16, 47, 23, 8, 17, 5, 8, 23, 16, 17, 5]}, "output": 47}, {"input": {"nums": [11, 11, 10, 3, 19, 46, 25, 3, 19, 25, 10]}, "output": 46}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "fcb10a9dbdbfc8a3"} |
vcr_4c442b32b857 | 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, 1, 3, 2, 2, 6, 1, 6]}, "output": [432, 1728, 576, 864, 864, 288, 1728, 288]}, {"input": {"nums": [2, 1, 2, 1, 1, 4]}, "output": [8, 16, 8, 16, 16, 4]}, {"input": {"nums": [1, 3, 4, 3, 2]}, "output": [72, 24, 18, 24, 36]}] | 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": "e1c906008a702023"} |
vcr_a4dc83d617f2 | 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 adae beabdc"}, "output": "beabdc adae b"}, {"input": {"s": "a aaa a"}, "output": "a aaa a"}, {"input": {"s": "bba baa bbab ba"}, "output": "ba bbab baa bba"}] | 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": "c0066199d7026675"} |
vcr_4d8c5d68b0cb | 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, 4, 6, 14, 15, 32, 35, 36, 40, 46, 49], "target": 43}, "output": 9}, {"input": {"nums": [10, 13, 20, 24, 28, 29, 33, 34, 35, 37, 39], "target": 9}, "output": 0}, {"input": {"nums": [0, 14, 17, 23, 26, 27, 29, 39], "target": 27}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "e9157cef93091cc0"} |
vcr_62b78b4d8a54 | 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": [30, 8, 24, 6, 30, 31, 6, 8, 65, 31, 24]}, "output": 65}, {"input": {"nums": [28, 8, 28, 8, 53]}, "output": 53}, {"input": {"nums": [10, 40, 20, 10, 20]}, "output": 40}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "3efa797240b9807b"} |
vcr_469c24091787 | 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": 25290}, "output": 7}, {"input": {"n": 8128}, "output": 7}, {"input": {"n": 2521}, "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": "0de879d334cdc6a4"} |
vcr_c6972b06d555 | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "dcacccbcccacd"}, "output": true}, {"input": {"s": "aabcacbb"}, "output": false}, {"input": {"s": "abbcaaabc"}, "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": "ba8c61ac13358859"} |
vcr_98c0f8d041db | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 7, 3, 6, 3, 5]}, "output": [1890, 1890, 4410, 2205, 4410, 2646]}, {"input": {"nums": [5, 1, 2, 3, 5, 3]}, "output": [90, 450, 225, 150, 90, 150]}, {"input": {"nums": [1, 4, 1, 1]}, "output": [4, 1, 4, 4]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "f4fbf957fd71ed97"} |
vcr_496759f4bd1c | medium | binary_search | Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing. | def first_bad_version(n: int, bad: int) -> int: | Lower-bound binary search on the is_bad predicate. | def first_bad_version(n: int, bad: int) -> int:
def is_bad(v):
return v >= bad
lo, hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
if is_bad(mid):
hi = mid
else:
lo = mid + 1
return lo
| [{"input": {"n": 40, "bad": 2}, "output": 2}, {"input": {"n": 6, "bad": 1}, "output": 1}, {"input": {"n": 19, "bad": 7}, "output": 7}] | true | python | 73591920a83cf64d | ### Problem
Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.
### Reasoning
Lower-bound binary search on the is_bad predicate.
### Solution
```python
def first_bad_version(n: int, bad: int) -> int:
def is_bad(v):
return v >= bad
lo, hi = 1, ... | {"v": "ok", "content_hash": "9c13c492996c32c7"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.