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_cdafd0c99b79 | 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": 200, "b": 232}, "output": 8}, {"input": {"a": 66, "b": 101}, "output": 1}, {"input": {"a": 99, "b": 80}, "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": "76db23afa20420d4"} |
vcr_99076ec2569b | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 8, 9, 12, 19, 23, 25, 37, 38, 40, 47, 48, 49], "target": 11}, "output": 3}, {"input": {"nums": [6, 15, 17, 19, 22, 26, 30, 36], "target": 33}, "output": 7}, {"input": {"nums": [7, 11, 17, 18, 21, 24, 27, 33, 36], "target": 15}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "728055d014583694"} |
vcr_162018cebedb | 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, 1, 7, 14, 2, 29]}, "output": 65}, {"input": {"nums": [1, 2, 17, 16, 19, 19]}, "output": 37}, {"input": {"nums": [18, 7, 10, 3, 14, 16]}, "output": 44}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "ca9a1542fb0faad1"} |
vcr_deb5a85fd2e9 | 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": [100, 120, 137, 1, 0, 9, 131, 158, 56]}, "output": false}, {"input": {"nums": [18, 18, 21]}, "output": true}, {"input": {"nums": [16, 6, 20, 10, 1, 2, 21, 24, 20]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "5032bc1556d434fd"} |
vcr_d1999c43984b | 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": [40, 16, 13, 2, 51, 5, 2, 15, 29, 9, 7, 18]}, "output": 49}, {"input": {"prices": [34, 35, 29, 11, 7, 40]}, "output": 33}, {"input": {"prices": [30, 34, 36, 4, 21, 27, 33, 3, 32]}, "output": 29}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "13b85761b83503d3"} |
vcr_d02ba405244c | 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": [6, 23, 28, 20, 34, 34, 39, 73, 23, 10, 39, 10, 28, 20, 6]}, "output": 73}, {"input": {"nums": [8, 14, 27, 15, 58, 14, 8, 27, 15, 7, 7]}, "output": 58}, {"input": {"nums": [17, 23, 27, 12, 23, 45, 12, 2, 27, 2, 17]}, "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": "ed6960dc58135163"} |
vcr_b406d28f8a6a | 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": [19, 20, 44, 24, 55, 16, -26, -32, 23, -34, -3, 2, -9, 35], "target": 15}, "output": [3, 12]}, {"input": {"nums": [-7, 32, 27, 17, 21, -2], "target": -9}, "output": [0, 5]}, {"input": {"nums": [-7, 40, 36, -20, 34, 1, 37, 28, 30, -26], "target": 67}, "output": [6, 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": "c2620d46605a06c3"} |
vcr_c27be7641d7d | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [20, -2, -4, 8, -6, 15, -20, -12, -19], "k": 0}, "output": [20, -2, -4, 8, -6, 15, -20, -12, -19]}, {"input": {"nums": [-5, -1, 0, -3], "k": 6}, "output": [0, -3, -5, -1]}, {"input": {"nums": [10, 10, -7, 3, 10, 3, -9], "k": 17}, "output": [10, 3, -9, 10, 10, -7, 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": "8f49d8e5b4c17ef8"} |
vcr_e826e3cd9799 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [23, 1, 26, 18, 34, 48, 46, 60, 20]}, "output": 59}, {"input": {"prices": [23, 39, 10, 8, 40, 31, 26]}, "output": 32}, {"input": {"prices": [14, 32, 5, 16, 14, 33]}, "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": "7a3b4b65003883fd"} |
vcr_515f54db67fe | 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": ["aaac", "bb", "abbb", "aabb", "aac", "abbb", "aacb", "cac", "bacc"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "bab", "bbb"]}, "output": [1, 1, 1]}, {"input": {"strs": ["baa", "aab", "aba", "abb"]}, "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": "f3e8a1809d244dbd"} |
vcr_cfb0c375e092 | 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": "babbabbbbaab"}, "output": 7}, {"input": {"s": "ccbcaacb"}, "output": 6}, {"input": {"s": "ccabbaabbca"}, "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": "1e8dcad6537add60"} |
vcr_5e0915cb82fd | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-16, 30, 15, -7, 31, 0, 14, -22, 20, 18, -14, -22]}, "output": [-16, 14, 29, 22, 53, 53, 67, 45, 65, 83, 69, 47]}, {"input": {"nums": [-8, 0, 6, -4, 0, -3, 17]}, "output": [-8, -8, -2, -6, -6, -9, 8]}, {"input": {"nums": [5, 4, 4]}, "output": [5, 9, 13]}] | 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": "68c844653273af02"} |
vcr_1b3e955f6855 | 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": [7, 11, 17, 19, 24, 25, 42, 45], "target": 13}, "output": 2}, {"input": {"nums": [4, 8, 18, 19, 29, 30], "target": 1}, "output": 0}, {"input": {"nums": [0, 5, 21, 24, 29, 35, 37], "target": 21}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "d55c08e5eb4e04b5"} |
vcr_135d417a61d9 | 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, 3, 3, 2, 4, 2, 1, 0, 0]}, "output": true}, {"input": {"nums": [1, 1, 2]}, "output": true}, {"input": {"nums": [3, 1, 3, 1, 2, 2, 3, 3]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "db817b831500692a"} |
vcr_e096bf0bef35 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 6, 7, 7, 8, 8, 9, 10, 11, 11, 12, 12, 12]}, "output": [2, 6, 7, 8, 9, 10, 11, 12]}, {"input": {"nums": [1, 2, 2, 7, 7, 8, 9, 10]}, "output": [1, 2, 7, 8, 9, 10]}, {"input": {"nums": [1, 5, 6, 6, 7]}, "output": [1, 5, 6, 7]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "9ec5d1d9126fb2ad"} |
vcr_6fe4808d13e6 | 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": [28, 16, 16, 12, 25, 30, 27, 3, 28, 15]}, "output": 124}, {"input": {"nums": [10, 7, 13, 5, 11, 20, 12]}, "output": 46}, {"input": {"nums": [6, 2, 8, 13]}, "output": 19}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "12ab2798e7511903"} |
vcr_8669301a82f5 | 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": [59, 25, 6, 25, 15, 38, 38, 6, 15, 28, 28, 21, 24, 24, 21]}, "output": 59}, {"input": {"nums": [2, 53, 10, 10, 2]}, "output": 53}, {"input": {"nums": [19, 10, 48, 10, 19, 22, 22]}, "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": "8226100412cd4b0b"} |
vcr_f3f22fa22dbd | 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": "caabbabcabcaba"}, "output": 12}, {"input": {"s": "acabbabbbcc"}, "output": 7}, {"input": {"s": "cbccbaab"}, "output": 6}] | 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": "69e00b1d7b8c1794"} |
vcr_ab54a9436023 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 1, 1, 2, 2, 2, 4, 0]}, "output": true}, {"input": {"nums": [3, 3, 1, 3, 1, 0, 2]}, "output": true}, {"input": {"nums": [1, 1, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "936ad3f8266141a1"} |
vcr_4fce6c71e935 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [20, 20, 38, 38, 59, 37, 37, 23, 23]}, "output": 59}, {"input": {"nums": [2, 13, 17, 20, 20, 51, 24, 26, 24, 17, 26, 13, 2]}, "output": 51}, {"input": {"nums": [16, 28, 28, 47, 16]}, "output": 47}] | 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": "0a999db308feaae8"} |
vcr_b6729e575899 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [2, 7, 2, 0, 6]}, "output": [0, 0, 0, 168, 0]}, {"input": {"nums": [1, 2, 4, 1, 3, 1, 3]}, "output": [72, 36, 18, 72, 24, 72, 24]}, {"input": {"nums": [1, 2, 4, 5, 4]}, "output": [160, 80, 40, 32, 40]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "48fd9bb77ddaafa4"} |
vcr_96e5a2535a91 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 3, 29, 32, 33, 35, 36, 38, 46], "target": 5}, "output": 2}, {"input": {"nums": [0, 1, 2, 11, 13, 23, 24, 35, 36, 37, 38], "target": 25}, "output": 7}, {"input": {"nums": [13, 17, 20, 28, 34, 37], "target": 3}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "860fed85f6e71a2a"} |
vcr_b6621f87c050 | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "aadba"}, "output": false}, {"input": {"s": "ccb"}, "output": false}, {"input": {"s": "aba"}, "output": true}] | 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": "c29f7aa9cb3f2775"} |
vcr_a2cfb112cc88 | 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, 9, 13, 17, 24, 26, 34, 36, 39, 41, 44, 47, 49], "target": 9}, "output": 1}, {"input": {"nums": [12, 13, 24, 27, 35], "target": 21}, "output": 2}, {"input": {"nums": [0, 4, 23, 26, 27, 29], "target": 6}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "27ad6507c36d73b0"} |
vcr_d49af1ba1fcd | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["cab", "bbb", "cb", "ab", "bcaa"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bab", "aab", "abb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "aaa", "bbb", "aab", "baa"]}, "output": [1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "8dc6f119a80e0669"} |
vcr_f0b9f414d7c3 | 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": "b ec aaea"}, "output": "aaea ec b"}, {"input": {"s": "b aaa abb b"}, "output": "b abb aaa b"}, {"input": {"s": "a bbbb bb aabb babb"}, "output": "babb aabb bb bbbb a"}] | 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": "9f2e04e7f5c15619"} |
vcr_8112911a54a7 | 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": "acca aaedec"}, "output": "aaedec acca"}, {"input": {"s": "ba aaa a aab"}, "output": "aab a aaa ba"}, {"input": {"s": "aab baa ab aaa aaba"}, "output": "aaba aaa ab baa 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": "748e45079d67c697"} |
vcr_58e913890829 | 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": [10, 25, 9, 26, 17, 22, 15, 12]}, "output": 85}, {"input": {"nums": [4, 0, 20, 8, 2, 3, 17, 0]}, "output": 43}, {"input": {"nums": [1, 19, 14, 11]}, "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": "e10be29bee83dc96"} |
vcr_5bef35e7e4ef | 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": [9, 12, 19, 47], "target": 40}, "output": 3}, {"input": {"nums": [7, 11, 12, 17, 29, 30, 34], "target": 6}, "output": 0}, {"input": {"nums": [4, 6, 14, 15, 17, 19, 24, 27, 39], "target": 17}, "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": "de7092d472dbbe27"} |
vcr_4459c20463d1 | 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": [20, -9, 7, -6, -12, 6, -3, 11, -15, 5, 15, -2, 16]}, "output": 34}, {"input": {"nums": [-10, -7, -4, 2]}, "output": 2}, {"input": {"nums": [0, 0, 8, 7, 2, 7, -7, -2, 2]}, "output": 24}] | 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": "f95536323c1ab58e"} |
vcr_802168b28aa9 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [14, 8, 7, 1]}, "output": 21}, {"input": {"nums": [6, 11, 17, 9, 11, 5, 7]}, "output": 41}, {"input": {"nums": [2, 11, 4]}, "output": 11}] | 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": "fca367ae358d3aaa"} |
vcr_1df6763fca2e | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [11, 15, 21, 22, 26, 28, 39, 43, 44, 45, 46], "target": 33}, "output": 6}, {"input": {"nums": [6, 9, 13, 14, 23, 27, 30, 31, 34, 37], "target": 35}, "output": 9}, {"input": {"nums": [0, 2, 4, 13, 18, 19, 20, 30, 36, 37], "target": 30}, "output": 7}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "9b5b7ad6ce8c64a3"} |
vcr_5a99e169001f | 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": [35, 4, 28, 37, 50, 24, 9, 24, 29, 19, 27, 37]}, "output": 46}, {"input": {"prices": [8, 38, 39]}, "output": 31}, {"input": {"prices": [7, 20, 8, 4]}, "output": 13}] | 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": "f4b22d86b073b9f2"} |
vcr_5e1c6eb8fba9 | 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": [8, 11, 17, 21, 23, 27, 32, 35, 41, 47], "target": 25}, "output": 5}, {"input": {"nums": [1, 2, 6, 11, 17, 21, 24, 26, 27, 30, 38], "target": 7}, "output": 3}, {"input": {"nums": [0, 18, 19, 30], "target": 26}, "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": "94a8a7433362897a"} |
vcr_592c8d919195 | 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": [21, 25, 30, 30, 10]}, "output": 61}, {"input": {"nums": [2, 8]}, "output": 8}, {"input": {"nums": [19, 14, 17, 2, 7, 1, 3, 10]}, "output": 53}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "b4a24c7d31db7b25"} |
vcr_a9246187e882 | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "aaabddacbbdacddb"}, "output": false}, {"input": {"s": "bccabbcc"}, "output": false}, {"input": {"s": "ccc"}, "output": true}] | 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": "7f7d200443654b48"} |
vcr_a9623ef06b4f | 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, 10, 11, 15, 17, 31, 35, 36, 39, 40, 42], "target": 16}, "output": 4}, {"input": {"nums": [6, 13, 20, 21, 34], "target": 28}, "output": 4}, {"input": {"nums": [5, 7, 11, 14, 16, 18, 19, 21, 37, 38], "target": 39}, "output": 10}] | 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": "b5096e0439d42609"} |
vcr_cd0e0fc760b0 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-16, -20, -8, -22, 10, 7, 12, -6, -1, 15, -1, 2]}, "output": [-16, -36, -44, -66, -56, -49, -37, -43, -44, -29, -30, -28]}, {"input": {"nums": [-4, 4, 17, 6, -4, -3, 1, 1, -9]}, "output": [-4, 0, 17, 23, 19, 16, 17, 18, 9]}, {"input": {"nums": [11, -6, 10]}, "output": [11, 5, 15]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "c24f2daf3a6135d0"} |
vcr_26ee1f87d032 | 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, 7, 23, 15, 18, 29]}, "output": [-9, -2, 21, 36, 54, 83]}, {"input": {"nums": [1, 15, 18, 13, 10, -6, 0, 14, 4, -1]}, "output": [1, 16, 34, 47, 57, 51, 51, 65, 69, 68]}, {"input": {"nums": [-3, -6, 3, 5, -1, -12]}, "output": [-3, -9, -6, -1, -2, -14]}] | 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": "93ff0a199c74fddb"} |
vcr_a3c3e3ac0fe4 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-27, -26, -24, -14, -13, -11, -4, 2, 26, 30, 31, 47], "target": 78}, "output": [10, 11]}, {"input": {"nums": [-20, -17, -15, -12, 2, 3, 11, 17, 20, 32], "target": -6}, "output": [1, 6]}, {"input": {"nums": [-18, -12, 12, 15, 16, 18, 20, 23, 33, 37, 38], "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": "14940af56206b7c6"} |
vcr_e75392b143c7 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -27, -25, -17, -4, 23, 25, 29, 39, 41, 46], "target": 21}, "output": [2, 10]}, {"input": {"nums": [-10, -4, 0, 3, 8, 10, 17, 28, 31, 33, 37], "target": 31}, "output": [2, 8]}, {"input": {"nums": [-17, -16, -12, 9, 19, 23, 26, 33], "target": -7}, "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": "7d7f7aee2698adae"} |
vcr_294316dc3f6b | 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": "aaccabcacaa"}, "output": 8}, {"input": {"s": "bac"}, "output": 3}, {"input": {"s": "ccbcaaacba"}, "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": "627500acf2be5ba5"} |
vcr_d4e910b85c43 | 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": ["ca", "baca", "bcb", "aca", "bc", "cbcb", "aba", "cab"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "abb", "bba", "aab", "bba"]}, "output": [1, 1, 3]}, {"input": {"strs": ["bab", "baa", "baa", "abb", "bbb", "baa", "aaa"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "6179d2f99f960a60"} |
vcr_0cb7eae1af0a | 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": "adcecb dedb bbcceb bcec"}, "output": "bcec bbcceb dedb adcecb"}, {"input": {"s": "a bba aaba ab"}, "output": "ab aaba bba a"}, {"input": {"s": "bab abab aaba"}, "output": "aaba abab bab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "ffbed07a7e50ff22"} |
vcr_2bc1cac8585d | 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": 27610}, "output": 10}, {"input": {"n": 11230}, "output": 10}, {"input": {"n": 3140}, "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": "b6b4e0062d5f0838"} |
vcr_6af6fd066dfe | 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": "abbbabcabbb"}, "output": 7}, {"input": {"s": "bcabab"}, "output": 6}, {"input": {"s": "bacca"}, "output": 4}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "b84e010f0179ec99"} |
vcr_90650769dafe | 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": [185, 11, 182, 107, 69, 48, 40, 99, 58, 75, 168, 150, 118, 156, 115, 68, 146]}, "output": false}, {"input": {"nums": [19, 1, 2, 21, 14]}, "output": false}, {"input": {"nums": [3, 4, 14, 15, 13, 24, 16, 16, 10, 7, 5]}, "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": "fd6faefe38b62ff6"} |
vcr_2e4de0977ffe | 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, 16, 2, -15, 20, -17, 17, -12, -19, -12], "k": 7}, "output": [-15, 20, -17, 17, -12, -19, -12, -11, 16, 2]}, {"input": {"nums": [5, -9, 4, 6, 5, 1], "k": 9}, "output": [6, 5, 1, 5, -9, 4]}, {"input": {"nums": [-3, 8, 5, -8, -10, 3, 8], "k": 16}, "output": [3, 8, -3, 8, 5, -8, -10]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "0751e42a20cc3eda"} |
vcr_185544dc773c | 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, 5, 11, 16, 22, 36], "target": 35}, "output": 5}, {"input": {"nums": [9, 18, 22], "target": 24}, "output": 3}, {"input": {"nums": [4, 5, 11, 12, 15, 16, 25, 27, 36, 37], "target": 37}, "output": 9}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "e13b8ff6112ff3ef"} |
vcr_64a347460cf0 | 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": "edaede ca ccbbe ba ebdcdb"}, "output": "ebdcdb ba ccbbe ca edaede"}, {"input": {"s": "aa aaa a abab aa"}, "output": "aa abab a aaa aa"}, {"input": {"s": "ab b baba"}, "output": "baba b ab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "ec8f0b47457d957f"} |
vcr_bbe345df688b | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [14, 2, 11, 16]}, "output": 30}, {"input": {"nums": [19, 17, 15, 9, 10]}, "output": 44}, {"input": {"nums": [20, 17, 18, 2, 5, 14, 2, 14, 8]}, "output": 66}] | 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": "2ce4943c3821492f"} |
vcr_0673dd3a4abf | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [14, 27, 46, 59, 4, 57]}, "output": 53}, {"input": {"prices": [22, 15, 9, 15, 28]}, "output": 19}, {"input": {"prices": [32, 8, 26, 36, 30, 19, 31, 31, 15, 39]}, "output": 31}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "9956d0bc4d650735"} |
vcr_94a9274f2681 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 3, 6, 30, 36, 46], "target": 11}, "output": 3}, {"input": {"nums": [6, 10, 11, 14, 20, 21, 22, 32, 36, 38], "target": 19}, "output": 4}, {"input": {"nums": [10, 12, 19, 22, 24, 32, 36], "target": 1}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "bc1d89ae5f9b5b89"} |
vcr_694bb7420d76 | 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": 120, "b": 215}, "output": 5}, {"input": {"a": 63, "b": 18}, "output": 9}, {"input": {"a": 79, "b": 90}, "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": "f9a9ca74e6b3a1a5"} |
vcr_2edd7b95758b | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [13, -10, 14, -13, -14, 14, 15, -1, 3, -1, 19, -17, 14, 4, -7]}, "output": 50}, {"input": {"nums": [-9, 12, 0, 13]}, "output": 25}, {"input": {"nums": [-6, -1, -1, -7, 5, 10, 12, 13, -4, -8]}, "output": 40}] | 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": "ddff82009ccde4f1"} |
vcr_06fb50efa220 | 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": "daeced aedd ed edeccd ea"}, "output": "ea edeccd ed aedd daeced"}, {"input": {"s": "baa aaba babb aaa aa"}, "output": "aa aaa babb aaba baa"}, {"input": {"s": "baa bbaa ba aba aa"}, "output": "aa aba ba bbaa baa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "78c01b8c708e73fc"} |
vcr_8455a31a2fae | 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": "debda aeed ddd a b aaa ecebdb"}, "output": "ecebdb aaa b a ddd aeed debda"}, {"input": {"s": "baba bb"}, "output": "bb baba"}, {"input": {"s": "aa abaa"}, "output": "abaa aa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "63bd304a0f377009"} |
vcr_dc7bd449b1b7 | 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": [73, 41, 8, 76, -37, -28, 79, 35, -33, 39, 64, 52, 53, 7], "target": 42}, "output": [4, 6]}, {"input": {"nums": [14, 37, 33, -9, 34, 26, 31, 27, -8, 35, -28], "target": 60}, "output": [4, 5]}, {"input": {"nums": [-28, 13, -1, 14, -3, 2, 6, 16], "target": -4}, "output": [2, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "1f1103c164d000de"} |
vcr_ea5e44b16d42 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [15, 12, 16, 0, 3]}, "output": 34}, {"input": {"nums": [2, 12, 1, 2, 3, 12]}, "output": 26}, {"input": {"nums": [6, 2, 0]}, "output": 6}] | 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": "ebaebe14b0a02a7a"} |
vcr_1a3aceb4e526 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 11, 14, 37, 39], "target": 14}, "output": 2}, {"input": {"nums": [5, 6, 11, 13, 16, 17, 20, 24, 26, 31, 33], "target": 9}, "output": 2}, {"input": {"nums": [2, 10, 16, 22, 23, 24, 29, 31, 32, 33, 38], "target": 33}, "output": 9}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "398565cf7765d01d"} |
vcr_4ae299e47add | 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, -14, 7, 22, 34, 39, 48], "target": 34}, "output": [1, 6]}, {"input": {"nums": [-16, -7, -5, 1, 7, 12, 27, 33, 36, 38, 39], "target": 71}, "output": [7, 9]}, {"input": {"nums": [-17, -10, 1, 19, 28, 31, 38, 39], "target": 14}, "output": [0, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "25c9f90920b6678e"} |
vcr_0616c38e0365 | 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": [50, 19, 7, 1, 15, 10, 16, 49, 53, 19, 10, 16, 41, 15, 49]}, "output": 52}, {"input": {"prices": [1, 3, 22, 22]}, "output": 21}, {"input": {"prices": [8, 25, 40, 37, 8, 11, 18, 32, 11]}, "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": "de70aee7ddf6fb69"} |
vcr_be00c29dde50 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [10, 17, -3, 10, 6, 22, 19, 1, 14]}, "output": 96}, {"input": {"nums": [12, -10, 9, -6, -12, -10, 8]}, "output": 12}, {"input": {"nums": [1, 10, -6, -11, -11, 4]}, "output": 11}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "f575d1058dc36ebe"} |
vcr_4fd70e16e1c4 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [19, 18, 18, 32, 16, 19, 32, 58, 16]}, "output": 58}, {"input": {"nums": [16, 1, 48, 10, 1, 20, 22, 16, 7, 22, 7, 10, 20]}, "output": 48}, {"input": {"nums": [10, 4, 4, 12, 12, 18, 18, 10, 56]}, "output": 56}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "bee4065a2282d2d3"} |
vcr_17df890e7265 | 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": [0, 29, 18, 8, 10, 24, 9, 8, 24, 14]}, "output": 85}, {"input": {"nums": [5, 8]}, "output": 8}, {"input": {"nums": [5, 13, 8, 20, 10, 3, 15, 5, 11]}, "output": 59}] | 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": "a9884de1e5ece9fc"} |
vcr_50fda7c12ea6 | 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": [36, 18, 18, 2, 29, 36, 12, 12, 2, 50, 29]}, "output": 50}, {"input": {"nums": [16, 17, 44, 1, 17, 16, 1]}, "output": 44}, {"input": {"nums": [9, 27, 9, 59, 10, 27, 10, 26, 26]}, "output": 59}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "ae3aec10805ce358"} |
vcr_eaef68a5644f | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-7, -6, -5, 27, 29, 35, 42, 48], "target": 36}, "output": [1, 6]}, {"input": {"nums": [-15, -13, 10, 12, 14, 15, 18, 21, 27, 34], "target": 32}, "output": [4, 6]}, {"input": {"nums": [-16, -13, -12, -10, 1, 21, 37, 39], "target": -29}, "output": [0, 1]}] | 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": "44f8d0e02999787c"} |
vcr_d3222a2aad76 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [6, 6, 3, 7, 7, 4, 7, 3]}, "output": [74088, 74088, 148176, 63504, 63504, 111132, 63504, 148176]}, {"input": {"nums": [2, 5, 5, 1]}, "output": [25, 10, 10, 50]}, {"input": {"nums": [2, 2, 1]}, "output": [2, 2, 4]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "dcd5786c3e531bf2"} |
vcr_7f15fe4491db | 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": [33, 20, 6, 6, 35]}, "output": [33, 53, 59, 65, 100]}, {"input": {"nums": [15, 17, 4, 13, 15, 14]}, "output": [15, 32, 36, 49, 64, 78]}, {"input": {"nums": [-9, 11, 8, -5, 13, 2, 1, 9, 10]}, "output": [-9, 2, 10, 5, 18, 20, 21, 30, 40]}] | 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": "1d98cf93fe4ce848"} |
vcr_d51a5848f630 | 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": 51151}, "output": 11}, {"input": {"n": 5924}, "output": 6}, {"input": {"n": 12479}, "output": 9}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "1d49ae6b4c8a4b3d"} |
vcr_eac6952c4073 | 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": "dadcddbabaadcbda"}, "output": false}, {"input": {"s": "bbbb"}, "output": true}, {"input": {"s": "cbbaa"}, "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": "5cf6b8717c248db0"} |
vcr_bd7794312115 | 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": [58, 40, 55, 43, 53, 26, 57, 12, 17, 35, 15, 51, 13]}, "output": 39}, {"input": {"prices": [22, 3, 10]}, "output": 7}, {"input": {"prices": [5, 21, 18, 33, 20, 5, 3, 22, 20]}, "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": "666cfc2ed8bb893f"} |
vcr_1e01a5f646bb | 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": "eadeb abad addba"}, "output": "addba abad eadeb"}, {"input": {"s": "baab a a aaab"}, "output": "aaab a a baab"}, {"input": {"s": "b b b ba aaba"}, "output": "aaba ba b b b"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "024cf057e9b2af14"} |
vcr_90d7ef6c7a14 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [12, 19, 27, 30, 45], "target": 19}, "output": 1}, {"input": {"nums": [0, 7, 18, 21, 29, 32], "target": 30}, "output": 5}, {"input": {"nums": [11, 19, 20, 22, 38], "target": 33}, "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": "2a77261486777e65"} |
vcr_7e02884b5acc | 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": [-18, -14, -12, 4, 25], "target": -32}, "output": [0, 1]}, {"input": {"nums": [-20, -14, -11, -6, 4, 12, 26, 27], "target": -17}, "output": [2, 3]}, {"input": {"nums": [-8, 4, 13, 17, 29, 31], "target": 42}, "output": [2, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "93b55b0f244bd6eb"} |
vcr_aac7bf7ffcad | 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, -15, 18, 10, -20, 13, -14, -2, -16, -2, 13, -9], "k": 5}, "output": [-2, -16, -2, 13, -9, 18, -15, 18, 10, -20, 13, -14]}, {"input": {"nums": [-9, -2, -6, -9, -8], "k": 15}, "output": [-9, -2, -6, -9, -8]}, {"input": {"nums": [-8, 5, 8, 2, 8, 9, -9, -3, -10], "k": 11}, "output": [-3, -10, -8, 5... | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "1e6220f46a5773c9"} |
vcr_3c3821627ab6 | 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": [-4, 4, 13, 17, 18, 26, 32, 38], "target": 9}, "output": [0, 2]}, {"input": {"nums": [-13, -4, 0, 4, 10, 18, 28, 32, 35, 36], "target": 0}, "output": [1, 3]}, {"input": {"nums": [-3, 22, 23, 26, 28, 30, 32, 35, 38], "target": 60}, "output": [1, 8]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "882bfce0e82a8c96"} |
vcr_225eb215cc20 | 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, 4, 6, -18, 17, 10, 3, -3, -16, 19], "k": 23}, "output": [-3, -16, 19, -18, 4, 6, -18, 17, 10, 3]}, {"input": {"nums": [-7, -5, 5, 10, -7], "k": 0}, "output": [-7, -5, 5, 10, -7]}, {"input": {"nums": [-3, -1, 4], "k": 8}, "output": [-1, 4, -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": "298d985f89433268"} |
vcr_15e0a6daa835 | 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": [19, -20, -17, 8, 12, 0, -18, 1, 16, -20], "k": 10}, "output": [19, -20, -17, 8, 12, 0, -18, 1, 16, -20]}, {"input": {"nums": [-2, -4, -3, 8, 9, 5, 10, -4, -4], "k": 5}, "output": [9, 5, 10, -4, -4, -2, -4, -3, 8]}, {"input": {"nums": [9, -3, 0], "k": 17}, "output": [-3, 0, 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": "c8d2c18842e3e855"} |
vcr_8b50883efd3b | 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": "bdcbacdbbacb"}, "output": false}, {"input": {"s": "baab"}, "output": true}, {"input": {"s": "bbcbbc"}, "output": false}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "26da5150771ef580"} |
vcr_cd822271ead5 | 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": 89, "b": 129}, "output": 1}, {"input": {"a": 100, "b": 137}, "output": 1}, {"input": {"a": 165, "b": 99}, "output": 33}] | 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": "1c54034bde2c3b93"} |
vcr_063ff84c1ce2 | 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": "baccadbbdcdcbb"}, "output": false}, {"input": {"s": "abc"}, "output": false}, {"input": {"s": "cabbabbca"}, "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": "7f445276e2918371"} |
vcr_5729c1550cdd | 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, 10, 24, 27, 33, 34, 35, 41, 47], "target": 10}, "output": 1}, {"input": {"nums": [1, 4, 8, 12, 13, 18, 23, 24, 27, 33, 39], "target": 10}, "output": 3}, {"input": {"nums": [9, 10, 25, 27, 34, 35, 38], "target": 4}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "f7b6d8c9421746e3"} |
vcr_5295711040a3 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [11, 29, 60, 28, 28, 14, 11, 29, 14]}, "output": 60}, {"input": {"nums": [47, 3, 6, 3, 6]}, "output": 47}, {"input": {"nums": [27, 28, 28, 15, 15, 5, 5, 58, 27]}, "output": 58}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "b7178158a8d56e4e"} |
vcr_0c7a52fdd89a | 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": [75, 46, 38, 24, 31, -37, -33, 50, 53], "target": 106}, "output": [0, 4]}, {"input": {"nums": [-10, 43, 8, -27, 37, 29, -16, 25, -12, -3, -17], "target": 17}, "output": [5, 8]}, {"input": {"nums": [-28, 46, 14, 22, 40, 8, -1, -11, 27, 3], "target": -12}, "output": [6, 7]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "c87545876de4e789"} |
vcr_50cc6f7a241a | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 3, 9, 16, 28, 41], "target": 34}, "output": 5}, {"input": {"nums": [0, 8, 10, 13, 14, 20, 21, 23, 27, 37], "target": 16}, "output": 5}, {"input": {"nums": [1, 2, 10, 11, 16, 17, 24, 30, 35, 36, 38], "target": 3}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "ede5be973fd23e95"} |
vcr_527a47d3ac4a | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 2, 2, 2, 4, 5, 5, 7, 10, 10, 11]}, "output": [0, 2, 4, 5, 7, 10, 11]}, {"input": {"nums": [2, 2, 4, 5, 8, 9, 11, 11, 12, 12]}, "output": [2, 4, 5, 8, 9, 11, 12]}, {"input": {"nums": [1, 2, 3, 7, 8, 10, 12]}, "output": [1, 2, 3, 7, 8, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "5cacf6b43041b811"} |
vcr_7dbc729aa4f8 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [0, -14, 5, -3, -6, 9, 10, -14, -6], "k": 11}, "output": [-14, -6, 0, -14, 5, -3, -6, 9, 10]}, {"input": {"nums": [-1, -1, -10], "k": 20}, "output": [-1, -10, -1]}, {"input": {"nums": [-1, 7, -1, 3, -10, -1, -3, 4, 3], "k": 16}, "output": [-1, 3, -10, -1, -3, 4, 3, -1, 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": "4d764cf4a379fc8a"} |
vcr_eaf66e19c93e | 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": 22, "b": 8}, "output": 2}, {"input": {"a": 56, "b": 115}, "output": 1}, {"input": {"a": 146, "b": 140}, "output": 2}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "9c619530c19d7931"} |
vcr_aca6138b6562 | 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": [0, 5, 8, 26, 31], "target": 5}, "output": [0, 1]}, {"input": {"nums": [10, 17, 20, 22, 24, 27, 30, 33], "target": 51}, "output": [4, 5]}, {"input": {"nums": [-2, 0, 5, 15], "target": 5}, "output": [1, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "19c9568b06e38945"} |
vcr_5b50aba047c8 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [7, 11, -14, -15], "k": 5}, "output": [-15, 7, 11, -14]}, {"input": {"nums": [-5, -8, 10, 7, -5], "k": 3}, "output": [10, 7, -5, -5, -8]}, {"input": {"nums": [4, -7, -6, 5, -7, -6], "k": 13}, "output": [-6, 4, -7, -6, 5, -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": "1ffb1d06e88e030d"} |
vcr_25b828fca87d | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-10, 8, -1, 2, -18, 21, 0, 21, 19, -9, -1]}, "output": 61}, {"input": {"nums": [9, -5, 8, -5, -12]}, "output": 12}, {"input": {"nums": [-10, -3, 8, 0, 14]}, "output": 22}] | 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": "31683d3846982e14"} |
vcr_aad82c46ec11 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [17, 4, -14, 3, -1, 13, -7, 26, 0, 6, -10, -18, 17, 16]}, "output": [17, 21, 7, 10, 9, 22, 15, 41, 41, 47, 37, 19, 36, 52]}, {"input": {"nums": [-9, 2]}, "output": [-9, -7]}, {"input": {"nums": [11, -4, 4, -12, 2, 1, 10, 6, 12]}, "output": [11, 7, 11, -1, 1, 2, 12, 18, 30]}] | 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": "f17cd07599cf8862"} |
vcr_67692142c2d3 | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "be cda a dcc eca bb"}, "output": "bb eca dcc a cda be"}, {"input": {"s": "bba bbba"}, "output": "bbba bba"}, {"input": {"s": "abb abab"}, "output": "abab abb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "bae6714fa7fe09a3"} |
vcr_01a4d7f5a895 | 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, 0, 2, 0, 4, 0, 3, 1, 0]}, "output": true}, {"input": {"nums": [0, 2, 0, 2, 3, 1, 3, 0, 2]}, "output": false}, {"input": {"nums": [1, 1, 1, 2, 0, 2, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "09c3e2907d5ffad5"} |
vcr_7b43c7dcc2b9 | 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": [17, 18, 38, 23, 24, 16, 35, 8, 37, 17]}, "output": true}, {"input": {"nums": [11, 2, 25, 7, 23, 18]}, "output": false}, {"input": {"nums": [8, 23, 21, 21, 22, 22, 6, 20, 11, 7, 16, 0]}, "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": "c06f6da7800f5135"} |
vcr_36f2a99abd24 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 1, 2, 2, 3, 5, 6, 7, 7, 7, 11, 15, 15]}, "output": [1, 2, 3, 5, 6, 7, 11, 15]}, {"input": {"nums": [1, 3, 6, 9, 9, 11, 12]}, "output": [1, 3, 6, 9, 11, 12]}, {"input": {"nums": [0, 1, 3, 3, 3, 5, 6, 9, 11, 11]}, "output": [0, 1, 3, 5, 6, 9, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "fd7b0e47572be893"} |
vcr_99d99d1113b0 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [9, -15, 1, 18, 2, 21, -14, -14, 20, -10, 8, 0, 7, -15, -5]}, "output": 42}, {"input": {"nums": [9, 13, 10]}, "output": 32}, {"input": {"nums": [6, -9, 13, 6, -4, 5, 1, 7, 8]}, "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": "5c3b6d23da9cbf3d"} |
vcr_ef90710fe7a1 | 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": [35, 47, 20, 23, 56, 49, 21, 58, 22, 53]}, "output": 38}, {"input": {"prices": [28, 3, 2, 10, 34, 7, 1, 6, 28]}, "output": 32}, {"input": {"prices": [24, 23, 13, 2, 40, 11, 27, 35]}, "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": "5aaac73cc7d16f07"} |
vcr_78299839e254 | 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": [-2, 35, -3, 5, 1, 0, 7, 11, 7, -3]}, "output": [-2, 33, 30, 35, 36, 36, 43, 54, 61, 58]}, {"input": {"nums": [14, 10, -10, -7, -8, 8, 14, -9]}, "output": [14, 24, 14, 7, -1, 7, 21, 12]}, {"input": {"nums": [3, -11, -8, -10, 0, -8]}, "output": [3, -8, -16, -26, -26, -34]}] | 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": "ed304d9db56a04dc"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.