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_aa41ac22c255 | 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": 20083}, "output": 9}, {"input": {"n": 14208}, "output": 6}, {"input": {"n": 4296}, "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": "568265c9858d50e0"} |
vcr_d53bd1296a8a | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [31, 64, 31, 13, 1, 13, 1]}, "output": 64}, {"input": {"nums": [5, 26, 54, 6, 6, 26, 28, 5, 28]}, "output": 54}, {"input": {"nums": [43, 22, 9, 1, 20, 22, 2, 1, 20, 2, 9]}, "output": 43}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "0b8cc27f191a3ccc"} |
vcr_d91ce8393754 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 3, 5, 14, 17, 18, 20, 21, 24, 26, 30, 32, 40, 44], "target": 32}, "output": 11}, {"input": {"nums": [4, 5, 6, 37], "target": 17}, "output": 3}, {"input": {"nums": [0, 9, 14, 18, 21, 25, 32], "target": 6}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "f34cfb5637137c47"} |
vcr_b6b1a3857e48 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 4, 3, 4, 4, 1, 0]}, "output": true}, {"input": {"nums": [2, 3, 0, 0, 0, 1, 3]}, "output": false}, {"input": {"nums": [2, 3, 0, 3, 0]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "2620ce21612a9ce3"} |
vcr_5c169fc7b42b | 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, -1, 0, 5, 17, 20, 24, 34, 38, 44, 48, 49], "target": 41}, "output": [4, 6]}, {"input": {"nums": [-2, 11, 24, 31, 38], "target": 49}, "output": [1, 4]}, {"input": {"nums": [-15, -8, 1, 3, 13, 32, 33], "target": 24}, "output": [1, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "8b33394e0325412f"} |
vcr_3356f28b310e | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [3, 3, 4, 7, 4]}, "output": [336, 336, 252, 144, 252]}, {"input": {"nums": [3, 2, 1, 5, 2, 2, 2]}, "output": [80, 120, 240, 48, 120, 120, 120]}, {"input": {"nums": [5, 2, 2]}, "output": [4, 10, 10]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "213eba5e825ca44b"} |
vcr_9d8a1aa8a64e | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [38, 4, 4, 10, 30, 8, 38, 17, 5, 5, 21, 10, 17, 74, 21, 30, 8]}, "output": 74}, {"input": {"nums": [6, 26, 11, 11, 43, 26, 6]}, "output": 43}, {"input": {"nums": [19, 56, 19, 17, 7, 18, 8, 12, 18, 8, 12, 17, 7]}, "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": "f88d7e5d280391a4"} |
vcr_cab566335e75 | 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": 45290}, "output": 8}, {"input": {"n": 2801}, "output": 7}, {"input": {"n": 5635}, "output": 5}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "566cb49d602faf32"} |
vcr_458ff3c6ba65 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [21, 33, 42, 43, 4, 23, 12, 21]}, "output": true}, {"input": {"nums": [24, 22, 18, 5, 13, 15, 17, 5, 21, 25, 11]}, "output": true}, {"input": {"nums": [20, 10, 4, 17, 2, 24, 22, 9, 12, 2]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "d509222e585d3a8c"} |
vcr_1fbd2049afe8 | 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": [29, 8, 1, 6]}, "output": 35}, {"input": {"nums": [4, 19, 5, 7, 20, 6]}, "output": 39}, {"input": {"nums": [0, 0, 15, 6, 14, 13, 18, 4, 9]}, "output": 56}] | 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": "04b6d3057aed8367"} |
vcr_2b71ca28e8d0 | 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": 250, "b": 30}, "output": 10}, {"input": {"a": 157, "b": 148}, "output": 1}, {"input": {"a": 38, "b": 129}, "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": "d21dc7d55a2e9ec8"} |
vcr_344498370141 | 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, 10, 14, 26, 38, 48], "target": 31}, "output": 4}, {"input": {"nums": [2, 5, 8, 10, 11, 14, 19, 29, 30, 34, 35, 36], "target": 18}, "output": 6}, {"input": {"nums": [14, 16, 17, 19, 23, 25, 38], "target": 30}, "output": 6}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "e5b0dc67c95139da"} |
vcr_177ecc6ea160 | 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": "babcaaaccbbbb"}, "output": 7}, {"input": {"s": "ccbbbaacc"}, "output": 4}, {"input": {"s": "cbbbcaabcbaa"}, "output": 8}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "67105ea7991a6f9d"} |
vcr_95973cc9ebcb | 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": [-21, -7, 15, 24, 31, 34, 48], "target": 27}, "output": [0, 6]}, {"input": {"nums": [-13, -6, 4, 10, 13, 25, 30, 31], "target": 17}, "output": [0, 6]}, {"input": {"nums": [-11, -8, -3, -2, 8, 9, 11, 16, 26, 30], "target": -5}, "output": [2, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "be577675c9dc0a01"} |
vcr_ae023940a5f5 | 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": "cbd d bdbe e eeacbd"}, "output": "eeacbd e bdbe d cbd"}, {"input": {"s": "ab aab abbb abbb"}, "output": "abbb abbb aab ab"}, {"input": {"s": "bbbb bbab a b b"}, "output": "b b a bbab bbbb"}] | 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": "1177ae5563348b29"} |
vcr_fd71e7022885 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [7, 9, 3, 12]}, "output": 21}, {"input": {"nums": [9, 19, 3, 11, 7, 15, 18, 5, 6]}, "output": 54}, {"input": {"nums": [6, 9, 17]}, "output": 23}] | 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": "11d4ccae88dbc882"} |
vcr_238b215a813b | 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": [16, 31, 37, 37, 31, 16, 51]}, "output": 51}, {"input": {"nums": [23, 23, 28, 42, 14, 28, 14]}, "output": 42}, {"input": {"nums": [19, 20, 1, 25, 1, 56, 19, 25, 20]}, "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": "28966dcfb16ae8a2"} |
vcr_b13edffa9260 | 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, -14, 15, -7, -20, -11], "k": 8}, "output": [-20, -11, 11, -14, 15, -7]}, {"input": {"nums": [0, -9, 1], "k": 8}, "output": [-9, 1, 0]}, {"input": {"nums": [-6, -2, 2, 3, -5, 9, 5, -9, 3, 1], "k": 12}, "output": [3, 1, -6, -2, 2, 3, -5, 9, 5, -9]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "424ae973123a52b1"} |
vcr_37085b88ba4d | 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, 3, 2, 1, 3, 4, 0, 3, 0]}, "output": true}, {"input": {"nums": [1, 2, 0, 0, 1, 3]}, "output": false}, {"input": {"nums": [3, 0, 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": "41dd357b7646ed66"} |
vcr_e1386a7e7469 | 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": ["cccb", "abcb", "bca", "aba", "cc", "aca", "bbba", "cba"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "baa", "baa", "bab", "bbb"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aaa", "bab", "aaa", "aba", "abb"]}, "output": [1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "6912cc3cd3e93dab"} |
vcr_e8ca89cc8ea7 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [9, -7, -8, -16, 10, 13, -2, -14, -3, -16, 12, 17, 14, -11]}, "output": 43}, {"input": {"nums": [-11, -7, 12, -1, 6, -4]}, "output": 17}, {"input": {"nums": [14, 1, 10]}, "output": 25}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "4bdd5862810b0bfe"} |
vcr_ee9ac1f844a3 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-25, -19, -1, 5, 10, 35, 40, 42], "target": 21}, "output": [1, 6]}, {"input": {"nums": [-20, -13, -12, 12, 14, 22, 25, 28, 29, 35, 37], "target": -25}, "output": [1, 2]}, {"input": {"nums": [-19, -15, -14, 4, 16, 19, 23, 28, 35, 38], "target": 54}, "output": [4, 9]}] | 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": "d9873faa42f7a899"} |
vcr_839362e0f2e4 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [18, 17, 22, -17, -14, 3, 18, 2, 14, -2]}, "output": 63}, {"input": {"nums": [-5, -12, 5, -7, -7, -12, -8, 0]}, "output": 5}, {"input": {"nums": [-8, 12, 7]}, "output": 19}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "dd5b1eecf8e16232"} |
vcr_295046b4064e | 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": "ba dddeba bad b ccccce"}, "output": "ccccce b bad dddeba ba"}, {"input": {"s": "bab a bbba baa"}, "output": "baa bbba a bab"}, {"input": {"s": "bbbb ba ab bbb"}, "output": "bbb ab ba bbbb"}] | 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": "d3893ca2bc0aff23"} |
vcr_497b4477f4e0 | 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": [62, 28, 52, 186, 64, 198, 77, 40, 85, 3, 197, 10, 18, 117, 157, 1, 169]}, "output": false}, {"input": {"nums": [10, 3, 21, 23]}, "output": false}, {"input": {"nums": [18, 3, 24, 14]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "d158a5801208d130"} |
vcr_f474f695f481 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [16, 21, 21, 6, 30, 21, 7, 6, 12, 7]}, "output": 86}, {"input": {"nums": [9, 6, 17, 7, 12]}, "output": 38}, {"input": {"nums": [3, 2, 0, 16, 2]}, "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": "c5508c154d0c621e"} |
vcr_7f396a39a0a8 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [31, 6, 17, 50, 17, 6, 27, 31, 27, 7, 7]}, "output": 50}, {"input": {"nums": [7, 28, 26, 28, 12, 26, 7, 55, 12]}, "output": 55}, {"input": {"nums": [26, 18, 22, 11, 22, 18, 19, 11, 19, 5, 54, 5, 26]}, "output": 54}] | 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": "fd3871e2c1a30ef3"} |
vcr_14a73c9c809c | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [6, 27, 23, 24, 9, 7, 25, 7]}, "output": 76}, {"input": {"nums": [18, 15, 14, 7, 19, 19, 7, 2, 19]}, "output": 77}, {"input": {"nums": [5, 3, 16, 16, 11, 3, 11]}, "output": 43}] | 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": "a32e7a1d41ac2465"} |
vcr_3bef196068d7 | 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": [6, 47, 53, 2, 44, 29, 33, 9, 3, 7, 23, 17, 10, 12]}, "output": 47}, {"input": {"prices": [3, 34, 14, 31, 38, 30, 20, 10, 1, 28]}, "output": 35}, {"input": {"prices": [17, 14, 32, 12]}, "output": 18}] | 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": "29cad1185aa30e55"} |
vcr_2ab2242256bd | 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": "ddcd dbedaa"}, "output": "dbedaa ddcd"}, {"input": {"s": "baba a aba"}, "output": "aba a baba"}, {"input": {"s": "abab a abaa b"}, "output": "b abaa a abab"}] | 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": "6e99528275d0252f"} |
vcr_d9bbcecfb334 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [9, -17, -1, -16, 3, 10, -11, -6], "k": 17}, "output": [-6, 9, -17, -1, -16, 3, 10, -11]}, {"input": {"nums": [-2, 2, 10], "k": 0}, "output": [-2, 2, 10]}, {"input": {"nums": [2, -1, 8, -6, 5, -4, 9], "k": 8}, "output": [9, 2, -1, 8, -6, 5, -4]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "88c49a3db57686fb"} |
vcr_975a6511ae47 | 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": "abcaaababcaaca"}, "output": 11}, {"input": {"s": "abc"}, "output": 3}, {"input": {"s": "cabcaa"}, "output": 5}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "4ba3b66fc0b362bb"} |
vcr_c90690102726 | 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": [27, 25, 13, 31, 13, 6, 35, 27, 4, 50, 25, 35, 4, 31, 6]}, "output": 50}, {"input": {"nums": [23, 13, 23, 58, 13]}, "output": 58}, {"input": {"nums": [43, 18, 7, 7, 18]}, "output": 43}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "e0c591c96104d879"} |
vcr_203f23f67afb | 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": "daadcbbbacdbbbab"}, "output": false}, {"input": {"s": "bcacacbc"}, "output": false}, {"input": {"s": "acab"}, "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": "9742b3eb206de81f"} |
vcr_6f067e4fba44 | 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": [25, 55, 32, 57, 3, 44, 40, 15, 32, 40, 3, 8]}, "output": 41}, {"input": {"prices": [12, 29, 25, 10, 7, 30, 2, 13, 24, 2, 3, 39]}, "output": 37}, {"input": {"prices": [14, 40, 11, 26, 24, 19, 20, 31, 39, 25, 13, 39]}, "output": 28}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "3581bd2adcc44e8f"} |
vcr_222cb3f2a401 | 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": [-21, 5, -15, 32, -17]}, "output": [-21, -16, -31, 1, -16]}, {"input": {"nums": [11, 4, 6, 6, 10, -7, 1]}, "output": [11, 15, 21, 27, 37, 30, 31]}, {"input": {"nums": [-1, 16, 6, -3, 11, 5, 6, -9, 17]}, "output": [-1, 15, 21, 18, 29, 34, 40, 31, 48]}] | 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": "ea1b08994b391bf3"} |
vcr_17bb13b83599 | 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": "e d c eebc ab"}, "output": "ab eebc c d e"}, {"input": {"s": "baba b baaa bb"}, "output": "bb baaa b baba"}, {"input": {"s": "b a aabb b abaa"}, "output": "abaa b aabb a 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": "3276c6b421beeea2"} |
vcr_35f094fb7412 | 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, 9, 18, 28, 47], "target": 19}, "output": 3}, {"input": {"nums": [0, 12, 16, 21, 25, 29, 32], "target": 4}, "output": 1}, {"input": {"nums": [2, 4, 6, 7, 13, 21, 22, 28, 30, 35, 38], "target": 15}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "70b129c0002e153b"} |
vcr_03152811fdc2 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [25, 11, 15, 38, 28, 15, 68, 38, 14, 28, 11, 14, 25]}, "output": 68}, {"input": {"nums": [14, 41, 14, 17, 17]}, "output": 41}, {"input": {"nums": [23, 3, 4, 41, 23, 3, 4]}, "output": 41}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "e1ebeba68e7ac084"} |
vcr_adf845e821f5 | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [43, -17, 47, -18, 4, 33, -8], "target": 26}, "output": [0, 1]}, {"input": {"nums": [2, 13, 14, 0, -27], "target": -25}, "output": [0, 4]}, {"input": {"nums": [2, -10, -16, -12, 14, 4], "target": -6}, "output": [1, 5]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "305c57662763f380"} |
vcr_a5571d864ed3 | 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, -14, 0, -5], "k": 3}, "output": [-14, 0, -5, -18]}, {"input": {"nums": [-5, 10, -5], "k": 15}, "output": [-5, 10, -5]}, {"input": {"nums": [-1, -6, 4, 0, 4, -2], "k": 18}, "output": [-1, -6, 4, 0, 4, -2]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "4744f6a8e0824313"} |
vcr_9db0809622ff | 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": [-3, 24, -1, 7, -19, 13, 22, 34, 19, 34, 34]}, "output": [-3, 21, 20, 27, 8, 21, 43, 77, 96, 130, 164]}, {"input": {"nums": [1, 1, -4, -2, 18, 10, 15, 11, 15, -6]}, "output": [1, 2, -2, -4, 14, 24, 39, 50, 65, 59]}, {"input": {"nums": [17, 15, 9, 4, 16, 6, 9, 18, 13]}, "output": [17, 32, 41, 45, 61,... | 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": "4350e5ec580a1958"} |
vcr_801b868aff23 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 4, 4, 5, 8, 9, 15]}, "output": [2, 4, 5, 8, 9, 15]}, {"input": {"nums": [0, 1, 1, 2, 3, 5, 5, 8, 10]}, "output": [0, 1, 2, 3, 5, 8, 10]}, {"input": {"nums": [0, 5, 6]}, "output": [0, 5, 6]}] | 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": "ff5faa897ed9a055"} |
vcr_aca69a9b7dac | 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", "caa", "bcca", "acaa", "bb", "acc", "caba", "abba"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "abb", "aab"]}, "output": [1, 2]}, {"input": {"strs": ["aba", "aaa", "bba", "baa", "bbb", "aba"]}, "output": [1, 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": "3ccdb9c5245c5c5f"} |
vcr_2de4e2dbe8e7 | 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, -6, -15, -19, 15], "k": 7}, "output": [-19, 15, -20, -6, -15]}, {"input": {"nums": [-10, 0, 3, 2, 2, 1], "k": 9}, "output": [2, 2, 1, -10, 0, 3]}, {"input": {"nums": [-4, 4, -2, -6, -8, -2, -8], "k": 2}, "output": [-2, -8, -4, 4, -2, -6, -8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "85b05df8dbe82e23"} |
vcr_4f0570c86637 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [5, 2, 0, 16, 10]}, "output": 21}, {"input": {"nums": [3, 4, 13, 12, 14, 19, 12, 5, 13]}, "output": 55}, {"input": {"nums": [17, 11, 2, 3, 18]}, "output": 37}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "0f141ffc9dbbd40f"} |
vcr_aaa948a35e24 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [2, -18, 17, 6, -3, -7, 4, 18, -4, -16, 17, -6, -6, -18, -18]}, "output": 35}, {"input": {"nums": [0, 13, -1, 5, 13, -8, 5, -5]}, "output": 30}, {"input": {"nums": [-3, 14, -1, -2, 12]}, "output": 23}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "b100705be2dcab46"} |
vcr_d21f96399200 | 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 db cc"}, "output": "cc db b"}, {"input": {"s": "b bab"}, "output": "bab b"}, {"input": {"s": "ab ab abba"}, "output": "abba ab 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": "55deeec72ce0326a"} |
vcr_0783dd776985 | 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, -22, -15, -6, -5, 5, 15, 27, 37, 42, 48], "target": 32}, "output": [4, 8]}, {"input": {"nums": [-19, -6, -3, -2, 2, 5, 13, 14, 15], "target": 13}, "output": [3, 8]}, {"input": {"nums": [-17, -1, 4, 7, 10, 20, 21, 23, 26], "target": 41}, "output": [5, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "eac915a18ffcb0a6"} |
vcr_ce6ee9c12ec7 | easy | math | Return FizzBuzz results from 1 to n inclusive. | def fizzbuzz(n: int) -> list[str]: | Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly. | def fizzbuzz(n: int) -> list[str]:
out = []
for i in range(1, n + 1):
if i % 15 == 0:
out.append('FizzBuzz')
elif i % 3 == 0:
out.append('Fizz')
elif i % 5 == 0:
out.append('Buzz')
else:
out.append(str(i))
return out
| [{"input": {"n": 44}, "output": ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz", "16", "17", "Fizz", "19", "Buzz", "Fizz", "22", "23", "Fizz", "Buzz", "26", "Fizz", "28", "29", "FizzBuzz", "31", "32", "Fizz", "34", "Buzz", "Fizz", "37", "38", "Fizz", "Buzz", "41", ... | true | python | 2da7b8aadef1ab6e | ### Problem
Return FizzBuzz results from 1 to n inclusive.
### Reasoning
Test divisibility by 15 before 3 or 5 so common multiples are labeled correctly.
### Solution
```python
def fizzbuzz(n: int) -> list[str]:
out = []
for i in range(1, n + 1):
if i % 15 == 0:
out.append('FizzBuzz')
... | {"v": "ok", "content_hash": "27037abca1d5ee7e"} |
vcr_3c0cd181a188 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 1, 4, 0, 3, 3, 1, 2, 0]}, "output": false}, {"input": {"nums": [2, 2, 1, 1, 2, 2, 2, 1, 1]}, "output": true}, {"input": {"nums": [1, 1, 2, 1, 1, 1, 3, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "741ccc65d998e989"} |
vcr_e729ab170f85 | 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": "abdccbc"}, "output": false}, {"input": {"s": "acacb"}, "output": false}, {"input": {"s": "aacbbcabca"}, "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": "4824915021f8a0c0"} |
vcr_eb3f2ce9d097 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [18, 19, 21, 41, 36, 23, 41, 36, 55, 44, 11, 25, 13, 57, 48]}, "output": 46}, {"input": {"prices": [14, 37, 36, 17, 6, 19, 33, 19, 7]}, "output": 27}, {"input": {"prices": [20, 22, 36, 9, 11, 40, 20, 28, 6, 2, 20]}, "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": "84da9ef5cd5a877d"} |
vcr_58afddffa279 | 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": "bbabdb"}, "output": false}, {"input": {"s": "cabcab"}, "output": false}, {"input": {"s": "abb"}, "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": "ebf2f078fc5c9f43"} |
vcr_2a3d199f8dd4 | 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": "deaee aeecb ab bbcbcd"}, "output": "bbcbcd ab aeecb deaee"}, {"input": {"s": "a aa"}, "output": "aa a"}, {"input": {"s": "bbba bbba abb"}, "output": "abb bbba bbba"}] | 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": "d26dea9c9b82ef35"} |
vcr_237d21cc4662 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-14, 9, 34, 1, 27]}, "output": [-14, -5, 29, 30, 57]}, {"input": {"nums": [-1, -3, -10, 17, -9]}, "output": [-1, -4, -14, 3, -6]}, {"input": {"nums": [-3, -9, -5, -8, 3, 16, 0, 1, 5]}, "output": [-3, -12, -17, -25, -22, -6, -6, -5, 0]}] | 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": "0ed0c900a34173ea"} |
vcr_a10f01c7535e | 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": [21, 24, -9, -23, -10, 35, 35, 22, 29, 30]}, "output": [21, 45, 36, 13, 3, 38, 73, 95, 124, 154]}, {"input": {"nums": [16, -5, 8, -2, 12, 8, 7]}, "output": [16, 11, 19, 17, 29, 37, 44]}, {"input": {"nums": [-3, 2, 8, 12]}, "output": [-3, -1, 7, 19]}] | 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": "e6d01bfe539ff338"} |
vcr_c36c41d6857a | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [27, 0, 5, 13]}, "output": 40}, {"input": {"nums": [8, 19, 5, 5, 12, 5, 3, 2, 19, 7]}, "output": 53}, {"input": {"nums": [11, 7, 3, 17, 8, 9, 8]}, "output": 37}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "497063195fde8721"} |
vcr_457c11793275 | 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, 11, 0, 14, 2, 27, 0, 13]}, "output": 65}, {"input": {"nums": [4, 17, 18, 16]}, "output": 33}, {"input": {"nums": [1, 17, 19, 19, 13]}, "output": 36}] | 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": "1656c6fafb43fbde"} |
vcr_5ea4a545a137 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [4, 1, 0, 6, 4, 3]}, "output": [0, 0, 288, 0, 0, 0]}, {"input": {"nums": [4, 5, 3, 3, 4, 2]}, "output": [360, 288, 480, 480, 360, 720]}, {"input": {"nums": [5, 2, 1, 5, 2, 3]}, "output": [60, 150, 300, 60, 150, 100]}] | 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": "66e0de935c54b7b5"} |
vcr_041e6b9521b2 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [14, 35, 19, 4, 15, 55, 19, 4, 13, 14, 35, 15, 13]}, "output": 55}, {"input": {"nums": [4, 26, 16, 58, 25, 5, 5, 25, 26, 17, 4, 16, 17]}, "output": 58}, {"input": {"nums": [11, 18, 18, 11, 47, 21, 5, 21, 4, 4, 5]}, "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": "eeae252ac679f7e4"} |
vcr_2c5d97cf990a | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 6, 9, 13, 17], "target": 42}, "output": 5}, {"input": {"nums": [1, 16, 18, 22, 29], "target": 31}, "output": 5}, {"input": {"nums": [1, 2, 14, 19, 20, 27, 28, 39], "target": 31}, "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": "a8ca7fd14c449f3f"} |
vcr_42c2f8d5e3c5 | 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": 26, "b": 57}, "output": 1}, {"input": {"a": 125, "b": 20}, "output": 5}, {"input": {"a": 144, "b": 136}, "output": 8}] | 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": "1f6e4f9de62b73ed"} |
vcr_4fafb9706bdf | 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": [16, 24, 35, 5, 54, 22, 41, 10, 49, 28]}, "output": 49}, {"input": {"prices": [13, 5, 6, 4, 2, 12, 23, 32, 23, 8, 38]}, "output": 36}, {"input": {"prices": [23, 34, 18]}, "output": 11}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "ec75dc89d3a59f5b"} |
vcr_ac75c235c13d | 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": 123, "b": 5}, "output": 1}, {"input": {"a": 22, "b": 82}, "output": 2}, {"input": {"a": 55, "b": 57}, "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": "bd00d25e18a8139c"} |
vcr_3283f58740f7 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [4, 0, 6, 4, 3]}, "output": [0, 288, 0, 0, 0]}, {"input": {"nums": [2, 5, 4, 4, 4, 5, 3]}, "output": [4800, 1920, 2400, 2400, 2400, 1920, 3200]}, {"input": {"nums": [4, 1, 1, 5, 5, 2, 5]}, "output": [250, 1000, 1000, 200, 200, 500, 200]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "7d9ed4e358042840"} |
vcr_eb88f85a6563 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-2, -9, -15, 15, 9, 1, -17, -9]}, "output": 25}, {"input": {"nums": [-6, 10, 0, -4, -6, -5]}, "output": 10}, {"input": {"nums": [4, 0, 4, -6]}, "output": 8}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "24c4cd6212dcf2d2"} |
vcr_e575f04c939f | 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": "ebdcd bcabe eeb abad aded bec edd"}, "output": "edd bec aded abad eeb bcabe ebdcd"}, {"input": {"s": "baba bb abbb a bbb"}, "output": "bbb a abbb bb baba"}, {"input": {"s": "ba aba aaaa abbb"}, "output": "abbb aaaa aba ba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "5d9245b22e89dc23"} |
vcr_ff08d71657f4 | 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": [15, -19, -9, 8, 17, 11, 3], "k": 0}, "output": [15, -19, -9, 8, 17, 11, 3]}, {"input": {"nums": [3, 2, 7, -4, 8, -9, 4, 9], "k": 20}, "output": [8, -9, 4, 9, 3, 2, 7, -4]}, {"input": {"nums": [8, 0, 8, 8, -10, 2, 6, -10, -5], "k": 15}, "output": [8, -10, 2, 6, -10, -5, 8, 0, 8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "8bc6ccd3180e24cd"} |
vcr_fbf084b0638d | 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": 157, "b": 230}, "output": 1}, {"input": {"a": 85, "b": 30}, "output": 5}, {"input": {"a": 89, "b": 16}, "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": "24d192220fad8343"} |
vcr_fe074e1c9cad | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [14, -11, -1, 18, 17, 5, -3, 10, -2, 19, -8, -7, -1, -2]}, "output": 66}, {"input": {"nums": [-6, 4, -7, 5, 4, 3, 9, 9, 4, 11]}, "output": 45}, {"input": {"nums": [8, 1, -9]}, "output": 9}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "32928177f993cd87"} |
vcr_6ef7a0262589 | 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": [49, 5, 30, 16, 43, 49, 41, 18, 7, 44, 0, 20]}, "output": true}, {"input": {"nums": [2, 25, 5, 11, 10, 2, 7, 2, 12, 7, 24]}, "output": true}, {"input": {"nums": [19, 25, 3, 6]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "810f9a7a8fa71757"} |
vcr_aca479bd1215 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [8, -1, -4, 11, 5, -20], "k": 5}, "output": [-1, -4, 11, 5, -20, 8]}, {"input": {"nums": [-7, 5, 0, 2, -2], "k": 3}, "output": [0, 2, -2, -7, 5]}, {"input": {"nums": [9, 1, -10, 1, -10, -1, 5, 3, -3, 8], "k": 4}, "output": [5, 3, -3, 8, 9, 1, -10, 1, -10, -1]}] | 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": "3e91ceca4bb0f7c4"} |
vcr_a1269876a06c | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "a aba dd bcabdd ea"}, "output": "ea bcabdd dd aba a"}, {"input": {"s": "abb a baa"}, "output": "baa a abb"}, {"input": {"s": "b aa"}, "output": "aa 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": "f7253703a8a5b970"} |
vcr_ff7044e8fdde | 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": 20957}, "output": 9}, {"input": {"n": 8225}, "output": 3}, {"input": {"n": 7287}, "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": "f0da9459a897156b"} |
vcr_74960f00591c | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 11, 17, 18, 25, 27, 40], "target": 44}, "output": 7}, {"input": {"nums": [6, 11, 14, 29], "target": 31}, "output": 4}, {"input": {"nums": [15, 18, 20, 38], "target": 38}, "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": "e170429fc5ce69c3"} |
vcr_319a6c041b30 | 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": ["cac", "acbc", "abba", "bb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "bba", "aaa", "bbb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["abb", "aab", "aba", "aab"]}, "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": "dd77fc85c7b49918"} |
vcr_6a58afd60b1c | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-14, 12, -19, -24, -11, 13, 29, 28, -1, -22, 22, 14, 14, 9]}, "output": [-14, -2, -21, -45, -56, -43, -14, 14, 13, -9, 13, 27, 41, 50]}, {"input": {"nums": [-5, 0, 14, -7, 10, -6, -7, -6, -9]}, "output": [-5, -5, 9, 2, 12, 6, -1, -7, -16]}, {"input": {"nums": [14, 15, 3, -2]}, "output": [14, 29, 32... | 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": "734515ce3880b680"} |
vcr_5e8fca8c3372 | 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": [-6, 48, 2, 31, -14, 36, 46, -10, -30, 6, 39, 79], "target": 9}, "output": [8, 10]}, {"input": {"nums": [0, 1, 13, -17], "target": -17}, "output": [0, 3]}, {"input": {"nums": [-20, 16, 48, -9, 27, -3, 38, 31, -24, -4], "target": 28}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "0065a220935fa149"} |
vcr_0c40f635c01d | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "a cdac ace bdc ddeace"}, "output": "ddeace bdc ace cdac a"}, {"input": {"s": "baaa a bbba a a"}, "output": "a a bbba a baaa"}, {"input": {"s": "bb aaa bb"}, "output": "bb aaa bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "92083169d098c002"} |
vcr_a2b325c81276 | 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, -4, 21]}, "output": [-9, -13, 8]}, {"input": {"nums": [13, -1, -1, -12, -3]}, "output": [13, 12, 11, -1, -4]}, {"input": {"nums": [14, -1, -7, 5, 4, 6, 6, -4, 13]}, "output": [14, 13, 6, 11, 15, 21, 27, 23, 36]}] | 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": "228bfc426044bca7"} |
vcr_1226f4c93cc6 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [16, 8, 29, 5, 17]}, "output": 62}, {"input": {"nums": [6, 2, 18, 19]}, "output": 25}, {"input": {"nums": [12, 20, 4, 14, 19, 7]}, "output": 41}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "6272fdefe0ffd066"} |
vcr_18bd38d4224e | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-28, -20, -17, -7, -3, 6, 8, 26, 37, 39, 44, 49], "target": 75}, "output": [7, 11]}, {"input": {"nums": [-13, -11, 0, 1, 5, 14, 20, 23, 29, 30], "target": 14}, "output": [2, 5]}, {"input": {"nums": [-20, -5, 1, 3, 15, 20, 27, 29], "target": 24}, "output": [1, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "65c6f273d4f6e61a"} |
vcr_885817e13a57 | 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": "bcba cbc dead abcc"}, "output": "abcc dead cbc bcba"}, {"input": {"s": "baba bab"}, "output": "bab baba"}, {"input": {"s": "bb b"}, "output": "b bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "5b456a39972ac982"} |
vcr_b71a1ef86c36 | 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": 71, "b": 164}, "output": 1}, {"input": {"a": 5, "b": 169}, "output": 1}, {"input": {"a": 79, "b": 7}, "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": "56c05dc130bd3981"} |
vcr_4798ef0ea93a | 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": [22, 8, -6, 12, -6]}, "output": 36}, {"input": {"nums": [-10, -9, 11, 9, -5, 10, 9, -11, 11]}, "output": 34}, {"input": {"nums": [7, 11, 5, -11]}, "output": 23}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "c78993d8d4775545"} |
vcr_69dd2702298a | 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": [-11, 22, -5, 61, -19, 44, -3, 66, -22, 35], "target": -8}, "output": [2, 6]}, {"input": {"nums": [39, 47, 9, -16, 43, 15], "target": 62}, "output": [1, 5]}, {"input": {"nums": [25, -17, 49, 37, -14, 36, 23, -11, 27, -15], "target": 21}, "output": [5, 9]}] | 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": "008c975d2f8096ef"} |
vcr_e63759078fad | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, -6, 33, 35, 42], "target": 3}, "output": [0, 2]}, {"input": {"nums": [-17, -9, -8, 5, 8, 15, 17, 28, 38], "target": -1}, "output": [1, 4]}, {"input": {"nums": [-18, 11, 13, 15, 24, 27, 29, 32], "target": 35}, "output": [1, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "30241ca9309bafb5"} |
vcr_be6ffb6a6fc5 | 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, 15, 17, 20, 25, 32, 35, 42, 43], "target": 33}, "output": 6}, {"input": {"nums": [4, 8, 13, 21, 25, 27, 30, 35], "target": 21}, "output": 3}, {"input": {"nums": [0, 18, 19, 29, 30, 39], "target": 35}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "96f26ddc0abd5219"} |
vcr_f0dc1dcbdc18 | 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, 7, 12, 17, 22, 30, 36, 37, 40, 42], "target": 40}, "output": 8}, {"input": {"nums": [5, 10, 12, 16], "target": 11}, "output": 2}, {"input": {"nums": [1, 4, 7, 26, 29, 30, 32], "target": 21}, "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": "5667a2d0f10d7793"} |
vcr_2d8caefa060d | 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": "aaaaaaaaabaac"}, "output": 4}, {"input": {"s": "bcaababccaab"}, "output": 9}, {"input": {"s": "aaccbaacac"}, "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": "19197e426d00a663"} |
vcr_b7d31137078e | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 12, 22, 28, 31, 36, 43, 45], "target": 39}, "output": 6}, {"input": {"nums": [3, 6, 7, 10, 12, 15, 25, 36], "target": 23}, "output": 6}, {"input": {"nums": [3, 14, 22, 26, 31], "target": 27}, "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": "e516abf016228da4"} |
vcr_93668b1d4a4d | 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": 8, "b": 198}, "output": 2}, {"input": {"a": 96, "b": 30}, "output": 6}, {"input": {"a": 61, "b": 108}, "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": "43c2c2f51c8fca5b"} |
vcr_b1e2ea4db3e9 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [26, 10, 17, 9, 11, 8, 6, 27, 21, 8]}, "output": 89}, {"input": {"nums": [6, 9, 16, 19, 11, 20, 5, 16, 11, 20]}, "output": 84}, {"input": {"nums": [12, 19, 20, 18]}, "output": 37}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "8a402b472ecf49a9"} |
vcr_31469ef346fe | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [21, 16, 41, 26, 0, 3, 39, 12, 38, 21, 44, 8, 11]}, "output": true}, {"input": {"nums": [17, 11, 9, 17]}, "output": true}, {"input": {"nums": [22, 0, 3, 6, 16, 3, 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": "343879bb897657f4"} |
vcr_994ec0599d97 | 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": "dcbcaaa"}, "output": false}, {"input": {"s": "ccabbab"}, "output": false}, {"input": {"s": "bcbbab"}, "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": "12edeaca7e86cb70"} |
vcr_2c10c2ae508e | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 4, 6, 8, 9, 10, 13]}, "output": [0, 2, 4, 6, 8, 9, 10, 13]}, {"input": {"nums": [1, 4, 4, 7, 8, 10, 11]}, "output": [1, 4, 7, 8, 10, 11]}, {"input": {"nums": [4, 6, 7, 9, 9, 9, 10, 11]}, "output": [4, 6, 7, 9, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "12bd2f11c3dc4a9f"} |
vcr_e2f9d5e28a3a | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 5, 11, 17, 22, 24, 25, 31, 33, 36, 43], "target": 22}, "output": 4}, {"input": {"nums": [0, 1, 2, 7, 8, 9, 11, 14, 34, 35], "target": 0}, "output": 0}, {"input": {"nums": [13, 20, 23, 27], "target": 5}, "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": "fdf6b7d98a27bc19"} |
vcr_337e757d4353 | 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": [22, 30, 10, 160, 92, 6]}, "output": false}, {"input": {"nums": [10, 5, 9, 17, 5, 1]}, "output": true}, {"input": {"nums": [9, 22, 5]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "f4a439f122dd1a22"} |
vcr_4bd83e0ea781 | 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": [37, 21, 21, 12, 8, 27, 37, 8, 13, 27, 34, 65, 30, 13, 30, 34, 12]}, "output": 65}, {"input": {"nums": [42, 19, 26, 26, 19]}, "output": 42}, {"input": {"nums": [14, 28, 4, 26, 14, 28, 26, 4, 44]}, "output": 44}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "16c94f141ab7fd91"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.