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_3f10286f4ad6 | 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": [199, 181, 41, 59, 11, 4, 177, 64, 146, 147, 121, 43, 155, 20, 170, 30, 36, 167]}, "output": false}, {"input": {"nums": [10, 15, 16, 9, 9, 7, 18, 14]}, "output": true}, {"input": {"nums": [11, 20, 8]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "1bf54560c64a9d5e"} |
vcr_38226a951c18 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [23, 26, 29, 1, 6, 10, 11, 9]}, "output": 71}, {"input": {"nums": [19, 3, 9, 10, 9, 7, 17]}, "output": 54}, {"input": {"nums": [12, 2, 6, 18, 1, 14, 16, 7]}, "output": 51}] | 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": "6630000412b31f50"} |
vcr_d0cd3dcfc782 | 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": 37428}, "output": 6}, {"input": {"n": 10703}, "output": 9}, {"input": {"n": 2967}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "325d1f6832075ace"} |
vcr_39f0ef005fe7 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [49, 69, 79, 76, -26], "target": 50}, "output": [3, 4]}, {"input": {"nums": [49, 32, 14, 17, -18, 45, 15], "target": -3}, "output": [4, 6]}, {"input": {"nums": [42, 7, -5, -1, 40, -18, 10, 15, 20, 25], "target": 9}, "output": [3, 6]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "5d2d96470dba7f7a"} |
vcr_ca68517ceb1d | 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, 5, 19, 33, 47], "target": 35}, "output": 4}, {"input": {"nums": [0, 1, 8, 22, 33, 34, 37], "target": 39}, "output": 7}, {"input": {"nums": [7, 16, 19, 21, 22, 23, 33, 35, 36], "target": 26}, "output": 6}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "31a8172eaf2bc905"} |
vcr_4e16d89f6e86 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [19, -12, -3, 59, -30, 2, -25, 73, 0, 67, 30], "target": 73}, "output": [7, 8]}, {"input": {"nums": [9, 36, -19, 21, -24, 29, 11], "target": 47}, "output": [1, 6]}, {"input": {"nums": [-12, 13, 12, 0], "target": 25}, "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": "9ca476af8d0f5b1e"} |
vcr_4b94eb5fb7dd | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 3, 5, 2, 2, 3]}, "output": [180, 60, 36, 90, 90, 60]}, {"input": {"nums": [3, 3, 4, 1, 2]}, "output": [24, 24, 18, 72, 36]}, {"input": {"nums": [3, 2, 4, 1, 4]}, "output": [32, 48, 24, 96, 24]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "4d66aaa996c12937"} |
vcr_92c52ff1fec0 | 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": [20, -9, -10, -14, -9, 14, -14, 11, -4, 0, 12, -12, 6]}, "output": 20}, {"input": {"nums": [14, 2, -8, 5, 2, 3, -8, -3]}, "output": 18}, {"input": {"nums": [-12, 5, -6, 9, 5, 14, -3, -12, 8, 2]}, "output": 28}] | 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": "4bb3ea96718039ce"} |
vcr_a07966e60ed1 | 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": "a eddcae ba"}, "output": "ba eddcae a"}, {"input": {"s": "b b"}, "output": "b b"}, {"input": {"s": "a aabb bb"}, "output": "bb aabb a"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "d837cf89f5717eaf"} |
vcr_a1d0aee6219b | 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, 27, 9, 20, 0, 30]}, "output": 77}, {"input": {"nums": [4, 4, 2, 16, 12, 15]}, "output": 35}, {"input": {"nums": [5, 12, 13, 17]}, "output": 29}] | 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": "ba3f99f45390acce"} |
vcr_cd077c24506e | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [9, 12, 16, 30, 33, 41, 44], "target": 35}, "output": 5}, {"input": {"nums": [7, 8, 14, 16, 18, 20, 22, 25, 27, 31, 35, 39], "target": 7}, "output": 0}, {"input": {"nums": [0, 7, 13, 20], "target": 12}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "df7129342975e8a8"} |
vcr_1a96db9bb9f4 | 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": "caabacabba"}, "output": 8}, {"input": {"s": "aac"}, "output": 2}, {"input": {"s": "caaa"}, "output": 2}] | 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": "e76b99a594b68b1d"} |
vcr_942fbb3c6843 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [3, -25, -5, -11, 22, -5, -9, -22, -9, 15]}, "output": [3, -22, -27, -38, -16, -21, -30, -52, -61, -46]}, {"input": {"nums": [10, 8, 3, -10]}, "output": [10, 18, 21, 11]}, {"input": {"nums": [1, -7, 3, -9, -8, -7]}, "output": [1, -6, -3, -12, -20, -27]}] | 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": "c308c81db010a788"} |
vcr_429139eb956a | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 2, 0, 0, 4, 2, 2, 3, 3, 0]}, "output": false}, {"input": {"nums": [0, 3, 0, 2, 3, 2, 3, 0, 3]}, "output": false}, {"input": {"nums": [2, 2, 3, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "64123c2eea57a0e3"} |
vcr_ca85a1658753 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-15, -4, -12, 13, 5, -5, 10, 1, -3, 5, -10, -15, -3, 3, -14]}, "output": 26}, {"input": {"nums": [4, 13, 7, 3]}, "output": 27}, {"input": {"nums": [0, 10, 0, 6, -10, 14, -3, 1, 3]}, "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": "25ba8fc759fddb86"} |
vcr_a970a941ea4b | 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": "deada adeabc be bcacda"}, "output": "bcacda be adeabc deada"}, {"input": {"s": "aab ba aa"}, "output": "aa ba aab"}, {"input": {"s": "abb bab a bbaa ab"}, "output": "ab bbaa a bab abb"}] | 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": "c629188c56cc41ea"} |
vcr_b17358e88a60 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-17, -25, 20, 16, 32, 31, 6]}, "output": [-17, -42, -22, -6, 26, 57, 63]}, {"input": {"nums": [1, 18, 16, 3, 2, 18, 1, 7]}, "output": [1, 19, 35, 38, 40, 58, 59, 66]}, {"input": {"nums": [-10, 4, -9, 6, 11, -3]}, "output": [-10, -6, -15, -9, 2, -1]}] | 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": "61356ce86b3d2219"} |
vcr_25a3ccae1b63 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [4, 21, 45, 9, 4]}, "output": true}, {"input": {"nums": [13, 25, 21]}, "output": false}, {"input": {"nums": [2, 11, 23, 22, 8, 24, 23]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "5be0b61e2799dcec"} |
vcr_1fbd1470eb4f | 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": [14, 21, 30, 36, 40, 49], "target": 22}, "output": 2}, {"input": {"nums": [5, 30, 38], "target": 37}, "output": 2}, {"input": {"nums": [5, 6, 25], "target": 39}, "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": "b918551a3fae6b70"} |
vcr_b074cf8b6db1 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [19, 6, 15, 6, 0, 6, 28, 16]}, "output": 62}, {"input": {"nums": [16, 1, 14, 19, 1]}, "output": 35}, {"input": {"nums": [15, 4, 16, 20, 16, 12, 12]}, "output": 59}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "20fd6d87faaffcf7"} |
vcr_49ae50f61dbc | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "baabdcccdaadab"}, "output": false}, {"input": {"s": "acab"}, "output": false}, {"input": {"s": "cbabcccab"}, "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": "6d0bf2b1546a2c3f"} |
vcr_89b2ef582968 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 13, 18, 19, 28], "target": 50}, "output": 5}, {"input": {"nums": [0, 5, 14, 20, 22, 25, 33], "target": 18}, "output": 3}, {"input": {"nums": [2, 3, 5, 6, 8, 16, 19, 23, 25, 27, 30, 38], "target": 1}, "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": "7616f65fd48febea"} |
vcr_211b68c764ad | 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": 46729}, "output": 8}, {"input": {"n": 3656}, "output": 5}, {"input": {"n": 13005}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "97705cadff8ee756"} |
vcr_a3fe3ac8f657 | 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": [4, 15, 12, 9, 18, 19, 2, 16, 7, 10, 23, 11]}, "output": 82}, {"input": {"nums": [7, 3, 5, 0, 9, 12, 2, 0, 5]}, "output": 29}, {"input": {"nums": [20, 12, 0, 1, 15, 0]}, "output": 35}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "f0003a6549dda4fd"} |
vcr_0ac635bb844d | 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", "aaa", "bbb", "aac", "cc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "baa", "aba", "aaa", "aba", "bba"]}, "output": [1, 1, 4]}, {"input": {"strs": ["aba", "aaa", "abb", "aaa"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "9d12f2c32fc9bd17"} |
vcr_938fa8944079 | 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": "decccb cdabae a acbcde eaea"}, "output": "eaea acbcde a cdabae decccb"}, {"input": {"s": "bab bbaa"}, "output": "bbaa bab"}, {"input": {"s": "aaaa b"}, "output": "b aaaa"}] | 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": "6265545b4fc71f9f"} |
vcr_adfbce428424 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 6, 2, 4, 3, 7]}, "output": [1008, 168, 504, 252, 336, 144]}, {"input": {"nums": [4, 4, 2, 2]}, "output": [16, 16, 32, 32]}, {"input": {"nums": [3, 2, 4]}, "output": [8, 12, 6]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "b2d3da5b31d99428"} |
vcr_afb9a49c1d04 | 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": [-6, 14, 23, 35, 46], "target": 37}, "output": [1, 2]}, {"input": {"nums": [-11, 7, 15, 27], "target": 16}, "output": [0, 3]}, {"input": {"nums": [-19, -18, -12, -2, 2, 8, 23, 32, 33, 39], "target": -10}, "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": "ab905d27f0cc5891"} |
vcr_c5de80595b22 | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "accdbcadbbbccada"}, "output": false}, {"input": {"s": "ccccbccab"}, "output": false}, {"input": {"s": "baabbbb"}, "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": "8e29f1b5091db593"} |
vcr_fe06bccbf969 | 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": "ddecc c ae be"}, "output": "be ae c ddecc"}, {"input": {"s": "abab aa a"}, "output": "a aa abab"}, {"input": {"s": "b abaa bb ab aa"}, "output": "aa ab bb abaa 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": "5e014687c27fd1f9"} |
vcr_e7df8c8a8f71 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [38, 78, 5, 5, 12, 12, 38]}, "output": 78}, {"input": {"nums": [40, 13, 5, 13, 5]}, "output": 40}, {"input": {"nums": [12, 25, 8, 19, 25, 19, 40, 8, 12, 18, 18]}, "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": "df6c72b9992ed266"} |
vcr_afa4f5e1530d | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [16, -6, 28, 0, -9, -3, -8, 14]}, "output": [16, 10, 38, 38, 29, 26, 18, 32]}, {"input": {"nums": [-3, 7, -7, -6, 13, -7, 18, 8, -2]}, "output": [-3, 4, -3, -9, 4, -3, 15, 23, 21]}, {"input": {"nums": [-6, -7]}, "output": [-6, -13]}] | 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": "9c475a2bd8edd4ff"} |
vcr_83f0fc09585f | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-19, -16, 2, 4, 13, 28, 31, 35], "target": 12}, "output": [0, 6]}, {"input": {"nums": [-16, -15, -10, -5, -1, 11, 14, 19, 32], "target": 16}, "output": [0, 8]}, {"input": {"nums": [-19, -16, 6, 10, 22], "target": -35}, "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": "4f93fee46a016483"} |
vcr_208da99415c1 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, -23, 17, 28, 36], "target": -6}, "output": [1, 2]}, {"input": {"nums": [-19, -7, 1, 3, 8, 10, 11, 17], "target": -16}, "output": [0, 3]}, {"input": {"nums": [-8, -7, 2, 5, 12, 18, 24], "target": -5}, "output": [1, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "c908a80607ab90d9"} |
vcr_9264933fad8a | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-9, 12, 6, 13, -7, 11, 10, 15, 19], "k": 7}, "output": [6, 13, -7, 11, 10, 15, 19, -9, 12]}, {"input": {"nums": [-2, -9, 1, -8, -9, 10, 9, 9, 2, 10], "k": 12}, "output": [2, 10, -2, -9, 1, -8, -9, 10, 9, 9]}, {"input": {"nums": [-2, -3, -2, -3, -5, -4, 10, -4, 8, 7], "k": 9}, "output": [-3, -2, -3,... | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "3a56e8c41c96d0fd"} |
vcr_a19e136c6a84 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [9, 22, 11, 26, 16]}, "output": 48}, {"input": {"nums": [11, 2, 6, 8, 14, 5, 8, 2, 4, 1]}, "output": 43}, {"input": {"nums": [12, 6, 10]}, "output": 22}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "ffb7684e7d6dc415"} |
vcr_809274241a97 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [34, 19, 34, 24, 32, 5, -6, 4]}, "output": [34, 53, 87, 111, 143, 148, 142, 146]}, {"input": {"nums": [-2, 16, -12, 10, -6, -8]}, "output": [-2, 14, 2, 12, 6, -2]}, {"input": {"nums": [6, 18]}, "output": [6, 24]}] | 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": "9df33df6114dfe8e"} |
vcr_67ad5cffdaa7 | 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, 72, 34, 36, 5, 34, 5]}, "output": 72}, {"input": {"nums": [8, 44, 8, 5, 5]}, "output": 44}, {"input": {"nums": [15, 14, 55, 4, 17, 2, 2, 14, 4, 15, 17]}, "output": 55}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "b87ea8f10b6e0494"} |
vcr_6002cdeb4829 | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [41, 26, -5, -36, 54, 11, 72, 40, 57], "target": 67}, "output": [0, 1]}, {"input": {"nums": [-21, 5, 14, 3, 39, 45, 27], "target": 53}, "output": [2, 4]}, {"input": {"nums": [-22, 32, 26, 42], "target": 4}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "7f181c368784020b"} |
vcr_6e979dc723ae | 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": [32, 13, -8, 17, 13, 31, -23, 23, -22, 9, 17]}, "output": [32, 45, 37, 54, 67, 98, 75, 98, 76, 85, 102]}, {"input": {"nums": [-10, 2]}, "output": [-10, -8]}, {"input": {"nums": [-7, -10, 17, 18, -6, 11, -10]}, "output": [-7, -17, 0, 18, 12, 23, 13]}] | 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": "12c76cdec00556b1"} |
vcr_fc8a53e655ea | 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, 9, 6, 1, 16, 14, 20]}, "output": 66}, {"input": {"nums": [20, 17, 20, 11, 1, 13, 5, 9, 1, 11]}, "output": 73}, {"input": {"nums": [1, 8, 7, 20]}, "output": 28}] | 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": "7320660c950fd06f"} |
vcr_8d52856c854b | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 4, 4, 2, 3, 1, 3, 0]}, "output": true}, {"input": {"nums": [0, 2, 0, 3, 3, 3, 0]}, "output": false}, {"input": {"nums": [3, 0, 1, 2, 0, 0, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "94b63985f7965cc6"} |
vcr_1d3f46f99874 | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "bbdbababdcabb"}, "output": false}, {"input": {"s": "ababa"}, "output": true}, {"input": {"s": "acbbcaccac"}, "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": "023404086de00197"} |
vcr_8db03a5266e5 | 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": "baa cdacc cd eccbc aae dccee"}, "output": "dccee aae eccbc cd cdacc baa"}, {"input": {"s": "aabb a ab baab aaaa"}, "output": "aaaa baab ab a aabb"}, {"input": {"s": "baaa ba a ba"}, "output": "ba a ba baaa"}] | 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": "3ebca776acaa0e6e"} |
vcr_e28c3c591bc3 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 1, 4, 3, 4, 2, 0]}, "output": true}, {"input": {"nums": [2, 3, 1, 1, 0, 3, 3, 2, 0]}, "output": false}, {"input": {"nums": [0, 1, 0, 2, 3, 3, 1, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "a2f6d38eb8efb579"} |
vcr_3dc1c64878bd | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [21, 49, 2, 45, 57, 26, 5, 54]}, "output": 55}, {"input": {"prices": [7, 23, 4, 24]}, "output": 20}, {"input": {"prices": [36, 13, 14]}, "output": 1}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "3c03a3f76837bc3d"} |
vcr_b4e08381e53f | 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": 182, "b": 204}, "output": 2}, {"input": {"a": 141, "b": 52}, "output": 1}, {"input": {"a": 179, "b": 112}, "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": "5becb9116402db3c"} |
vcr_a07bee803074 | 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": [34, 10, 56, 6, 17, 56, 2, 28, 42, 45]}, "output": 50}, {"input": {"prices": [22, 16, 24, 33, 12, 35, 36, 17, 14, 29]}, "output": 24}, {"input": {"prices": [30, 17, 13, 11, 25, 39]}, "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": "625ee4abe849c17b"} |
vcr_cf716192a6ab | 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", "ab", "ab", "ca", "aba", "cb", "bbb"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "aaa", "bba", "bbb", "aba", "bbb", "bba"]}, "output": [1, 2, 2, 2]}, {"input": {"strs": ["aba", "aab", "bba", "aab"]}, "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": "16823a960026108c"} |
vcr_f1c86c655120 | 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": [22, 41, 31, 54, 38, 31, 24, 20, 44, 16, 56, 25]}, "output": 40}, {"input": {"prices": [5, 17, 9, 12, 21, 10, 6, 13, 2, 32, 5, 6]}, "output": 30}, {"input": {"prices": [31, 14, 37, 26, 25]}, "output": 23}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "a879c1112d794ee8"} |
vcr_695ed3def9a5 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["aa", "aba", "bc", "cbb", "ca", "abac", "ccab", "bc"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "baa", "abb", "aab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aaa", "aab", "bbb"]}, "output": [1, 1, 1]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "aa6cef4ac57eeb45"} |
vcr_f91c1a661ad6 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 3, 0, 6, 2]}, "output": [0, 0, 180, 0, 0]}, {"input": {"nums": [1, 4, 4, 4, 1, 1]}, "output": [64, 16, 16, 16, 64, 64]}, {"input": {"nums": [5, 5, 4]}, "output": [20, 20, 25]}] | 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": "50c5e3882f5a2ac8"} |
vcr_4ffa3d7722dd | 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": [-27, -25, -14, -8, 0, 2, 30, 34, 40, 49], "target": 5}, "output": [1, 6]}, {"input": {"nums": [6, 10, 18, 39], "target": 16}, "output": [0, 1]}, {"input": {"nums": [-14, -12, -6, -5, 6, 15, 16, 18, 20, 26, 34], "target": -6}, "output": [1, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "4415c53e09a0ff3e"} |
vcr_bf74af220035 | 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": "bdcccababacccdb"}, "output": true}, {"input": {"s": "aa"}, "output": true}, {"input": {"s": "bb"}, "output": true}] | 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": "473bbe8b022a944e"} |
vcr_d9896becc051 | 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": 46, "bad": 45}, "output": 45}, {"input": {"n": 11, "bad": 1}, "output": 1}, {"input": {"n": 3, "bad": 2}, "output": 2}] | 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": "1c6f54dc79422231"} |
vcr_38ba03411aa7 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -12, -1, 18, 48], "target": -11}, "output": [0, 3]}, {"input": {"nums": [-19, -14, -13, 0, 6, 10, 15, 19, 30, 33], "target": 6}, "output": [2, 7]}, {"input": {"nums": [5, 6, 10, 13, 20, 27, 38], "target": 40}, "output": [3, 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": "0cb4291219854c2b"} |
vcr_1c13de0173d6 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [4, 15, -13, 4, 1, 0, -14, -19, 7, 15, -6], "k": 20}, "output": [-13, 4, 1, 0, -14, -19, 7, 15, -6, 4, 15]}, {"input": {"nums": [3, 9, -10, 4, -6, 9, 6, 6, 7, 1], "k": 0}, "output": [3, 9, -10, 4, -6, 9, 6, 6, 7, 1]}, {"input": {"nums": [-5, -6, 10, 5, -2, 8, -3, 4, 2, 9], "k": 8}, "output": [10, 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": "b738224ce02c6b2f"} |
vcr_ce1f88d4ce6a | 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, 3, 2, 3, 4, 1, 4, 1, 2, 0]}, "output": true}, {"input": {"nums": [2, 2, 2, 1, 2, 2, 3, 2]}, "output": true}, {"input": {"nums": [2, 0, 3, 2, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "dbe4df9984f017d5"} |
vcr_353ec81d67df | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-9, 11, -4, 7, -10, 3, -13, 16, -3, -9, 10], "k": 31}, "output": [-4, 7, -10, 3, -13, 16, -3, -9, 10, -9, 11]}, {"input": {"nums": [-6, 5, 10, 10], "k": 14}, "output": [10, 10, -6, 5]}, {"input": {"nums": [5, -1, 2, 4, 6, 5, -5, 6], "k": 2}, "output": [-5, 6, 5, -1, 2, 4, 6, 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": "52731336bdfb950f"} |
vcr_c64e860c56cb | 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": [-7, -6, -1, 11, 14, 19, 23, 31, 36, 37, 41], "target": 77}, "output": [8, 10]}, {"input": {"nums": [-19, -12, -6, 2, 5, 11, 29, 31, 36, 39], "target": 60}, "output": [6, 7]}, {"input": {"nums": [-15, -14, -13, -8, 7, 8, 32], "target": -6}, "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": "a065e80cecbcbf95"} |
vcr_312c29e57f74 | 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, -10, -13, -8, 6, 14]}, "output": 20}, {"input": {"nums": [-1, 6, 7, 2, -9, 2, 2, 4, 11, 0]}, "output": 25}, {"input": {"nums": [3, 12, -7, -9, -11, -7, 14, 14, 8, -1]}, "output": 36}] | 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": "6e7ef7eedb38aff1"} |
vcr_863f1d481417 | 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, -22, -18, -16, -8, 1, 4, 8, 30, 33, 36, 40], "target": 14}, "output": [1, 10]}, {"input": {"nums": [-19, -15, -7, 5, 9, 22, 31], "target": -2}, "output": [2, 3]}, {"input": {"nums": [-18, -3, 2, 11, 20, 33, 35], "target": 17}, "output": [0, 6]}] | 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": "7bd7f496c8165190"} |
vcr_f26d087fece3 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [6, 6, 22, -15, -8, 3, 11, 17]}, "output": 42}, {"input": {"nums": [14, 2, 12, -5, 6, 10, 4]}, "output": 43}, {"input": {"nums": [13, -8, 1, 10, 10, -7, -12, -3, 5]}, "output": 26}] | 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": "2e880db6229d61dd"} |
vcr_80284273fe64 | 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": [5, -20, 2, -15, 15, 10, 9, -16, -11, -20], "k": 20}, "output": [5, -20, 2, -15, 15, 10, 9, -16, -11, -20]}, {"input": {"nums": [7, 4, -5, 3, 6, 1, 9, -1, 5], "k": 4}, "output": [1, 9, -1, 5, 7, 4, -5, 3, 6]}, {"input": {"nums": [2, 10, -5, -2], "k": 6}, "output": [-5, -2, 2, 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": "d58fe2cbd111d903"} |
vcr_8dbbafcb24bf | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 1, 0, 0, 0, 0, 0]}, "output": false}, {"input": {"nums": [1, 0, 2, 2, 1, 1, 1]}, "output": false}, {"input": {"nums": [3, 0, 1, 0, 1, 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": "d81787273a7630a9"} |
vcr_82cf44543d78 | 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": [147, 181, 52, 156, 30, 37, 61, 11, 110]}, "output": false}, {"input": {"nums": [4, 19, 12, 8, 6, 12, 8]}, "output": true}, {"input": {"nums": [18, 25, 4, 24, 20, 15]}, "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": "5214ef3b014f582b"} |
vcr_75e506c8003a | 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, -11, -8, -10, 1, -3, -9, -15, 11, 13, -16], "k": 28}, "output": [-3, -9, -15, 11, 13, -16, 11, -11, -8, -10, 1]}, {"input": {"nums": [-8, -10, 10, 5, -8, -5, -10, 5, 9], "k": 9}, "output": [-8, -10, 10, 5, -8, -5, -10, 5, 9]}, {"input": {"nums": [5, 6, -1, -8], "k": 5}, "output": [-8, 5, 6, -1]... | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "68f116886241a52a"} |
vcr_62c3e7ba5541 | 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": ["bba", "bcc", "cacb", "bcc", "cbb", "bcb", "aa", "cbca", "ccc"]}, "output": [1, 1, 1, 2, 2, 2]}, {"input": {"strs": ["bab", "bbb", "bba", "aba", "aba"]}, "output": [1, 2, 2]}, {"input": {"strs": ["baa", "aab", "baa", "aba", "bbb", "bab", "bba"]}, "output": [1, 2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "f59203e184131c82"} |
vcr_ac855a80965a | 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": [0, 39, 20, 171, 43, 54, 53, 177, 158, 172, 135, 125, 132, 167, 3, 86, 159]}, "output": false}, {"input": {"nums": [15, 23, 6]}, "output": false}, {"input": {"nums": [14, 24, 12, 25, 24, 1, 13, 21, 1, 16, 19, 21]}, "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": "a313f688050c681c"} |
vcr_d72d1ae19a11 | 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": [105, 3, 146, 62, 129, 186, 156, 11, 192, 66]}, "output": false}, {"input": {"nums": [5, 22, 23, 6]}, "output": false}, {"input": {"nums": [16, 23, 3, 7, 17, 20]}, "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": "d6ffc01090d86140"} |
vcr_21c4d92c55cf | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [14, 23, 4, 23, 71, 25, 19, 28, 19, 14, 6, 26, 6, 4, 25, 26, 28]}, "output": 71}, {"input": {"nums": [17, 59, 17, 26, 26]}, "output": 59}, {"input": {"nums": [47, 2, 15, 2, 15, 3, 6, 6, 3]}, "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": "98c9d40acba19814"} |
vcr_f7e98dfea1d8 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-12, -21, -16, 32, -13, 35, 20, 6, 28, -22, -2, 29]}, "output": [-12, -33, -49, -17, -30, 5, 25, 31, 59, 37, 35, 64]}, {"input": {"nums": [-10, 3, -2, -3, 4, -4]}, "output": [-10, -7, -9, -12, -8, -12]}, {"input": {"nums": [-3, 3, 1, 4, 12, -4, 18, -4]}, "output": [-3, 0, 1, 5, 17, 13, 31, 27]}] | 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": "efb4f2a5ad42575f"} |
vcr_6616ee9c2194 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 2, 3, 16, 25, 26, 31, 32, 40, 45, 47, 48], "target": 1}, "output": 1}, {"input": {"nums": [0, 6, 12, 14, 18, 23, 27, 28, 29, 31, 35, 37], "target": 26}, "output": 6}, {"input": {"nums": [0, 20, 22, 38], "target": 26}, "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": "17a9fa735233b68b"} |
vcr_4e44efcd316f | 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": "acdbaadbb"}, "output": false}, {"input": {"s": "aaabbbccb"}, "output": false}, {"input": {"s": "ccacbcac"}, "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": "c3e645dfab5929e5"} |
vcr_7456f3976960 | 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": 43956}, "output": 9}, {"input": {"n": 7597}, "output": 9}, {"input": {"n": 14933}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "9677c8f006e7ef86"} |
vcr_12aeba8cd043 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [15, -10, -4, -15, 12, 3, 11, -5, -16, 13, -13, 21]}, "output": 26}, {"input": {"nums": [0, -1, -6, 0, 14]}, "output": 14}, {"input": {"nums": [-12, 4, -10, 6, 5, 13]}, "output": 24}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "7a7d3ce2992d15b7"} |
vcr_1eadb4b31c1e | 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": "ccedc bea"}, "output": "bea ccedc"}, {"input": {"s": "bbb a bbbb aa"}, "output": "aa bbbb a bbb"}, {"input": {"s": "bb abb bbaa bab"}, "output": "bab bbaa abb bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "640195318d915065"} |
vcr_b4fae6f58e1b | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [26, 18, 2]}, "output": 28}, {"input": {"nums": [18, 12, 16, 20, 6, 19, 4]}, "output": 57}, {"input": {"nums": [3, 19, 2, 1]}, "output": 20}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "154257818da31f55"} |
vcr_0c9486ed6cfd | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [28, 8, 15, 27, 0, 13, 16]}, "output": 71}, {"input": {"nums": [2, 7, 1, 5, 17, 6, 18, 11]}, "output": 42}, {"input": {"nums": [20, 0, 5, 0, 16, 19, 9]}, "output": 50}] | 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": "67a0a0fed6a296fd"} |
vcr_ce4e19847545 | 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": [13, 1, 19, 39, 10, 43, 31, 14, 46, 15, 41, 12, 45, 50, 19, 14]}, "output": true}, {"input": {"nums": [2, 14, 21, 3]}, "output": false}, {"input": {"nums": [16, 24, 25, 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": "73f068646e692715"} |
vcr_8d364357a5d5 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [6, -15, -12, 14, -10]}, "output": 14}, {"input": {"nums": [4, -3, -1, 3]}, "output": 4}, {"input": {"nums": [0, 8, 2, 10, 14, 3, -1]}, "output": 37}] | 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": "29f2787f7270cc4d"} |
vcr_0777ac5a3b0b | 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, 11, 12, 15, 28, 40, 45, 46], "target": 27}, "output": 4}, {"input": {"nums": [2, 7, 8, 9, 15, 20, 23, 31, 32, 33, 35], "target": 9}, "output": 3}, {"input": {"nums": [5, 6, 15, 34, 37], "target": 26}, "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": "7b5834f46378a7d9"} |
vcr_b95738009188 | 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": [18, 22, 24, 26, 27, 36, 37, 38, 41, 47, 48], "target": 24}, "output": 2}, {"input": {"nums": [1, 9, 14, 16, 18, 30, 31, 35, 37, 38], "target": 11}, "output": 2}, {"input": {"nums": [6, 10, 14, 15, 16, 25, 35, 37, 39], "target": 25}, "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": "f2a1d188bd3960e1"} |
vcr_c5694d7d4696 | 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, -15, -13, -10, -3, 2, 6, 11, 18], "target": 17}, "output": [6, 7]}, {"input": {"nums": [-4, 4, 6, 16, 25], "target": 31}, "output": [2, 4]}, {"input": {"nums": [-12, -5, 8, 9, 14, 19, 20, 21, 22, 24], "target": 43}, "output": [5, 9]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "a046f5829bad09b1"} |
vcr_c85decba46df | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 5, 2, 6, 3, 2, 1]}, "output": [360, 360, 900, 300, 600, 900, 1800]}, {"input": {"nums": [3, 2, 5, 3, 2, 1, 4]}, "output": [240, 360, 144, 240, 360, 720, 180]}, {"input": {"nums": [3, 4, 3]}, "output": [12, 9, 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": "1f8e6e01379645e5"} |
vcr_6a3c1dabdc0d | 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, 27, 9, 33, 28, 27, 76, 9, 30, 28, 33]}, "output": 76}, {"input": {"nums": [5, 18, 23, 21, 4, 5, 18, 50, 23, 4, 21]}, "output": 50}, {"input": {"nums": [23, 13, 24, 24, 23, 6, 6, 27, 7, 13, 54, 27, 7]}, "output": 54}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "732fef0437c6418a"} |
vcr_a2f9c31b45ef | 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": ["bab", "cbaa", "abc", "bcb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "aab", "aab", "bbb", "bab"]}, "output": [1, 2, 2]}, {"input": {"strs": ["aba", "abb", "aab", "bbb", "aaa"]}, "output": [1, 1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "1cd51c58ccad37c6"} |
vcr_be8d00da061e | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bc", "cb", "ccac", "cba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bba", "aaa", "aba", "baa", "aaa"]}, "output": [1, 2, 2]}, {"input": {"strs": ["aab", "baa", "abb"]}, "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": "c715a32b48798df9"} |
vcr_10572d4083e7 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [2, 22, 4, 10, 16, 9, 2, 0, 12, 28, 6, 23]}, "output": 92}, {"input": {"nums": [17, 8, 12, 10, 11, 19, 16, 15, 13]}, "output": 69}, {"input": {"nums": [6, 19, 18, 8]}, "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": "e47584fefeabf05f"} |
vcr_e6cd342444bd | 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": [-19, 14, -10, -10, -15, 6, 0, 7, 6], "k": 19}, "output": [6, -19, 14, -10, -10, -15, 6, 0, 7]}, {"input": {"nums": [0, -5, -6, 0, 3, -10], "k": 19}, "output": [-10, 0, -5, -6, 0, 3]}, {"input": {"nums": [-8, 5, -7, 9, -9, -10, 10], "k": 9}, "output": [-10, 10, -8, 5, -7, 9, -9]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "deedf1bfbb2615f0"} |
vcr_453e301484b7 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 4, 7, 8, 9, 10, 11, 14, 15, 15]}, "output": [0, 2, 4, 7, 8, 9, 10, 11, 14, 15]}, {"input": {"nums": [7, 7, 8]}, "output": [7, 8]}, {"input": {"nums": [0, 1, 1, 2, 3, 4, 5, 6, 10, 12]}, "output": [0, 1, 2, 3, 4, 5, 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": "88b9f16b4cce958c"} |
vcr_eb47c8dbb181 | 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": [28, 22, 41, 8, 41, 35, 43, 41, 43, 10, 31]}, "output": true}, {"input": {"nums": [14, 24, 3, 10, 16, 5]}, "output": false}, {"input": {"nums": [24, 20, 12, 6, 14, 17, 2, 0]}, "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": "20ec74376668fbab"} |
vcr_832ebc28a318 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 2, 2, 3, 6, 7, 8, 8, 12, 14, 14, 15]}, "output": [1, 2, 3, 6, 7, 8, 12, 14, 15]}, {"input": {"nums": [3, 4, 5, 6, 7, 9, 9, 11]}, "output": [3, 4, 5, 6, 7, 9, 11]}, {"input": {"nums": [0, 4, 5, 6, 7, 8, 8, 8]}, "output": [0, 4, 5, 6, 7, 8]}] | 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": "ecb7dcf78a61af59"} |
vcr_f710cd987a6d | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [9, -10, -19, -10, 18, 18, -14, -13, -19, -17, 1], "k": 8}, "output": [-10, 18, 18, -14, -13, -19, -17, 1, 9, -10, -19]}, {"input": {"nums": [6, 1, 8, -1, -4, -4, -8, -2], "k": 8}, "output": [6, 1, 8, -1, -4, -4, -8, -2]}, {"input": {"nums": [-1, 5, 6, 4, 3, 10, -1, -4], "k": 10}, "output": [-1, -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": "ebebada3ca9c2d8a"} |
vcr_00c978f334aa | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [13, 7, 4, 3, -13, 4, 12, -3, 5, -9, 3, 3, 11, 13, 7]}, "output": 60}, {"input": {"nums": [5, 9, 1]}, "output": 15}, {"input": {"nums": [-6, -6, -1, 11, 3]}, "output": 14}] | 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": "b4ba836e289a3af4"} |
vcr_e48fbf33125d | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-26, -23, -17, -14, -11, -10, -7, -1, 20, 33, 38], "target": 21}, "output": [2, 10]}, {"input": {"nums": [-8, -6, -3, 5, 6, 9, 25, 26, 33, 36, 39], "target": 20}, "output": [1, 7]}, {"input": {"nums": [-17, -12, -10, -5, 11, 37], "target": 6}, "output": [3, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "ea0506aed830a9d3"} |
vcr_11f91d759980 | 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": "cbccad ddaa ec be eb ebab bddd"}, "output": "bddd ebab eb be ec ddaa cbccad"}, {"input": {"s": "bb b a bb"}, "output": "bb a b bb"}, {"input": {"s": "bb bbaa baba"}, "output": "baba bbaa bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "a874435d57de979e"} |
vcr_c4615efe20ef | 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": [179, 76, 25, 66, 114, 136, 9, 187, 131, 177, 193, 168, 124, 23, 78, 60]}, "output": false}, {"input": {"nums": [19, 7, 21]}, "output": false}, {"input": {"nums": [10, 2, 16, 16, 10, 16, 12, 9, 8, 3]}, "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": "32ee2b649517fdf6"} |
vcr_c92c4f0cafa7 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [6, -18, 2, 16, -17, 1, -8, 14, 13, 21, 16, 22, 8, 20]}, "output": 114}, {"input": {"nums": [-12, 10, 4, -3, -4, -3, -9, 12, 7, 9, -3]}, "output": 28}, {"input": {"nums": [1, 12, 3]}, "output": 16}] | 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": "b59fd0b138c86e83"} |
vcr_6c127869f31f | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [9, 18, -9, 14, 10, -11, 2, 16]}, "output": 49}, {"input": {"nums": [-11, 6, -1, 5]}, "output": 10}, {"input": {"nums": [2, 1, 8, 5, 14, 4, 10, -4, -8]}, "output": 44}] | 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": "727502ca2568e609"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.