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_772e082b0bdc | 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": "bcad bce"}, "output": "bce bcad"}, {"input": {"s": "a a aaa ab"}, "output": "ab aaa a a"}, {"input": {"s": "b bb aba aa"}, "output": "aa aba bb b"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "c49ce4e60ee58d7d"} |
vcr_51049f3cfa80 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [6, 7, 4, 5, 2, 5]}, "output": [1400, 1200, 2100, 1680, 4200, 1680]}, {"input": {"nums": [2, 5, 1]}, "output": [5, 2, 10]}, {"input": {"nums": [2, 5, 3]}, "output": [15, 6, 10]}] | 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": "701b0e67a86b77f3"} |
vcr_9e04c76c1635 | 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, 4, 1, 2, 4, 3, 0, 0]}, "output": true}, {"input": {"nums": [1, 3, 2, 3, 1, 3, 3, 0, 3]}, "output": true}, {"input": {"nums": [1, 2, 3, 1, 1, 0]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "9fc601dd8f6ad644"} |
vcr_f8d6e3a4a3cc | 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": [26, 2, -12]}, "output": [26, 28, 16]}, {"input": {"nums": [-5, -10, 18, 3, -7, -11, 7, 4]}, "output": [-5, -15, 3, 6, -1, -12, -5, -1]}, {"input": {"nums": [13, 11]}, "output": [13, 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": "9f3a17dcc0cc194b"} |
vcr_d60d23fc46c3 | 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": [71, -14, -35, 46, -9, 60, -10, 67, 30, 36], "target": 58}, "output": [4, 7]}, {"input": {"nums": [29, -1, -19, 40, 14, 12, -9, 4, -3, 27], "target": -7}, "output": [2, 5]}, {"input": {"nums": [-9, 27, -15, 46, 36], "target": 18}, "output": [0, 1]}] | 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": "6c5c56c7467f3418"} |
vcr_b58ed12013f6 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [3, 4, 5, 8, 12, 14, 15]}, "output": [3, 4, 5, 8, 12, 14, 15]}, {"input": {"nums": [0, 1, 4, 4, 6, 7, 9, 9]}, "output": [0, 1, 4, 6, 7, 9]}, {"input": {"nums": [0, 0, 3, 3, 8, 10]}, "output": [0, 3, 8, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "855f468a9bc8a834"} |
vcr_c6c0de15001a | 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": ["acc", "cca", "bacb", "bb", "bbb"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aba", "bba", "aab", "aaa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["baa", "aba", "bbb", "abb", "bba", "baa", "baa"]}, "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": "a0da163458d45699"} |
vcr_e743fd1a0319 | 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, 4, 2, 4, 7, 7]}, "output": [1568, 392, 784, 392, 224, 224]}, {"input": {"nums": [1, 1, 2, 2, 5, 1]}, "output": [20, 20, 10, 10, 4, 20]}, {"input": {"nums": [1, 2, 1, 5, 3, 4, 1]}, "output": [120, 60, 120, 24, 40, 30, 120]}] | 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": "fbeec800a24cdc19"} |
vcr_5227e90f049c | 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, 10, 13, 16, 22, 28, 30, 36, 39, 45], "target": 9}, "output": 1}, {"input": {"nums": [15, 21, 26, 31, 33, 38], "target": 4}, "output": 0}, {"input": {"nums": [2, 22, 23, 31, 34, 35, 39], "target": 32}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "b7e04f6b80d247c9"} |
vcr_27dc1bd36a3e | 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": [-9, 1, 3, 11, 15, 39], "target": 30}, "output": [0, 5]}, {"input": {"nums": [-9, 1, 14, 25], "target": -8}, "output": [0, 1]}, {"input": {"nums": [-19, -17, -11, 4, 11, 13, 15, 21, 30, 33, 39], "target": -15}, "output": [0, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "fe0caae0bf76e9fe"} |
vcr_859d4a282eb6 | 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, 5, 16, -1, 1, -7, 6, 19, 13, -10, 1]}, "output": 61}, {"input": {"nums": [11, -10, -4, 12, -2, -8, -7, 8, 9, -8, -12]}, "output": 17}, {"input": {"nums": [-4, 4, 11, 3, 3, 12, 2, -8]}, "output": 35}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "7f8247f02fd78dd8"} |
vcr_f883a7a024d0 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 7, 24, 32, 43, 48], "target": 6}, "output": 1}, {"input": {"nums": [4, 6, 13, 14, 16, 17, 31, 32, 38], "target": 19}, "output": 6}, {"input": {"nums": [0, 9, 16, 18, 21, 24, 25, 28, 34, 37, 38], "target": 14}, "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": "5d35b324cbaca750"} |
vcr_13e1af943e01 | 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, 4, 2, 3, 3, 2, 4, 3, 0]}, "output": false}, {"input": {"nums": [1, 1, 1, 2, 3, 2]}, "output": true}, {"input": {"nums": [1, 0, 1, 1, 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": "3354aa39e6207e1e"} |
vcr_ab7e8d05f2f2 | 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, 4, 5, 8, 9, 9, 10, 11]}, "output": [1, 2, 4, 5, 8, 9, 10, 11]}, {"input": {"nums": [1, 4, 7, 11]}, "output": [1, 4, 7, 11]}, {"input": {"nums": [0, 2, 2, 4, 4, 4, 5, 5, 9, 11]}, "output": [0, 2, 4, 5, 9, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "df8f59853955a0b8"} |
vcr_e7e96db9775e | 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": ["ccba", "ccb", "caa", "aca", "cbc"]}, "output": [1, 2, 2]}, {"input": {"strs": ["aaa", "baa", "bbb", "aab", "bbb", "bba"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["aaa", "aaa", "abb", "bba", "abb", "bab", "bba"]}, "output": [2, 5]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "1dca129fa22d2737"} |
vcr_29b7bdc79fd9 | 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, 18, 21, 26, 29, 34, 47, 48], "target": 13}, "output": 1}, {"input": {"nums": [0, 3, 8, 25, 31], "target": 7}, "output": 2}, {"input": {"nums": [1, 6, 8, 9, 12, 14, 20, 28, 31], "target": 40}, "output": 9}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "0d1f750c58d7ca85"} |
vcr_cb363bbe8630 | 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": [10, 11, 7, 7, 4, 10, 3, -14, 5, -16, 14, 17, 0, -1, -7]}, "output": 58}, {"input": {"nums": [14, 7, 1, -6, -12]}, "output": 22}, {"input": {"nums": [-12, -9, -10]}, "output": -9}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "c97c8159331f117b"} |
vcr_61ff9d48e61e | 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": 34403}, "output": 7}, {"input": {"n": 11298}, "output": 5}, {"input": {"n": 3927}, "output": 9}] | 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": "f597dcb4af8f1169"} |
vcr_a721dc3b66d2 | 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": ["abb", "bbac", "aca", "aaaa", "ccba"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "aaa", "aba", "aaa", "aab"]}, "output": [2, 3]}, {"input": {"strs": ["bba", "bbb", "aba", "bbb", "abb", "aaa"]}, "output": [1, 1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "094144709fd2f077"} |
vcr_9cb9aae918f2 | 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": [0, 6, 1, 7, 2, 4, 4]}, "output": [1344, 0, 0, 0, 0, 0, 0]}, {"input": {"nums": [3, 4, 3, 5]}, "output": [60, 45, 60, 36]}, {"input": {"nums": [3, 3, 1, 3]}, "output": [9, 9, 27, 9]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "34d1b484f8f5c861"} |
vcr_feeb0fc51479 | 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": [2, 6, 35, 35, 39, 2, 68, 6, 39]}, "output": 68}, {"input": {"nums": [7, 7, 24, 6, 26, 23, 23, 42, 26, 6, 24]}, "output": 42}, {"input": {"nums": [58, 2, 9, 9, 21, 19, 14, 27, 19, 2, 27, 14, 21]}, "output": 58}] | 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": "1a525bc961ad7002"} |
vcr_fc171ce7d77b | 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": "ed aabaee bbcade"}, "output": "bbcade aabaee ed"}, {"input": {"s": "ab b bbb aabb a"}, "output": "a aabb bbb b ab"}, {"input": {"s": "aa aa"}, "output": "aa aa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "4af05b7819099c09"} |
vcr_1811c34b11db | 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": [160, 30, 14, 80, 129]}, "output": false}, {"input": {"nums": [7, 14, 12, 3, 23]}, "output": false}, {"input": {"nums": [14, 25, 4, 21, 1, 23, 18, 5, 2]}, "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": "98ff4a550bdc4283"} |
vcr_f44c1e1e7940 | 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": [-10, -11, 2, -13, -10, -8], "k": 4}, "output": [2, -13, -10, -8, -10, -11]}, {"input": {"nums": [8, 3, -5, -10, -3, 1, 10, 1], "k": 5}, "output": [-10, -3, 1, 10, 1, 8, 3, -5]}, {"input": {"nums": [0, -3, -2, 10, 1, 7, 4, -1], "k": 14}, "output": [-2, 10, 1, 7, 4, -1, 0, -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": "f92f3511c082a6b7"} |
vcr_24fc521af46e | 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": 249, "b": 216}, "output": 3}, {"input": {"a": 44, "b": 131}, "output": 1}, {"input": {"a": 54, "b": 11}, "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": "568fa12a95e317ac"} |
vcr_658514ed7986 | 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, 10, 23, 7, 30, 7, 19, 4, 6, 7]}, "output": 102}, {"input": {"nums": [17, 16, 16, 10, 1, 16, 13, 18]}, "output": 67}, {"input": {"nums": [14, 10, 17, 19, 1, 19, 18, 5]}, "output": 57}] | 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": "5d10ae42c90b1828"} |
vcr_bef36ab6f514 | 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, -10, -9, 6, 18, 21, 31, 41], "target": -19}, "output": [1, 2]}, {"input": {"nums": [-20, -14, -2, 14], "target": -6}, "output": [0, 3]}, {"input": {"nums": [-15, -14, -9, -6, -4, 2, 10, 15, 16, 32, 39], "target": -15}, "output": [2, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "52661ae82ce2aef2"} |
vcr_095124f0d9fc | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-18, 4, -8, 15, 11], "k": 13}, "output": [-8, 15, 11, -18, 4]}, {"input": {"nums": [0, -2, 5, -6, -1], "k": 0}, "output": [0, -2, 5, -6, -1]}, {"input": {"nums": [6, -9, -7, 2], "k": 1}, "output": [2, 6, -9, -7]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "2c6a88e36e5c0208"} |
vcr_79a07f78380f | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-6, 9, 49, -31, 53, 79], "target": 132}, "output": [4, 5]}, {"input": {"nums": [22, 44, -10, 27, -19, 34, -26], "target": 61}, "output": [3, 5]}, {"input": {"nums": [-19, 39, -5, -3, 9, -16, 31, -4, -26, -25], "target": -10}, "output": [0, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "525cb2537d01767c"} |
vcr_e6114620420a | 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": ["bbb", "cca", "cca", "bbbc", "bc"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["bab", "aba", "bab", "abb"]}, "output": [1, 3]}, {"input": {"strs": ["aba", "bbb", "abb"]}, "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": "ced7bedb0853060b"} |
vcr_c76195854f01 | 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": "abadacadaba"}, "output": true}, {"input": {"s": "abbccbcab"}, "output": false}, {"input": {"s": "ccbaccbac"}, "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": "344bde7b88c67295"} |
vcr_7e6d001a1190 | 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": 61, "b": 74}, "output": 1}, {"input": {"a": 99, "b": 144}, "output": 9}, {"input": {"a": 45, "b": 147}, "output": 3}] | 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": "28029affae12ee62"} |
vcr_0a9a9747325d | 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, 1, 11, 6, 0]}, "output": 34}, {"input": {"nums": [5, 16, 1, 16, 7, 18, 6, 7, 1, 7]}, "output": 64}, {"input": {"nums": [7, 12, 16, 13, 4]}, "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": "6ca5636b23358821"} |
vcr_193a8e356fb0 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 2, 3, 7, 9, 11, 15]}, "output": [0, 1, 2, 3, 7, 9, 11, 15]}, {"input": {"nums": [1, 2, 4, 12]}, "output": [1, 2, 4, 12]}, {"input": {"nums": [4, 5, 8, 11, 11, 11]}, "output": [4, 5, 8, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "aaba97914b7b790b"} |
vcr_37dfb1c1f368 | 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": [72, 30, 59, 120, 129, 186, 45, 13, 37, 10, 128]}, "output": false}, {"input": {"nums": [17, 19, 2, 13, 13, 4, 14, 21, 6, 0]}, "output": true}, {"input": {"nums": [10, 14, 10, 12]}, "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": "c32a4e14c8660f90"} |
vcr_086e183dc335 | 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": [7, 5, -5, -5, 3, -13, -5, 12, -15, -16, 9], "k": 0}, "output": [7, 5, -5, -5, 3, -13, -5, 12, -15, -16, 9]}, {"input": {"nums": [-10, 6, 10, -6, -2, -6, -8, 1], "k": 13}, "output": [-6, -2, -6, -8, 1, -10, 6, 10]}, {"input": {"nums": [-9, 4, -10, -8, 2], "k": 17}, "output": [-8, 2, -9, 4, -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": "7bb12f461256f8a7"} |
vcr_6808412fd7fe | 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": 65192}, "output": 10}, {"input": {"n": 675}, "output": 5}, {"input": {"n": 8150}, "output": 10}] | 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": "4e75ce7724cfb89b"} |
vcr_dbf8333572c9 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-18, -22, -14, 7]}, "output": [-18, -40, -54, -47]}, {"input": {"nums": [9, -6, -6, 3, 10, 12]}, "output": [9, 3, -3, 0, 10, 22]}, {"input": {"nums": [-1, 3]}, "output": [-1, 2]}] | 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": "3a2fe1ba9085b7af"} |
vcr_857b23209429 | 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": [9, 3, 23, 9, 38, 2, 2, 1, 55, 36, 36, 23, 38, 3, 1]}, "output": 55}, {"input": {"nums": [43, 24, 6, 10, 6, 24, 10]}, "output": 43}, {"input": {"nums": [14, 15, 7, 27, 21, 52, 7, 14, 27, 22, 22, 21, 15]}, "output": 52}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "505eff8a61834671"} |
vcr_da03a2633cc6 | 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": [13, 1, 65, 29, 1, 29, 13]}, "output": 65}, {"input": {"nums": [6, 48, 3, 6, 3]}, "output": 48}, {"input": {"nums": [10, 3, 48, 1, 10, 3, 25, 1, 19, 19, 25]}, "output": 48}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "ce20daec5a335c08"} |
vcr_bc1600c89a9c | 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": "bcabe cbecc ebdaa"}, "output": "ebdaa cbecc bcabe"}, {"input": {"s": "a aaab aabb b abb"}, "output": "abb b aabb aaab a"}, {"input": {"s": "baa baaa baa aaab"}, "output": "aaab baa baaa baa"}] | 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": "cb733529a5d4a38f"} |
vcr_d19849c821e3 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 3, 4, 4, 12]}, "output": [0, 1, 3, 4, 12]}, {"input": {"nums": [0, 1, 2, 4, 4, 6, 6, 7, 9, 9, 12]}, "output": [0, 1, 2, 4, 6, 7, 9, 12]}, {"input": {"nums": [0, 3, 8, 10]}, "output": [0, 3, 8, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "680d74058cd1e37a"} |
vcr_dcf36b6c3c61 | 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, 19, -20, -21, -13, -8, 17, 23, -4, -21, -24]}, "output": [3, 22, 2, -19, -32, -40, -23, 0, -4, -25, -49]}, {"input": {"nums": [-12, 2, -6, -11, 11]}, "output": [-12, -10, -16, -27, -16]}, {"input": {"nums": [18, 5, -2, 17]}, "output": [18, 23, 21, 38]}] | 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": "d978a5aff4df77f7"} |
vcr_6c63e8be63af | 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": [5, 20, -6, -9, -2, -6, 6, 14, 3]}, "output": 25}, {"input": {"nums": [8, 2, 11, -3, -11, 1, 9, 14, 1]}, "output": 32}, {"input": {"nums": [-9, 10, -8, 6, -3]}, "output": 10}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "3e28db1ac0f19474"} |
vcr_644b792125d8 | 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": [15, -21, 9, -3, -9, -4, -3, 32, -4]}, "output": [15, -6, 3, 0, -9, -13, -16, 16, 12]}, {"input": {"nums": [-6, -11]}, "output": [-6, -17]}, {"input": {"nums": [9, 13, 15, 16, 6]}, "output": [9, 22, 37, 53, 59]}] | 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": "a6a873a4709c0bbf"} |
vcr_df39ce29e103 | 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": 50517}, "output": 8}, {"input": {"n": 1653}, "output": 7}, {"input": {"n": 2204}, "output": 5}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "2c43502fbb00bf80"} |
vcr_021eaf7d43d8 | 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": "dbaacdbdcaabd"}, "output": true}, {"input": {"s": "cabbabbcb"}, "output": false}, {"input": {"s": "accccca"}, "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": "27fc033422bb715d"} |
vcr_5079b3190814 | 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": [10, 1, 50, 1, 19, 21, 10, 21, 19]}, "output": 50}, {"input": {"nums": [21, 29, 25, 21, 6, 6, 5, 42, 5, 25, 29]}, "output": 42}, {"input": {"nums": [57, 4, 22, 4, 11, 22, 1, 11, 8, 1, 8]}, "output": 57}] | 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": "eba8aeec71ac7cff"} |
vcr_7369a6bafeeb | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 4, 5, 6, 9, 9, 14]}, "output": [2, 4, 5, 6, 9, 14]}, {"input": {"nums": [1, 1, 6, 8, 9, 11]}, "output": [1, 6, 8, 9, 11]}, {"input": {"nums": [0, 1, 5, 6, 7, 7, 10, 10, 10]}, "output": [0, 1, 5, 6, 7, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "f9dd4de13af579ef"} |
vcr_a9e3ee97c138 | 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": 92, "b": 100}, "output": 4}, {"input": {"a": 160, "b": 113}, "output": 1}, {"input": {"a": 61, "b": 175}, "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": "42125197c9d93c34"} |
vcr_3929ba080ca6 | 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, 7, 11, 15, 20, 22, 26, 28, 38], "target": 15}, "output": 4}, {"input": {"nums": [10, 27, 29, 36], "target": 5}, "output": 0}, {"input": {"nums": [2, 8, 10, 20, 24, 27, 32, 34, 36], "target": 27}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "4856aa69b3a32da0"} |
vcr_2adf93c15575 | 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": [56, 2, 17, 46, 45, 7]}, "output": 44}, {"input": {"prices": [11, 36, 31, 23, 34, 14, 8]}, "output": 25}, {"input": {"prices": [6, 26, 40, 13]}, "output": 34}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "2b730c6237a9c5bf"} |
vcr_a08fc4c819c1 | 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": "acbcabccbacbca"}, "output": true}, {"input": {"s": "aabbaaabb"}, "output": false}, {"input": {"s": "babaccabbc"}, "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": "f1f60b74b0aa27d1"} |
vcr_86a27cf8083c | 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": [23, 18, 28, 21, 18, 28, 23, 21, 62]}, "output": 62}, {"input": {"nums": [28, 5, 28, 40, 8, 8, 5]}, "output": 40}, {"input": {"nums": [17, 18, 21, 21, 19, 50, 19, 18, 17]}, "output": 50}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "781d8438979df952"} |
vcr_eb804af8eff1 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [12, 9, -1, -13, 4, -17, 0, -15]}, "output": 21}, {"input": {"nums": [-1, 2, 1, 8, -9]}, "output": 11}, {"input": {"nums": [0, -12, -7, 10, -9, -6]}, "output": 10}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "183cfcb8a1aa00f8"} |
vcr_2efae7b7445d | 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": [49, 6, 39, 19, 36, 53, 27, 18, 49]}, "output": 47}, {"input": {"prices": [5, 16, 30, 3]}, "output": 25}, {"input": {"prices": [23, 31, 24, 20, 38, 18, 17, 1, 4]}, "output": 18}] | 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": "68d70b220fab8736"} |
vcr_5d37c013ee64 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [29, 21, 7, 19, 22, 16, 25, 18]}, "output": 83}, {"input": {"nums": [16, 7, 2, 7, 10, 1]}, "output": 28}, {"input": {"nums": [19, 16, 9, 14]}, "output": 33}] | 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": "3c52f09028d6e5d4"} |
vcr_7a2e7186c758 | 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": [11, 43, 43, 26, 1]}, "output": 32}, {"input": {"prices": [30, 34, 22, 6, 10, 13, 29, 30, 20, 20]}, "output": 24}, {"input": {"prices": [40, 27, 18, 29, 39, 10, 19, 21, 37, 34]}, "output": 27}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "7625f1c83a2dac0d"} |
vcr_8e43ea54dd05 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 6, 5, 6, 3, 2]}, "output": [1080, 1260, 1512, 1260, 2520, 3780]}, {"input": {"nums": [1, 3, 2, 3]}, "output": [18, 6, 9, 6]}, {"input": {"nums": [4, 4, 4, 1, 2, 5]}, "output": [160, 160, 160, 640, 320, 128]}] | 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": "69f4cda7b2725f25"} |
vcr_82dd502ad857 | 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", "ab", "ba", "caa", "bc"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aab", "abb", "baa", "aab"]}, "output": [1, 3]}, {"input": {"strs": ["bbb", "baa", "aab", "bbb"]}, "output": [2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "3d72452bf0ff4af5"} |
vcr_e179403163ab | 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": [83, 192, 2, 181, 17, 151, 127, 102, 167, 144, 16, 13]}, "output": false}, {"input": {"nums": [10, 24, 10, 19, 18, 7, 22, 12, 24, 14, 19]}, "output": true}, {"input": {"nums": [7, 15, 21, 16, 23, 9]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "61b05c903e178a4e"} |
vcr_7b26bc35f922 | hard | 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": "bddbbcacdbdc"}, "output": false}, {"input": {"s": "bcc"}, "output": false}, {"input": {"s": "ab"}, "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": "a1a62690d5968134"} |
vcr_1a42af86b8cc | 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": [68, 46, -19, 12, -13, -2, 8, -17], "target": 33}, "output": [1, 4]}, {"input": {"nums": [-26, 2, -18, -5, 24, 37, 1, 35, 31, -11], "target": 6}, "output": [2, 4]}, {"input": {"nums": [4, 18, 33, 25, 9, -27, 44, 28, -29], "target": 32}, "output": [0, 7]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "cc05e94d9c74ee43"} |
vcr_84cfa7fcf857 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [6, 12, 17, 19, 25, 29, 31, 47], "target": 7}, "output": 1}, {"input": {"nums": [9, 13, 15, 19, 23, 27, 28, 31, 32, 33, 38], "target": 10}, "output": 1}, {"input": {"nums": [8, 18, 34, 36], "target": 2}, "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": "18bbb235fb66f5d3"} |
vcr_1770b6bc7476 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [20, 8, 27, 13, 32, 32, 54, 27, 8, 20, 37, 37, 7, 13, 7]}, "output": 54}, {"input": {"nums": [11, 5, 11, 5, 2, 8, 2, 1, 21, 21, 1, 57, 8]}, "output": 57}, {"input": {"nums": [9, 9, 12, 18, 25, 25, 40, 14, 12, 14, 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": "eb1994c8ce328bd1"} |
vcr_09389fa85e63 | 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, 20, 27, 8, 1, 20, 19, 21]}, "output": 96}, {"input": {"nums": [14, 3, 6, 9]}, "output": 23}, {"input": {"nums": [14, 9, 12, 18, 0, 4, 0]}, "output": 36}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "2f3c876abbb8198d"} |
vcr_ec7c86019371 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 3, 4, 11, 12, 15, 20, 22, 30, 32, 37], "target": 39}, "output": 11}, {"input": {"nums": [10, 20, 26, 32], "target": 34}, "output": 4}, {"input": {"nums": [23, 25, 33], "target": 8}, "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": "df19b66952a69e9e"} |
vcr_06aca5788fea | 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": [26, 19, 51, 80, 59, 24, 151, 58, 50, 84, 95, 144, 71, 34]}, "output": false}, {"input": {"nums": [4, 13, 13, 15, 9, 21]}, "output": true}, {"input": {"nums": [4, 3, 22, 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": "b70a393e96047798"} |
vcr_8b20860e7c87 | 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": 2732}, "output": 6}, {"input": {"n": 16381}, "output": 13}, {"input": {"n": 12251}, "output": 11}] | 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": "787ddb1cbdd08d1f"} |
vcr_4be768905e2a | 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": 45829}, "output": 7}, {"input": {"n": 7943}, "output": 8}, {"input": {"n": 4811}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "612c89e77f750b1e"} |
vcr_a7644a15818d | 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": [20, 1, 6, -10], "k": 11}, "output": [1, 6, -10, 20]}, {"input": {"nums": [1, 0, 10, -4, -4, 1], "k": 11}, "output": [0, 10, -4, -4, 1, 1]}, {"input": {"nums": [-5, 0, 2, 1, 5, -9, 8, 5, 7, -1], "k": 5}, "output": [-9, 8, 5, 7, -1, -5, 0, 2, 1, 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": "a7a2952646ba7c9b"} |
vcr_1e4bd351d7ac | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [14, 4, 12, 5, 16, 1, 30, 28, 9, 16]}, "output": 88}, {"input": {"nums": [7, 0, 12, 15, 8, 0, 1, 10]}, "output": 37}, {"input": {"nums": [2, 16, 1, 10, 0]}, "output": 26}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "290d53d74083013e"} |
vcr_4df4c48707a3 | 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, 0, 4, 3, 4, 0]}, "output": false}, {"input": {"nums": [2, 2, 3, 3, 3]}, "output": true}, {"input": {"nums": [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": "2cc15d1ce9ecb152"} |
vcr_4eeb736f6a54 | 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, 6, 7, 11, 14, 22, 25, 28, 31, 39, 46, 49], "target": 26}, "output": 8}, {"input": {"nums": [5, 12, 24, 33], "target": 1}, "output": 0}, {"input": {"nums": [4, 9, 19, 21, 25, 30, 37, 38, 39], "target": 0}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "6145bce5d190e8f8"} |
vcr_4c0f4b0a75b6 | 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": [8, 8, 8, 10, 14, 15]}, "output": [8, 10, 14, 15]}, {"input": {"nums": [0, 0, 1, 2, 3, 4, 5, 7, 7, 10, 12]}, "output": [0, 1, 2, 3, 4, 5, 7, 10, 12]}, {"input": {"nums": [4, 6, 6, 8, 9]}, "output": [4, 6, 8, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "425bd3e2d196d2e4"} |
vcr_e519324df87d | 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": "bcbcccabbba"}, "output": 7}, {"input": {"s": "ccac"}, "output": 3}, {"input": {"s": "caaccbbab"}, "output": 6}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "396acd1cea91aeac"} |
vcr_ce025cbe46a9 | 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, 5, 15, 18, 27, 30, 38, 41, 47, 48], "target": 43}, "output": 9}, {"input": {"nums": [3, 9, 17, 19, 20, 21, 27, 28, 29, 30, 38], "target": 18}, "output": 3}, {"input": {"nums": [2, 5, 8, 9, 12, 15, 21, 27, 31, 32, 39], "target": 35}, "output": 10}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "6449ca769547f193"} |
vcr_038047be5f2c | 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": [8, 19, -14, -5, -19, 4], "k": 11}, "output": [19, -14, -5, -19, 4, 8]}, {"input": {"nums": [7, 7, -3, 8, -9], "k": 20}, "output": [7, 7, -3, 8, -9]}, {"input": {"nums": [8, -5, 6, -5, -10, 4, -5, 3, -1, 10], "k": 5}, "output": [4, -5, 3, -1, 10, 8, -5, 6, -5, -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": "10d64e137f91fba1"} |
vcr_3029c8272f99 | 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": 23919}, "output": 11}, {"input": {"n": 32}, "output": 1}, {"input": {"n": 8296}, "output": 4}] | 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": "7904a6b6c93f4771"} |
vcr_00127c2d06e1 | 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": [3, 26, 32, 11, 44, 28, 42, 3]}, "output": true}, {"input": {"nums": [19, 17, 17, 22, 17, 15]}, "output": true}, {"input": {"nums": [21, 21, 10, 0]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "3ae0e1acf1d7905d"} |
vcr_66bde1bf167f | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [5, 12, 25, 15, 26, 2, 11, 23, 3]}, "output": 79}, {"input": {"nums": [15, 20, 8, 10, 8, 14, 12]}, "output": 44}, {"input": {"nums": [2, 7, 0, 3, 15, 11, 7, 5, 13, 15]}, "output": 44}] | 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": "c94745dfd440cf06"} |
vcr_cbc92bf7d926 | 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": [27, 14, -12, -23, 16, 24, 20]}, "output": [27, 41, 29, 6, 22, 46, 66]}, {"input": {"nums": [-1, 5, -9, 12, -12]}, "output": [-1, 4, -5, 7, -5]}, {"input": {"nums": [-7, -10, -4, 18, 16, 12, -9, 17, 11, 2]}, "output": [-7, -17, -21, -3, 13, 25, 16, 33, 44, 46]}] | 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": "70372670fd992a12"} |
vcr_13ef7b01504d | 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, 5, 6, 7, 12, 23, 33, 35, 45], "target": 28}, "output": 6}, {"input": {"nums": [8, 11, 12, 18, 21, 24, 29], "target": 21}, "output": 4}, {"input": {"nums": [4, 6, 9, 10, 14, 31, 36, 39], "target": 38}, "output": 7}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "27c8dad461adc4f4"} |
vcr_1b2c3febb91d | 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": [0, 7, 25, 7, 15, 14, 9, 1, 24, 20, 28]}, "output": 101}, {"input": {"nums": [7, 12, 7, 17]}, "output": 29}, {"input": {"nums": [6, 13, 3, 10]}, "output": 23}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "bb1d230e735f93d6"} |
vcr_6c9b881e2e48 | 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": [-20, -7, -7, 9, -11, 1, -5, -8], "k": 22}, "output": [-7, 9, -11, 1, -5, -8, -20, -7]}, {"input": {"nums": [3, -3, 5, 1, -2, -3, 1, -8, 5, 9], "k": 7}, "output": [1, -2, -3, 1, -8, 5, 9, 3, -3, 5]}, {"input": {"nums": [10, 10, -6, 8, -5, -9], "k": 7}, "output": [-9, 10, 10, -6, 8, -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": "b42e5b64b7759497"} |
vcr_39d9e06f27a7 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["ba", "aa", "ca", "bb", "bba"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bbb", "bba"]}, "output": [1, 2]}, {"input": {"strs": ["aba", "bab", "aab", "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": "3b1df34dbfc716bf"} |
vcr_fefc802e8fbc | 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": [7, 14, 10, 10, 16, 29, 21, 11, 5, 26, 0]}, "output": 90}, {"input": {"nums": [4, 8, 11, 5, 18]}, "output": 33}, {"input": {"nums": [12, 15, 12, 20, 14, 11]}, "output": 46}] | 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": "27d15b40d0e9c34c"} |
vcr_31c626e2d512 | 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, 14, 9, 17, 26, 12, 26, 8, 17, 3, 18, 11]}, "output": 119}, {"input": {"nums": [15, 17, 12, 9, 15]}, "output": 42}, {"input": {"nums": [12, 10]}, "output": 12}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "b1ece6c607e0895b"} |
vcr_7aa8a9c1a6cc | 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, 5, 5, 8, 9, 14]}, "output": [1, 2, 5, 8, 9, 14]}, {"input": {"nums": [0, 1, 3, 3, 8, 8, 9, 9, 9, 11, 12, 12]}, "output": [0, 1, 3, 8, 9, 11, 12]}, {"input": {"nums": [3, 5, 6, 9, 12]}, "output": [3, 5, 6, 9, 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": "fc88a0c24c8e0df4"} |
vcr_cdf3e5596f19 | 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, 22, 12, 5, 15, 9, 16, 19, 21, 30, 21, 26]}, "output": 126}, {"input": {"nums": [3, 6, 7, 4, 16, 5, 5, 6, 7, 20]}, "output": 52}, {"input": {"nums": [13, 6, 12, 2, 14, 10]}, "output": 39}] | 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": "342a917539801cd0"} |
vcr_3f0773b55010 | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [6, -23, 24, -37, 65, 55, -20, 79, -24, 25, 29, 41, 58], "target": -43}, "output": [1, 6]}, {"input": {"nums": [-19, 34, -9, -29, 43, 14, 28, 48], "target": 14}, "output": [3, 4]}, {"input": {"nums": [-6, -19, -20, -9], "target": -28}, "output": [1, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "894151ab9650c289"} |
vcr_334dde15202c | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [10, -7, 16, -24, 19, 5, -10, -8, 4, -3]}, "output": [10, 3, 19, -5, 14, 19, 9, 1, 5, 2]}, {"input": {"nums": [-10, 3, 13, 12, -12, 5, -4]}, "output": [-10, -7, 6, 18, 6, 11, 7]}, {"input": {"nums": [-1, 5, 14, 7]}, "output": [-1, 4, 18, 25]}] | 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": "27351b8157c57d3d"} |
vcr_ed1b062edc99 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 9, 10, 31, 45], "target": 38}, "output": 4}, {"input": {"nums": [0, 11, 12, 25, 26, 27, 29, 31, 36, 38], "target": 7}, "output": 1}, {"input": {"nums": [9, 22, 27, 30, 34, 35, 36, 39], "target": 22}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "d109e026e48b73f6"} |
vcr_d3134188cf76 | 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": ["cb", "ca", "ca", "ab", "acab", "ca", "bb", "cbbb"]}, "output": [1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bab", "aaa", "aaa"]}, "output": [1, 2]}, {"input": {"strs": ["aba", "bba", "bbb", "bba", "baa", "bab"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "1f0c56cb70c43909"} |
vcr_b07f3432521a | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 1, 8, 9, 10, 30, 49], "target": 22}, "output": 5}, {"input": {"nums": [0, 2, 4, 5, 11, 12, 18, 23, 25, 28, 30, 33], "target": 24}, "output": 8}, {"input": {"nums": [9, 20, 23, 36, 39], "target": 23}, "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": "f006e1d9a6d2c404"} |
vcr_bcef67870869 | 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": [-19, -25, 33, -17, -13]}, "output": [-19, -44, -11, -28, -41]}, {"input": {"nums": [7, -3, 4]}, "output": [7, 4, 8]}, {"input": {"nums": [-10, 11, 7]}, "output": [-10, 1, 8]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "9de0c8f0f9f75357"} |
vcr_fd654c103700 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [7, 8, 9, 24, 27, 29, 30, 31, 34, 36, 37, 49], "target": 29}, "output": 5}, {"input": {"nums": [4, 13, 17, 25, 28, 36, 37], "target": 2}, "output": 0}, {"input": {"nums": [4, 6, 8, 12, 15, 19, 23, 27, 28, 33, 36], "target": 21}, "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": "65cdef78a9be7a12"} |
vcr_29a498bc5a37 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [7, 15, 36, 7, 1, 75, 34, 1, 15, 36, 32, 34, 32]}, "output": 75}, {"input": {"nums": [14, 13, 46, 4, 13, 14, 4]}, "output": 46}, {"input": {"nums": [43, 3, 4, 15, 28, 3, 2, 2, 4, 28, 15]}, "output": 43}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "381140b6e3935ecb"} |
vcr_273bdabbe967 | 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": [29, 30, 21, 25, 8, 19, 13, 29]}, "output": true}, {"input": {"nums": [7, 8, 1, 15, 3, 18, 22, 1, 10, 12]}, "output": true}, {"input": {"nums": [9, 4, 3, 2, 18]}, "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": "c779eeb5bb3cd4ef"} |
vcr_14b436603d57 | 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, 8, 24, -21]}, "output": [-12, -4, 20, -1]}, {"input": {"nums": [18, -7, 6, 2, 15, 6, -8]}, "output": [18, 11, 17, 19, 34, 40, 32]}, {"input": {"nums": [8, -10]}, "output": [8, -2]}] | 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": "8307b93460e9cef6"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.