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_780663a16550 | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [40, -5, -2, 21, 61, -9, 10, -32, 57], "target": 52}, "output": [4, 5]}, {"input": {"nums": [8, -14, -13, -3, 43, 19], "target": -17}, "output": [1, 3]}, {"input": {"nums": [-2, -6, -29, 35, 31, 40, 8, -16, 2, -10], "target": 15}, "output": [4, 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": "12636652260df969"} |
vcr_1aab6366f1a7 | 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": 39135}, "output": 10}, {"input": {"n": 117}, "output": 5}, {"input": {"n": 9382}, "output": 6}] | 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": "efb0f06b642589bd"} |
vcr_d857608cb06f | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [20, 10, 20, -20, 5, -13, -17, -6, -16, -20, -14], "k": 10}, "output": [10, 20, -20, 5, -13, -17, -6, -16, -20, -14, 20]}, {"input": {"nums": [7, -7, 6], "k": 9}, "output": [7, -7, 6]}, {"input": {"nums": [7, -7, 4, 3, -10], "k": 0}, "output": [7, -7, 4, 3, -10]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "ae3d19d3207a62c4"} |
vcr_55a9c6531393 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 9, 21, 27, 31, 32, 35, 37, 38, 41, 46, 48], "target": 23}, "output": 3}, {"input": {"nums": [3, 18, 26, 32, 34, 39], "target": 3}, "output": 0}, {"input": {"nums": [7, 11, 14, 37], "target": 22}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "3f8310e4324c30af"} |
vcr_019b481c9858 | 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": 12006}, "output": 9}, {"input": {"n": 1180}, "output": 5}, {"input": {"n": 5927}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "ff91f6ff5d32b63d"} |
vcr_69e599dcf375 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [38, 1, 36, 62, 1, 31, 31, 36, 14, 14, 38]}, "output": 62}, {"input": {"nums": [19, 12, 3, 19, 17, 17, 26, 26, 43, 12, 3]}, "output": 43}, {"input": {"nums": [14, 27, 40, 21, 27, 22, 22, 14, 21]}, "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": "e9e0583a3a8574fc"} |
vcr_14bbf0b7551a | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-14, -23, -4, 79, -3, 23, 27, -7, 75, -11, 29, -39], "target": -15}, "output": [2, 9]}, {"input": {"nums": [-25, -8, 37, 42, 32, 30], "target": 62}, "output": [4, 5]}, {"input": {"nums": [48, 11, 42, 35, -16, -23, 2], "target": -5}, "output": [1, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "ea69507581b6bb0b"} |
vcr_222056d0c2cd | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [2, -13, 1, 3, -3, 0, 19, 5, 10, -4, -4, -13]}, "output": 35}, {"input": {"nums": [3, -5, 13, 11, 11, -10, 2, 7, 5, -2, 2]}, "output": 39}, {"input": {"nums": [-1, -9, -5, 6, 13, -10, -12]}, "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": "4216bbb9925c9266"} |
vcr_ee70cb542d4d | 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": [-4, -22, 18, 10, -9, -7, 32, 31, 20, 15, -10, 33, 33]}, "output": [-4, -26, -8, 2, -7, -14, 18, 49, 69, 84, 74, 107, 140]}, {"input": {"nums": [13, 11, 10, -1, 6, -4, -8, -1]}, "output": [13, 24, 34, 33, 39, 35, 27, 26]}, {"input": {"nums": [10, -12, 8]}, "output": [10, -2, 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": "f1da45e5457d00db"} |
vcr_4f7c9d92893c | 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": [0, 13, -20, 15, -1, 4, -4, -4, -20], "k": 1}, "output": [-20, 0, 13, -20, 15, -1, 4, -4, -4]}, {"input": {"nums": [2, -9, -9], "k": 8}, "output": [-9, -9, 2]}, {"input": {"nums": [-5, -6, -6, 2, 2, -10, -3, 0], "k": 20}, "output": [2, -10, -3, 0, -5, -6, -6, 2]}] | 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": "b6288834cb773963"} |
vcr_a483100abb7b | 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": "accbaccaabbaba"}, "output": 10}, {"input": {"s": "acbaccc"}, "output": 5}, {"input": {"s": "ccbc"}, "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": "4872a45d151aa153"} |
vcr_e7e9e4670f34 | 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, 13, 14, 16, 23, 25, 27, 28, 35, 38, 40, 41, 49], "target": 33}, "output": 8}, {"input": {"nums": [1, 8, 10, 11, 21, 27, 30, 36, 38], "target": 16}, "output": 4}, {"input": {"nums": [0, 3, 30, 31, 39], "target": 18}, "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": "61a9a31c65c6a7e9"} |
vcr_cf5605274786 | 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": "caadbadabdaac"}, "output": true}, {"input": {"s": "bbcbbcabb"}, "output": false}, {"input": {"s": "aaaac"}, "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": "6c20aca0c93e3b86"} |
vcr_ee7a67dc6d90 | 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", "baba", "ccca", "caa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aba", "abb", "bab", "aaa", "baa", "abb", "aab"]}, "output": [1, 3, 3]}, {"input": {"strs": ["bbb", "bbb", "bba", "bab", "baa"]}, "output": [1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "24cc6739a83ca5cb"} |
vcr_d07fe2963ff7 | 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": [33, 19, 33, 66, 38, 13, 3, 12, 13, 38, 36, 12, 3, 19, 36]}, "output": 66}, {"input": {"nums": [19, 19, 8, 22, 8, 48, 22, 9, 9]}, "output": 48}, {"input": {"nums": [53, 10, 25, 25, 28, 28, 10]}, "output": 53}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "1014aac9c2b55614"} |
vcr_a03f8e093c9d | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-19, -13, 16, 20, 21, 29, 30, 46, 47], "target": 50}, "output": [3, 6]}, {"input": {"nums": [-20, -7, -4, 9, 16, 18, 23, 25], "target": -24}, "output": [0, 2]}, {"input": {"nums": [-5, 0, 4, 35], "target": -1}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "e42a17f6c4f6f9fb"} |
vcr_0e31f9670bed | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 1, 2, 4, 4, 9, 10, 11, 13, 15]}, "output": [1, 2, 4, 9, 10, 11, 13, 15]}, {"input": {"nums": [3, 3, 7, 8, 8]}, "output": [3, 7, 8]}, {"input": {"nums": [1, 7, 7, 7, 8, 10, 12]}, "output": [1, 7, 8, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "86134592134e9c6b"} |
vcr_203d6cabede7 | 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": [8, 42, 41, 39, 17, 5, 6, 3, 7, 45, 11, 15, 37, 7, 0]}, "output": true}, {"input": {"nums": [2, 7, 8, 25, 19]}, "output": false}, {"input": {"nums": [12, 22, 17, 13]}, "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": "f6cbbdd3a0d9d92f"} |
vcr_5d67e1dac432 | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [59, -36, -5, -4, -29, 31, 70, 16, -25], "target": 86}, "output": [6, 7]}, {"input": {"nums": [-6, 23, 11, 49, -28, 20, 21, -19, -25, 40, -27], "target": 44}, "output": [1, 6]}, {"input": {"nums": [25, 21, 4, -27, 13, 47, 5], "target": 29}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "7624ab836bf27cfe"} |
vcr_3cece4eeb802 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [37, 24, 3, 17, 47, 16, 15, 28, 14, 11, 29, 0, 26, 37, 9, 19]}, "output": true}, {"input": {"nums": [2, 7, 3, 11, 5, 0, 4, 25, 3]}, "output": true}, {"input": {"nums": [22, 18, 15, 20, 23, 17, 13, 4, 10, 17, 21, 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": "24eccdf647f7fc32"} |
vcr_4b1920b40932 | 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": [8, 10, 33, 15, 50, 37, 2, 43, 6, 43, 11, 29, 0]}, "output": true}, {"input": {"nums": [1, 12, 1, 15, 13, 9]}, "output": true}, {"input": {"nums": [24, 8, 24, 10, 11, 23, 21, 15, 4, 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": "c164b217c04e907e"} |
vcr_b15f13603b18 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-16, -20, 17, 26, -20, 15, 12]}, "output": [-16, -36, -19, 7, -13, 2, 14]}, {"input": {"nums": [6, 0, 8, -1, -7, 9]}, "output": [6, 6, 14, 13, 6, 15]}, {"input": {"nums": [-3, 17, -2, -4]}, "output": [-3, 14, 12, 8]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "94878c681d003de6"} |
vcr_bf6ff08c3849 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 6, 7, 7, 4]}, "output": [1176, 196, 168, 168, 294]}, {"input": {"nums": [3, 2, 5, 3, 4]}, "output": [120, 180, 72, 120, 90]}, {"input": {"nums": [4, 1, 4, 3]}, "output": [12, 48, 12, 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": "2bab77269797fa12"} |
vcr_e8485ca82745 | 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": 62085}, "output": 8}, {"input": {"n": 16192}, "output": 7}, {"input": {"n": 7344}, "output": 6}] | 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": "94d69c8f872bbccd"} |
vcr_a54ec041c4c3 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [34, -2, -13, -24, 8, 28]}, "output": [34, 32, 19, -5, 3, 31]}, {"input": {"nums": [3, 11, 10, -2, 0, 8]}, "output": [3, 14, 24, 22, 22, 30]}, {"input": {"nums": [14, 7, 13, 13, -10, -10, -8, -11]}, "output": [14, 21, 34, 47, 37, 27, 19, 8]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "35199e6ae8910c90"} |
vcr_73a105691ef9 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-19, -14, -12, -4, 6, 7, 12, 27, 36, 44], "target": 63}, "output": [7, 8]}, {"input": {"nums": [-19, 4, 31, 34], "target": 38}, "output": [1, 3]}, {"input": {"nums": [-15, -14, -4, -3, 5, 9, 13, 30, 32, 33, 36], "target": 62}, "output": [7, 8]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "168c538132330ef3"} |
vcr_2bc609c8d54c | 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": "ac dedcbc b eab c"}, "output": "c eab b dedcbc ac"}, {"input": {"s": "b ba"}, "output": "ba b"}, {"input": {"s": "aa baaa baba baaa a"}, "output": "a baaa baba baaa aa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "17a7136f65c4b020"} |
vcr_e2871662882f | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [2, 3, 0, 2, 1]}, "output": [0, 0, 12, 0, 0]}, {"input": {"nums": [2, 5, 2, 5, 5, 3]}, "output": [750, 300, 750, 300, 300, 500]}, {"input": {"nums": [5, 2, 1, 1, 2]}, "output": [4, 10, 20, 20, 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": "ee32f9d543f169e1"} |
vcr_93434988a115 | 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": 175, "b": 158}, "output": 1}, {"input": {"a": 11, "b": 114}, "output": 1}, {"input": {"a": 91, "b": 173}, "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": "255f1e4e41abc13f"} |
vcr_bbabca82dc61 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [19, 77, 10, 16, 2, 37, 27, 27, 37, 10, 16, 19, 2]}, "output": 77}, {"input": {"nums": [9, 58, 20, 9, 20, 10, 28, 4, 4, 10, 28, 14, 14]}, "output": 58}, {"input": {"nums": [18, 3, 15, 52, 3, 18, 15]}, "output": 52}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "5e3d241b4d1995a6"} |
vcr_f387b07873de | 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, 7, 0, 1, 7]}, "output": [0, 0, 147, 0, 0]}, {"input": {"nums": [1, 5, 1, 1]}, "output": [5, 1, 5, 5]}, {"input": {"nums": [1, 1, 4, 2, 3]}, "output": [24, 24, 6, 12, 8]}] | 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": "ab05bb7af7454157"} |
vcr_ea812cd11228 | 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": [16, 20, 35, 42], "target": 50}, "output": 4}, {"input": {"nums": [2, 13, 18], "target": 34}, "output": 3}, {"input": {"nums": [6, 14, 22, 33], "target": 1}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "47bfe992066afd90"} |
vcr_be454930ccb1 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 3, 5, 5, 8, 10, 11, 14, 15]}, "output": [0, 1, 3, 5, 8, 10, 11, 14, 15]}, {"input": {"nums": [2, 4, 4, 5, 8, 8, 8, 10]}, "output": [2, 4, 5, 8, 10]}, {"input": {"nums": [0, 3, 4, 4, 4, 7, 8, 8, 9]}, "output": [0, 3, 4, 7, 8, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "4cc04fcb8b958a5d"} |
vcr_d661b9980d00 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 4, 1, 4, 2, 0, 4, 3, 0]}, "output": false}, {"input": {"nums": [1, 1, 3]}, "output": true}, {"input": {"nums": [1, 1, 0]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "c2644e9d39de2d62"} |
vcr_b209f7ac54cc | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 15, 20, 25, 39, 42], "target": 18}, "output": 2}, {"input": {"nums": [1, 26, 33, 36], "target": 20}, "output": 1}, {"input": {"nums": [5, 14, 15, 22, 25, 31, 32, 33, 36, 38], "target": 30}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "5603c3c9830273cd"} |
vcr_4a3bf51b8803 | 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, 6, 8, 38, 49], "target": 44}, "output": 4}, {"input": {"nums": [4, 5, 8, 9, 13, 15, 19, 22, 28, 31, 36, 39], "target": 40}, "output": 12}, {"input": {"nums": [4, 15, 16, 21, 26, 27, 29, 32, 35, 37], "target": 29}, "output": 6}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "ed9a15b76550d9a8"} |
vcr_e0b771422f60 | 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": [78, 72, -14, -10, 40, -34, -3, 62, 20, 2, 30, 71], "target": 91}, "output": [8, 11]}, {"input": {"nums": [21, 12, -26, 46, 20, 28], "target": 33}, "output": [0, 1]}, {"input": {"nums": [-23, 39, -17, -2, 21, -15, -11], "target": 22}, "output": [1, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "9092f4b435124e8e"} |
vcr_e27349566cdc | 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": [-14, -3, 0, 0, -2]}, "output": 0}, {"input": {"nums": [0, 0, -11, 0, -1, 8, -4, -1, -5]}, "output": 8}, {"input": {"nums": [-4, 6, 6, 8, -2, -8, 7, -3, 1, 0, 10]}, "output": 25}] | 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": "55ea8ff885e08578"} |
vcr_2abebd646f9c | 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": [-7, 9, 0, 26, -12, 12, -2, -9, 32]}, "output": [-7, 2, 2, 28, 16, 28, 26, 17, 49]}, {"input": {"nums": [2, 15, 4, 14, 0, 8, 10, 14]}, "output": [2, 17, 21, 35, 35, 43, 53, 67]}, {"input": {"nums": [-10, -12, -3, -7, -7, 12, -12, 11, 13]}, "output": [-10, -22, -25, -32, -39, -27, -39, -28, -15]}] | 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": "a7e288f85701d7a1"} |
vcr_93ea18281ed8 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [30, 1, 16, 30, 10, 20, 21, 19, 19, 1, 16, 21, 28, 20, 61, 28, 10]}, "output": 61}, {"input": {"nums": [3, 22, 2, 5, 10, 10, 22, 3, 2, 11, 11, 5, 52]}, "output": 52}, {"input": {"nums": [12, 15, 15, 43, 12]}, "output": 43}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "003da871c394807b"} |
vcr_22c9ff6b133d | 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, 14, -19, 6, -15], "k": 2}, "output": [6, -15, 14, 14, -19]}, {"input": {"nums": [-10, 9, 2, 8, 7, -10, -8, 9, -10, -10], "k": 14}, "output": [-8, 9, -10, -10, -10, 9, 2, 8, 7, -10]}, {"input": {"nums": [6, -5, 8], "k": 10}, "output": [8, 6, -5]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "b02c462928169aab"} |
vcr_39e328192440 | 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": [10, 36, 37, 42], "target": 41}, "output": 3}, {"input": {"nums": [2, 8, 13, 25, 29, 39], "target": 34}, "output": 5}, {"input": {"nums": [0, 15, 19, 23, 36], "target": 13}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "c95de7feb48e82b7"} |
vcr_c3c8af30369f | 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": [37, 20, 72, 22, 4, 60, -29, 8, 13, 79, 78, -31], "target": 86}, "output": [7, 10]}, {"input": {"nums": [45, 25, 37, 6, 35, -28, 0, 8, 47, 7], "target": 32}, "output": [1, 9]}, {"input": {"nums": [21, 49, -18, -22], "target": 31}, "output": [1, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "a2c972dbe3242727"} |
vcr_1cc74c26923f | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 7, 0, 1, 5]}, "output": [0, 0, 175, 0, 0]}, {"input": {"nums": [1, 2, 2, 1, 5, 2, 5]}, "output": [200, 100, 100, 200, 40, 100, 40]}, {"input": {"nums": [5, 3, 1]}, "output": [3, 5, 15]}] | 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": "c0e476f6f581a47e"} |
vcr_f94c0b81cf0b | 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": [12, 20, 23, 24, 28, 29, 32, 33, 37, 41, 42], "target": 9}, "output": 0}, {"input": {"nums": [2, 8, 16, 26, 29], "target": 4}, "output": 1}, {"input": {"nums": [0, 2, 4, 5, 7, 8, 9, 23, 28, 37, 38], "target": 30}, "output": 9}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "d706670d8d5f7b54"} |
vcr_39754ad1cbd8 | 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, 3, 7, 3, 4]}, "output": [252, 252, 108, 252, 189]}, {"input": {"nums": [5, 4, 3]}, "output": [12, 15, 20]}, {"input": {"nums": [5, 3, 5, 5]}, "output": [75, 125, 75, 75]}] | 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": "66f425f4cd4566ba"} |
vcr_7ca200552de8 | 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": [54, 9, 38, 50, 43, 56, 20]}, "output": 47}, {"input": {"prices": [9, 10, 24]}, "output": 15}, {"input": {"prices": [34, 30, 38, 38, 8, 20, 31, 24, 7, 16]}, "output": 23}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "50246848ec9ef689"} |
vcr_d19e4ee3f37e | 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": [18, 47, 14, 15, 17, 14]}, "output": 29}, {"input": {"prices": [38, 1, 25, 17, 40, 13, 27, 22, 32, 17]}, "output": 39}, {"input": {"prices": [9, 33, 36, 38, 10, 17]}, "output": 29}] | 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": "af97db7a00a8fdcb"} |
vcr_a439d8449d14 | 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": [-10, -2, -1, 6, 10, 26, 36], "target": 36}, "output": [4, 5]}, {"input": {"nums": [-20, -11, -1, 2, 3, 6, 8, 23, 36, 39], "target": 2}, "output": [2, 4]}, {"input": {"nums": [-16, -10, -7, -1, 0, 4, 8, 15, 24], "target": 1}, "output": [2, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "de4bca730619138c"} |
vcr_5938c16b726b | 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": [8, 53, 10, 9, 25, 18, 41, 22, 25, 21, 57]}, "output": 49}, {"input": {"prices": [40, 9, 40, 10, 24, 12]}, "output": 31}, {"input": {"prices": [38, 38, 27, 39, 39, 16, 36, 26, 1]}, "output": 20}] | 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": "72252e476c6f53d9"} |
vcr_c8d1be8c8628 | 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": 46069}, "output": 11}, {"input": {"n": 14047}, "output": 11}, {"input": {"n": 7605}, "output": 9}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "11140070d1122a8a"} |
vcr_7d0cd386eb75 | 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": [-10, 1, 13, 15, 22, 33, 35, 39, 42, 43], "target": 52}, "output": [2, 7]}, {"input": {"nums": [-12, -8, 1, 16, 18, 28, 29, 31], "target": 23}, "output": [1, 7]}, {"input": {"nums": [-18, 2, 8, 13, 16], "target": -10}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "4bde4610e6fc72aa"} |
vcr_ab3edf2cd7c7 | 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": "cc b accecd eceaeb aadd ddb"}, "output": "ddb aadd eceaeb accecd b cc"}, {"input": {"s": "aab bba b"}, "output": "b bba aab"}, {"input": {"s": "baaa bba ab a bab"}, "output": "bab a ab 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": "8368c0ca7b86b786"} |
vcr_ef58c7f06197 | 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, -28, -24, -23, -22, -12, -9, 10, 34], "target": -32}, "output": [3, 6]}, {"input": {"nums": [-15, -11, 27, 36], "target": 21}, "output": [0, 3]}, {"input": {"nums": [-15, -11, 9, 20], "target": -6}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "836a26e54b36764e"} |
vcr_7f9cb519a394 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-11, 5, -9, 18, 1, 1, 8, 16]}, "output": 44}, {"input": {"nums": [0, -2, -9, -9, 6]}, "output": 6}, {"input": {"nums": [1, 7, 2, 6, 7, -1, 5, -9, -7, -9]}, "output": 27}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "1ed80b9258de21ed"} |
vcr_c83a92fae23f | 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, 8, 9, 10]}, "output": [3, 6, 8, 9, 10]}, {"input": {"nums": [1, 3, 4, 5, 6, 7, 8]}, "output": [1, 3, 4, 5, 6, 7, 8]}, {"input": {"nums": [4, 6, 11, 12]}, "output": [4, 6, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "c4eebe11d33d5c69"} |
vcr_89a6e6a13b03 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-26, -22, -17, -12, -3, 2, 7, 23, 29, 49], "target": 6}, "output": [2, 7]}, {"input": {"nums": [-20, 4, 8, 9, 20, 21, 25, 34], "target": 55}, "output": [5, 7]}, {"input": {"nums": [-13, -8, 4, 11, 15, 17, 20, 27, 28, 36], "target": 19}, "output": [1, 7]}] | 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": "0a733fb73160b547"} |
vcr_21675732fdd0 | 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": [4, 14, -17, -21, -10, -4, 12, 27, -10, 26, 7, 34, 4]}, "output": [4, 18, 1, -20, -30, -34, -22, 5, -5, 21, 28, 62, 66]}, {"input": {"nums": [-8, 0, 0, -9]}, "output": [-8, -8, -8, -17]}, {"input": {"nums": [4, 5, 0, 16, 15, 18, 17]}, "output": [4, 9, 9, 25, 40, 58, 75]}] | 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": "24ebffe66501083c"} |
vcr_b8f04c9eee69 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 8, 10, 11, 11, 13, 14, 15]}, "output": [1, 8, 10, 11, 13, 14, 15]}, {"input": {"nums": [4, 5, 11]}, "output": [4, 5, 11]}, {"input": {"nums": [3, 6, 8, 9, 9, 9, 11]}, "output": [3, 6, 8, 9, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "4ddec3a8855243b6"} |
vcr_30b6388c31c1 | 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": 135, "b": 173}, "output": 1}, {"input": {"a": 142, "b": 166}, "output": 2}, {"input": {"a": 92, "b": 59}, "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": "cae53bbc416baf40"} |
vcr_129c0ff8006d | 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": [133, 149, 38, 67, 46, 18, 56, 143, 62, 188, 113, 81, 50]}, "output": false}, {"input": {"nums": [2, 0, 10, 4]}, "output": false}, {"input": {"nums": [19, 5, 15, 18, 20, 8, 8, 12, 21]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "22b2ff5a4b0d5080"} |
vcr_e29eea43d63d | 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": [8, 32, 48, 27, 28, 55, 8, 40, 5]}, "output": 47}, {"input": {"prices": [4, 30, 10, 36, 23, 3, 39, 34, 35, 33]}, "output": 36}, {"input": {"prices": [29, 5, 38]}, "output": 33}] | 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": "7a4b394a5c6ab7d1"} |
vcr_62cff876feef | 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, 8, 21, 32, 35], "target": 22}, "output": 3}, {"input": {"nums": [13, 27, 35], "target": 29}, "output": 2}, {"input": {"nums": [0, 6, 20, 32, 38], "target": 28}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "5f887415c8a25cfd"} |
vcr_dcbcd4c5eab0 | 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": "bcbabab"}, "output": 7}, {"input": {"s": "cbcc"}, "output": 3}, {"input": {"s": "bacacabbb"}, "output": 7}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "f91995e3a1b9fd85"} |
vcr_4e71848c2e52 | 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": 37, "b": 162}, "output": 1}, {"input": {"a": 20, "b": 150}, "output": 10}, {"input": {"a": 135, "b": 41}, "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": "3c19c76b3cadb98d"} |
vcr_1bf611598ba8 | 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": ["ab", "bb", "cc", "aca", "bac", "cac"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "abb", "bba", "bab", "aab", "abb"]}, "output": [2, 4]}, {"input": {"strs": ["aab", "baa", "baa", "bba", "abb", "aba", "baa"]}, "output": [2, 5]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "ddb0113b81112c6e"} |
vcr_4f087beaccfb | 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": "cdcaadbbcb"}, "output": false}, {"input": {"s": "bb"}, "output": true}, {"input": {"s": "cbacbccbab"}, "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": "afac068f9328d504"} |
vcr_924945c028fd | 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, 7, 8, 12, 14, 27, 42, 43, 44], "target": 18}, "output": 5}, {"input": {"nums": [2, 9, 21], "target": 4}, "output": 1}, {"input": {"nums": [1, 3, 10, 11, 19, 21, 25, 28], "target": 13}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "2d00d626bdbfd037"} |
vcr_e52a52556052 | 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": [-25, -2, 3, -7, 21, 28, 11, 7, 10, -2, 5, 15, -8, 2]}, "output": [-25, -27, -24, -31, -10, 18, 29, 36, 46, 44, 49, 64, 56, 58]}, {"input": {"nums": [7, 1, -8, -4]}, "output": [7, 8, 0, -4]}, {"input": {"nums": [-1, -4, -6, 11, 5, -2, -10, 1]}, "output": [-1, -5, -11, 0, 5, 3, -7, -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": "ccb97e835a651299"} |
vcr_6d980bde50f2 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 4, 12, 16, 19, 23, 25, 32, 33, 36, 38, 39, 42], "target": 47}, "output": 13}, {"input": {"nums": [3, 4, 6, 14, 22, 34], "target": 40}, "output": 6}, {"input": {"nums": [3, 8, 9, 11, 14, 18, 24], "target": 18}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "88484a793877deb9"} |
vcr_c7bdef4db0c4 | 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": ["aca", "abbc", "cac", "ccc", "abc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "bbb", "bab", "baa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bba", "bbb", "bba"]}, "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": "d25a720025c07cec"} |
vcr_d9c7df74b9f5 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [17, 10, -2, 13, 17, 7], "k": 1}, "output": [7, 17, 10, -2, 13, 17]}, {"input": {"nums": [7, 3, -1, 0, 0, 1, 3, 2, 0], "k": 5}, "output": [0, 1, 3, 2, 0, 7, 3, -1, 0]}, {"input": {"nums": [10, 6, -5, 0, -5, 10, 9, 0], "k": 19}, "output": [10, 9, 0, 10, 6, -5, 0, -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": "c7f5929590aca2fc"} |
vcr_e7b569133afd | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-9, 6, -18, -5, -7, 9, -3, 4, -6, -18], "k": 17}, "output": [-5, -7, 9, -3, 4, -6, -18, -9, 6, -18]}, {"input": {"nums": [-7, -1, -5, 0, 3, -10, 10, 2, 8, -8], "k": 13}, "output": [2, 8, -8, -7, -1, -5, 0, 3, -10, 10]}, {"input": {"nums": [7, 5, -6, 6, -4, 8, 7, -6], "k": 9}, "output": [-6, 7, 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": "428a2840ae340d59"} |
vcr_58167291aad5 | 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": [-2, -1, 5, 11, 17, 19, 35, 36, 48], "target": 52}, "output": [4, 6]}, {"input": {"nums": [-17, -12, -3, 5, 7, 9, 11, 16, 28, 35], "target": -29}, "output": [0, 1]}, {"input": {"nums": [-5, -4, 4, 11, 13, 14, 16, 24, 33, 39], "target": 11}, "output": [0, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "4ccc77b4dde8c8c9"} |
vcr_ca045e938a88 | 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": 109, "b": 216}, "output": 1}, {"input": {"a": 37, "b": 142}, "output": 1}, {"input": {"a": 74, "b": 112}, "output": 2}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "cc4be4b4b05063e0"} |
vcr_11457bbab05b | 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": "baaacabbc"}, "output": 6}, {"input": {"s": "abcbbbbc"}, "output": 5}, {"input": {"s": "bccbba"}, "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": "73fb3ba3924517e9"} |
vcr_e943aadfcf99 | 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, 9, 23, 10, 15, 25]}, "output": 67}, {"input": {"nums": [15, 17, 12, 11, 18, 10, 13, 13, 2, 13]}, "output": 71}, {"input": {"nums": [8, 15]}, "output": 15}] | 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": "d1c59f67a5d40b15"} |
vcr_81d00292dc49 | 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": "acababacc"}, "output": 8}, {"input": {"s": "acbbcababca"}, "output": 10}, {"input": {"s": "caacbcbbcca"}, "output": 8}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "a4bf693559e02ccf"} |
vcr_e830737ee81e | 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": "baac bb dbdd d eaedd cbadc dad"}, "output": "dad cbadc eaedd d dbdd bb baac"}, {"input": {"s": "abaa a bbb bab b"}, "output": "b bab bbb a abaa"}, {"input": {"s": "bba aa abbb"}, "output": "abbb aa bba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "fffddff44da6be3e"} |
vcr_b08e3c578088 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 3, 5, 5, 6, 7, 12, 13, 14, 14, 15]}, "output": [2, 3, 5, 6, 7, 12, 13, 14, 15]}, {"input": {"nums": [0, 2, 2, 3, 5, 5, 5, 6, 8, 9, 9]}, "output": [0, 2, 3, 5, 6, 8, 9]}, {"input": {"nums": [0, 4, 5]}, "output": [0, 4, 5]}] | 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": "a615f8fd960cefd5"} |
vcr_c61e1373cc70 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [2, 3, 5, 7, 6, 7]}, "output": [4410, 2940, 1764, 1260, 1470, 1260]}, {"input": {"nums": [2, 3, 1, 4]}, "output": [12, 8, 24, 6]}, {"input": {"nums": [4, 4, 2, 1, 5, 4, 2]}, "output": [320, 320, 640, 1280, 256, 320, 640]}] | 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": "8fc346745825dab1"} |
vcr_83fa081eae85 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [12, 20, 26, 4, 28, 14]}, "output": 66}, {"input": {"nums": [6, 20, 16, 7, 8]}, "output": 30}, {"input": {"nums": [8, 7, 19, 14, 20]}, "output": 47}] | 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": "dc49a3455dc70560"} |
vcr_dedfc1aa0ea1 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [14, 6, 17, 7, 28, 18, 4, 2]}, "output": 63}, {"input": {"nums": [19, 9]}, "output": 19}, {"input": {"nums": [16, 9, 15]}, "output": 31}] | 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": "aa2020a1ec081d1d"} |
vcr_3ad284873421 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 7, 11, 16, 19, 21, 22, 23, 29, 48], "target": 23}, "output": 7}, {"input": {"nums": [2, 11, 12, 14, 17, 23, 27, 30, 34, 37, 38], "target": 17}, "output": 4}, {"input": {"nums": [2, 4, 5, 10, 12, 14, 16, 24, 25, 27, 34, 38], "target": 10}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "55752932c36b05c5"} |
vcr_73c2d45d253c | 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": [31, 72, 39, 21, 31, 7, 39, 21, 3, 3, 7]}, "output": 72}, {"input": {"nums": [16, 1, 25, 25, 45, 1, 13, 16, 13]}, "output": 45}, {"input": {"nums": [28, 28, 8, 45, 19, 13, 13, 12, 12, 19, 8]}, "output": 45}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "c05f2e46cd65e222"} |
vcr_a4842a2a649b | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "ed b eecadd"}, "output": "eecadd b ed"}, {"input": {"s": "b aaaa aabb"}, "output": "aabb aaaa b"}, {"input": {"s": "baab ab bba"}, "output": "bba ab baab"}] | 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": "0f04072e443ed29b"} |
vcr_65ea19ddfc0a | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 3, 7, 14, 19, 23, 26, 27, 31, 39, 42, 46, 48], "target": 5}, "output": 2}, {"input": {"nums": [1, 16, 24, 26, 30, 32, 33], "target": 32}, "output": 5}, {"input": {"nums": [3, 11, 19, 25, 26, 27], "target": 25}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "67dad80ad2c90b8d"} |
vcr_f878616c54e1 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 1, 0, 1, 4, 2, 3, 0]}, "output": true}, {"input": {"nums": [0, 1, 1, 0]}, "output": false}, {"input": {"nums": [3, 2, 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": "b55fe7701c1f7529"} |
vcr_400d6d001497 | 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": "aadcbaacaaca"}, "output": false}, {"input": {"s": "acca"}, "output": true}, {"input": {"s": "bccccaaaa"}, "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": "504e506caf19051b"} |
vcr_f686dc1ae9dc | easy | math | Return the greatest common divisor of positive integers a and b. | def gcd(a: int, b: int) -> int: | Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. | def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
| [{"input": {"a": 249, "b": 147}, "output": 3}, {"input": {"a": 38, "b": 107}, "output": 1}, {"input": {"a": 116, "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": "a0566b88b20d307c"} |
vcr_2d372de3da6f | 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": ["aaab", "ac", "bc", "bca"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["baa", "baa", "aab", "aab", "bba"]}, "output": [1, 4]}, {"input": {"strs": ["bbb", "aba", "aab"]}, "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": "d5da8918706d3b7d"} |
vcr_88ecd78acf9c | 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": "ecda ccba bc cc ccd c daa"}, "output": "daa c ccd cc bc ccba ecda"}, {"input": {"s": "aab aaba a abaa"}, "output": "abaa a aaba aab"}, {"input": {"s": "aaa bbb"}, "output": "bbb aaa"}] | 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": "3c0b63cd59e01363"} |
vcr_f756d7203314 | 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": ["bacc", "cac", "ca", "abab", "ba", "ccab", "bb", "acbc"]}, "output": [1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bab", "aaa", "aaa"]}, "output": [1, 2]}, {"input": {"strs": ["aab", "bbb", "abb"]}, "output": [1, 1, 1]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "4c24d914a0455799"} |
vcr_a1e6197e0f4a | 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": [3, 1, -19, -19, -6, 11, 19, -10, -9, -19, -17], "k": 28}, "output": [11, 19, -10, -9, -19, -17, 3, 1, -19, -19, -6]}, {"input": {"nums": [8, -6, 7, -9, -10, 4, 8, 1, 1, 7], "k": 6}, "output": [-10, 4, 8, 1, 1, 7, 8, -6, 7, -9]}, {"input": {"nums": [8, -3, -9, 4, 2, 4, -5], "k": 14}, "output": [8, -... | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "b58b4ee5de5ee169"} |
vcr_7bf711227a6f | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-8, 10, 25, 26, 27, 8, -16, 13, -19, -23, -4, 23, -11]}, "output": [-8, 2, 27, 53, 80, 88, 72, 85, 66, 43, 39, 62, 51]}, {"input": {"nums": [0, 14, 9, -5, 17, 9, 6, 10, 9, -4]}, "output": [0, 14, 23, 18, 35, 44, 50, 60, 69, 65]}, {"input": {"nums": [18, -8, 7, -2, -4, 0, 0, 3, 5, 9]}, "output": [18... | 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": "950635f5d42c5f71"} |
vcr_52aab5f077e6 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-7, -4, 4, 5, 8, 16, 21], "target": 37}, "output": [5, 6]}, {"input": {"nums": [17, 23, 25, 30, 34, 36, 37], "target": 59}, "output": [1, 5]}, {"input": {"nums": [-20, -12, -7, -6, -1, 7, 10, 14, 15, 33], "target": -13}, "output": [0, 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": "e6a8c507a0f2b11a"} |
vcr_47ad716f9f1d | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 4, 10, 14, 18, 23, 32, 44], "target": 20}, "output": 5}, {"input": {"nums": [2, 3, 4, 29, 31, 33, 35, 36], "target": 9}, "output": 3}, {"input": {"nums": [0, 2, 3, 6, 11, 14, 22, 25, 34, 35], "target": 20}, "output": 6}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "74df987f18b2c692"} |
vcr_8e96ba0b0d5c | 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, 16, 16, 3, 10, 12, -14], "k": 3}, "output": [10, 12, -14, 18, 18, 16, 16, 3]}, {"input": {"nums": [7, -6, 5, -4], "k": 6}, "output": [5, -4, 7, -6]}, {"input": {"nums": [1, 4, 10, 10], "k": 16}, "output": [1, 4, 10, 10]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "1028bbb4d7e28421"} |
vcr_bf0f09483db3 | 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": ["ab", "bb", "cc", "aa", "cb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bab", "baa", "bab", "abb", "bbb"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aaa", "bab", "bba"]}, "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": "2ed713106a9be69a"} |
vcr_96733ffa33fa | 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, 18, -23, 16, -23]}, "output": [15, 33, 10, 26, 3]}, {"input": {"nums": [-10, 13, -1]}, "output": [-10, 3, 2]}, {"input": {"nums": [-3, 10, -2, 11]}, "output": [-3, 7, 5, 16]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "0b0201765f68462d"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.