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_ccf0b51ef5d6 | 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, 10, 13, 21, 29, 31, 34, 38], "target": 39}, "output": 8}, {"input": {"nums": [9, 18, 20, 22, 29, 31, 39], "target": 19}, "output": 2}, {"input": {"nums": [1, 4, 8, 14, 16, 17, 19, 32, 35, 37], "target": 9}, "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": "b17bc8356318cc76"} |
vcr_ffe91e06a260 | 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": [-2, 34, 27, 23], "target": 25}, "output": [0, 2]}, {"input": {"nums": [6, 24, -25, 45, -10, 9, -13, 39], "target": 48}, "output": [5, 7]}, {"input": {"nums": [-9, 2, 17, 41, 9, 36, -26], "target": -17}, "output": [4, 6]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "9d5c9ae424e28108"} |
vcr_bbb32ba688a1 | 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": [45, 74, 10, 14, -14, -26, -23, 29, 53, -8], "target": 59}, "output": [0, 3]}, {"input": {"nums": [-22, 47, -14, -30, 21, -28, 6, 24, 26, -5], "target": -27}, "output": [0, 9]}, {"input": {"nums": [15, 9, 46, 24, -2], "target": 13}, "output": [0, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "99b3274a870fa6d0"} |
vcr_ee79a91636c2 | 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": [11, 14, 16, 33, 47], "target": 23}, "output": 3}, {"input": {"nums": [8, 11, 19, 20, 22, 31, 33, 37], "target": 32}, "output": 6}, {"input": {"nums": [0, 3, 5, 6, 17, 22, 23, 24, 25, 30, 32, 38], "target": 24}, "output": 7}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "c2b3b3f27220697c"} |
vcr_87d73d833bb8 | 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, 7, 18, 19, 24, 32, 34, 37, 42], "target": 51}, "output": [3, 5]}, {"input": {"nums": [-16, -15, -12, 5, 9, 10, 23, 24, 28, 37], "target": 8}, "output": [0, 7]}, {"input": {"nums": [-10, -8, -7, -3, 7, 18, 21, 34], "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": "ba73960840be44c8"} |
vcr_aeeec3bd27c5 | 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": [18, 1, 24, 5, 26, 43, 35, 17, 7, 0, 12, 42, 48, 14, 10, 25, 18]}, "output": true}, {"input": {"nums": [20, 3, 0, 7, 1, 19]}, "output": false}, {"input": {"nums": [25, 12, 15, 15, 22]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "17f12ed4201151c9"} |
vcr_73538cb3c57d | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bbb", "ba", "cbac", "aa", "bbc", "acaa"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "aba", "bab", "bab", "baa", "aaa", "abb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["baa", "aba", "aba", "aaa", "aba"]}, "output": [1, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "c38249559d67c9e2"} |
vcr_ee16c648f09a | 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": ["caa", "ab", "aab", "bcc", "ccca", "aba"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bab", "aab", "abb"]}, "output": [1, 3]}, {"input": {"strs": ["aab", "aba", "abb"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "b083fccac8315b9d"} |
vcr_ce43cbead3a2 | 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, -23, -1, -18, 9, 7, -24, 1, -4, -15]}, "output": [15, -8, -9, -27, -18, -11, -35, -34, -38, -53]}, {"input": {"nums": [9, -8, -2, 17, 13, -12, 13]}, "output": [9, 1, -1, 16, 29, 17, 30]}, {"input": {"nums": [-8, -8, 13, 13, -9, 17]}, "output": [-8, -16, -3, 10, 1, 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": "a5aa7e3b8b1957ff"} |
vcr_4c0b71c052b9 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [28, 28, 20, 36, 36, 62, 20]}, "output": 62}, {"input": {"nums": [11, 9, 54, 12, 27, 21, 12, 27, 21, 9, 11]}, "output": 54}, {"input": {"nums": [16, 16, 53, 19, 23, 9, 26, 29, 19, 9, 26, 23, 29]}, "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": "509b9c079dcfc999"} |
vcr_4aced026a07c | 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": 29183}, "output": 12}, {"input": {"n": 2280}, "output": 5}, {"input": {"n": 3770}, "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": "20895106cb4d2a39"} |
vcr_c7bd0f086b66 | 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": 158, "b": 184}, "output": 2}, {"input": {"a": 92, "b": 165}, "output": 1}, {"input": {"a": 80, "b": 86}, "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": "c0a02acea216f4fa"} |
vcr_690532670932 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 2, 2, 3, 9, 10, 10, 10, 12, 12, 13, 14, 14]}, "output": [0, 2, 3, 9, 10, 12, 13, 14]}, {"input": {"nums": [1, 4, 6, 7, 12]}, "output": [1, 4, 6, 7, 12]}, {"input": {"nums": [1, 2, 9, 10, 12]}, "output": [1, 2, 9, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "544160c52783465f"} |
vcr_966f172edc1c | 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": ["baab", "acc", "ccb", "ccca", "bbc", "baaa", "bcc", "abbb"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["abb", "bba", "aaa", "baa", "bab", "bab"]}, "output": [1, 1, 4]}, {"input": {"strs": ["aab", "abb", "bab", "aba", "abb"]}, "output": [2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "666894f66fa6e743"} |
vcr_5c8a202b93da | 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": [35, 31, -20, -20, 32, 15]}, "output": [35, 66, 46, 26, 58, 73]}, {"input": {"nums": [6, 12, -3, 1, 9, 11, -1, 12, -10]}, "output": [6, 18, 15, 16, 25, 36, 35, 47, 37]}, {"input": {"nums": [-2, 14, 5, 15, -12, 6]}, "output": [-2, 12, 17, 32, 20, 26]}] | 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": "dba0a4812f490a75"} |
vcr_be3d08e38143 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [6, 19, 25, 26, 27, 35, 38, 40, 41, 42, 45], "target": 3}, "output": 0}, {"input": {"nums": [3, 4, 12, 13, 17, 32, 35, 36, 37], "target": 38}, "output": 9}, {"input": {"nums": [2, 26, 29, 31, 33, 36, 37], "target": 20}, "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": "aec9389983a548e4"} |
vcr_255c3ee54e74 | 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, -1, -14, -6, 9, -15, 4, 10, 1, 12]}, "output": 27}, {"input": {"nums": [-4, 13, 0, 0, 3, 2, -12, 13, 5, 2]}, "output": 26}, {"input": {"nums": [9, -3, -8, -7, -7]}, "output": 9}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "b1b516b98709407a"} |
vcr_00d348277c42 | 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": 27351}, "output": 10}, {"input": {"n": 7604}, "output": 8}, {"input": {"n": 11033}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "2e2b727ee74a2eac"} |
vcr_193f73b6074a | 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": [17, 4, 6, 24, 14, 2, 3, 16, 13, 9, 6]}, "output": 68}, {"input": {"nums": [7, 12, 15, 2, 8, 8, 17]}, "output": 47}, {"input": {"nums": [17, 9, 16, 5, 3, 7]}, "output": 40}] | 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": "a8d29ed61965f909"} |
vcr_8a9a2a6fcf42 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-15, 22, -6, -12, -3, -16, 22, 5, -4, 11, 2]}, "output": 36}, {"input": {"nums": [-9, -6, -6, 9, 10, -4, -12, 12, -5]}, "output": 19}, {"input": {"nums": [11, -3, -7]}, "output": 11}] | 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": "3ad5650ef0e4ff5b"} |
vcr_890c98d37679 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [16, 16, 11, 24, 0, 24]}, "output": 64}, {"input": {"nums": [6, 7, 15, 3, 14]}, "output": 35}, {"input": {"nums": [16, 8, 4, 19, 9, 18]}, "output": 53}] | 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": "6f255b827795c5b0"} |
vcr_fc051c706fee | 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": [62, 5, 35, 19, 19, 25, 35, 25, 5]}, "output": 62}, {"input": {"nums": [28, 9, 15, 41, 23, 15, 28, 23, 9]}, "output": 41}, {"input": {"nums": [28, 20, 11, 20, 28, 43, 11]}, "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": "3625de21eaf77025"} |
vcr_67f29090b063 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["abc", "babb", "acb", "ac", "ab", "bb"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "abb", "aab", "baa", "aaa", "aaa", "aba"]}, "output": [1, 2, 4]}, {"input": {"strs": ["bbb", "bbb", "baa", "aab"]}, "output": [2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "3c1265da68b99421"} |
vcr_40f93ab4126d | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [160, 40, 173, 23, 15, 125, 149, 142, 118, 34, 169, 67, 88, 110, 108, 45, 28, 93]}, "output": false}, {"input": {"nums": [3, 12, 7, 4, 25, 19, 25, 21, 20, 2, 2]}, "output": true}, {"input": {"nums": [17, 12, 2, 19, 2, 15, 7, 15, 11, 7, 6]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "849c0d277b797576"} |
vcr_3a65b883c3f6 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [10, 12, 11, 46, 28, 8, 11]}, "output": true}, {"input": {"nums": [0, 20, 1, 13, 2, 13, 3, 23, 17, 7]}, "output": true}, {"input": {"nums": [6, 11, 7, 1, 2, 10, 10, 15, 2, 1, 1, 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": "2aa40b9f46707b86"} |
vcr_f60e5c3c05b3 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [12, -18, 12, -3, 3, -17, 12, 10], "k": 12}, "output": [3, -17, 12, 10, 12, -18, 12, -3]}, {"input": {"nums": [3, -8, 0, -3, 0, 3, 5, -4], "k": 9}, "output": [-4, 3, -8, 0, -3, 0, 3, 5]}, {"input": {"nums": [-5, 8, 7, -5, 4], "k": 16}, "output": [4, -5, 8, 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": "ac72dde08772b9b2"} |
vcr_ea21c342c3ee | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [29, 29, 43, 32, 29, 14, 35, 45, 44]}, "output": true}, {"input": {"nums": [8, 23, 18, 15, 10]}, "output": false}, {"input": {"nums": [14, 21, 18, 21, 17]}, "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": "f9fba694b916cfd0"} |
vcr_f6fc05a8f3d0 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [1, 0, 3, 1, 2, 3, 2, 0]}, "output": false}, {"input": {"nums": [3, 2, 2, 1, 0, 0, 1]}, "output": false}, {"input": {"nums": [2, 2, 1, 0, 2]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "2bf6aedd6f3cad24"} |
vcr_53d7ae5f46c0 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [14, 37, 74, 31, 37, 31, 14]}, "output": 74}, {"input": {"nums": [24, 19, 24, 57, 28, 5, 19, 28, 5]}, "output": 57}, {"input": {"nums": [19, 28, 28, 27, 43, 27, 19]}, "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": "5c0cd0afd6b87ad0"} |
vcr_d490edba94a0 | 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": [41, 114, 9, 68, 24, 118, 143, 95, 60, 15]}, "output": false}, {"input": {"nums": [4, 11, 6, 22, 11, 15, 1, 5, 24]}, "output": true}, {"input": {"nums": [7, 18, 5, 18, 20, 2, 7]}, "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": "868dbfa166cd465b"} |
vcr_ef5063a87433 | 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": [36, 79, 17, 19, 179, 173]}, "output": false}, {"input": {"nums": [11, 24, 16, 15, 18, 25, 24, 17]}, "output": true}, {"input": {"nums": [7, 16, 25]}, "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": "2a3290cdde2df3cb"} |
vcr_bd7a074f3376 | 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", "acb", "ca", "bbaa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bab", "aab", "abb"]}, "output": [1, 2]}, {"input": {"strs": ["bba", "bab", "bba", "abb", "bab", "aaa", "aba"]}, "output": [1, 1, 5]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "80e5f63b2c126e08"} |
vcr_76190ee84835 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [9, 7, 21, 14, -14, 1, 9, 6, 17]}, "output": 70}, {"input": {"nums": [11, 1, 14, -12]}, "output": 26}, {"input": {"nums": [-7, -10, -5, 8, 7, -5, 0, -12]}, "output": 15}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "8c8114419c24069b"} |
vcr_08b10041cacf | 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": [110, 28, 173, 102, 3]}, "output": false}, {"input": {"nums": [21, 21, 6, 13, 15, 17, 18, 3, 22]}, "output": true}, {"input": {"nums": [3, 24, 0, 3, 24, 1, 15, 7, 11, 12, 21, 1]}, "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": "852f94fdc22fe459"} |
vcr_9d526f73fef7 | 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": [25, 15, 28, 0, 24, 6, 14]}, "output": 91}, {"input": {"nums": [10, 6, 2, 4, 17, 10, 14, 10, 18, 0]}, "output": 61}, {"input": {"nums": [8, 1, 7, 18, 11, 10, 4, 14, 16]}, "output": 52}] | 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": "1603b3afc9066a9e"} |
vcr_ea47553be848 | 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": "dacc dacae acdd"}, "output": "acdd dacae dacc"}, {"input": {"s": "bb abb b"}, "output": "b abb bb"}, {"input": {"s": "b aab aba bbb"}, "output": "bbb aba aab b"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "8200dc21c8b5b8e0"} |
vcr_7625f4ab0f26 | 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, 4, 0, 1, 4, 0, 0]}, "output": false}, {"input": {"nums": [3, 0, 3, 2, 2, 2, 0]}, "output": true}, {"input": {"nums": [2, 3, 0, 0, 2, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "ffb6097d06df099c"} |
vcr_a35672eb1010 | 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": 63852}, "output": 10}, {"input": {"n": 5275}, "output": 7}, {"input": {"n": 77}, "output": 4}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "4519c2ae0af97d02"} |
vcr_2f62172596b1 | 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": "acdabbbbbbadca"}, "output": true}, {"input": {"s": "bbcccc"}, "output": false}, {"input": {"s": "aaabb"}, "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": "d610c0348d320047"} |
vcr_4990fdd6638e | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [13, 17, 11, 34, 54, 17, 13, 11, 34]}, "output": 54}, {"input": {"nums": [2, 12, 2, 10, 10, 26, 8, 8, 16, 16, 12, 42, 26]}, "output": 42}, {"input": {"nums": [17, 22, 17, 16, 4, 56, 4, 22, 16]}, "output": 56}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "42ce8bcc7cc19b48"} |
vcr_211eee094868 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [24, 13, 10, 29, 18, 5, 10]}, "output": 63}, {"input": {"nums": [19, 15, 6, 18, 20, 17, 7]}, "output": 54}, {"input": {"nums": [9, 20, 16, 10, 16]}, "output": 41}] | 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": "c0e3344a128225f7"} |
vcr_f30a3ab86cde | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["cbc", "bba", "ca", "cb", "cab", "ccc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "abb", "bbb", "aab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bba", "aab", "abb", "bab"]}, "output": [1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "efdd1a56415b1025"} |
vcr_04c8edf5ca5e | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [10, 15, -23, 16, -22, -15, 12, 3, 16]}, "output": [10, 25, 2, 18, -4, -19, -7, -4, 12]}, {"input": {"nums": [16, 5, -11, 16, 10, -5, 2, -7, -1]}, "output": [16, 21, 10, 26, 36, 31, 33, 26, 25]}, {"input": {"nums": [8, 6, -7]}, "output": [8, 14, 7]}] | 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": "70203f9e4c9bef53"} |
vcr_843b4b8bc0f5 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [26, -11, -22, 5, -18, 5]}, "output": [26, 15, -7, -2, -20, -15]}, {"input": {"nums": [6, 17, 3, -7]}, "output": [6, 23, 26, 19]}, {"input": {"nums": [15, 7, 8, 8, -2, 2, 1, 0]}, "output": [15, 22, 30, 38, 36, 38, 39, 39]}] | 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": "c47941ab7cd18072"} |
vcr_3d04bdfdfefc | 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": [26, 33, 63, 22, -15, -6, 25, 35], "target": 29}, "output": [5, 7]}, {"input": {"nums": [2, 12, -2, 24, 4, -30, 5, 43, 0], "target": -26}, "output": [4, 5]}, {"input": {"nums": [-9, 14, -17, 46], "target": 37}, "output": [0, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "0332d3a2ad9cf266"} |
vcr_3fcd793afd45 | 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, -12, 8, -7], "k": 3}, "output": [-12, 8, -7, 3]}, {"input": {"nums": [-8, -2, -1], "k": 6}, "output": [-8, -2, -1]}, {"input": {"nums": [5, 5, 0], "k": 17}, "output": [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": "31457cc748fe36c7"} |
vcr_6175be5e6e7f | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [21, 40, 45, 16, 17, 13, 31, 56, 50, 1, 49, 32, 28, 47, 51, 44]}, "output": 50}, {"input": {"prices": [32, 19, 11, 1, 37, 39, 34, 25, 28, 7, 2, 13]}, "output": 38}, {"input": {"prices": [22, 11, 18, 15, 30, 29, 24, 15, 31, 39]}, "output": 28}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "6ffada39e19b210b"} |
vcr_b3d7b8687026 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 3, 7, 6, 4, 5, 5, 7]}, "output": [88200, 205800, 88200, 102900, 154350, 123480, 123480, 88200]}, {"input": {"nums": [4, 3, 1, 5, 4, 3, 5]}, "output": [900, 1200, 3600, 720, 900, 1200, 720]}, {"input": {"nums": [5, 5, 4]}, "output": [20, 20, 25]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "d50a39241ddcef8d"} |
vcr_02c797e9632f | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [11, 15, 5, 8, 41, 51]}, "output": 46}, {"input": {"prices": [30, 39, 15, 34, 38, 36, 15, 11, 16, 30]}, "output": 23}, {"input": {"prices": [17, 20, 39, 30]}, "output": 22}] | 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": "675e5af4e04f792a"} |
vcr_bfff949c1274 | 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": [112, 2, 121, 59, 16, 189, 153, 90, 35, 170, 12, 9, 180, 32, 134, 10]}, "output": false}, {"input": {"nums": [3, 21, 13, 24, 12, 16, 7, 24, 16, 5]}, "output": true}, {"input": {"nums": [13, 11, 15, 16, 14, 25, 9]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "523e59a2ee1ef888"} |
vcr_d9a7703dddcf | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [20, 20, 2, 15, 9, 37, 26, 5, 36, 5, 2, 51, 15, 26, 9, 37, 36]}, "output": 51}, {"input": {"nums": [44, 16, 18, 22, 22, 16, 18]}, "output": 44}, {"input": {"nums": [8, 2, 15, 58, 13, 2, 18, 13, 18, 4, 4, 15, 8]}, "output": 58}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "89b283251a8ff6b9"} |
vcr_a940b06e5653 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 3, 2, 4, 3, 4, 1, 0]}, "output": true}, {"input": {"nums": [3, 3, 1, 3]}, "output": true}, {"input": {"nums": [0, 2, 3, 3, 0, 2, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "182992336a87329f"} |
vcr_b58e381853c7 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [7, 10, 2, -9, -17, 9, -2], "k": 12}, "output": [2, -9, -17, 9, -2, 7, 10]}, {"input": {"nums": [-2, -4, -2, -1, -6, -3, 10], "k": 4}, "output": [-1, -6, -3, 10, -2, -4, -2]}, {"input": {"nums": [-3, 4, -4, 10], "k": 18}, "output": [-4, 10, -3, 4]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "20ab777629d5623b"} |
vcr_40045310e82d | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-25, -22, 2, 12, 13, 27], "target": -12}, "output": [0, 4]}, {"input": {"nums": [-20, -15, -11, -10, 3, 10, 11, 13, 19, 29, 34], "target": -9}, "output": [0, 6]}, {"input": {"nums": [-19, 4, 8, 17, 35], "target": 39}, "output": [1, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "59a98f20f2ffcca6"} |
vcr_d545e9d1befc | 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": [-22, 22, 15, 8, 43, 68, 38, 46, 14, 18, 2, 16, 61], "target": 40}, "output": [1, 9]}, {"input": {"nums": [41, -4, 30, 47, 3, 31, 40], "target": 50}, "output": [3, 4]}, {"input": {"nums": [-21, 36, -29, -6, 9], "target": 3}, "output": [3, 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": "3d4bb563add815dc"} |
vcr_d09dd634fb37 | 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": [-23, 28, -20]}, "output": [-23, 5, -15]}, {"input": {"nums": [15, 9, -8, 3, -12, 15, 17, -5, 1, 9]}, "output": [15, 24, 16, 19, 7, 22, 39, 34, 35, 44]}, {"input": {"nums": [2, 12, 9, 12, 6, 3]}, "output": [2, 14, 23, 35, 41, 44]}] | 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": "9bcefdd7470a11cd"} |
vcr_466b279062be | 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": [26, 72, 27, -13, -31, -18], "target": -49}, "output": [4, 5]}, {"input": {"nums": [-23, 40, -25, -28, 38, -19, -14], "target": 21}, "output": [1, 5]}, {"input": {"nums": [-3, 13, 20, 35, -27, 6, 40, 41, 49, 4, 16], "target": -7}, "output": [2, 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": "b2f4099fe4e56467"} |
vcr_5da8957a79c9 | 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, 3, 5, 5, 6, 10]}, "output": [1, 3, 5, 6, 10]}, {"input": {"nums": [0, 0, 2, 3, 5, 6, 6, 9, 9, 11, 12, 12]}, "output": [0, 2, 3, 5, 6, 9, 11, 12]}, {"input": {"nums": [1, 4, 4, 5, 6, 7, 10, 10, 11, 11, 12]}, "output": [1, 4, 5, 6, 7, 10, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "4a7feb4f6c3a9b17"} |
vcr_bc98cabcad51 | 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": [19, 40, 13, 33, 0, 15, 1, 43, 19]}, "output": true}, {"input": {"nums": [16, 8, 10, 15, 10]}, "output": true}, {"input": {"nums": [24, 1, 23, 3, 6, 20, 11, 12, 18]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "f3dc6e33d69b18c9"} |
vcr_3f4ddf1b46b2 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-5, -6, 14, -4, 12, -7, -11, 15, -6, -18, 14]}, "output": 22}, {"input": {"nums": [-3, 12, -3, 3, 10]}, "output": 22}, {"input": {"nums": [-8, 9, -8, 11, 2, -4, -11, -5, 0, 7]}, "output": 14}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "5579d35cdef521db"} |
vcr_76dc5a7a50a9 | 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": [-21, 11, 71, -13, -3, -37, 5], "target": -2}, "output": [1, 3]}, {"input": {"nums": [-11, 18, -8, 3, -22, 5, 32, -20, -15], "target": 24}, "output": [2, 6]}, {"input": {"nums": [28, 36, 45, -24, 33, 16, 3, 11, 4, 8, -19], "target": 32}, "output": [0, 8]}] | 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": "7ddc05283bd5f011"} |
vcr_142f20fa5225 | 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": ["cbb", "aacc", "aca", "caaa", "baa", "ba", "aca", "bba"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bab", "bba", "baa", "bab"]}, "output": [1, 4]}, {"input": {"strs": ["aab", "aba", "bab"]}, "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": "4e575796bbf00c69"} |
vcr_56dddcecfed2 | 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": "babbbaaac"}, "output": 5}, {"input": {"s": "aba"}, "output": 3}, {"input": {"s": "caa"}, "output": 2}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "557e3146237d6f72"} |
vcr_4752d9b04854 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [10, 11, -3, -11, 0, 2, -4, -10, 0, 12, -10, 6, 11]}, "output": 21}, {"input": {"nums": [1, 3, 5, 10, 8, 6, -2, -1]}, "output": 33}, {"input": {"nums": [10, 4, 10, 12, 6, 6]}, "output": 48}] | 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": "039da84c39e3eead"} |
vcr_318b70bfbd6b | 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": "be abeac caba"}, "output": "caba abeac be"}, {"input": {"s": "baaa baa bbb bbba baa"}, "output": "baa bbba bbb baa baaa"}, {"input": {"s": "b baab abba aa"}, "output": "aa abba baab b"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "cf6d8ee87026664c"} |
vcr_a7851d0515b2 | 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": "badd aaeab eddbdb acce d"}, "output": "d acce eddbdb aaeab badd"}, {"input": {"s": "aaab abb a bab aaaa"}, "output": "aaaa bab a abb aaab"}, {"input": {"s": "baa aa"}, "output": "aa baa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "9062fc5052504eb6"} |
vcr_977eac8dedfc | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 12, 15, 17, 18, 20, 22, 27, 29, 31, 33, 36], "target": 49}, "output": 12}, {"input": {"nums": [2, 14, 23, 29], "target": 19}, "output": 2}, {"input": {"nums": [0, 3, 5, 8, 10, 16, 39], "target": 23}, "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": "67614fb793a7192f"} |
vcr_6b30d18e1629 | 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": [-20, 0, 15, 20, 22, 23, 26, 36], "target": 0}, "output": [0, 3]}, {"input": {"nums": [-19, 1, 11, 16, 20, 32], "target": 33}, "output": [1, 5]}, {"input": {"nums": [-9, 16, 24, 37], "target": 61}, "output": [2, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "bdcff27371bc377c"} |
vcr_4707b68ea4d6 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-20, -1, -10, -2, -20], "k": 3}, "output": [-10, -2, -20, -20, -1]}, {"input": {"nums": [7, -10, 1, 7, 2, 7, -9, -8, -1, -6], "k": 8}, "output": [1, 7, 2, 7, -9, -8, -1, -6, 7, -10]}, {"input": {"nums": [0, 10, -5, 6, -7, -10], "k": 17}, "output": [10, -5, 6, -7, -10, 0]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "1f80aeaf3b44a90b"} |
vcr_6968f0533409 | 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": [43, 46, 54, -32], "target": 22}, "output": [2, 3]}, {"input": {"nums": [-28, 7, 13, 39, -30, 23, 18, 42], "target": 62}, "output": [3, 5]}, {"input": {"nums": [21, 35, 34, 48, 19, 49, -10, 41, 14], "target": 83}, "output": [1, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "c311ec91415cb0b6"} |
vcr_660b73e48f4e | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-1, -10, 17, 14, -3, 14, 1], "k": 12}, "output": [17, 14, -3, 14, 1, -1, -10]}, {"input": {"nums": [-5, -3, -3, -7, -10, -7], "k": 18}, "output": [-5, -3, -3, -7, -10, -7]}, {"input": {"nums": [9, 2, 4], "k": 5}, "output": [2, 4, 9]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "7c9a31b0648d8b1f"} |
vcr_e386d08e4214 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [12, 3, 17, 1, -4, -12, -7, -2]}, "output": 33}, {"input": {"nums": [14, 3, -10, -2, 14, 11, 12, 1, -9, -1]}, "output": 43}, {"input": {"nums": [13, -8, 3, 7, -3]}, "output": 15}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "15c8465be71d7a87"} |
vcr_5997944bf9e8 | 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": [-24, 55, -29, -2, 14, 67, -21, 11, -34, 79, -38, 53, 8], "target": 43}, "output": [0, 5]}, {"input": {"nums": [38, 2, 20, 23, 40, 17, -1, 13], "target": 30}, "output": [5, 7]}, {"input": {"nums": [-7, -18, -21, -5, 37, 14, -16, 32, -3], "target": -21}, "output": [3, 6]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "cd22f4d5eab4fa0f"} |
vcr_d9d9639568c1 | 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": "ccabcabba"}, "output": 7}, {"input": {"s": "ccc"}, "output": 1}, {"input": {"s": "abbcbcbcac"}, "output": 9}] | 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": "87a9fc9a0e856e9f"} |
vcr_4ccc7b0724b0 | 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": [14, 15, 18, 29]}, "output": 15}, {"input": {"prices": [26, 34, 7, 10, 32, 29]}, "output": 25}, {"input": {"prices": [4, 15, 1, 15, 14]}, "output": 14}] | 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": "626936711e530c22"} |
vcr_ae6dcf58394d | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 3, 1, 1, 3, 4, 4, 4, 2, 0]}, "output": true}, {"input": {"nums": [2, 3, 1, 1]}, "output": true}, {"input": {"nums": [3, 3, 3, 3]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "31a8e4cb3f064b55"} |
vcr_aae5d683e123 | 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": "dcbea aeeece"}, "output": "aeeece dcbea"}, {"input": {"s": "a bba"}, "output": "bba a"}, {"input": {"s": "abba aa abb"}, "output": "abb aa abba"}] | 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": "85ed8303d787fea3"} |
vcr_adbcf71b0bb5 | 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": 104, "b": 25}, "output": 1}, {"input": {"a": 87, "b": 59}, "output": 1}, {"input": {"a": 37, "b": 15}, "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": "a36b7b279555c751"} |
vcr_c59169d67507 | 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": "cbaababaababaabc"}, "output": true}, {"input": {"s": "acca"}, "output": true}, {"input": {"s": "abcbcbabac"}, "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": "880e7f9a5d35f9c1"} |
vcr_c25b3ff47a84 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [49, 3, 29, 60]}, "output": 57}, {"input": {"prices": [37, 13, 32, 32, 26, 39, 19]}, "output": 26}, {"input": {"prices": [40, 25, 31, 27, 27, 31, 28, 15, 26, 15, 4]}, "output": 11}] | 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": "0eb7d5f65a2b5760"} |
vcr_19f015b3b583 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 0, 3, 1, 4, 2, 3, 0, 0]}, "output": true}, {"input": {"nums": [2, 3, 3]}, "output": true}, {"input": {"nums": [2, 2, 1, 0, 3, 1, 1, 2]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "bd4f15811a92bf52"} |
vcr_daa5612559b8 | 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": "acbdbababdbca"}, "output": true}, {"input": {"s": "bbcab"}, "output": false}, {"input": {"s": "abacc"}, "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": "018c15a0b5017c1e"} |
vcr_ccf9ec35462b | 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, 2, 6, 10, 15, 18, 21, 23, 30, 41, 46, 47], "target": 25}, "output": 8}, {"input": {"nums": [12, 13, 22, 25, 26, 28, 35, 39], "target": 9}, "output": 0}, {"input": {"nums": [1, 4, 6, 14, 15, 16, 18, 23, 30, 35, 39], "target": 6}, "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": "a41138672883ee3a"} |
vcr_2e860486808a | 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": [23, 52, 3, 33, 36]}, "output": 33}, {"input": {"prices": [35, 36, 19, 35, 35, 20, 23]}, "output": 16}, {"input": {"prices": [11, 10, 8, 12, 29, 18, 4, 35, 17]}, "output": 31}] | 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": "aec8e633e50aa5de"} |
vcr_d0ea1b6b2df9 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-4, -14, 16, 19, -10, -1, -15, 0, 18]}, "output": 35}, {"input": {"nums": [12, 6, 3, 11, -8, -1, 14, -6, 4]}, "output": 37}, {"input": {"nums": [-11, 6, -7, 13]}, "output": 13}] | 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": "bf8f5d7b5b991435"} |
vcr_73d4af6ebf52 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 3, 7, 4, 6, 1, 0]}, "output": [0, 0, 0, 0, 0, 0, 504]}, {"input": {"nums": [4, 3, 5, 5, 1]}, "output": [75, 100, 60, 60, 300]}, {"input": {"nums": [1, 2, 5, 2]}, "output": [20, 10, 4, 10]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "df3d5dbd077430eb"} |
vcr_f2b07660a934 | 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": [54, 33, 64, -36, -30, -22, 24, 36], "target": 97}, "output": [1, 2]}, {"input": {"nums": [34, -6, 19, 29], "target": 63}, "output": [0, 3]}, {"input": {"nums": [-16, 7, -19, 2], "target": -12}, "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": "7149394e8d1d8931"} |
vcr_3df7323afd48 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [23, 20, 29, 18, 5, 5, 11, 16, 30]}, "output": 98}, {"input": {"nums": [19, 3, 14, 8]}, "output": 33}, {"input": {"nums": [11, 15, 19, 15, 18, 3, 7, 13, 18]}, "output": 73}] | 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": "ef5a23f5893e6c10"} |
vcr_7c173b3426ee | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [14, 16, 22, 23, 40, 44], "target": 5}, "output": 0}, {"input": {"nums": [14, 17, 26, 27, 29, 30], "target": 5}, "output": 0}, {"input": {"nums": [0, 5, 6, 8, 9, 11], "target": 33}, "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": "34e35bf87a5e2d66"} |
vcr_378a272e8091 | 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": [-22, -10, 30, 32, 15]}, "output": [-22, -32, -2, 30, 45]}, {"input": {"nums": [-3, -10, -4, 0, -12, 14, 2]}, "output": [-3, -13, -17, -17, -29, -15, -13]}, {"input": {"nums": [-9, -11, 6, 0, 8, 12, -11]}, "output": [-9, -20, -14, -14, -6, 6, -5]}] | 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": "e186aa8dda2aa725"} |
vcr_041356c89879 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 2, 3, 7, 11, 11, 14, 15]}, "output": [2, 3, 7, 11, 14, 15]}, {"input": {"nums": [0, 1, 1, 1, 4, 4, 10, 11, 12, 12]}, "output": [0, 1, 4, 10, 11, 12]}, {"input": {"nums": [0, 0, 4, 6, 6, 11, 12]}, "output": [0, 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": "12624f92211b29de"} |
vcr_b9407e580872 | 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": [36, 35, 13, 32, 22, 40, 30, 38, 31, 34, 15, 20, 46, 44, 15, 37]}, "output": true}, {"input": {"nums": [5, 0, 0, 3, 5, 16, 23, 22, 4]}, "output": true}, {"input": {"nums": [1, 0, 11, 23, 17, 10, 14, 17, 11, 18, 7]}, "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": "450f35658f86b379"} |
vcr_bba2688fabfc | 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": [13, 15, -5, 6, 15, 18, -17, -6, 19, 7, -17, 19], "k": 17}, "output": [-6, 19, 7, -17, 19, 13, 15, -5, 6, 15, 18, -17]}, {"input": {"nums": [-7, 7, -8, -1], "k": 2}, "output": [-8, -1, -7, 7]}, {"input": {"nums": [9, -8, 7, 4, -4, 0, -9], "k": 5}, "output": [7, 4, -4, 0, -9, 9, -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": "45b09be643966b0b"} |
vcr_0e7c10aa566b | 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": [38, 40, 1, 54, 7, 13, 23, 5, 49, 56, 25, 55, 19, 26, 23, 22]}, "output": 55}, {"input": {"prices": [25, 15, 26, 19, 19]}, "output": 11}, {"input": {"prices": [8, 2, 14, 19, 8, 33, 40]}, "output": 38}] | 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": "bfb4e61f0137e8a6"} |
vcr_2432d55e5061 | 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, 5, -6, -7, -1, 19, -10, -5]}, "output": 24}, {"input": {"nums": [12, -12, 7, -12]}, "output": 12}, {"input": {"nums": [14, -6, 2, 8, -12, -9]}, "output": 18}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "b55b5d990e30c1fb"} |
vcr_218dd51d910c | 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": "eabbcd dc e dbcdb d ebba"}, "output": "ebba d dbcdb e dc eabbcd"}, {"input": {"s": "baa baab a aa ab"}, "output": "ab aa a baab baa"}, {"input": {"s": "aabb b a"}, "output": "a b aabb"}] | 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": "ebbf8a83136ffbc3"} |
vcr_9810bf357536 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [11, -10, 2, -6], "k": 8}, "output": [11, -10, 2, -6]}, {"input": {"nums": [6, -6, -8, -5, 10, 9, -6, 4, 4, 3], "k": 6}, "output": [10, 9, -6, 4, 4, 3, 6, -6, -8, -5]}, {"input": {"nums": [-7, 6, -2, 10, 6, -5, -2, 4, -8], "k": 0}, "output": [-7, 6, -2, 10, 6, -5, -2, 4, -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": "d16a8e1b647df447"} |
vcr_482a5bd9f74e | 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": [43, 2, 29, 41, 11, 59]}, "output": 57}, {"input": {"prices": [18, 11, 18, 24, 25, 21, 17, 6, 28, 17]}, "output": 22}, {"input": {"prices": [10, 27, 9]}, "output": 17}] | 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": "d06c366b93ea5829"} |
vcr_71825d18b5da | 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, 6, 6, 6, 7, 11, 12, 14]}, "output": [1, 6, 7, 11, 12, 14]}, {"input": {"nums": [1, 2, 5, 7]}, "output": [1, 2, 5, 7]}, {"input": {"nums": [0, 0, 0, 1, 3, 3, 3, 3, 5, 6, 6, 7]}, "output": [0, 1, 3, 5, 6, 7]}] | 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": "494bff0cd400865a"} |
vcr_e3d3ef08bebe | 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": "cabc baea cbdaeb baaa eb aeaa bbab"}, "output": "bbab aeaa eb baaa cbdaeb baea cabc"}, {"input": {"s": "abbb baab ba b"}, "output": "b ba baab abbb"}, {"input": {"s": "aabb bb ba aaa"}, "output": "aaa ba bb aabb"}] | 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": "92108f148352bf92"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.