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_ed187c1498ce | easy | math | Return the greatest common divisor of positive integers a and b. | def gcd(a: int, b: int) -> int: | Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. | def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
| [{"input": {"a": 109, "b": 247}, "output": 1}, {"input": {"a": 43, "b": 168}, "output": 1}, {"input": {"a": 118, "b": 143}, "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": "8d6109f72fa587e3"} |
vcr_87dd4f497860 | 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": [57, 184, 75, 79, 56, 107, 91, 92, 47, 113, 42, 143, 34, 186]}, "output": false}, {"input": {"nums": [25, 12, 13, 20]}, "output": false}, {"input": {"nums": [16, 10, 12, 4, 23, 9, 0, 7]}, "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": "723b1df889c70208"} |
vcr_bba5b67734eb | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 4, 6, 8, 11, 12, 14]}, "output": [0, 2, 4, 6, 8, 11, 12, 14]}, {"input": {"nums": [0, 2, 4, 7, 8, 8, 12, 12]}, "output": [0, 2, 4, 7, 8, 12]}, {"input": {"nums": [0, 1, 1, 1, 4, 6, 7, 7, 8, 10, 11, 11]}, "output": [0, 1, 4, 6, 7, 8, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "e9445007704e3994"} |
vcr_71f821ad5ff8 | 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": [7, 25, 4, 57, 30, 38, 30, 15, 15, 31, 51]}, "output": 53}, {"input": {"prices": [4, 27, 36]}, "output": 32}, {"input": {"prices": [16, 12, 28, 14, 9, 10, 20]}, "output": 16}] | 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": "82386f48e64ea4ce"} |
vcr_3d4f47f626ed | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [68, 9, 78, 19, -21, -9, 62, 31, -2, 4], "target": -17}, "output": [4, 9]}, {"input": {"nums": [39, 2, -4, -28, 11], "target": 13}, "output": [1, 4]}, {"input": {"nums": [43, 26, 12, 16, 17, -6], "target": 33}, "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": "b772152932f58dd2"} |
vcr_d0939b42dd56 | 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, 1, 2, 3, 2, 3, 0]}, "output": true}, {"input": {"nums": [0, 1, 3, 0, 2, 2]}, "output": false}, {"input": {"nums": [1, 3, 0, 3, 3, 0, 3, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "17033f93daf70db3"} |
vcr_3d08a77fabcb | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, -13, -6, 1, 9, 31, 42], "target": 36}, "output": [2, 6]}, {"input": {"nums": [-17, -15, -7, -6, 2, 4, 10, 11, 23, 32], "target": 36}, "output": [5, 9]}, {"input": {"nums": [-18, -16, -11, -7, 4, 35], "target": -3}, "output": [3, 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": "e93fae76a098ca5d"} |
vcr_1250f7c7c43a | 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": [-6, 13, -2, -18, 3, 15, -14, -22, 19, 13]}, "output": [-6, 7, 5, -13, -10, 5, -9, -31, -12, 1]}, {"input": {"nums": [-5, 8, -10, 1, 14, -3, 8]}, "output": [-5, 3, -7, -6, 8, 5, 13]}, {"input": {"nums": [-7, 5]}, "output": [-7, -2]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "56ed726352dac137"} |
vcr_c98d25d2f4be | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-26, -25, -21, -18, -14, 5, 26, 31, 36], "target": 22}, "output": [4, 8]}, {"input": {"nums": [-8, -5, -2, 2, 7, 9, 14, 19, 22], "target": 24}, "output": [3, 8]}, {"input": {"nums": [-18, 5, 10, 16, 17], "target": -2}, "output": [0, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "48fda76ff94b5fb8"} |
vcr_36453519af5a | 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": "abaccbaa"}, "output": 6}, {"input": {"s": "cabbaca"}, "output": 6}, {"input": {"s": "acbbbcb"}, "output": 5}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "ce2d6411cec0870e"} |
vcr_4164fd17e5aa | 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": [25, 24, 21, 3, 35, 35, 3, 17, 37, 24, 70, 21, 25, 17, 37]}, "output": 70}, {"input": {"nums": [16, 11, 12, 7, 11, 7, 12, 5, 56, 29, 29, 16, 5]}, "output": 56}, {"input": {"nums": [19, 14, 19, 14, 45]}, "output": 45}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "239e5668404a4c20"} |
vcr_bd568e15a80e | 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": [21, 17, 20, 20, 13, 14, 13, 21, 17, 30, 14, 30, 53]}, "output": 53}, {"input": {"nums": [53, 7, 23, 7, 22, 2, 23, 2, 22]}, "output": 53}, {"input": {"nums": [15, 7, 14, 7, 16, 14, 1, 16, 50, 1, 15]}, "output": 50}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "85a3546eb17092f5"} |
vcr_1aab52a5d021 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [10, 11, 20, 40, 41], "target": 45}, "output": 5}, {"input": {"nums": [0, 7, 10, 14, 17, 28, 31, 33, 37, 38, 39], "target": 29}, "output": 6}, {"input": {"nums": [0, 7, 10, 22, 31], "target": 35}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "ef9cea59a3c860b8"} |
vcr_6be686ad5636 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [11, 3, 26, 25, 12, 26]}, "output": 63}, {"input": {"nums": [6, 9, 3]}, "output": 9}, {"input": {"nums": [1, 2, 2, 20, 6, 11, 10, 12, 5, 0]}, "output": 45}] | 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": "a412ec1cdef1bda4"} |
vcr_9bac37631d32 | 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": [-22, -17, -4, 20, 39, 46], "target": 35}, "output": [2, 4]}, {"input": {"nums": [-20, 10, 18, 26, 27, 32, 34], "target": 59}, "output": [4, 5]}, {"input": {"nums": [-16, -9, -6, -2, 5, 6, 15, 17, 24, 38], "target": -3}, "output": [1, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "6ec824b85cae0f9d"} |
vcr_20a0dd5a7a88 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [33, 64, 3, 33, 1, 3, 36, 1, 31, 31, 36]}, "output": 64}, {"input": {"nums": [7, 42, 7, 17, 17]}, "output": 42}, {"input": {"nums": [3, 9, 53, 5, 3, 9, 5]}, "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": "cd8e67d8ce1d193e"} |
vcr_737ac93632ab | 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, 4, 6, 7, 9, 12, 13]}, "output": [1, 4, 6, 7, 9, 12, 13]}, {"input": {"nums": [1, 7, 11, 11]}, "output": [1, 7, 11]}, {"input": {"nums": [1, 2, 11, 12]}, "output": [1, 2, 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": "83e5c0ebf9db10e0"} |
vcr_33cf0b8c0c4e | 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, 18, 9, 18, 10, 11, 4, 0, 18]}, "output": 65}, {"input": {"nums": [16, 9, 17, 2]}, "output": 33}, {"input": {"nums": [16, 8, 2]}, "output": 18}] | 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": "cc3c5664dfb55724"} |
vcr_f3e2c7dc15be | 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": [26, 8, 21, 30, 24, 20, 27, 7]}, "output": 98}, {"input": {"nums": [2, 13, 18, 6, 4, 0]}, "output": 24}, {"input": {"nums": [8, 13, 18, 20, 7, 17, 13, 14, 13, 17]}, "output": 81}] | 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": "3734b2dc8c57d01a"} |
vcr_082736f99890 | 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, 1, 2, 2, 0, 4, 2, 0, 0]}, "output": true}, {"input": {"nums": [3, 1, 3]}, "output": true}, {"input": {"nums": [0, 1, 0, 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": "68daf6db752a3ef4"} |
vcr_134391f2d0b4 | 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": [29, 66, 24, 12, 27, 17, 21, 17, 24, 36, 36, 29, 6, 6, 21, 12, 27]}, "output": 66}, {"input": {"nums": [29, 3, 5, 14, 47, 3, 5, 29, 14]}, "output": 47}, {"input": {"nums": [41, 9, 9, 24, 3, 3, 24]}, "output": 41}] | 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": "e17828512d301659"} |
vcr_cbbe60eecbc8 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [42, 15, -22, 44, -31, 50, -37], "target": 5}, "output": [0, 6]}, {"input": {"nums": [-20, -27, 12, 4, 40, 16], "target": 20}, "output": [0, 4]}, {"input": {"nums": [27, 43, -4, 34, -28, -22, 48], "target": 61}, "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": "6e5317a04e5c95d5"} |
vcr_8f7f8871ae05 | 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": "abe ea aabccb aea bdeda eea bbbb"}, "output": "bbbb eea bdeda aea aabccb ea abe"}, {"input": {"s": "baab bb bbab"}, "output": "bbab bb baab"}, {"input": {"s": "babb b abb"}, "output": "abb b babb"}] | 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": "6e29df24f367ac4f"} |
vcr_9675a9021185 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 2, 3, 4, 4, 1, 0]}, "output": true}, {"input": {"nums": [1, 3, 1, 0]}, "output": true}, {"input": {"nums": [1, 2, 0, 0, 2, 3, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "eaee3884bae318bf"} |
vcr_6e1e77c4293b | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [78, 2, 30, 7, 11, 0, -16, -28], "target": 108}, "output": [0, 2]}, {"input": {"nums": [-14, 41, -11, 2, 15, 31, 4, 34, 40, 9], "target": -2}, "output": [2, 9]}, {"input": {"nums": [42, 47, 36, -15, -14, 32, 11], "target": 58}, "output": [1, 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": "e4e5bc8f861e407e"} |
vcr_bdfa0449c766 | 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, 10, 20, 21, 26, 31, 44, 47], "target": 2}, "output": 0}, {"input": {"nums": [1, 2, 10, 11, 14, 15, 18, 19, 26, 31, 39], "target": 0}, "output": 0}, {"input": {"nums": [1, 2, 5, 7, 21, 22, 24, 30, 36], "target": 10}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "4d31ed43a3d10393"} |
vcr_f0d0ecaf0d9c | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-8, 17, 10, -19, -10, -15, 17, 3, 0, 18, -17], "k": 11}, "output": [-8, 17, 10, -19, -10, -15, 17, 3, 0, 18, -17]}, {"input": {"nums": [-5, 5, 8, -5, 9, -8], "k": 20}, "output": [9, -8, -5, 5, 8, -5]}, {"input": {"nums": [-8, 3, 5, 9], "k": 20}, "output": [-8, 3, 5, 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": "0e9a19a499b3442d"} |
vcr_c959d53b0ff0 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [3, 5, 7, 6, 5, 1, 2]}, "output": [2100, 1260, 900, 1050, 1260, 6300, 3150]}, {"input": {"nums": [1, 1, 2, 2, 1]}, "output": [4, 4, 2, 2, 4]}, {"input": {"nums": [3, 1, 4]}, "output": [4, 12, 3]}] | 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": "9f1e49a0c817f913"} |
vcr_5ee2cb97de50 | 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": [-20, -9, 2, -2, 28, 29, 14, 4, 24, 22, 28]}, "output": [-20, -29, -27, -29, -1, 28, 42, 46, 70, 92, 120]}, {"input": {"nums": [2, -8, -10, -10, 11]}, "output": [2, -6, -16, -26, -15]}, {"input": {"nums": [-1, 7, 5, 0, -7]}, "output": [-1, 6, 11, 11, 4]}] | 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": "e026355577585637"} |
vcr_0f2c15a973ce | 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, 1, 1, 6, 6, 7, 8, 8, 8, 8, 9, 11, 11]}, "output": [1, 6, 7, 8, 9, 11]}, {"input": {"nums": [0, 3, 6, 6, 7, 7, 10, 11]}, "output": [0, 3, 6, 7, 10, 11]}, {"input": {"nums": [3, 4, 4, 5, 7, 11]}, "output": [3, 4, 5, 7, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "65b406a7b4da7e58"} |
vcr_fc067a73d930 | 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": "aaabaaababaac"}, "output": 8}, {"input": {"s": "abbbca"}, "output": 4}, {"input": {"s": "caacaac"}, "output": 5}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "3981deb7520e3b8e"} |
vcr_fda81969ff19 | 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": [-18, 19, 11, 7, 16, 7, -9, -9, -2, -18]}, "output": 60}, {"input": {"nums": [-8, 2, -2, -7]}, "output": 2}, {"input": {"nums": [2, 4, 7, -5, -3, 10, 8, -9]}, "output": 23}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "d674cc66b71ecb2b"} |
vcr_f1e57f7bc307 | 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": 177, "b": 62}, "output": 1}, {"input": {"a": 62, "b": 80}, "output": 2}, {"input": {"a": 122, "b": 121}, "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": "0edf780634e9fd75"} |
vcr_29de023722fd | 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": [39, 3, 55, 19, 20, 52, 29, 16, 47, 59, 34, 33, 44, 52, 15, 12]}, "output": 56}, {"input": {"prices": [25, 22, 22, 4, 32, 11, 8]}, "output": 28}, {"input": {"prices": [3, 28, 19, 13, 10, 27, 18]}, "output": 25}] | 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": "39d318254f0c3d35"} |
vcr_a092af75ec33 | 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": [53, 59, 45, 54, 37]}, "output": 9}, {"input": {"prices": [6, 33, 10, 20, 4, 20, 21, 27]}, "output": 27}, {"input": {"prices": [32, 23, 22]}, "output": 0}] | 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": "90033782ab248170"} |
vcr_f98c47ac9b9f | 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": "aba bca ecb baceed"}, "output": "baceed ecb bca aba"}, {"input": {"s": "ba b"}, "output": "b ba"}, {"input": {"s": "aaa bba"}, "output": "bba aaa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "e79d78b65bf6ff7b"} |
vcr_6a0eccf6a574 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [9, 12, 19, 17], "k": 1}, "output": [17, 9, 12, 19]}, {"input": {"nums": [3, -7, -8, 4, 0, 10, -2], "k": 19}, "output": [-8, 4, 0, 10, -2, 3, -7]}, {"input": {"nums": [6, -2, 1, 3, -1], "k": 19}, "output": [-2, 1, 3, -1, 6]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "b3efa2f68b0913db"} |
vcr_2d5b8591f6c8 | 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": [31, 36, 25, 18, 57, 42, 7, 23]}, "output": 39}, {"input": {"prices": [26, 34, 10, 10, 29, 4]}, "output": 19}, {"input": {"prices": [16, 7, 26, 36, 37, 12, 39, 8, 14, 4, 36]}, "output": 32}] | 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": "bef158a417090284"} |
vcr_76412c81e9a5 | 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": 27730}, "output": 7}, {"input": {"n": 15804}, "output": 10}, {"input": {"n": 1889}, "output": 6}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "8efb13ab1d053e05"} |
vcr_3de35269ea46 | 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": "aaaabbbbcabaa"}, "output": 6}, {"input": {"s": "cbbbccaaca"}, "output": 6}, {"input": {"s": "bababcccca"}, "output": 7}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "ae132788e9a763ee"} |
vcr_40723ab4df90 | 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": [-15, -10, 26, 40], "target": -25}, "output": [0, 1]}, {"input": {"nums": [2, 37, 39, 32, -11, 30, 38, -29, 9, -3], "target": 26}, "output": [1, 4]}, {"input": {"nums": [12, 29, -14, -27, -20, -9, -29, 37, 40], "target": -15}, "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": "6f5809040e1d1706"} |
vcr_fad6ec395655 | 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": "cdbddadddbcb"}, "output": false}, {"input": {"s": "caa"}, "output": false}, {"input": {"s": "bacac"}, "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": "00ac160599b8cf28"} |
vcr_b4a288e05472 | 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": 55320}, "output": 6}, {"input": {"n": 11976}, "output": 7}, {"input": {"n": 422}, "output": 5}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "58bd492d1b5a616b"} |
vcr_6f7952f153d7 | 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": ["cc", "ccaa", "ba", "bbbb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["abb", "aab", "aaa", "aab", "baa", "bab", "abb"]}, "output": [1, 3, 3]}, {"input": {"strs": ["aab", "abb", "aba", "aba"]}, "output": [1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "4a799ca5c8f308d2"} |
vcr_4c6de063f231 | 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": "c edcd bcad ee ddeea"}, "output": "ddeea ee bcad edcd c"}, {"input": {"s": "abb b aba ba"}, "output": "ba aba b abb"}, {"input": {"s": "bb b bbab bbbb"}, "output": "bbbb bbab b bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "fd6c2efa37839579"} |
vcr_4d40535f1cfd | 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": 31, "b": 183}, "output": 1}, {"input": {"a": 15, "b": 14}, "output": 1}, {"input": {"a": 120, "b": 153}, "output": 3}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "b6fa022b8a61cb0c"} |
vcr_65d8b8af9e67 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 0, 0, 4, 0, 3, 2, 0]}, "output": false}, {"input": {"nums": [1, 3, 2]}, "output": true}, {"input": {"nums": [0, 2, 3, 2, 1, 1]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "cabcbd8557a33db1"} |
vcr_49aec782d598 | 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": [-24, -20, -11, 2, 10, 17, 20, 22, 31, 32, 39, 44], "target": -2}, "output": [0, 7]}, {"input": {"nums": [-17, -16, 3, 20, 22, 23, 26, 35, 36], "target": 55}, "output": [3, 7]}, {"input": {"nums": [-15, -9, -4, 15, 32, 35], "target": 50}, "output": [3, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "9cf8cf35ec5c5ccb"} |
vcr_1560bbf151a7 | 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, 6, 6, 6, 9, 10, 12]}, "output": [0, 6, 9, 10, 12]}, {"input": {"nums": [1, 4, 4, 5, 7, 7, 9, 10, 12, 12]}, "output": [1, 4, 5, 7, 9, 10, 12]}, {"input": {"nums": [4, 7, 7, 11, 12]}, "output": [4, 7, 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": "7d51e9e208cfb0a8"} |
vcr_256be3c27e43 | 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, 0, 2, 2, 3, 4, 4, 7, 10, 10, 11, 14, 15]}, "output": [0, 2, 3, 4, 7, 10, 11, 14, 15]}, {"input": {"nums": [1, 4, 4, 4, 5, 7, 7, 9, 11, 11]}, "output": [1, 4, 5, 7, 9, 11]}, {"input": {"nums": [9, 11, 11, 12]}, "output": [9, 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": "f89538cd87ccf931"} |
vcr_99650e92ca4c | 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": [-6, -3, 1, 2, 8, 26, 35], "target": 43}, "output": [4, 6]}, {"input": {"nums": [-13, 1, 12, 25, 27], "target": 39}, "output": [2, 4]}, {"input": {"nums": [-17, -16, -11, -3, -1, 3, 6, 12, 23, 32, 37], "target": 20}, "output": [0, 10]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "8544ef3d6f0e3ce8"} |
vcr_699bdbd3fc0d | 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": ["accb", "baca", "abb", "cca", "bcb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "baa", "abb", "abb", "aaa"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aab", "bab", "bbb", "bba", "baa", "bbb", "bab"]}, "output": [2, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "d3def11ab95beeb5"} |
vcr_0a629a761c60 | 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": "eebdc d eeaad cdc cecde c"}, "output": "c cecde cdc eeaad d eebdc"}, {"input": {"s": "baa a"}, "output": "a baa"}, {"input": {"s": "bba ba"}, "output": "ba bba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "1e9b766dce1d0cd6"} |
vcr_893fa365e49e | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [5, 3, 5, 20, 25, 14, 30, 10, 3, 11, 28]}, "output": 96}, {"input": {"nums": [9, 8, 3, 6, 13, 4, 12, 3, 4, 2]}, "output": 41}, {"input": {"nums": [14, 1]}, "output": 14}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "c9738c2ea0efc6b4"} |
vcr_2117524ec791 | 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": [48, 38, 52, 20, 54, 10, 23]}, "output": 34}, {"input": {"prices": [38, 12, 3, 8, 13, 24, 22, 19, 19, 20, 29, 7]}, "output": 26}, {"input": {"prices": [10, 11, 9, 37, 2, 23]}, "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": "f90d8d1aa3307a4e"} |
vcr_507844683086 | 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": ["aab", "aca", "ab", "ac", "ab", "bc", "bcb", "bbbb", "cb"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["aab", "aab", "abb", "aab", "bab"]}, "output": [2, 3]}, {"input": {"strs": ["bba", "aab", "aab"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "016610dece4e29f7"} |
vcr_8b15f49c7764 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [18, 18, 13, -2, 13, -14, -10, -2, 4], "k": 26}, "output": [18, 13, -2, 13, -14, -10, -2, 4, 18]}, {"input": {"nums": [4, 4, 4, 3, -1], "k": 15}, "output": [4, 4, 4, 3, -1]}, {"input": {"nums": [-2, 5, 8, 6, -5, 5, -1], "k": 11}, "output": [6, -5, 5, -1, -2, 5, 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": "ce4e9d4f720fa229"} |
vcr_7c08f366bb5f | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [22, 10, 11, 24, 9, 20, 28, 2, 20, 5, 21]}, "output": 115}, {"input": {"nums": [6, 0, 17, 20, 18, 5, 6, 14, 5, 9]}, "output": 64}, {"input": {"nums": [0, 15, 7, 3, 11]}, "output": 26}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "70b2ade7a964fc6e"} |
vcr_14a74c2306aa | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["cca", "aa", "bba", "abbc", "aa", "baa", "aa"]}, "output": [1, 1, 1, 1, 3]}, {"input": {"strs": ["aaa", "bab", "aab", "abb", "aab", "aba"]}, "output": [1, 2, 3]}, {"input": {"strs": ["aaa", "bbb", "abb", "aab", "baa"]}, "output": [1, 1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "7b70f14b18045afa"} |
vcr_4767d85f83bd | 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": [48, 12, 24, 9, 3, 24, 29, 43]}, "output": 40}, {"input": {"prices": [38, 38, 25, 14, 30, 40]}, "output": 26}, {"input": {"prices": [38, 10, 15, 2, 21, 39, 40, 3]}, "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": "6045799142186ddb"} |
vcr_211fa60a4bc8 | 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, 32, 10, 11, 29, 20, 32, 74, 29, 10, 11, 38, 38]}, "output": 74}, {"input": {"nums": [17, 28, 3, 3, 28, 19, 19, 52, 17]}, "output": 52}, {"input": {"nums": [19, 19, 3, 16, 12, 3, 16, 48, 12]}, "output": 48}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "121c1bc806a70597"} |
vcr_9531d1fe9ae7 | 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": [-20, -22, 16, 0, 25, -9, 24, 8, 9, -10, 30, 26, -18]}, "output": [-20, -42, -26, -26, -1, -10, 14, 22, 31, 21, 51, 77, 59]}, {"input": {"nums": [-10, -3, 14, 14, 9, -5, 10]}, "output": [-10, -13, 1, 15, 24, 19, 29]}, {"input": {"nums": [9, 17, 9, 2, 1, -7, -8]}, "output": [9, 26, 35, 37, 38, 31, 23... | 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": "032e21027f1e8a52"} |
vcr_334ecc23e4ce | 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": [27, 23, 28, 7, 5, 7, 21]}, "output": 81}, {"input": {"nums": [12, 14, 20, 8, 17, 8, 11, 17]}, "output": 66}, {"input": {"nums": [9, 8, 3, 5, 18]}, "output": 30}] | 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": "7926909f9c253d21"} |
vcr_c10242fe1e23 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 14, 20, 24, 25, 36, 38, 49], "target": 38}, "output": 6}, {"input": {"nums": [2, 5, 10, 15, 23, 24, 26, 35, 36], "target": 3}, "output": 1}, {"input": {"nums": [1, 2, 4, 6, 10, 12, 18, 20, 21, 24, 30, 36], "target": 5}, "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": "73d1af0de62eded4"} |
vcr_9fa31309616c | 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, -18, 6, 0, 14, -13, 3, 6, 4, -14, -13, -16, -10]}, "output": 20}, {"input": {"nums": [12, -8, -7, 13, 3, -4]}, "output": 16}, {"input": {"nums": [4, 11, 9, 11, 1]}, "output": 36}] | 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": "07d6e4ada1a3702f"} |
vcr_94e20fcb63a0 | 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": [6, 22, 3, 17, 17, 26, 10, 17, 23]}, "output": 88}, {"input": {"nums": [12, 2, 19]}, "output": 31}, {"input": {"nums": [3, 7, 19, 12, 9]}, "output": 31}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "b204ccb30e36a76b"} |
vcr_a647aea24cb8 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [9, -10, 7, 35, -25, -23, -24, 1, -22, 9, 9, 32, 10, 19]}, "output": [9, -1, 6, 41, 16, -7, -31, -30, -52, -43, -34, -2, 8, 27]}, {"input": {"nums": [7, 10, 4, -5, -6]}, "output": [7, 17, 21, 16, 10]}, {"input": {"nums": [17, 18, -9, 15]}, "output": [17, 35, 26, 41]}] | 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": "17b0d21769591a45"} |
vcr_443d121070f8 | 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": 34, "b": 156}, "output": 2}, {"input": {"a": 65, "b": 22}, "output": 1}, {"input": {"a": 68, "b": 56}, "output": 4}] | 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": "6b02f2de45c6f359"} |
vcr_1294ecc13111 | 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": [12, 33, 17, 78, 12, 17, 33]}, "output": 78}, {"input": {"nums": [26, 28, 1, 26, 25, 5, 25, 28, 42, 5, 1]}, "output": 42}, {"input": {"nums": [9, 1, 19, 29, 9, 13, 1, 13, 29, 42, 19]}, "output": 42}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "2ce061ca07c5a695"} |
vcr_3b3600ca4d30 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 2, 2, 3, 5, 6, 10, 11, 11, 11, 12, 14]}, "output": [1, 2, 3, 5, 6, 10, 11, 12, 14]}, {"input": {"nums": [2, 6, 9]}, "output": [2, 6, 9]}, {"input": {"nums": [8, 9, 9, 12]}, "output": [8, 9, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "2d5e82729fda8648"} |
vcr_60709a451fa7 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [4, 1, 4, 1, 5, 5]}, "output": [100, 400, 100, 400, 80, 80]}, {"input": {"nums": [4, 2, 5, 3]}, "output": [30, 60, 24, 40]}, {"input": {"nums": [5, 2, 4, 2, 4, 5]}, "output": [320, 800, 400, 800, 400, 320]}] | 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": "520923993f4bbab0"} |
vcr_7a619dba2c7e | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 16, 23, 45], "target": 41}, "output": 3}, {"input": {"nums": [1, 6, 8, 10, 11, 35], "target": 7}, "output": 2}, {"input": {"nums": [8, 14, 19, 24, 25, 26], "target": 32}, "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": "bd9e68fb8b47fea2"} |
vcr_2f756126451e | 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": [-23, -21, -15, -9, 11, 15, 28, 39, 41, 44, 48], "target": 80}, "output": [7, 8]}, {"input": {"nums": [-14, -12, -4, -2, 2, 14, 15, 20, 30], "target": 45}, "output": [6, 8]}, {"input": {"nums": [-20, -15, -10, -7, 3, 6, 17, 29, 35], "target": -22}, "output": [1, 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": "b73b656e312b8437"} |
vcr_b3a6741ebc8a | 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, -3, -2, 0, 21, 47], "target": 18}, "output": [2, 5]}, {"input": {"nums": [-14, -11, 6, 23, 34, 37, 38, 39], "target": 40}, "output": [2, 4]}, {"input": {"nums": [-17, -15, -8, 8, 13, 19, 30, 34, 38], "target": 43}, "output": [4, 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": "3759be52b435df62"} |
vcr_b0514ccfca75 | 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, -7, 11, 10, -10, 5], "k": 2}, "output": [-10, 5, -13, -7, 11, 10]}, {"input": {"nums": [-6, 5, -10, -7, -4, 1, -9, 3, -7], "k": 5}, "output": [-4, 1, -9, 3, -7, -6, 5, -10, -7]}, {"input": {"nums": [-8, -2, -2, -6, 8, 9, -3, -8], "k": 3}, "output": [9, -3, -8, -8, -2, -2, -6, 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": "c94a549966ee6d85"} |
vcr_5fbba2002e40 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [25, 2, 19, 41, 44, 43, 48, 43, 21, 17, 18, 31, 30]}, "output": true}, {"input": {"nums": [4, 7, 15, 24, 22, 8, 2, 2, 6, 10, 15]}, "output": true}, {"input": {"nums": [13, 15, 6, 13, 24, 7, 15, 6, 19, 19, 25, 18]}, "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": "7d1e94fdb43845a5"} |
vcr_b1f5a2366599 | 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, 0, 15, 3, -9, 4, -17, 6, -4, 2], "k": 4}, "output": [-17, 6, -4, 2, -11, 0, 15, 3, -9, 4]}, {"input": {"nums": [-6, 1, 10, -4, 9, -6, 4, 9], "k": 8}, "output": [-6, 1, 10, -4, 9, -6, 4, 9]}, {"input": {"nums": [-5, -3, -9], "k": 13}, "output": [-9, -5, -3]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "cb02996eca340d67"} |
vcr_94e97ba9b7f1 | 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": 28982}, "output": 8}, {"input": {"n": 4549}, "output": 6}, {"input": {"n": 2793}, "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": "815c8b02710dfd04"} |
vcr_e4f4ed132a65 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 1, 4, 7, 8, 8, 12, 15, 15]}, "output": [0, 1, 4, 7, 8, 12, 15]}, {"input": {"nums": [0, 3, 4, 12]}, "output": [0, 3, 4, 12]}, {"input": {"nums": [1, 3, 3, 8, 9, 10, 12]}, "output": [1, 3, 8, 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": "d8a0a3ca31b007c9"} |
vcr_f813462d24e1 | 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": "adb cbea bdc bac"}, "output": "bac bdc cbea adb"}, {"input": {"s": "a aabb baaa a"}, "output": "a baaa aabb a"}, {"input": {"s": "baba bbba bab bbb ba"}, "output": "ba bbb bab bbba baba"}] | 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": "b8c3df85c65c0db6"} |
vcr_96b89df6f838 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [7, 20, 18]}, "output": 25}, {"input": {"nums": [8, 20, 4, 15]}, "output": 35}, {"input": {"nums": [2, 18, 3, 13, 11, 7]}, "output": 38}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "24b7800d8489c307"} |
vcr_b5778a146a86 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [20, 2, 9, 28]}, "output": 48}, {"input": {"nums": [0, 8, 18, 16, 2, 3, 13, 18]}, "output": 45}, {"input": {"nums": [19, 9, 18, 6]}, "output": 37}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "2e16c43b78cae673"} |
vcr_5a0d7fa41f2f | 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, 18, -11, 2, -8, -1, 8, 4]}, "output": 18}, {"input": {"nums": [9, 6, 13, 7, 3, -4, 3, -10, -9, -1]}, "output": 38}, {"input": {"nums": [-1, -6, 1, -12, -10]}, "output": 1}] | 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": "43c8d587b039c1eb"} |
vcr_1eccab795b5d | hard | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "bdacbadcaacddc"}, "output": false}, {"input": {"s": "bbbccbb"}, "output": false}, {"input": {"s": "ccbbbbaa"}, "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": "72ddc8a7ed8ce8cd"} |
vcr_88f6918e579f | 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": [144, 74, 155, 62, 98, 116, 17, 199, 96]}, "output": false}, {"input": {"nums": [13, 12, 23, 3, 16, 18, 16, 1]}, "output": true}, {"input": {"nums": [22, 17, 1]}, "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": "c109afc104b594fd"} |
vcr_e3756e362e2b | 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": 25, "b": 196}, "output": 1}, {"input": {"a": 95, "b": 91}, "output": 1}, {"input": {"a": 136, "b": 84}, "output": 4}] | 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": "a4703cdeb087b1ac"} |
vcr_55c23f37b067 | 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": 38424}, "output": 6}, {"input": {"n": 103}, "output": 5}, {"input": {"n": 147}, "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": "ef0797ed9cad18c0"} |
vcr_7691a25ea446 | hard | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "cdcdbbabbdcdc"}, "output": true}, {"input": {"s": "abacccbbb"}, "output": false}, {"input": {"s": "bbaa"}, "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": "7209124cee656611"} |
vcr_2c67afef7294 | 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": [-1, 36, 69, -21, 0, 22, 40, 39, -17, 47], "target": 52}, "output": [2, 8]}, {"input": {"nums": [9, -15, 25, -21, -23, 27, 28, -7, 29, -10], "target": 19}, "output": [8, 9]}, {"input": {"nums": [20, -30, -1, 45, -11, -25, 38, -2], "target": -55}, "output": [1, 5]}] | 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": "70c5fe3aa06eb6fd"} |
vcr_fe91bc787b26 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [3, 23, 1, 27, 50, 12, 21, 36, 36, 40, 6, 43]}, "output": true}, {"input": {"nums": [24, 17, 0]}, "output": false}, {"input": {"nums": [20, 16, 0, 13, 22, 9, 1, 24, 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": "8995c9efb248be5f"} |
vcr_e5f483adcbaf | 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": "dd cbeb ddaaa c debce e"}, "output": "e debce c ddaaa cbeb dd"}, {"input": {"s": "a aba ba a"}, "output": "a ba aba a"}, {"input": {"s": "aab aaaa"}, "output": "aaaa aab"}] | 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": "081db753c127de94"} |
vcr_6b1190c946a2 | 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": [53, 57, 72, 64, 32, -2, 78], "target": 51}, "output": [0, 5]}, {"input": {"nums": [30, -22, 7, -8, 14, 29, -16, 22, -25], "target": 43}, "output": [4, 5]}, {"input": {"nums": [-23, 1, -30, 48, -25, 49, -6, 27, 8], "target": -17}, "output": [4, 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": "e8366e64b0b90bb2"} |
vcr_5a524f110a8f | 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": ["cc", "cbab", "cb", "cc", "bb", "bcc", "caca", "caaa"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "baa", "aba", "baa", "bbb", "aaa"]}, "output": [1, 1, 4]}, {"input": {"strs": ["baa", "abb", "bba", "aba", "aab"]}, "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": "844cc235276d2804"} |
vcr_5c608887d4fd | 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": "bedbdb dbd ad"}, "output": "ad dbd bedbdb"}, {"input": {"s": "baa a bbaa"}, "output": "bbaa a baa"}, {"input": {"s": "aaba bba baa"}, "output": "baa bba aaba"}] | 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": "cca7a87d2e4e47f0"} |
vcr_5ee71eb1f8d4 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 1, 2, 2, 2, 6, 6, 11, 12, 15, 15, 15]}, "output": [0, 1, 2, 6, 11, 12, 15]}, {"input": {"nums": [0, 4, 10]}, "output": [0, 4, 10]}, {"input": {"nums": [2, 2, 2, 4, 4, 6, 7, 8, 8, 9, 9]}, "output": [2, 4, 6, 7, 8, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "aeedc7507a38785b"} |
vcr_ca43a3d1f6a5 | 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": 239, "b": 217}, "output": 1}, {"input": {"a": 134, "b": 10}, "output": 2}, {"input": {"a": 171, "b": 98}, "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": "21c9eae93f2c5f47"} |
vcr_51566faa0f12 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 11, 14, 16, 25, 30, 43], "target": 35}, "output": 6}, {"input": {"nums": [2, 6, 27, 28, 35], "target": 16}, "output": 2}, {"input": {"nums": [14, 22, 27, 32, 33], "target": 30}, "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": "6fee6d34297ddf74"} |
vcr_4d7bb57793da | 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": [35, 7, 20, 26, 35]}, "output": true}, {"input": {"nums": [24, 21, 10, 21, 22, 25, 21, 19, 23, 11]}, "output": true}, {"input": {"nums": [8, 25, 9, 9, 18, 19, 16, 23]}, "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": "2f57b95f79f9402d"} |
vcr_88b40a2889a5 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-17, -18, -13, -6, -12, 7, 13, -8, 16], "k": 14}, "output": [-12, 7, 13, -8, 16, -17, -18, -13, -6]}, {"input": {"nums": [7, -8, 0], "k": 16}, "output": [0, 7, -8]}, {"input": {"nums": [-5, -3, 1, -4, 7, 1, 6, 7, -2], "k": 13}, "output": [1, 6, 7, -2, -5, -3, 1, -4, 7]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "99aab99d0404867a"} |
vcr_25259553e20f | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 8, 12, 19, 25, 31, 33, 36], "target": 19}, "output": 3}, {"input": {"nums": [3, 6, 9, 20, 26, 28, 37], "target": 36}, "output": 6}, {"input": {"nums": [1, 14, 16, 17, 21, 25, 26, 29, 30], "target": 17}, "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": "87d2d4aad2e77c47"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.