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_31f4931f2cf3 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 3, 3, 4, 5, 7, 8, 12]}, "output": [2, 3, 4, 5, 7, 8, 12]}, {"input": {"nums": [0, 4, 4, 4, 5, 6, 8, 9, 11, 11, 11, 12]}, "output": [0, 4, 5, 6, 8, 9, 11, 12]}, {"input": {"nums": [3, 10, 11]}, "output": [3, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "e626c79efc116e18"} |
vcr_0b23cef5970e | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-14, -13, -8, 19, 18, -15, -2, -20, -3, -16, 6], "k": 20}, "output": [-8, 19, 18, -15, -2, -20, -3, -16, 6, -14, -13]}, {"input": {"nums": [10, 1, 7, 3, -3, 4, 5, -6], "k": 3}, "output": [4, 5, -6, 10, 1, 7, 3, -3]}, {"input": {"nums": [3, 9, -3, 10, -1, 1, -3, 8, 2], "k": 8}, "output": [9, -3, 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": "b5b8d54588eb227b"} |
vcr_0fc1229d91a0 | 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": "eb ced daaea dabd ddddec"}, "output": "ddddec dabd daaea ced eb"}, {"input": {"s": "aab bb bbab bbba"}, "output": "bbba bbab bb aab"}, {"input": {"s": "bbbb aba"}, "output": "aba bbbb"}] | 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": "b7126ed6f38f8a6c"} |
vcr_b2e08d224ef7 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-12, 20, -9, -3, 17], "k": 10}, "output": [-12, 20, -9, -3, 17]}, {"input": {"nums": [4, -2, -5], "k": 9}, "output": [4, -2, -5]}, {"input": {"nums": [-3, 1, -3, 0, 1], "k": 11}, "output": [1, -3, 1, -3, 0]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "26bccfce55ea55c1"} |
vcr_5bd3a4fa80a8 | 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", "ab", "bcb", "bbcb", "bacc"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["abb", "aab", "bba", "baa", "aaa", "aba"]}, "output": [1, 2, 3]}, {"input": {"strs": ["aba", "bba", "abb", "bbb"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "bd3bfdb50f53740b"} |
vcr_77e605d3041d | 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": [-5, -2, 8, 38, 40], "target": 36}, "output": [1, 3]}, {"input": {"nums": [-20, -5, 1, 4, 16, 23, 30, 39], "target": 34}, "output": [1, 7]}, {"input": {"nums": [-13, -6, -4, -3, -2, 6, 15, 21, 28], "target": -17}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "eb456b90e6080c64"} |
vcr_d111b38d614b | 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": ["cac", "abbb", "bbcc", "bb", "cbb", "bb"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "bbb", "abb", "abb", "aaa", "aab", "bab"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["aaa", "bba", "bab", "baa"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "edbae23ce2736a02"} |
vcr_f9b3356e7a03 | 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, 29, 2, 10, 7, 53, 10, 2, 37, 11, 29, 11, 28, 37, 28]}, "output": 53}, {"input": {"nums": [26, 7, 52, 7, 9, 9, 22, 22, 26]}, "output": 52}, {"input": {"nums": [7, 22, 7, 8, 3, 8, 22, 53, 3]}, "output": 53}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "102a4122f9d87605"} |
vcr_20854a37faef | 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": "abacbcccbc"}, "output": 8}, {"input": {"s": "abcaa"}, "output": 4}, {"input": {"s": "bbac"}, "output": 3}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "60d13fb6fcd61c06"} |
vcr_885d5ff12750 | 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": [44, 29, 27, 49, 39, 43, 32, 20, 5, 15, 44]}, "output": true}, {"input": {"nums": [13, 16, 6, 19, 7, 13, 1]}, "output": true}, {"input": {"nums": [18, 20, 24, 4, 0, 2, 2, 9, 11, 6]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "e57a6b6f07edd8d8"} |
vcr_d3bcaf049318 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [2, 4, 6, 7, 5, 5, 1]}, "output": [4200, 2100, 1400, 1200, 1680, 1680, 8400]}, {"input": {"nums": [4, 1, 5, 2, 2, 2, 3]}, "output": [120, 480, 96, 240, 240, 240, 160]}, {"input": {"nums": [4, 5, 2, 3]}, "output": [30, 24, 60, 40]}] | 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": "e25f1e897574436d"} |
vcr_c32d2a2e8650 | 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": [19, 14, 14, 29, 68, 5, 5, 29, 19]}, "output": 68}, {"input": {"nums": [21, 21, 8, 44, 8]}, "output": 44}, {"input": {"nums": [4, 53, 18, 2, 20, 4, 1, 29, 20, 18, 29, 1, 2]}, "output": 53}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "bb81be2484c0ca59"} |
vcr_a8f92824d686 | 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": [129, 77, 180, 28, 149, 135, 195]}, "output": false}, {"input": {"nums": [16, 11, 21, 0, 7, 15, 1, 18, 17]}, "output": false}, {"input": {"nums": [5, 7, 24, 25, 12, 19, 19, 20, 20]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "ccd2f43022a31c86"} |
vcr_c58f4c476ba6 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-9, 7, -10, -8, 13, 15], "k": 12}, "output": [-9, 7, -10, -8, 13, 15]}, {"input": {"nums": [10, -6, 2, 1, 3, -9, 6, 8, 2], "k": 1}, "output": [2, 10, -6, 2, 1, 3, -9, 6, 8]}, {"input": {"nums": [10, 7, -1, 8, 2, -4], "k": 14}, "output": [2, -4, 10, 7, -1, 8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "835f9c2ea8fd3cd9"} |
vcr_461fe829988d | 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, 17, 12, -7, -12, -22, 27, -2, -10, 26, 4, -10, 12, 10]}, "output": [-15, 2, 14, 7, -5, -27, 0, -2, -12, 14, 18, 8, 20, 30]}, {"input": {"nums": [-3, 14, -7, 9, 14, 12, 3, 15]}, "output": [-3, 11, 4, 13, 27, 39, 42, 57]}, {"input": {"nums": [-6, -3, 6, 13, 5, 11, 15, 8]}, "output": [-6, -9, -3,... | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "33269d849b159384"} |
vcr_cf71dc072b12 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 16, 21, 32, 36], "target": 36}, "output": 4}, {"input": {"nums": [4, 16, 21, 29, 34, 35, 38], "target": 13}, "output": 1}, {"input": {"nums": [3, 18, 21, 30, 38], "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": "36bba979a633c9ce"} |
vcr_bb663988c19b | 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": 4223}, "output": 8}, {"input": {"n": 15339}, "output": 11}, {"input": {"n": 11175}, "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": "ff0572991bf8f815"} |
vcr_8d6e138f408a | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 2, 5, 4, 6, 2]}, "output": [480, 1200, 480, 600, 400, 1200]}, {"input": {"nums": [5, 1, 1, 3]}, "output": [3, 15, 15, 5]}, {"input": {"nums": [2, 1, 5, 2]}, "output": [10, 20, 4, 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": "4cdc1dad6eb153f1"} |
vcr_edb5af7dc9c5 | 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": [-8, -11, 33, -13, -9, 12]}, "output": [-8, -19, 14, 1, -8, 4]}, {"input": {"nums": [11, 7]}, "output": [11, 18]}, {"input": {"nums": [-5, 16]}, "output": [-5, 11]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "2eff469ba508449a"} |
vcr_5f7f8aeda51f | 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": [107, 11, 177, 180, 89, 110, 191]}, "output": false}, {"input": {"nums": [17, 14, 15, 9, 10, 20, 13, 18, 11]}, "output": false}, {"input": {"nums": [17, 20, 0, 0, 6, 0, 0, 2, 15, 24, 21]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "ab796d9bdc42e9e1"} |
vcr_01a0a3b10c0d | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, -19, -4, 6, 16, 26, 39, 47, 49], "target": 53}, "output": [3, 7]}, {"input": {"nums": [-12, -9, -5, 21, 22, 27, 29, 32, 34, 35], "target": 24}, "output": [2, 6]}, {"input": {"nums": [-16, -4, 9, 12, 15, 18], "target": 5}, "output": [1, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "382733b967b04429"} |
vcr_2b811d16daa7 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [48, 12, 29, 14, 17, 48]}, "output": true}, {"input": {"nums": [6, 0, 9, 22, 16, 0, 4, 1, 17, 15, 18]}, "output": true}, {"input": {"nums": [5, 25, 17, 25, 21, 23, 17, 11, 19]}, "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": "431fbf0568d7d7cb"} |
vcr_69cb4b205e7c | 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": [24, 14, 17, 16, 73, 24, 2, 4, 26, 16, 14, 7, 2, 17, 26, 7, 4]}, "output": 73}, {"input": {"nums": [9, 3, 5, 5, 3, 17, 53, 13, 9, 17, 13]}, "output": 53}, {"input": {"nums": [25, 17, 12, 27, 22, 22, 57, 19, 25, 27, 17, 12, 19]}, "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": "33f85ac3289a0c84"} |
vcr_c1e75c8dd224 | 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, 3, 0, 1, 1, 3, 0]}, "output": true}, {"input": {"nums": [0, 1, 1, 0, 3, 2]}, "output": false}, {"input": {"nums": [3, 1, 2, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "c30e4243e4a618bb"} |
vcr_6ea7492482ae | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-16, -11, 15, -2, 15], "k": 2}, "output": [-2, 15, -16, -11, 15]}, {"input": {"nums": [9, -3, -9, -10, 10, -4, 5, -7, -5, 9], "k": 14}, "output": [5, -7, -5, 9, 9, -3, -9, -10, 10, -4]}, {"input": {"nums": [3, 4, -8], "k": 13}, "output": [-8, 3, 4]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "7d13c18faca37122"} |
vcr_3ded631e9a4a | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-13, -6, -12, -10, 2, 1, 4, 12, -7, -18, -5, 15, 1, -9, 14]}, "output": 21}, {"input": {"nums": [3, 13, 9, -5, 4, -7, 2, 5, 9, 1, -7]}, "output": 34}, {"input": {"nums": [2, 0, -12, 5, -7, 10, -6, 11]}, "output": 15}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "b6df115b3cadb4e2"} |
vcr_334d6185f6f5 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bc", "aacc", "bcab", "bccc", "caca", "caca", "bacb", "aaa"]}, "output": [1, 1, 1, 2, 3]}, {"input": {"strs": ["aaa", "bab", "aab", "bbb", "abb"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aba", "aaa", "aab", "aaa", "bba", "aaa", "bab"]}, "output": [2, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "ad53be79dbfda7a3"} |
vcr_14f2838f383b | 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": ["bca", "cc", "abca", "accc", "cb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bba", "bba", "aab", "baa", "bab", "abb"]}, "output": [1, 2, 4]}, {"input": {"strs": ["bba", "bab", "bba", "baa"]}, "output": [1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "56f8b6b9734e6bd7"} |
vcr_fcf46703ad19 | 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": [183, 195, 74, 64, 123, 46, 109, 190, 199, 147, 110, 77, 133, 119]}, "output": false}, {"input": {"nums": [8, 8, 6, 16, 12, 5]}, "output": true}, {"input": {"nums": [0, 5, 1, 25, 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": "51b2f1884702f998"} |
vcr_872f683b30bd | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [35, 14, 3, 58, 14, 35, 38, 37, 3, 26, 37, 26, 1, 38, 1]}, "output": 58}, {"input": {"nums": [45, 25, 8, 8, 25, 24, 15, 21, 21, 15, 24]}, "output": 45}, {"input": {"nums": [4, 28, 4, 15, 24, 15, 24, 56, 28]}, "output": 56}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "929da3e8b0a5e977"} |
vcr_a927b136675b | 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, 3, 9, 9, 14]}, "output": [3, 9, 14]}, {"input": {"nums": [0, 0, 3, 5, 8, 8, 10, 12]}, "output": [0, 3, 5, 8, 10, 12]}, {"input": {"nums": [0, 1, 6, 11]}, "output": [0, 1, 6, 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": "445c830f7b421583"} |
vcr_bcacaaac1ec7 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 5, 5, 5, 8, 9, 15]}, "output": [0, 1, 5, 8, 9, 15]}, {"input": {"nums": [5, 7, 8, 11]}, "output": [5, 7, 8, 11]}, {"input": {"nums": [0, 5, 7, 8, 9, 9, 10, 10, 10, 11, 11, 12]}, "output": [0, 5, 7, 8, 9, 10, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "5fcae127630f1175"} |
vcr_f3797c75d095 | 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": 39583}, "output": 10}, {"input": {"n": 49}, "output": 3}, {"input": {"n": 368}, "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": "cd924e1b3020768d"} |
vcr_eb541862f4f8 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [20, 8, 28, 25, 3, 3, 30, 24]}, "output": 81}, {"input": {"nums": [2, 0, 10, 20]}, "output": 22}, {"input": {"nums": [1, 10, 1, 4, 7, 0, 19, 2, 8, 1]}, "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": "f1b49e5675353819"} |
vcr_110ea12592fe | 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": "bccaee ebecb a ccebc deced"}, "output": "deced ccebc a ebecb bccaee"}, {"input": {"s": "bba b b aa"}, "output": "aa b b bba"}, {"input": {"s": "baa bab a aaba aab"}, "output": "aab aaba a bab 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": "cd7083881445e6db"} |
vcr_0ccc81aa1e38 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-27, -24, -13, -12, -8, -6, 24, 38, 41], "target": -3}, "output": [0, 6]}, {"input": {"nums": [-15, -7, 13, 24, 31], "target": 6}, "output": [1, 2]}, {"input": {"nums": [-16, -9, -8, -6, -2, 5, 7, 9, 13, 31, 36], "target": 25}, "output": [3, 9]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "9996f10efb5deeca"} |
vcr_f863a73f77b6 | 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": [37, 63, 66, 84, 60, 22, 187, 31, 41, 173, 171, 12, 127, 124]}, "output": false}, {"input": {"nums": [5, 11, 12, 1, 11, 17, 15, 2, 7, 16, 18]}, "output": true}, {"input": {"nums": [5, 18, 7, 6, 14]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "0b361fc0c69f11bb"} |
vcr_0167e1e632fb | 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": "aabbaaabba"}, "output": 5}, {"input": {"s": "aaaacbbcbc"}, "output": 6}, {"input": {"s": "abcba"}, "output": 5}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "ae13a797e3172c67"} |
vcr_05f4dc9d4274 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 8, 13, 16, 22, 36, 40, 42], "target": 3}, "output": 1}, {"input": {"nums": [0, 1, 2, 3, 4, 5, 11, 23, 25, 27, 30], "target": 23}, "output": 7}, {"input": {"nums": [4, 5, 7, 9, 18, 23, 26, 31], "target": 33}, "output": 8}] | 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": "12d743bb597b75df"} |
vcr_fcb5855630a9 | 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": [137, 176, 182, 3, 149]}, "output": false}, {"input": {"nums": [8, 9, 18, 24, 3, 8, 3, 16, 15, 22]}, "output": true}, {"input": {"nums": [21, 22, 1, 12, 24, 2, 16, 5]}, "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": "11bce247bbd78240"} |
vcr_4b7fd95e2d61 | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "baa eebcc cdcd dedea"}, "output": "dedea cdcd eebcc baa"}, {"input": {"s": "bbaa aa a bbbb abba"}, "output": "abba bbbb a aa bbaa"}, {"input": {"s": "abb aaba ab"}, "output": "ab aaba abb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "b7034858938dae22"} |
vcr_15fb833c86a6 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 3, 3, 3, 6, 7, 10, 13, 14, 15]}, "output": [0, 3, 6, 7, 10, 13, 14, 15]}, {"input": {"nums": [2, 6, 7]}, "output": [2, 6, 7]}, {"input": {"nums": [0, 2, 4, 7, 7, 8, 9, 10]}, "output": [0, 2, 4, 7, 8, 9, 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": "d99917eba1b8e05d"} |
vcr_df490e26643b | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [4, 3, 3, 2]}, "output": [18, 24, 24, 36]}, {"input": {"nums": [2, 3, 4, 2, 2, 1]}, "output": [48, 32, 24, 48, 48, 96]}, {"input": {"nums": [5, 1, 4, 2]}, "output": [8, 40, 10, 20]}] | 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": "7ab9864e82de0029"} |
vcr_fcf639fad994 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [5, 59, 26, 26, 31, 16, 29, 29, 31, 5, 16]}, "output": 59}, {"input": {"nums": [16, 19, 42, 19, 14, 16, 25, 1, 14, 25, 1]}, "output": 42}, {"input": {"nums": [26, 6, 5, 6, 4, 5, 1, 21, 59, 4, 21, 1, 26]}, "output": 59}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "a31a87775ae69c03"} |
vcr_043577ccce4a | 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": [2, 29, 45, 3, 24, 44, 15, 29, 8, 44, 9]}, "output": true}, {"input": {"nums": [10, 16, 11, 9]}, "output": false}, {"input": {"nums": [3, 23, 4, 25, 14]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "e7856169f038f21a"} |
vcr_450a481743b0 | 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": [37, 34, 12, 18, 34, 37, 13, 12, 31, 58, 31, 18, 3, 13, 3]}, "output": 58}, {"input": {"nums": [27, 6, 27, 6, 46]}, "output": 46}, {"input": {"nums": [15, 26, 44, 14, 15, 26, 4, 4, 14]}, "output": 44}] | 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": "0124ac691a7390b6"} |
vcr_f933c541f0be | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [9, 26, 0, 11]}, "output": 37}, {"input": {"nums": [18, 1, 20, 3, 2]}, "output": 40}, {"input": {"nums": [6, 0, 13, 15, 1, 17, 3]}, "output": 38}] | 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": "2eb97d4d3864c967"} |
vcr_f2f91d2bae82 | 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": "becda ccddb c dcecd addbd adcd"}, "output": "adcd addbd dcecd c ccddb becda"}, {"input": {"s": "baba ab a aaaa"}, "output": "aaaa a ab baba"}, {"input": {"s": "b bbaa a"}, "output": "a bbaa 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": "06dfdf98314394f0"} |
vcr_55ac74df046b | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "cbbccbcbccbbc"}, "output": true}, {"input": {"s": "bc"}, "output": false}, {"input": {"s": "aaccca"}, "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": "49d504b889991b17"} |
vcr_f10ce78d6968 | 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": [17, 8, -2, -11, -6, -8, 19, -8, 12], "k": 21}, "output": [19, -8, 12, 17, 8, -2, -11, -6, -8]}, {"input": {"nums": [0, 5, 8], "k": 11}, "output": [5, 8, 0]}, {"input": {"nums": [-5, -7, -5, -9], "k": 7}, "output": [-7, -5, -9, -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": "b8ccb6a539591222"} |
vcr_42b386bd226b | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [1, 3, 29]}, "output": [1, 4, 33]}, {"input": {"nums": [9, 1, 10, 8, 11, -12, -3]}, "output": [9, 10, 20, 28, 39, 27, 24]}, {"input": {"nums": [-11, 6, -11, 6, 17, 15, 0]}, "output": [-11, -5, -16, -10, 7, 22, 22]}] | 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": "6656e9a919a44aee"} |
vcr_4f3aef861a3d | 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": "ecbe cccec bcd"}, "output": "bcd cccec ecbe"}, {"input": {"s": "babb aaa a b bb"}, "output": "bb b a aaa babb"}, {"input": {"s": "bab bab bbba"}, "output": "bbba bab bab"}] | 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": "38c4e92fa8256993"} |
vcr_b74bc188961e | 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": ["aac", "abc", "bac", "bbaa", "cacc"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["bba", "abb", "aab"]}, "output": [1, 2]}, {"input": {"strs": ["bab", "bab", "aab", "aaa", "abb", "baa", "bbb"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "ea8d9599e3ce75e9"} |
vcr_d20c7e09ae49 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [6, -6, 13, -17, -10, 13, 11]}, "output": 24}, {"input": {"nums": [2, 7, 1, -8]}, "output": 10}, {"input": {"nums": [5, -11, 4, 1, -6, 1, 13]}, "output": 14}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "38520bdb65ab74a2"} |
vcr_8fdfcdda73c4 | 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": "aaaaaaabb"}, "output": 2}, {"input": {"s": "abcaaaac"}, "output": 5}, {"input": {"s": "bbabcabc"}, "output": 7}] | 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": "d031d387feb9867e"} |
vcr_b9860bd6d2ce | 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, 6, 6, 7, 8, 8, 11, 11, 14, 15]}, "output": [0, 1, 2, 3, 6, 7, 8, 11, 14, 15]}, {"input": {"nums": [0, 0, 4, 5, 7, 8, 8, 11, 12, 12]}, "output": [0, 4, 5, 7, 8, 11, 12]}, {"input": {"nums": [1, 6, 6, 8, 10]}, "output": [1, 6, 8, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "b701b1f434a72f62"} |
vcr_bd0af85ccb66 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [2, 13, 6, 25, 1, 5, 24, 11, 24]}, "output": 86}, {"input": {"nums": [15, 20, 3, 2, 1]}, "output": 22}, {"input": {"nums": [6, 16, 7, 4, 2, 17]}, "output": 37}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "6d83efe7e16e6365"} |
vcr_c9281323a850 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [3, 5, 3, 4, 4, 6, 7, 7]}, "output": [70560, 42336, 70560, 52920, 52920, 35280, 30240, 30240]}, {"input": {"nums": [4, 1, 4, 4, 1]}, "output": [16, 64, 16, 16, 64]}, {"input": {"nums": [2, 2, 1]}, "output": [2, 2, 4]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "44ff6312b6a081a1"} |
vcr_bb91e1918144 | 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": 53599}, "output": 10}, {"input": {"n": 12846}, "output": 7}, {"input": {"n": 11107}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "49d3ffdfae807711"} |
vcr_5910f5ffac02 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 2, 2, 0, 7]}, "output": [0, 0, 0, 196, 0]}, {"input": {"nums": [2, 5, 5, 5]}, "output": [125, 50, 50, 50]}, {"input": {"nums": [1, 1, 3, 5, 4, 4, 1]}, "output": [240, 240, 80, 48, 60, 60, 240]}] | 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": "43e2f2d061210239"} |
vcr_c23cc6c576e5 | 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": 19026}, "output": 6}, {"input": {"n": 2533}, "output": 7}, {"input": {"n": 5697}, "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": "5e5eeffb71265298"} |
vcr_7a551f790d43 | 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": "ec ebe dacbde aeb aadde"}, "output": "aadde aeb dacbde ebe ec"}, {"input": {"s": "a a baaa a"}, "output": "a baaa a a"}, {"input": {"s": "aaaa b bbaa ab baa"}, "output": "baa ab bbaa b aaaa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "ecfb422e8b816095"} |
vcr_0109a75a5e95 | 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": ["cbc", "cbcc", "cac", "cbb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["baa", "baa", "abb", "aaa", "aaa", "aba", "bbb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["aaa", "aab", "aba"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "73524063c6bfaa43"} |
vcr_7fb1a27dad7d | 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": 48, "b": 245}, "output": 1}, {"input": {"a": 28, "b": 2}, "output": 2}, {"input": {"a": 94, "b": 21}, "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": "59f1511da4a923ea"} |
vcr_b29950d6aef8 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 1, 4, 4, 6, 6, 7, 8, 9, 12, 13, 13, 14]}, "output": [0, 1, 4, 6, 7, 8, 9, 12, 13, 14]}, {"input": {"nums": [1, 1, 2, 3, 5, 6, 7, 8, 9, 10]}, "output": [1, 2, 3, 5, 6, 7, 8, 9, 10]}, {"input": {"nums": [1, 2, 2, 2, 4, 7, 7, 7, 8, 9, 11]}, "output": [1, 2, 4, 7, 8, 9, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "369131dada36cb68"} |
vcr_45b4d2509625 | 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, 7, 9, 10, 10, 10, 12, 12, 13, 13, 13]}, "output": [0, 1, 3, 7, 9, 10, 12, 13]}, {"input": {"nums": [0, 1, 1, 2, 3, 5, 8, 8, 8, 9]}, "output": [0, 1, 2, 3, 5, 8, 9]}, {"input": {"nums": [0, 4, 4, 5, 5, 5, 7, 12]}, "output": [0, 4, 5, 7, 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": "e6af900719d9ff67"} |
vcr_e180f1f63ec7 | 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, 14, 16, 22, 33, 35], "target": 42}, "output": 7}, {"input": {"nums": [4, 9, 10, 16, 23, 26, 28, 29], "target": 29}, "output": 7}, {"input": {"nums": [0, 13, 26, 27, 32, 34, 37], "target": 35}, "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": "fb0fd0b579d0fcfe"} |
vcr_973d02f06faa | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [9, 18, 0, -12, 9, 2, 10, -9, 22, 16, 11, -8, 22, -12]}, "output": 90}, {"input": {"nums": [-2, -5, -10, -8, 6, -7, -9, 5, -10]}, "output": 6}, {"input": {"nums": [-8, 9, -1, 4, -7, 12, 11, -4]}, "output": 28}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "0a6c0bfdd43ec6a8"} |
vcr_013c8c4bfd72 | 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, 5, 6, 17, -19, 3, 1, 12, 3], "k": 8}, "output": [5, 6, 17, -19, 3, 1, 12, 3, -8]}, {"input": {"nums": [10, -5, 7, 8, 0, 0], "k": 14}, "output": [0, 0, 10, -5, 7, 8]}, {"input": {"nums": [1, 3, 7, 6, 8, -1], "k": 20}, "output": [8, -1, 1, 3, 7, 6]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "86a9962f00b8bb9f"} |
vcr_bd682c334165 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 3, 3, 4, 4, 7, 9, 11, 14]}, "output": [1, 3, 4, 7, 9, 11, 14]}, {"input": {"nums": [0, 1, 1, 2, 4, 4, 7, 11, 12]}, "output": [0, 1, 2, 4, 7, 11, 12]}, {"input": {"nums": [2, 2, 2, 3, 4, 4, 7, 7, 9, 10, 10, 12]}, "output": [2, 3, 4, 7, 9, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "8a4465b2a9605a28"} |
vcr_5e04df77a527 | 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": "bbdaae debaeb bcad dda da bab b"}, "output": "b bab da dda bcad debaeb bbdaae"}, {"input": {"s": "bb b aaa aa"}, "output": "aa aaa b bb"}, {"input": {"s": "ab aa aabb baaa"}, "output": "baaa aabb aa ab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "cb0415515b177151"} |
vcr_19ffcd5835ce | 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, 15, 9, 9], "k": 7}, "output": [15, 9, 9, 10]}, {"input": {"nums": [7, -9, 9, -1, 0], "k": 7}, "output": [-1, 0, 7, -9, 9]}, {"input": {"nums": [4, 7, 7], "k": 18}, "output": [4, 7, 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": "d353b34bb416330b"} |
vcr_54400834e636 | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "adabbdcd"}, "output": false}, {"input": {"s": "bac"}, "output": false}, {"input": {"s": "abcbabcb"}, "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": "07dfe1bfad7e70f0"} |
vcr_6c204b97edc6 | 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": 46359}, "output": 9}, {"input": {"n": 13979}, "output": 9}, {"input": {"n": 5327}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "3adeb011da99cc98"} |
vcr_eb88f78f91f9 | 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": [-14, -6, -2, 11, 14, 22, 24, 27, 35], "target": 46}, "output": [3, 8]}, {"input": {"nums": [-17, -15, 4, 8, 14, 30, 32, 33, 36], "target": 12}, "output": [2, 3]}, {"input": {"nums": [-17, -16, -3, 2, 3, 5, 10, 14, 21, 38], "target": 15}, "output": [5, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "a7099e0656bd0a03"} |
vcr_1deff7f118b4 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [13, -4, -11, -14, -10, -9, -6, -5, -14, 12, -14]}, "output": 13}, {"input": {"nums": [4, 8, -12, -4, 6, -11, 12, -3, -11]}, "output": 12}, {"input": {"nums": [-9, 3, -5, 7, 7, 10, -1, 7, 0, 4]}, "output": 34}] | 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": "b318e10c3dd16f73"} |
vcr_faddf8792633 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bcb", "aaba", "cb", "bbbb", "bbc"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["baa", "bab", "bab", "baa", "aab", "bba"]}, "output": [3, 3]}, {"input": {"strs": ["aab", "bba", "bab", "aaa"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "81afe4c7ef31db63"} |
vcr_23729aa4ee08 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 14, 24, 35, 43, 49], "target": 8}, "output": 1}, {"input": {"nums": [6, 29, 30, 33, 35], "target": 26}, "output": 1}, {"input": {"nums": [3, 4, 9, 22, 28, 31, 36], "target": 36}, "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": "4d62f485c0a17923"} |
vcr_728c36ff5800 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [25, 39, 20, 3, 32, 24, 6, 7, 25]}, "output": true}, {"input": {"nums": [3, 24, 19, 20, 17, 9, 19, 9, 3]}, "output": true}, {"input": {"nums": [14, 15, 23]}, "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": "b78c7e49e5b00e2f"} |
vcr_ec94f07eaf7d | 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": "bd c eaade aadaa bee aecd adced"}, "output": "adced aecd bee aadaa eaade c bd"}, {"input": {"s": "aab aaa bbb abbb ba"}, "output": "ba abbb bbb aaa aab"}, {"input": {"s": "aa aab babb"}, "output": "babb aab 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": "dbd768c816c02489"} |
vcr_e0ea1ab06f93 | 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": [28, 6, 17, 29, 28, 12, 65, 12, 29, 21, 17, 21, 6]}, "output": 65}, {"input": {"nums": [8, 2, 2, 8, 24, 24, 57, 15, 15]}, "output": 57}, {"input": {"nums": [27, 2, 21, 27, 21, 45, 2]}, "output": 45}] | 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": "0b1608c46c24c6f2"} |
vcr_ce21736fa283 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [15, 12, 27, 9, 22, 26, 20, 2, 29, 15, 6]}, "output": 119}, {"input": {"nums": [2, 0, 18, 4, 7, 7, 9, 17, 19, 0]}, "output": 55}, {"input": {"nums": [9, 14]}, "output": 14}] | 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": "05caaa2cba66712f"} |
vcr_b0b6aa8bbb55 | 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": ["bcaa", "caa", "abc", "aacb", "bcab", "baac"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["aaa", "bba", "aaa", "aba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aaa", "aba", "baa", "aba"]}, "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": "cd419855cff67abe"} |
vcr_673b8e30f64c | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "cccaadcbcdddca"}, "output": false}, {"input": {"s": "aaacccaaca"}, "output": false}, {"input": {"s": "ccaccb"}, "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": "5f6f80e5cc811304"} |
vcr_0a8b95349722 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 2, 5, 7, 7, 7, 14, 14, 15]}, "output": [2, 5, 7, 14, 15]}, {"input": {"nums": [3, 6, 7, 8, 8, 12]}, "output": [3, 6, 7, 8, 12]}, {"input": {"nums": [2, 4, 4, 4, 5, 6, 9]}, "output": [2, 4, 5, 6, 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": "2e027ecb10bbad8a"} |
vcr_2c4d6ebaf412 | 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": [59, 6, 55, 54]}, "output": 49}, {"input": {"prices": [14, 33, 28, 16, 8]}, "output": 19}, {"input": {"prices": [29, 39, 4, 5]}, "output": 10}] | 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": "e187f2f5d335cb29"} |
vcr_7aa3cedea7c2 | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-27, 57, 23, -12, 43, 14, 75, -28, 9], "target": -40}, "output": [3, 7]}, {"input": {"nums": [24, 6, 26, 43, -8, 7, -26], "target": 33}, "output": [2, 5]}, {"input": {"nums": [-25, 43, 4, -12, 42, 3, -2, 13], "target": 40}, "output": [4, 6]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "0abc16d9c6f59970"} |
vcr_863c148da6bd | 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": 7413}, "output": 9}, {"input": {"n": 7294}, "output": 9}, {"input": {"n": 15546}, "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": "3f076f7565d4adcc"} |
vcr_ad561bf82657 | 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, -17, 1, 1], "k": 10}, "output": [1, 1, 7, -17]}, {"input": {"nums": [-2, -3, 6, 0], "k": 10}, "output": [6, 0, -2, -3]}, {"input": {"nums": [-6, 2, -4, 1, -8, -2, -4], "k": 6}, "output": [2, -4, 1, -8, -2, -4, -6]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "30b4b7f3b83f3676"} |
vcr_1885642bb43a | 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": "abcabbbb"}, "output": 5}, {"input": {"s": "bbaa"}, "output": 2}, {"input": {"s": "ccaaacbcaab"}, "output": 7}] | 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": "54ebf83a5a402d9e"} |
vcr_32420f06244b | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-17, -15, -15, -4, 9, -5, -17, 11, -4, 29, -12, -19]}, "output": [-17, -32, -47, -51, -42, -47, -64, -53, -57, -28, -40, -59]}, {"input": {"nums": [16, -6, -3]}, "output": [16, 10, 7]}, {"input": {"nums": [7, 10, 10, -11]}, "output": [7, 17, 27, 16]}] | 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": "f3f42b48bf3ef30e"} |
vcr_e0c67de8a41e | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-25, -23, -20, -18, -2, 2, 28, 35, 38, 44, 45, 46], "target": 66}, "output": [6, 8]}, {"input": {"nums": [-19, -12, -1, 9, 13, 14, 24, 36, 37], "target": 13}, "output": [2, 5]}, {"input": {"nums": [10, 12, 13, 14, 28, 37], "target": 49}, "output": [1, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "97042bd442baeedc"} |
vcr_ee15fd41900c | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [21, 4, -5, -17, 16, -16, 22, 11, -5, 12, 3, 6, -1, 21]}, "output": 72}, {"input": {"nums": [4, 4, 13, 5, 1, -12, -9, -10, -1, 11, -4]}, "output": 27}, {"input": {"nums": [-7, -9, -5, 5, -4, -6, 6, 0, -9, -10]}, "output": 6}] | 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": "ac8bdd189c693e5d"} |
vcr_2c15c3dd0111 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-12, 1, -5, -16, 6, 13, 18, -8], "k": 0}, "output": [-12, 1, -5, -16, 6, 13, 18, -8]}, {"input": {"nums": [-7, -6, -3, 1, -7], "k": 12}, "output": [1, -7, -7, -6, -3]}, {"input": {"nums": [9, -10, -10, -4, -10, 0, -7, 0, -10], "k": 2}, "output": [0, -10, 9, -10, -10, -4, -10, 0, -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": "e3dd45b127f553eb"} |
vcr_1a21c47ecd37 | 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": "de ecc ebaab dadbb"}, "output": "dadbb ebaab ecc de"}, {"input": {"s": "b ba b"}, "output": "b ba b"}, {"input": {"s": "bab bbb"}, "output": "bbb bab"}] | 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": "0b4d1ace63ad787e"} |
vcr_e1c5f2b7882c | 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, 3, 7, 3, 1, 6, 2, 1]}, "output": [756, 1764, 756, 1764, 5292, 882, 2646, 5292]}, {"input": {"nums": [3, 4, 2]}, "output": [8, 6, 12]}, {"input": {"nums": [5, 2, 4, 5, 1, 4]}, "output": [160, 400, 200, 160, 800, 200]}] | 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": "1614870de4aa8ea2"} |
vcr_0706d0aaa2e0 | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [3, -32, -5, -19, 54, 33, 14, -21, 47, 31, 48, 52], "target": 36}, "output": [0, 5]}, {"input": {"nums": [39, 47, 15, -25], "target": 14}, "output": [0, 3]}, {"input": {"nums": [26, -24, 47, -3, 14, 38, 6, 39], "target": 23}, "output": [1, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "810c868c77ab4b0a"} |
vcr_004f98fd00fb | 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, 9, 13, 15, 24, 29, 34, 36, 39, 45, 49], "target": 35}, "output": 7}, {"input": {"nums": [18, 19, 20, 29, 30, 32, 35, 37], "target": 4}, "output": 0}, {"input": {"nums": [0, 6, 7, 9, 12, 21, 24, 25, 26, 37], "target": 2}, "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": "3fdfcb1797ec2ae3"} |
vcr_7e25a6cace7f | 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, 6, 6, 10, 14]}, "output": [2, 4, 6, 10, 14]}, {"input": {"nums": [0, 0, 1, 2, 2, 5, 6, 10, 11]}, "output": [0, 1, 2, 5, 6, 10, 11]}, {"input": {"nums": [0, 2, 3, 6, 9, 10, 11]}, "output": [0, 2, 3, 6, 9, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "890bd98322ceefe3"} |
vcr_2a83d2a8884f | 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": [27, 10, 33, 33, 10, 27, 77]}, "output": 77}, {"input": {"nums": [12, 18, 7, 6, 26, 12, 6, 24, 18, 26, 54, 24, 7]}, "output": 54}, {"input": {"nums": [8, 13, 25, 8, 42, 25, 13]}, "output": 42}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "83a3941dd3372409"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.