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_c0fb7e078f64 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [45, 26, 64, -32, 59, -28], "target": 27}, "output": [3, 4]}, {"input": {"nums": [47, 44, -11, 38, -26, 19, -4, 15, 9, -16], "target": 4}, "output": [2, 7]}, {"input": {"nums": [45, 12, 10, -26, -4, -13, 16, -24, 46, 4, 27], "target": 21}, "output": [0, 7]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "4f8e66fa00c0c37b"} |
vcr_f813451ebc2e | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [24, 17, 22, 25, 21, 0, 9, 9, 12, 10]}, "output": 88}, {"input": {"nums": [4, 4, 20, 8, 11, 10, 6, 19]}, "output": 54}, {"input": {"nums": [4, 13, 20, 7, 11, 19, 3, 12]}, "output": 55}] | 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": "43b1761533573952"} |
vcr_06c03370b835 | 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": ["ccbb", "babb", "aaa", "ba", "bba", "ca"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "abb", "aba", "abb", "bab", "aba"]}, "output": [1, 2, 3]}, {"input": {"strs": ["bba", "baa", "aba", "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": "6928c4bf277d84fc"} |
vcr_6dcff9cc74de | 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, 13, 13]}, "output": [0, 1, 2, 13]}, {"input": {"nums": [2, 2, 3, 4, 5, 7, 8, 10, 10, 11, 11, 12]}, "output": [2, 3, 4, 5, 7, 8, 10, 11, 12]}, {"input": {"nums": [0, 2, 4, 5, 6, 6, 6, 9, 9, 10, 10, 11]}, "output": [0, 2, 4, 5, 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": "5e94cc366d3084b0"} |
vcr_6f2ef39cc08a | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-28, -12, 0, 13, 21, 34, 42, 49], "target": 55}, "output": [3, 6]}, {"input": {"nums": [-20, -12, -11, 0, 18], "target": -32}, "output": [0, 1]}, {"input": {"nums": [-7, -4, 0, 4, 5, 8, 18, 23, 25, 37], "target": 5}, "output": [2, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "8c4bb6d8c6b82c2f"} |
vcr_767958145886 | 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": 14, "b": 71}, "output": 1}, {"input": {"a": 47, "b": 179}, "output": 1}, {"input": {"a": 11, "b": 23}, "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": "273f3e11103aa68e"} |
vcr_b5a5b9132fd2 | 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": [0, 25, 33, 28, -19, 18, -20]}, "output": [0, 25, 58, 86, 67, 85, 65]}, {"input": {"nums": [9, -2, -11, 4, -5]}, "output": [9, 7, -4, 0, -5]}, {"input": {"nums": [13, 10, -3]}, "output": [13, 23, 20]}] | 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": "ac30f9bcb0f600c5"} |
vcr_513f7241a7f1 | 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": [21, 192, 166, 122, 138, 103, 141, 144]}, "output": false}, {"input": {"nums": [8, 25, 24, 8, 2, 12]}, "output": true}, {"input": {"nums": [14, 0, 11, 18, 5, 14]}, "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": "44883ab0e6759ca6"} |
vcr_0d87fba77c2d | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [7, 20, 27, 19, 10, 17, 30, 26, 2, 5, 8]}, "output": 90}, {"input": {"nums": [6, 12, 12, 12, 2, 7]}, "output": 31}, {"input": {"nums": [3, 10, 9, 5, 2, 7]}, "output": 22}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "a95074a33335f4a4"} |
vcr_814a4b887ce5 | 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": 125, "b": 209}, "output": 1}, {"input": {"a": 88, "b": 141}, "output": 1}, {"input": {"a": 67, "b": 113}, "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": "a9180db7de89bb6a"} |
vcr_31e9c835069e | 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": "cacda"}, "output": false}, {"input": {"s": "ccbca"}, "output": false}, {"input": {"s": "bbcbbc"}, "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": "40c99f6fd2ee7d4a"} |
vcr_43b77247191f | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "adcdac"}, "output": false}, {"input": {"s": "accaaaacb"}, "output": false}, {"input": {"s": "caabcabb"}, "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": "be1d264f19a34a75"} |
vcr_aa719ea3b90b | 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, 19, 11, 21, -9, 4, -1, 6, 7, 22]}, "output": 80}, {"input": {"nums": [-1, 5, 9, -5, -8, -11]}, "output": 14}, {"input": {"nums": [-4, -4, 8, -2, 4]}, "output": 10}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "a359a368de769862"} |
vcr_776c7b7f28ca | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-1, -19, -14, -19, -19, 5, -5, 14, -20, 15, -19, -13], "k": 31}, "output": [5, -5, 14, -20, 15, -19, -13, -1, -19, -14, -19, -19]}, {"input": {"nums": [6, 5, 5, 0, 3, -4, 0], "k": 5}, "output": [5, 0, 3, -4, 0, 6, 5]}, {"input": {"nums": [-6, -5, 4], "k": 5}, "output": [-5, 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": "5fe6ec8cea69acfb"} |
vcr_dbe82b208aad | 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": "baccbaaa"}, "output": 5}, {"input": {"s": "bbaa"}, "output": 2}, {"input": {"s": "abbaaaa"}, "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": "6d549d4ddfaf27e9"} |
vcr_7770c02eba67 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["cb", "bc", "aaa", "aca", "bac", "cacb", "babc", "bcc"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["baa", "abb", "bab", "bab", "baa", "bbb", "bbb"]}, "output": [2, 2, 3]}, {"input": {"strs": ["bba", "abb", "bba", "bbb", "bbb", "abb", "bbb"]}, "output": [3, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "bf3e24d1f4fd05de"} |
vcr_e26ad8a7827d | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "a cbbebc"}, "output": "cbbebc a"}, {"input": {"s": "a aaba b ba"}, "output": "ba b aaba a"}, {"input": {"s": "abb aaa bbbb ba ba"}, "output": "ba ba bbbb aaa 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": "1dbb9b088b2ae149"} |
vcr_b2e22c9cf05b | 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": "aadb bb eeadc acad aabe"}, "output": "aabe acad eeadc bb aadb"}, {"input": {"s": "ab aa"}, "output": "aa ab"}, {"input": {"s": "aaab aba"}, "output": "aba aaab"}] | 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": "94b4fff3725d0d77"} |
vcr_a0ff0de7f057 | 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": ["baa", "cca", "ccb", "abb", "acca", "bbb", "cb", "abca"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["baa", "bab", "aab"]}, "output": [1, 2]}, {"input": {"strs": ["baa", "bbb", "bbb", "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": "1fa61e65af7425b3"} |
vcr_99449a70cf50 | 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": 20149}, "output": 9}, {"input": {"n": 12196}, "output": 8}, {"input": {"n": 3069}, "output": 10}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "59f228328b993d69"} |
vcr_c5027028f17e | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [14, 29, 29, 30, 10, 30, 11, 1, 14, 1, 75, 12, 12, 10, 11, 4, 4]}, "output": 75}, {"input": {"nums": [27, 27, 19, 15, 15, 19, 46]}, "output": 46}, {"input": {"nums": [8, 7, 40, 5, 5, 3, 7, 8, 3]}, "output": 40}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "2107b9f919eb9e73"} |
vcr_b77ec8a274e9 | 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": [126, 1, 175, 140, 32, 119, 107]}, "output": false}, {"input": {"nums": [19, 5, 9, 8, 11, 14, 24, 16, 16, 16]}, "output": true}, {"input": {"nums": [6, 18, 6, 4, 22, 10]}, "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": "73ccc0d5c8794fe0"} |
vcr_726a548716f0 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["aa", "ab", "ca", "ac", "aacc", "aba"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["baa", "aab", "aaa", "bba", "aba", "aaa", "bba"]}, "output": [2, 2, 3]}, {"input": {"strs": ["aab", "aab", "aaa"]}, "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": "dc68cf4616b84791"} |
vcr_cb7e86f2b4c9 | 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, 5, 5, 5, 0]}, "output": [0, 0, 0, 0, 875]}, {"input": {"nums": [1, 2, 5]}, "output": [10, 5, 2]}, {"input": {"nums": [1, 4, 2, 2, 4]}, "output": [64, 16, 32, 32, 16]}] | 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": "34368486d28543a7"} |
vcr_ca7bfee67a83 | 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, 21, 28, 34, 36, 47, 49], "target": 35}, "output": 4}, {"input": {"nums": [0, 2, 8, 18, 22, 25, 27, 30, 32], "target": 38}, "output": 9}, {"input": {"nums": [0, 7, 9, 12, 17, 23, 26, 32, 37, 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": "d5c222aa90f95a5d"} |
vcr_62e299ef4be4 | 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": [0, 7, -1, -5, -13, 3, 4, 8, 2, -2, -1, 19, 8, -9, 6]}, "output": 41}, {"input": {"nums": [6, -2, 1, 3, -11, 5]}, "output": 8}, {"input": {"nums": [-3, 6, 3, -3, -3]}, "output": 9}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "e2c6b106ebfe47c1"} |
vcr_16c9d1bf5b00 | 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": ["cab", "ca", "ab", "bb", "bcaa", "acb", "cb", "cccc", "ca"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["aba", "abb", "bab", "bbb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aba", "bbb", "aab", "aba", "aba", "baa", "bab"]}, "output": [1, 1, 5]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "405610abefc5dfe6"} |
vcr_1fb8c843f996 | 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, -16, 22, -15, -14, 0, 6, -7, 12, -18]}, "output": 27}, {"input": {"nums": [-9, 3, -7, 2, 7, -1]}, "output": 9}, {"input": {"nums": [2, -9, -4, 4, -10, -10]}, "output": 4}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "8baae38f85de0167"} |
vcr_da94eaeceec6 | 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": [31, 34, 41, 42, 46], "target": 13}, "output": 0}, {"input": {"nums": [5, 9, 11, 12, 13, 24, 34], "target": 30}, "output": 6}, {"input": {"nums": [13, 14, 27, 39], "target": 11}, "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": "ac0db1e9eb342f87"} |
vcr_cae6861ba047 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [10, 21, 18, -6, -11, 18, -14, -16, 14, 21, 2]}, "output": 57}, {"input": {"nums": [-4, 8, -8, -6, -4, 14, -6, 11]}, "output": 19}, {"input": {"nums": [7, -2, 3, -4]}, "output": 8}] | 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": "b775e5f319fd96ce"} |
vcr_5800e182c510 | 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": "aaccabbbaba"}, "output": 7}, {"input": {"s": "bbbbaaa"}, "output": 2}, {"input": {"s": "ccbcbb"}, "output": 4}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "062df5aa6e4ac6e3"} |
vcr_f28f88b3dee7 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [42, -36, -14, 13, 28, -32, 78, 48], "target": 28}, "output": [0, 2]}, {"input": {"nums": [11, 12, -9, 24, 15, 18, -8, -28, 38, -14], "target": 23}, "output": [0, 1]}, {"input": {"nums": [37, 0, -20, -26, 14, 32, -3], "target": -46}, "output": [2, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "8d21cf49c7143860"} |
vcr_df07693c05cc | 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": [10, 20, 50, 21, 25, 0, 31, 48, 30, 37, 43, 39, 34, 48, 27]}, "output": true}, {"input": {"nums": [15, 17, 2, 14, 24, 1, 12]}, "output": false}, {"input": {"nums": [3, 24, 12, 20, 5, 12, 18, 4]}, "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": "3af0efcd5170bb1c"} |
vcr_00654d963c19 | 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": [11, 21, 21, 36, 27, 27, 39, 68, 36, 34, 34, 11, 39]}, "output": 68}, {"input": {"nums": [9, 4, 50, 9, 4]}, "output": 50}, {"input": {"nums": [24, 2, 2, 27, 4, 27, 8, 22, 24, 4, 42, 22, 8]}, "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": "79ae62e2675c391f"} |
vcr_b890af083d73 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [11, -4, 0, -16, 11, -13], "k": 4}, "output": [0, -16, 11, -13, 11, -4]}, {"input": {"nums": [-5, 5, 9, -4, 5, -6, -9, -2, 3, -4], "k": 3}, "output": [-2, 3, -4, -5, 5, 9, -4, 5, -6, -9]}, {"input": {"nums": [-1, -5, 5], "k": 3}, "output": [-1, -5, 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": "627420db31809fec"} |
vcr_02587a2b38dc | 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": "bc b acdbaa ae b"}, "output": "b ae acdbaa b bc"}, {"input": {"s": "bb bbab"}, "output": "bbab bb"}, {"input": {"s": "bb aab abb b abab"}, "output": "abab b abb aab bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "8ce172fe7dea5101"} |
vcr_21b322f62b30 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [6, 4, 6, 6, 3, 4, 0]}, "output": [0, 0, 0, 0, 0, 0, 10368]}, {"input": {"nums": [3, 5, 2, 5, 1, 1]}, "output": [50, 30, 75, 30, 150, 150]}, {"input": {"nums": [5, 5, 4, 2, 5]}, "output": [200, 200, 250, 500, 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": "7a7d7ae1c1198ec2"} |
vcr_aeb60cab3f2a | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-13, 32, 17, -23, 30, -20]}, "output": [-13, 19, 36, 13, 43, 23]}, {"input": {"nums": [-7, -11, 17, 15, -10, -11, 10]}, "output": [-7, -18, -1, 14, 4, -7, 3]}, {"input": {"nums": [14, 6]}, "output": [14, 20]}] | 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": "c27f452c7e15f05c"} |
vcr_84f61429803e | 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, 6, 9, 9, 10, 12, 12, 13, 15]}, "output": [3, 6, 9, 10, 12, 13, 15]}, {"input": {"nums": [1, 4, 4, 4, 5, 7, 7, 9, 11]}, "output": [1, 4, 5, 7, 9, 11]}, {"input": {"nums": [5, 5, 7, 10]}, "output": [5, 7, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "bee0f1b252262cfa"} |
vcr_f6e6de658176 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [19, 2, 17, 6, 9, 16, 2, 7, 10, 23, 11]}, "output": 82}, {"input": {"nums": [14, 9, 11, 5, 7, 0]}, "output": 32}, {"input": {"nums": [19, 19]}, "output": 19}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "590a510952495511"} |
vcr_4c4e6ded3ec5 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [16, 30, 26, 24, 6, 5, 19, 9, 30, 9]}, "output": 103}, {"input": {"nums": [8, 5, 16, 5, 3, 10]}, "output": 34}, {"input": {"nums": [0, 3, 3, 0]}, "output": 3}] | 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": "f75c9206d3802de7"} |
vcr_b893460c4ed8 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 3, 4, 0, 4, 3, 0, 4, 0, 0]}, "output": true}, {"input": {"nums": [0, 3, 1]}, "output": false}, {"input": {"nums": [1, 2, 1, 3, 0]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "eea4254608a1a8e5"} |
vcr_7bd63eb3ecea | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [1, -17, 9, 13, 18, -16, -1, -11, 8, 20, -17, -18, 1, 1, 11]}, "output": 40}, {"input": {"nums": [8, 0, -11, -3, 5]}, "output": 8}, {"input": {"nums": [-6, 12, -9, 8, 12]}, "output": 23}] | 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": "e25a72cf04cbcb0f"} |
vcr_bbc63b575015 | 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": [46, 11, 24, 7, 38, 16]}, "output": 31}, {"input": {"prices": [29, 13, 18, 8, 31, 17, 11, 36]}, "output": 28}, {"input": {"prices": [33, 21, 27, 4, 3, 12, 38, 37, 36]}, "output": 35}] | 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": "41379940a9eea46e"} |
vcr_a01cef5067bb | 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": ["caaa", "bcb", "bbbc", "abc", "baa", "bbca", "bcbb", "cbc"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bbb", "aaa", "bbb", "bbb"]}, "output": [1, 3]}, {"input": {"strs": ["bbb", "bab", "aaa", "aab", "aab", "aaa", "bab"]}, "output": [1, 2, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "111e210f9c02262b"} |
vcr_29cc46148cec | 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, -21, -19, -6, 7, 13, 35, 43, 48], "target": 56}, "output": [5, 7]}, {"input": {"nums": [-18, -17, -15, -12, -8, -7, 2, 5, 13, 23], "target": 6}, "output": [1, 9]}, {"input": {"nums": [-14, -10, 3, 9, 25, 29, 31], "target": 54}, "output": [4, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "9987f7f12eb5a801"} |
vcr_982d757f2d1b | 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": [50, 19, 22, 38, 15, 15, 50, 19, 13]}, "output": true}, {"input": {"nums": [25, 12, 22, 3]}, "output": false}, {"input": {"nums": [3, 7, 18, 0]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "dcd0d12ae29ef0b6"} |
vcr_e09ed0f5fafe | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-7, 12, 7, 18, 2, 16, -8, -2, -3, -13, 7, -15, -12, 19, -15]}, "output": 55}, {"input": {"nums": [-4, 12, 0, 11, 8]}, "output": 31}, {"input": {"nums": [7, 2, -2, 9, 2]}, "output": 18}] | 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": "a6e798a11c2e969d"} |
vcr_85919f464c1c | 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, 18, 22, 27, 24, 26, 21]}, "output": 76}, {"input": {"nums": [20, 17, 8, 8, 18, 20, 12, 16]}, "output": 64}, {"input": {"nums": [10, 1, 7, 10, 0, 5, 0]}, "output": 25}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "53e00c130f86d298"} |
vcr_60b91ab71e38 | 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": "bc ddebb ece decc be edec bbdede"}, "output": "bbdede edec be decc ece ddebb bc"}, {"input": {"s": "baab bbba abb ba"}, "output": "ba abb bbba baab"}, {"input": {"s": "baaa bba aaba bbab"}, "output": "bbab aaba bba baaa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "c81b3c697741c06e"} |
vcr_a6451accfcd6 | 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": [-9, -8, -22, -7, -2, -19, -10, 32, 28, 10, -14, 13, -24]}, "output": [-9, -17, -39, -46, -48, -67, -77, -45, -17, -7, -21, -8, -32]}, {"input": {"nums": [1, -4, 17, -8]}, "output": [1, -3, 14, 6]}, {"input": {"nums": [-11, -2, 1, -8]}, "output": [-11, -13, -12, -20]}] | 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": "97b00e5d7e189ee4"} |
vcr_b90085778479 | 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, 2, 5, 3, 0, 4, 6]}, "output": [0, 0, 0, 0, 2160, 0, 0]}, {"input": {"nums": [1, 4, 1, 2, 5, 2]}, "output": [80, 20, 80, 40, 16, 40]}, {"input": {"nums": [1, 1, 2, 5, 4]}, "output": [40, 40, 20, 8, 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": "478f6e69e2cba4e8"} |
vcr_86bf517f53ee | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 12, 14, 22, 32, 35, 41], "target": 2}, "output": 0}, {"input": {"nums": [4, 11, 12, 14, 16, 18, 25, 26, 29, 31], "target": 18}, "output": 5}, {"input": {"nums": [17, 38, 39], "target": 15}, "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": "e989c0f514783f8f"} |
vcr_03a151b54ac0 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-4, 5, -11, -13, -6, -5, 15]}, "output": 15}, {"input": {"nums": [-6, 3, -9, 10, -9, -1, 12, 9, 10]}, "output": 31}, {"input": {"nums": [10, 0, 0, 14, 5, -7]}, "output": 29}] | 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": "2b9f6ac59ef0e1aa"} |
vcr_83c4bccf4ceb | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 2, 2, 3, 4, 5, 7, 9, 12, 15, 15, 15, 15]}, "output": [0, 2, 3, 4, 5, 7, 9, 12, 15]}, {"input": {"nums": [1, 4, 9, 10]}, "output": [1, 4, 9, 10]}, {"input": {"nums": [2, 3, 4, 5, 10, 12]}, "output": [2, 3, 4, 5, 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": "151f20e1b95c9701"} |
vcr_e182b2eb1b24 | 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": ["cca", "ccb", "bba", "aa", "baac"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "bbb", "bbb", "aab", "bab"]}, "output": [1, 2, 2]}, {"input": {"strs": ["bab", "bba", "aaa", "bab", "bab", "abb"]}, "output": [1, 5]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "11a31a2094e2a17b"} |
vcr_d246c9f37bbc | 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": [11, 20, 28]}, "output": 39}, {"input": {"nums": [2, 11, 19, 11, 10]}, "output": 31}, {"input": {"nums": [9, 4, 7, 11, 19]}, "output": 35}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "cbe286b501292304"} |
vcr_59bc66293d51 | 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": "eedeeb acacde eec dcedbe bdce abb eed"}, "output": "eed abb bdce dcedbe eec acacde eedeeb"}, {"input": {"s": "baaa a aab bba"}, "output": "bba aab a baaa"}, {"input": {"s": "aaab bbba aa a bba"}, "output": "bba a aa bbba aaab"}] | 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": "d1506c5a3dd7569e"} |
vcr_1c281a19fb2f | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -24, -23, -16, -2, 0, 11, 18, 39, 49], "target": -16}, "output": [3, 5]}, {"input": {"nums": [-14, -9, -8, -7], "target": -21}, "output": [0, 3]}, {"input": {"nums": [-18, -7, -5, 7, 9, 12, 14, 24, 25, 34, 39], "target": 64}, "output": [8, 10]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "ef48116f984d890e"} |
vcr_ed09bd29b11d | 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, 10, 10, 11, 12, 15]}, "output": [2, 4, 6, 10, 11, 12, 15]}, {"input": {"nums": [3, 3, 5, 6, 6, 8, 9, 9, 9, 10]}, "output": [3, 5, 6, 8, 9, 10]}, {"input": {"nums": [0, 7, 8, 12]}, "output": [0, 7, 8, 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": "5ab2b83c66654f97"} |
vcr_fa4fcad2533b | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 1, 4, 1, 3, 1, 2, 0]}, "output": true}, {"input": {"nums": [0, 0, 2, 3, 3]}, "output": false}, {"input": {"nums": [2, 3, 0, 1, 2, 3, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "4c50d133650a02a8"} |
vcr_3a426e277716 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [65, 64, 67, -8, 47], "target": 39}, "output": [3, 4]}, {"input": {"nums": [-7, 28, 2, -12, 30], "target": -19}, "output": [0, 3]}, {"input": {"nums": [0, -28, 33, 42, 32, 8], "target": -28}, "output": [0, 1]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "b62c1a6ef9897c98"} |
vcr_93f1c48f0ee0 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [13, -5, 16, 22, 32, 2, -14, 23, 3]}, "output": [13, 8, 24, 46, 78, 80, 66, 89, 92]}, {"input": {"nums": [10, 11, -9, 17, 10]}, "output": [10, 21, 12, 29, 39]}, {"input": {"nums": [1, 9, 13, 10, 9, 7, 7, -7]}, "output": [1, 10, 23, 33, 42, 49, 56, 49]}] | 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": "00c3155b236000d5"} |
vcr_f78c5a434c1e | 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, -10, 18, -19, 6, -6, 15, -16], "k": 14}, "output": [18, -19, 6, -6, 15, -16, -14, -10]}, {"input": {"nums": [-8, -3, 4, -7, -8, 3, -3], "k": 15}, "output": [-3, -8, -3, 4, -7, -8, 3]}, {"input": {"nums": [10, 5, 3, -8, 4, 2, 2, 10], "k": 19}, "output": [2, 2, 10, 10, 5, 3, -8, 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": "673decb2e68af8f3"} |
vcr_f93ae3bc44ae | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [13, 38, 38, 29, 6, 34, 29, 34, 64, 13, 6]}, "output": 64}, {"input": {"nums": [55, 10, 14, 10, 13, 14, 13]}, "output": 55}, {"input": {"nums": [13, 44, 13, 21, 21]}, "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": "7f5a13a3860f97a4"} |
vcr_644034e797a6 | 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": [126, 194, 27, 190, 66, 145, 175, 64, 159, 12]}, "output": false}, {"input": {"nums": [25, 8, 14, 24, 20, 13, 11, 18, 0, 17, 21]}, "output": false}, {"input": {"nums": [18, 3, 25, 8, 22, 17, 3, 17, 21, 2, 11, 2]}, "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": "f203ad85eec8c3ca"} |
vcr_aee762daebc5 | 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": [14, 26, 5, 35, 1]}, "output": [14, 40, 45, 80, 81]}, {"input": {"nums": [5, 6, 17, 7, 0, 12, 10, 0]}, "output": [5, 11, 28, 35, 35, 47, 57, 57]}, {"input": {"nums": [-10, -1, -8, 12, -11, -8, -9, -11, -8, 2]}, "output": [-10, -11, -19, -7, -18, -26, -35, -46, -54, -52]}] | 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": "099a7dc2c3264791"} |
vcr_438d419a1d9b | 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": [13, 12, 13, 11, 9, 7, 30, 7, 21, 30, 22, 23]}, "output": 118}, {"input": {"nums": [11, 8, 14, 11, 11, 18, 8, 1, 10]}, "output": 54}, {"input": {"nums": [9, 7, 2, 8, 13, 15]}, "output": 32}] | 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": "b6aca107bd569467"} |
vcr_07443e404ce3 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [34, 41, 54, 1, 34, 44]}, "output": 43}, {"input": {"prices": [2, 9, 28, 39]}, "output": 37}, {"input": {"prices": [25, 27, 40, 37, 10]}, "output": 15}] | 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": "eda7a72dad143a6f"} |
vcr_60b632ff29f8 | 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": "eed ecd be ccdd aca dabcdb"}, "output": "dabcdb aca ccdd be ecd eed"}, {"input": {"s": "a b aaa ab"}, "output": "ab aaa b a"}, {"input": {"s": "baa b bab"}, "output": "bab b 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": "d273ad80e443abb3"} |
vcr_e0a517907be2 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [18, -9, -8, -8, -22, -6, -3, -21, 35, 16, -9, -1]}, "output": [18, 9, 1, -7, -29, -35, -38, -59, -24, -8, -17, -18]}, {"input": {"nums": [-2, -10]}, "output": [-2, -12]}, {"input": {"nums": [4, 18, 16, 12, 18, -1, -10, 0]}, "output": [4, 22, 38, 50, 68, 67, 57, 57]}] | 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": "8d4830fa8b7301be"} |
vcr_dabea77e7f69 | 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": ["aba", "abc", "cbb", "aaba", "cac"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bab", "bbb", "aaa", "baa", "bba", "aab"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["bba", "aab", "aba", "aab", "bba", "aba"]}, "output": [2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "f56837450fde9e17"} |
vcr_c27ee866cd8d | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bbb", "ac", "bc", "cacc", "cabb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["abb", "abb", "aaa", "aba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aba", "aab", "aba", "bbb", "bbb", "baa"]}, "output": [2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "fc70a9b7c6c627a3"} |
vcr_883c60dd5ed5 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [14, 35, 18, 35, 14, 56, 9, 21, 16, 33, 29, 16, 18, 21, 9, 29, 33]}, "output": 56}, {"input": {"nums": [9, 26, 45, 10, 26, 10, 9]}, "output": 45}, {"input": {"nums": [40, 6, 27, 27, 14, 6, 14]}, "output": 40}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "eecba89470f86e62"} |
vcr_2a468c0b5502 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-18, -18, -15, 4, -9, 16, -6], "k": 20}, "output": [-18, -15, 4, -9, 16, -6, -18]}, {"input": {"nums": [-9, -8, -9], "k": 14}, "output": [-8, -9, -9]}, {"input": {"nums": [-5, -7, 0, 3, -4, -3, 0], "k": 2}, "output": [-3, 0, -5, -7, 0, 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": "93816b6acba49a3e"} |
vcr_8768294c3c66 | 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": [34, 13, 49, 11, 30, 11, 25, 11, 6, 16]}, "output": true}, {"input": {"nums": [6, 19, 23, 25, 20, 8, 22, 20, 1]}, "output": true}, {"input": {"nums": [20, 19, 25, 5, 20, 14, 7, 22, 6, 7, 21, 22]}, "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": "afe8fbde67eb04d6"} |
vcr_2a4a80721af4 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [30, -1, 39, 63, -21, 67, 17, -18, 49, 33], "target": 84}, "output": [5, 6]}, {"input": {"nums": [14, 7, 26, 3, -5, -10, -21], "target": 4}, "output": [0, 5]}, {"input": {"nums": [3, -29, -12, 36, 41, -2, -6, 1, -30, 28], "target": 4}, "output": [0, 7]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "740eb39e842b4fe6"} |
vcr_1dd2dd6f6c56 | 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, 32, -5, 17, 14, 15, -1, 15, -4, 4, 7]}, "output": [-15, 17, 12, 29, 43, 58, 57, 72, 68, 72, 79]}, {"input": {"nums": [12, 2, 10, -10, 4, -6]}, "output": [12, 14, 24, 14, 18, 12]}, {"input": {"nums": [-4, 0, 5, 3, 3, 4]}, "output": [-4, -4, 1, 4, 7, 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": "538bcc15349fa263"} |
vcr_98ad4c519b87 | 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, 3, -4, 9, -8, -14, -14, -16, -14, -9, -17, -10]}, "output": 15}, {"input": {"nums": [-4, -2, 12]}, "output": 12}, {"input": {"nums": [7, -10, -9, -6, 11, 0, 8, -3, -12, -2]}, "output": 19}] | 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": "8886e634e6b4d569"} |
vcr_e8597bbc8524 | 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, 20, 28, 26]}, "output": 46}, {"input": {"nums": [19, 9, 3, 12, 13, 6, 17, 0, 1]}, "output": 53}, {"input": {"nums": [6, 15, 8, 18, 11, 18, 19, 14]}, "output": 65}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "e34a513d8bdbd698"} |
vcr_a5c331c5d92a | 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": "bece bedcce abbcc eaadd ce"}, "output": "ce eaadd abbcc bedcce bece"}, {"input": {"s": "a bbb abaa b"}, "output": "b abaa bbb a"}, {"input": {"s": "ba bbb a abb a"}, "output": "a abb a bbb ba"}] | 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": "13f09fd2be595864"} |
vcr_f2fe9f2ed1a1 | 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, 34, 39, 70, 39, 34, 28]}, "output": 70}, {"input": {"nums": [16, 16, 49, 28, 28]}, "output": 49}, {"input": {"nums": [21, 3, 9, 16, 13, 40, 3, 28, 28, 21, 13, 9, 16]}, "output": 40}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "b2723a2c470b0255"} |
vcr_5dca3cf1469a | 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, 4, 6, 10, 12, 13, 14, 22, 23, 25, 28, 31, 34, 46], "target": 50}, "output": 14}, {"input": {"nums": [0, 8, 15, 18, 21, 23], "target": 26}, "output": 6}, {"input": {"nums": [14, 19, 33, 34], "target": 7}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "9ce80b61abc41559"} |
vcr_d9e4767879b3 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [22, 26, 27, 21, 26, 8, 28, 16]}, "output": 103}, {"input": {"nums": [5, 0, 14, 20, 7]}, "output": 26}, {"input": {"nums": [8, 16, 19, 3, 16, 16, 17]}, "output": 60}] | 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": "c8e016002294df94"} |
vcr_34011fbe5cb5 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [6, 4, 3, 3, 5, 6]}, "output": [1080, 1620, 2160, 2160, 1296, 1080]}, {"input": {"nums": [4, 4, 4]}, "output": [16, 16, 16]}, {"input": {"nums": [4, 5, 1, 5]}, "output": [25, 20, 100, 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": "5c0632f39e2b3e23"} |
vcr_bbab30938aac | 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": "bde eaacd bcd c dee dbc"}, "output": "dbc dee c bcd eaacd bde"}, {"input": {"s": "bbba aab bbba"}, "output": "bbba aab bbba"}, {"input": {"s": "b bbbb a b"}, "output": "b a bbbb 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": "d4ecb1ac4315920a"} |
vcr_60ee16479d31 | 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": "ad a dcaae acde adc ead"}, "output": "ead adc acde dcaae a ad"}, {"input": {"s": "abab bbb aa"}, "output": "aa bbb abab"}, {"input": {"s": "bab baa"}, "output": "baa 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": "079aeea31861072a"} |
vcr_9685dedfd94a | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [19, -9, -20, -25, -3, -14, 5, 18, 29, 19]}, "output": [19, 10, -10, -35, -38, -52, -47, -29, 0, 19]}, {"input": {"nums": [10, -10, 5, -11, 0, 15, -10, 10, 9]}, "output": [10, 0, 5, -6, -6, 9, -1, 9, 18]}, {"input": {"nums": [-10, 6, -2]}, "output": [-10, -4, -6]}] | 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": "7a9bd4bfb1755a1c"} |
vcr_dac06e4ea472 | 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, -15, -4, 13, 34], "target": -45}, "output": [0, 1]}, {"input": {"nums": [-15, 2, 8, 13, 21, 31, 37], "target": 21}, "output": [2, 3]}, {"input": {"nums": [-11, -9, 3, 5, 18, 19, 29, 33], "target": 7}, "output": [0, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "818530fe29ad5dd1"} |
vcr_f742f52dac69 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 1, 1, 0, 0, 0, 0]}, "output": false}, {"input": {"nums": [1, 1, 3, 1, 3, 3, 0, 3]}, "output": true}, {"input": {"nums": [2, 2, 0, 3, 3]}, "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": "863f523b1b5f1b18"} |
vcr_e564f947c3a8 | 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": [32, 10, 9, 48, 27, 34, 1, 23, 8, 50, 31, 46, 14, 28, 32]}, "output": true}, {"input": {"nums": [3, 25, 22, 2, 21, 3, 7, 15, 17, 12]}, "output": true}, {"input": {"nums": [14, 22, 17, 19, 7, 7, 17, 13, 17, 11]}, "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": "f9a544d9fc6b36fb"} |
vcr_e4ac38a05f40 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 4, 5, 7, 7, 8, 9, 9, 11, 12, 13, 13, 14]}, "output": [0, 2, 4, 5, 7, 8, 9, 11, 12, 13, 14]}, {"input": {"nums": [0, 1, 3]}, "output": [0, 1, 3]}, {"input": {"nums": [1, 1, 4, 6, 6, 6, 8, 8, 12]}, "output": [1, 4, 6, 8, 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": "257e80a87f6db2f3"} |
vcr_0b8c0862f03e | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 4, 3, 0, 2, 2, 3, 0]}, "output": true}, {"input": {"nums": [2, 3, 0, 0, 1, 1, 3, 1, 0]}, "output": true}, {"input": {"nums": [0, 0, 3, 3, 0, 3, 0, 3, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "8d9624558eeccdef"} |
vcr_f095ecb922fe | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [5, -16, 20, -10], "k": 8}, "output": [5, -16, 20, -10]}, {"input": {"nums": [-3, 4, -2, 0, -5, 4], "k": 0}, "output": [-3, 4, -2, 0, -5, 4]}, {"input": {"nums": [-4, -2, -1, -6, -1, 7, -5], "k": 10}, "output": [-1, 7, -5, -4, -2, -1, -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": "527ed27e04ecda05"} |
vcr_9a1c9a341604 | 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, 3, 22, 8, 20]}, "output": 44}, {"input": {"nums": [12, 9, 16, 1, 11, 18, 7, 11, 9, 17]}, "output": 74}, {"input": {"nums": [17, 1]}, "output": 17}] | 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": "5a462e06ac09334d"} |
vcr_276846987c57 | 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": 223, "b": 56}, "output": 1}, {"input": {"a": 90, "b": 118}, "output": 2}, {"input": {"a": 75, "b": 135}, "output": 15}] | 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": "09c0bb99294aa2db"} |
vcr_68572a1b2acd | 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, 19, 12, -9, 12, -7, 6, 4], "k": 26}, "output": [-11, 19, 12, -9, 12, -7, 6, 4, 16]}, {"input": {"nums": [-2, 10, 9, 8], "k": 2}, "output": [9, 8, -2, 10]}, {"input": {"nums": [-3, 9, 10, 4, -6], "k": 18}, "output": [10, 4, -6, -3, 9]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "192c61e116cc4907"} |
vcr_31b8cd7a467a | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 16, 20, 22, 23, 24, 25, 29, 35, 36, 45], "target": 25}, "output": 6}, {"input": {"nums": [1, 2, 5, 7, 10, 13, 17, 18, 24, 28, 31, 39], "target": 22}, "output": 8}, {"input": {"nums": [2, 8, 15, 24, 29, 33, 34, 36, 38], "target": 10}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "513108e6db2af850"} |
vcr_6efe6e9dd50b | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bac", "abc", "babb", "aca", "cca"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["abb", "aab", "aaa", "aab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aaa", "bba", "bba", "baa", "bba"]}, "output": [1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "90fc5040a354b4e3"} |
vcr_3d8299ae7dd3 | 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": [55, 12, 13, 60, 26, 48, 37, 23, 17, 31, 41, 41, 19, 51, 46, 30]}, "output": 48}, {"input": {"prices": [34, 5, 31, 14]}, "output": 26}, {"input": {"prices": [20, 37, 21, 4, 28, 24, 37, 27, 20, 16, 40, 24]}, "output": 36}] | 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": "66b51b253eb64750"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.