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_783016e3f369 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [11, 19, 1, 27, 5, 15, 10, 29, 1]}, "output": 90}, {"input": {"nums": [8, 7]}, "output": 8}, {"input": {"nums": [19, 20, 14, 19, 5, 0]}, "output": 39}] | 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": "1067cb8948b214d2"} |
vcr_fdbec22d434c | 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": "ddaaaabadadcbca"}, "output": false}, {"input": {"s": "bbabccacab"}, "output": false}, {"input": {"s": "aabac"}, "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": "fdca973d88e680ee"} |
vcr_91dad59c99fa | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [21, 30, 2, 9, 36, 36, 2, 30, 9, 25, 25, 11, 24, 11, 71, 24, 21]}, "output": 71}, {"input": {"nums": [21, 27, 21, 4, 27, 18, 18, 13, 40, 19, 4, 19, 13]}, "output": 40}, {"input": {"nums": [26, 29, 26, 17, 29, 48, 28, 28, 8, 17, 8]}, "output": 48}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "11d213f1e811ba41"} |
vcr_8866cf9b3eb2 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-4, 70, 13, 59, 57, 5, 67, 40, 19, 51, 72, -28, 28, -2], "target": 12}, "output": [7, 11]}, {"input": {"nums": [22, 39, 37, 33, 29, -18, -2], "target": 68}, "output": [1, 4]}, {"input": {"nums": [-8, 37, 17, -13, 35, 28, 3, -2, 25], "target": 29}, "output": [0, 1]}] | 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": "e9b725e1fa53ddeb"} |
vcr_2ec84e0cd0f6 | 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": [-10, -4, 6, 9, -8, 17, -5, -7, 14, -11], "k": 17}, "output": [9, -8, 17, -5, -7, 14, -11, -10, -4, 6]}, {"input": {"nums": [-4, 2, 5, 8, -8, 1, -10, 1], "k": 14}, "output": [5, 8, -8, 1, -10, 1, -4, 2]}, {"input": {"nums": [-7, -2, -9], "k": 13}, "output": [-9, -7, -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": "262eaac81a696f0e"} |
vcr_8141a3586d1a | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-26, -18, -7, 6, 28, 39, 45, 49], "target": -12}, "output": [1, 3]}, {"input": {"nums": [-11, 0, 9, 12, 33, 37], "target": 1}, "output": [0, 3]}, {"input": {"nums": [-19, -15, -6, 0, 8, 10, 15, 32], "target": -11}, "output": [0, 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": "c57524cfa4ef9079"} |
vcr_6c3d72838f6c | 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": [33, 54, 27, 41, 41, 9, 48, 28, 12, 6, 30, 2, 47, 46, 23]}, "output": 45}, {"input": {"prices": [10, 11, 14, 19, 11, 35, 22, 18]}, "output": 25}, {"input": {"prices": [12, 11, 19, 19, 7, 38, 21, 24]}, "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": "11bab357186b197e"} |
vcr_6898570ebcd7 | 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": [-5, 6, -4, 13, 16, 8], "k": 9}, "output": [13, 16, 8, -5, 6, -4]}, {"input": {"nums": [1, 2, -3, -10, -2, -7], "k": 9}, "output": [-10, -2, -7, 1, 2, -3]}, {"input": {"nums": [-1, 6, -7], "k": 17}, "output": [6, -7, -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": "651e2d95866d3f84"} |
vcr_8598c5938092 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-18, -7, -6, 18, 34, 42, 43], "target": 24}, "output": [0, 5]}, {"input": {"nums": [2, 3, 9, 23, 26, 30], "target": 39}, "output": [2, 5]}, {"input": {"nums": [12, 15, 23, 33], "target": 48}, "output": [1, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "fade8b54d2c0521d"} |
vcr_0bc3b0ac5719 | 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, 2, 3, 1, 3, 3, 0, 4, 0]}, "output": true}, {"input": {"nums": [0, 2, 3, 2]}, "output": false}, {"input": {"nums": [0, 2, 0]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "088c31566d51a19f"} |
vcr_0940cb40e2ed | 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, 12, 19, 24, 25, 27, 32, 38, 43, 45, 46, 47, 48], "target": 37}, "output": 7}, {"input": {"nums": [0, 1, 14, 24, 38], "target": 37}, "output": 4}, {"input": {"nums": [11, 13, 15, 22, 32, 37, 39], "target": 24}, "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": "15931b83907890fc"} |
vcr_8649c67899a7 | 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": 240, "b": 26}, "output": 2}, {"input": {"a": 172, "b": 134}, "output": 2}, {"input": {"a": 44, "b": 3}, "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": "bf86b04572848aba"} |
vcr_f0674ec5f9da | 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": 154, "b": 229}, "output": 1}, {"input": {"a": 44, "b": 116}, "output": 4}, {"input": {"a": 104, "b": 117}, "output": 13}] | 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": "4158d8d965448609"} |
vcr_8375b6a151f4 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-12, 20, 18, 15, -15, 2], "k": 4}, "output": [18, 15, -15, 2, -12, 20]}, {"input": {"nums": [-1, 2, 10, 7], "k": 19}, "output": [2, 10, 7, -1]}, {"input": {"nums": [7, -10, 4], "k": 13}, "output": [4, 7, -10]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "0a9b9bcca957a679"} |
vcr_9ed5e6e3ff53 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [76, 24, 41, 60, 1, -9, 66, -23, -17, -15, -16, 33, -6], "target": -32}, "output": [5, 7]}, {"input": {"nums": [20, -14, 7, -10, 23, -23, 32, 6], "target": -33}, "output": [3, 5]}, {"input": {"nums": [33, 38, -6, -20, -13, -17], "target": 71}, "output": [0, 1]}] | 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": "83365040d30772c5"} |
vcr_b12a6bb38921 | 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": 237, "b": 141}, "output": 3}, {"input": {"a": 91, "b": 52}, "output": 13}, {"input": {"a": 168, "b": 129}, "output": 3}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "8e954663d7b308ad"} |
vcr_461819d4b202 | 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": ["ccbc", "bbcb", "ccb", "ccc", "abcc", "ba", "bbab", "ba", "ba"]}, "output": [1, 1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bab", "abb", "bba", "aaa", "bbb"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aaa", "aab", "aaa", "baa", "bbb", "aaa", "bab"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "a078835931b26ff3"} |
vcr_9fc6e25f0c2f | 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, 13, -2, 15, -14, -11, -15, -2, 18, -15, -7, -11], "k": 7}, "output": [-11, -15, -2, 18, -15, -7, -11, -18, 13, -2, 15, -14]}, {"input": {"nums": [-8, 9, -2, 6, 5, 7, 8, -5], "k": 2}, "output": [8, -5, -8, 9, -2, 6, 5, 7]}, {"input": {"nums": [-4, -1, -9, -4, -3, -10, 9, 4, 0, 1], "k": 0}, "out... | 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": "9d8dbed919a909a2"} |
vcr_1e832cc6e762 | 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": ["bbcb", "bcc", "bcc", "bbac", "ac", "bbc", "abac", "ccb", "bc"]}, "output": [1, 1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bba", "aaa", "abb", "aba", "aab", "baa", "bbb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["aab", "abb", "abb", "bab", "aab", "bab"]}, "output": [2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "3b40169ff7c507f2"} |
vcr_46ade4a3f2db | 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": [2, 9, 2, 7, 10, 10]}, "output": 26}, {"input": {"nums": [12, 3, 20, 9, 8]}, "output": 40}, {"input": {"nums": [16, 7, 1, 0, 12, 1, 1, 19, 18, 16]}, "output": 64}] | 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": "3f9180bbe6b0827e"} |
vcr_17ef425c3971 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [17, 20, 20, -4, 14, -17, -11, -9, 3, 17, 4, -19], "k": 26}, "output": [4, -19, 17, 20, 20, -4, 14, -17, -11, -9, 3, 17]}, {"input": {"nums": [-5, 0, -1, -10], "k": 18}, "output": [-1, -10, -5, 0]}, {"input": {"nums": [-8, -3, 5, -2, 6, -5, -10, -9, -2, 1], "k": 19}, "output": [-3, 5, -2, 6, -5, -10... | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "52a27290db1277eb"} |
vcr_4412fe73b241 | 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": [2, -7, -11, 15, 8, -3, -1, 7, -4, -18, 10], "k": 29}, "output": [8, -3, -1, 7, -4, -18, 10, 2, -7, -11, 15]}, {"input": {"nums": [-5, 5, 7], "k": 2}, "output": [5, 7, -5]}, {"input": {"nums": [10, 4, -3, -6, 3, -4, 9, 4, 2], "k": 4}, "output": [-4, 9, 4, 2, 10, 4, -3, -6, 3]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "3b15308af5745aa4"} |
vcr_fedc03902275 | 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, 5, 3, 1, 2, 2, 3]}, "output": [180, 720, 144, 240, 720, 360, 360, 240]}, {"input": {"nums": [2, 1, 2, 1, 3, 5, 1]}, "output": [30, 60, 30, 60, 20, 12, 60]}, {"input": {"nums": [5, 4, 5, 5, 5, 4]}, "output": [2000, 2500, 2000, 2000, 2000, 2500]}] | 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": "75cf73269ff48989"} |
vcr_9bcf657ec105 | 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, 4, 5, 15, 22, 23, 25, 34, 40, 43, 44, 46, 47], "target": 17}, "output": 5}, {"input": {"nums": [20, 21, 38], "target": 40}, "output": 3}, {"input": {"nums": [3, 7, 13, 16, 22, 24], "target": 17}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "c0127d4a3799e576"} |
vcr_d8f6a60cab48 | 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": [-4, -14, 5, -12, 17, 11], "k": 16}, "output": [5, -12, 17, 11, -4, -14]}, {"input": {"nums": [2, 9, 3], "k": 7}, "output": [3, 2, 9]}, {"input": {"nums": [-9, -10, 4, 6, -3, 9], "k": 8}, "output": [-3, 9, -9, -10, 4, 6]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "22dd83f54207f0d3"} |
vcr_8035b6f8ce36 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-1, 18, -13, -20, 15, -4, -20, 8], "k": 3}, "output": [-4, -20, 8, -1, 18, -13, -20, 15]}, {"input": {"nums": [-2, -3, 0, -1, -5], "k": 9}, "output": [-3, 0, -1, -5, -2]}, {"input": {"nums": [10, 8, 2, 0], "k": 1}, "output": [0, 10, 8, 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": "ce5b71b2a68fa981"} |
vcr_a8115f2deb83 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [14, 1, 0, 27, 19, 21, 15, 4, 25, 26, 1]}, "output": 92}, {"input": {"nums": [8, 19, 11, 19, 20, 20, 4, 7, 8]}, "output": 66}, {"input": {"nums": [15, 7, 20, 3, 1, 12, 9]}, "output": 47}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "076b8cf037db33cf"} |
vcr_9b5f82a4d966 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [23, 43, 40, 55, 47]}, "output": 32}, {"input": {"prices": [3, 19, 21]}, "output": 18}, {"input": {"prices": [12, 33, 28, 21, 7, 22, 6, 7, 20, 40]}, "output": 34}] | 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": "6e26d08878049eea"} |
vcr_3ba51a324cdf | 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": 125, "b": 162}, "output": 1}, {"input": {"a": 114, "b": 27}, "output": 3}, {"input": {"a": 87, "b": 30}, "output": 3}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "1b1f314769b431ac"} |
vcr_542664b1f54c | 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, -29, -25, -19, -13, -3], "target": -33}, "output": [0, 5]}, {"input": {"nums": [-19, -13, -9, -4, 6, 14, 35], "target": -32}, "output": [0, 1]}, {"input": {"nums": [0, 3, 26, 33], "target": 26}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "4464dfcf273b0653"} |
vcr_729f4bce881f | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [13, 31, 2, 59, 31, 13, 6, 2, 6]}, "output": 59}, {"input": {"nums": [22, 7, 8, 1, 8, 10, 22, 27, 50, 1, 27, 10, 7]}, "output": 50}, {"input": {"nums": [24, 42, 23, 24, 17, 14, 9, 17, 23, 8, 14, 8, 9]}, "output": 42}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "ed438e66b5f1cdca"} |
vcr_12e19c6b869b | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [11, 23, 11, 7, 23, 7, 31, 31, 63]}, "output": 63}, {"input": {"nums": [6, 22, 59, 13, 14, 14, 13, 6, 22]}, "output": 59}, {"input": {"nums": [24, 22, 16, 29, 29, 42, 22, 18, 18, 16, 24]}, "output": 42}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "4019f6959933fb76"} |
vcr_294a8a8eaa55 | 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, 2, 6, 11, 12, 21, 29, 31], "target": 10}, "output": 3}, {"input": {"nums": [2, 7, 8, 10, 18, 26, 34, 35, 38], "target": 40}, "output": 9}, {"input": {"nums": [7, 11, 15, 16, 20, 28, 32], "target": 31}, "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": "cc49ac16ee26706f"} |
vcr_05b59f2d8af1 | 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": ["bbbc", "ccc", "cc", "accc", "abc", "aca", "cb"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bab", "abb"]}, "output": [3]}, {"input": {"strs": ["aaa", "aab", "aab", "baa", "aaa", "bba"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "4f55246a44c8bdf1"} |
vcr_4f7b660966c9 | 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": [29, -8, -19, 69], "target": -27}, "output": [1, 2]}, {"input": {"nums": [25, -3, -1, 13, -30, -10, 28, -19, -18], "target": -29}, "output": [5, 7]}, {"input": {"nums": [-19, -14, -23, 43], "target": -33}, "output": [0, 1]}] | 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": "e0714a3c40b595da"} |
vcr_5b7f0ac08f24 | 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": "dabbcddddddcbbad"}, "output": true}, {"input": {"s": "ca"}, "output": false}, {"input": {"s": "abcccb"}, "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": "9ca8d01e9b7f70f2"} |
vcr_eb4e8fb14ed2 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [50, 54, 14, 53, 19, 21, 13, 21, 18, 57, 45, 26, 6]}, "output": 44}, {"input": {"prices": [8, 3, 21, 12, 6, 1, 20, 20]}, "output": 19}, {"input": {"prices": [7, 7, 4, 21]}, "output": 17}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "99d00d059d2fc1fc"} |
vcr_37a6ffe93bfb | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -11, 0, 1, 9, 13, 20], "target": 33}, "output": [5, 6]}, {"input": {"nums": [-20, -8, 19, 28], "target": -28}, "output": [0, 1]}, {"input": {"nums": [-12, -7, -6, 6, 9, 13, 20, 22, 26, 36, 37], "target": -18}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "73b6bb6f32a0c9f0"} |
vcr_4f3e359d23b4 | 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, 5, 7, 21, 36, 38], "target": 28}, "output": 4}, {"input": {"nums": [4, 14, 23, 25, 28, 31], "target": 3}, "output": 0}, {"input": {"nums": [5, 11, 13, 20, 22, 26, 27, 30], "target": 18}, "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": "e06e34e5464268ab"} |
vcr_dbcfac084e32 | 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": "ccb bdeab add"}, "output": "add bdeab ccb"}, {"input": {"s": "baa a ab"}, "output": "ab a baa"}, {"input": {"s": "baab ba bbbb abbb"}, "output": "abbb bbbb ba baab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "4663645c04576553"} |
vcr_7deb5c7dc742 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-16, 30, 17, -6, -14, 4, -4]}, "output": [-16, 14, 31, 25, 11, 15, 11]}, {"input": {"nums": [12, 12, 3, -12, -8, 11, 6, 9]}, "output": [12, 24, 27, 15, 7, 18, 24, 33]}, {"input": {"nums": [-9, -2, 14, 7, -4, 6, 8, 1, 15, -12]}, "output": [-9, -11, 3, 10, 6, 12, 20, 21, 36, 24]}] | 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": "29a74d950f577631"} |
vcr_6b6d1502866b | 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": ["bb", "cac", "abcc", "ccb", "aaab", "cc", "bb", "ac", "cb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "bbb", "aab", "bab", "bbb", "bbb", "baa"]}, "output": [1, 3, 3]}, {"input": {"strs": ["bbb", "aba", "bab", "aaa", "bbb"]}, "output": [1, 1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "e75f12c4ee406206"} |
vcr_e14ab3323349 | 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": [6, -13, 18, -13, 13], "k": 9}, "output": [-13, 18, -13, 13, 6]}, {"input": {"nums": [7, -3, 2, -8, -9, -5, 0, -5, -7, 8], "k": 13}, "output": [-5, -7, 8, 7, -3, 2, -8, -9, -5, 0]}, {"input": {"nums": [2, -2, -3, -3, 9, 7, 4], "k": 4}, "output": [-3, 9, 7, 4, 2, -2, -3]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "eb02a08f332f8dba"} |
vcr_43e447d11228 | 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, -13, -2, -1, -8, -15, 9, 5, -2, -12, 9, 4, -7, -17]}, "output": 22}, {"input": {"nums": [-1, 9, -9, 6, 11, 7, -3, 0, 6, -8]}, "output": 27}, {"input": {"nums": [-9, -6, 12, -1, -8, 11, 10]}, "output": 24}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "5b6c7b3f86ccc98b"} |
vcr_6470eb72a951 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 5, 5, 4, 3, 2]}, "output": [600, 600, 600, 750, 1000, 1500]}, {"input": {"nums": [4, 4, 5, 5, 5, 2, 3]}, "output": [3000, 3000, 2400, 2400, 2400, 6000, 4000]}, {"input": {"nums": [2, 4, 1, 4, 4, 5]}, "output": [320, 160, 640, 160, 160, 128]}] | 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": "45aef8ef251e9b88"} |
vcr_a7663519113e | 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, 2, 1, 4, 4, 4, 2, 4, 0]}, "output": false}, {"input": {"nums": [1, 1, 0]}, "output": true}, {"input": {"nums": [1, 0, 2, 0, 0]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "af0ab45c398d91ed"} |
vcr_6f5667b1b350 | 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, 13, 14, 29, 2, 24, 3, 30, 24]}, "output": 96}, {"input": {"nums": [2, 7, 16, 1, 7, 7, 14, 5, 11]}, "output": 50}, {"input": {"nums": [20, 9, 9, 2, 4, 14, 17, 20]}, "output": 63}] | 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": "02ce82b6166cfc40"} |
vcr_2fd067c86bdc | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [19, -5, -1, -13, 7, 9, 18, 12, -6], "k": 16}, "output": [-1, -13, 7, 9, 18, 12, -6, 19, -5]}, {"input": {"nums": [1, -8, -3, 2, -6], "k": 0}, "output": [1, -8, -3, 2, -6]}, {"input": {"nums": [5, 8, 9, -5, -4, 7], "k": 2}, "output": [-4, 7, 5, 8, 9, -5]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "d6d78351d18208ec"} |
vcr_73e1dec7d81b | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [19, 19, 25, 2, 9, 27, 26, 3, 8, 26]}, "output": 105}, {"input": {"nums": [7, 10, 9, 4, 16]}, "output": 32}, {"input": {"nums": [17, 0, 16, 20, 18, 0, 13]}, "output": 64}] | 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": "c4be1934e9124bdf"} |
vcr_0107241b70db | 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": "d eaebd cdbce eec bdaa cac"}, "output": "cac bdaa eec cdbce eaebd d"}, {"input": {"s": "a abb bbbb bab bbb"}, "output": "bbb bab bbbb abb a"}, {"input": {"s": "aaba a bbab"}, "output": "bbab a aaba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "35689f6a31a30d9c"} |
vcr_3ecc905a8d63 | 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": "dbeddc adc ddada aabecd eebec"}, "output": "eebec aabecd ddada adc dbeddc"}, {"input": {"s": "b ab abb"}, "output": "abb ab b"}, {"input": {"s": "aabb bba"}, "output": "bba aabb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "f8c763265c07e5fe"} |
vcr_10b85a16cad2 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 3, 4, 5, 6, 9, 12, 15, 15]}, "output": [2, 3, 4, 5, 6, 9, 12, 15]}, {"input": {"nums": [0, 1, 2, 3, 3, 7, 7, 8, 9, 9, 12]}, "output": [0, 1, 2, 3, 7, 8, 9, 12]}, {"input": {"nums": [0, 2, 2, 5]}, "output": [0, 2, 5]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "41f352c4c93b9b7f"} |
vcr_e821c006f7b6 | 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, 15, 20, 24, 26, 29, 36, 37, 40, 42, 43, 45, 49], "target": 35}, "output": 6}, {"input": {"nums": [8, 21, 27], "target": 30}, "output": 3}, {"input": {"nums": [0, 4, 6, 20], "target": 36}, "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": "90e1eebf75c4587a"} |
vcr_370985a5810c | 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, 5, 6, 11, 12, 17, 24, 26, 28, 31, 43, 47], "target": 24}, "output": 6}, {"input": {"nums": [0, 2, 33, 39], "target": 23}, "output": 2}, {"input": {"nums": [6, 25, 38], "target": 4}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "9deb9b08635ab886"} |
vcr_3a07c1e6d76f | 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, -14, -9, -8, -3, 8, 9, 14, 23, 27, 47], "target": 6}, "output": [4, 8]}, {"input": {"nums": [-18, -17, -16, -12, -6, 1, 4, 18, 25, 28, 34], "target": 16}, "output": [0, 10]}, {"input": {"nums": [-14, -10, -9, -8, 1, 7, 18, 30], "target": -22}, "output": [0, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "3da1a252d35434f5"} |
vcr_09b1d099df02 | 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": [16, -14, 4, -17, 1, -4, -10, -5, -17, -18, -9, -15]}, "output": 16}, {"input": {"nums": [-4, -8, -3, -9, -6, 6, -12, -2, -3, -11, -2]}, "output": 6}, {"input": {"nums": [-5, 7, 11, -9, 0, 13]}, "output": 22}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "0f013548b5ee53e8"} |
vcr_d0ded0d2cccc | 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": [1, 26, 3, 7, 36, 1, 79, 3, 7, 30, 12, 36, 17, 30, 26, 12, 17]}, "output": 79}, {"input": {"nums": [1, 14, 15, 15, 10, 49, 14, 10, 1]}, "output": 49}, {"input": {"nums": [28, 45, 2, 2, 7, 28, 7]}, "output": 45}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "eeb242080ce32722"} |
vcr_d5058afbbcfd | 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": [-6, -20, -11, -3], "k": 7}, "output": [-20, -11, -3, -6]}, {"input": {"nums": [-9, 3, -6, -9], "k": 10}, "output": [-6, -9, -9, 3]}, {"input": {"nums": [-5, 10, 2, -7, 4, 10, 9, 5], "k": 8}, "output": [-5, 10, 2, -7, 4, 10, 9, 5]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "5b827fae9ba5a642"} |
vcr_f028b95dbdd9 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [20, 7, 12, 30]}, "output": 50}, {"input": {"nums": [10, 20, 5, 1, 3, 13, 1]}, "output": 34}, {"input": {"nums": [3, 5, 9, 17]}, "output": 22}] | 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": "7a929fc96c489fd4"} |
vcr_ec16a4a3b06a | 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": "bddcdcaddb"}, "output": false}, {"input": {"s": "cbccb"}, "output": false}, {"input": {"s": "cca"}, "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": "74faa3d2204ca0be"} |
vcr_82264e3313b9 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [15, 23, 24, 27, 24]}, "output": 63}, {"input": {"nums": [10, 17, 11, 15, 6, 0]}, "output": 32}, {"input": {"nums": [11, 15, 10, 14, 1, 0, 19, 13, 1, 2]}, "output": 50}] | 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": "22d9eae9318c046d"} |
vcr_55d020630be9 | 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": 37870}, "output": 10}, {"input": {"n": 2721}, "output": 5}, {"input": {"n": 6084}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "ece18a91404b1ed8"} |
vcr_69ed6bfe3bc5 | 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, 17, 18, 23, 30, 31, 42, 46], "target": 13}, "output": 2}, {"input": {"nums": [2, 9, 11, 15, 16, 25, 27, 30], "target": 16}, "output": 4}, {"input": {"nums": [0, 6, 9, 16, 21, 25, 28, 30, 35, 37], "target": 32}, "output": 8}] | 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": "11d94335e45f4203"} |
vcr_76091de5e43e | 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": [28, 32, 7, 52, 58, 51, 3]}, "output": 51}, {"input": {"prices": [22, 29, 2, 21, 27, 6, 16, 5, 3, 13]}, "output": 25}, {"input": {"prices": [40, 17, 28]}, "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": "209d562528ea841a"} |
vcr_367aa3e25e03 | 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, 16, -17, 6, -7, 0, -12, -4, -6, 12, 22, 15, 11, 19, 11]}, "output": 90}, {"input": {"nums": [2, -1, 13]}, "output": 14}, {"input": {"nums": [5, -12, -1]}, "output": 5}] | 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": "71eac8fad78ead5b"} |
vcr_e8fcacc0a56d | 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": [-24, -11, 35, 20, 26, -22, -17, -13, -6, 28]}, "output": [-24, -35, 0, 20, 46, 24, 7, -6, -12, 16]}, {"input": {"nums": [-12, 11, 5]}, "output": [-12, -1, 4]}, {"input": {"nums": [-11, 8, 11, -9]}, "output": [-11, -3, 8, -1]}] | 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": "eda86bd2e18e8551"} |
vcr_c4f260b511fe | 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": [36, 20, 5, 48, 5, 29, 34, 11]}, "output": 43}, {"input": {"prices": [31, 38, 31, 18, 7, 3, 8, 17, 16]}, "output": 14}, {"input": {"prices": [32, 15, 22, 19, 27, 35, 2, 26, 27, 24]}, "output": 25}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "b4c30a70d18ab866"} |
vcr_7f8ccd7eed07 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 3, 3, 6, 8, 8, 10, 15]}, "output": [0, 1, 3, 6, 8, 10, 15]}, {"input": {"nums": [5, 7, 7]}, "output": [5, 7]}, {"input": {"nums": [0, 0, 2, 3, 3, 5, 5, 5, 10]}, "output": [0, 2, 3, 5, 10]}] | 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": "c3df013ac08b2915"} |
vcr_4873f2661b5d | 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": [-3, 20, -5, 18, 9]}, "output": 42}, {"input": {"nums": [11, 2, -1, 0, -8, 13, -6, 4]}, "output": 17}, {"input": {"nums": [4, 13, 5, 6, 6, -9, 12, -7, 2]}, "output": 37}] | 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": "f9196e06cae21830"} |
vcr_d150c4bae6c4 | 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, -7, 7, 3, -4, -4, 16, 4, -6, -15, 15]}, "output": 29}, {"input": {"nums": [3, -11, 5, 3, 7]}, "output": 15}, {"input": {"nums": [-9, 14, -3, -9, 2, 3, -6, -2, -1, -4, -7]}, "output": 14}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "3a6d995fa5697b9f"} |
vcr_04de2787d7c4 | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-15, 36, -14, -16, 66, -36, -2, 17], "target": 53}, "output": [1, 7]}, {"input": {"nums": [14, -26, -23, 27, -13, 16, 38, 37, 31], "target": 3}, "output": [4, 5]}, {"input": {"nums": [5, -27, 13, -29, -3, -28, 3], "target": -16}, "output": [2, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "81ce603da9c77ca1"} |
vcr_73fc3f266854 | 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": ["abac", "bca", "ba", "caaa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "baa", "bab", "bab"]}, "output": [1, 3]}, {"input": {"strs": ["abb", "bbb", "abb", "baa", "aab", "bab", "baa"]}, "output": [1, 3, 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": "6cd5c08c9769ba1d"} |
vcr_b2247cea39e5 | 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": "acbbcbabab"}, "output": 9}, {"input": {"s": "baaab"}, "output": 3}, {"input": {"s": "aabcccbccaa"}, "output": 6}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "7a8d45fd6f75bf9e"} |
vcr_70348a385cdd | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [25, 4, 4, 24, 9, 27, 27, 15, 30]}, "output": 106}, {"input": {"nums": [5, 12, 14, 12, 7, 4, 20]}, "output": 46}, {"input": {"nums": [4, 4, 18, 0]}, "output": 22}] | 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": "7cf0290b3c15e013"} |
vcr_4c929ef5e0fb | 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": [26, 60, 2, 27, 20]}, "output": 34}, {"input": {"prices": [29, 22, 19, 23, 29, 27, 9]}, "output": 10}, {"input": {"prices": [29, 16, 40, 28, 28]}, "output": 24}] | 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": "aeb545727d5b1e58"} |
vcr_f0ba552d52f9 | 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": 50552}, "output": 8}, {"input": {"n": 10649}, "output": 7}, {"input": {"n": 15805}, "output": 11}] | 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": "f79b48f67dfd9e8f"} |
vcr_051f50428fb1 | 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, -23, -4, 3, 19, 39], "target": -22}, "output": [0, 3]}, {"input": {"nums": [-11, -10, -9, -2, 4, 20, 23, 33], "target": 14}, "output": [2, 6]}, {"input": {"nums": [-8, 12, 24, 28, 31, 32, 33], "target": 55}, "output": [2, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "3bd0e33d1bb26e14"} |
vcr_8d19fd037562 | 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, 12, 27, 17, 28, 27, 12, 16, 27, 0]}, "output": 121}, {"input": {"nums": [19, 14, 7, 18, 18, 11, 9]}, "output": 53}, {"input": {"nums": [20, 6, 9, 6, 7, 17, 3]}, "output": 46}] | 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": "8040d266ff330b03"} |
vcr_c6deeb9f7e7e | 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": [4, 1, -1, -11, 14, -4, -6, 3, 4, -9, -11, -16], "k": 17}, "output": [3, 4, -9, -11, -16, 4, 1, -1, -11, 14, -4, -6]}, {"input": {"nums": [2, -5, 1, -1, 0, -2, -6], "k": 0}, "output": [2, -5, 1, -1, 0, -2, -6]}, {"input": {"nums": [0, 10, -8, -6, 1, -9, -8, 5, 0], "k": 10}, "output": [0, 0, 10, -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": "ca78fa83639cf19c"} |
vcr_833462ac14d3 | 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": [10, 13, 39, 8, 37, 38, 1, 13, 8, 30, 39, 78, 30, 38, 1, 10, 37]}, "output": 78}, {"input": {"nums": [25, 3, 17, 20, 6, 11, 6, 11, 20, 3, 25, 17, 45]}, "output": 45}, {"input": {"nums": [24, 12, 41, 24, 12]}, "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": "20af269dd5643e60"} |
vcr_2a52d45d04d0 | 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, 4, -18, 22, -18, -11, -6, -11, -6]}, "output": 22}, {"input": {"nums": [2, 0, 1, 0, 0, -8]}, "output": 3}, {"input": {"nums": [6, -3, -4, 3, -9]}, "output": 6}] | 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": "e20dd11d0a635082"} |
vcr_060f48f6dcb8 | 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": 65, "b": 239}, "output": 1}, {"input": {"a": 83, "b": 76}, "output": 1}, {"input": {"a": 125, "b": 84}, "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": "e9707b3f6dcb160f"} |
vcr_1b81a0892829 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-20, 21, -24, 34, 9, -21]}, "output": [-20, 1, -23, 11, 20, -1]}, {"input": {"nums": [4, 13, 9, -12, -4, 10]}, "output": [4, 17, 26, 14, 10, 20]}, {"input": {"nums": [-2, -4]}, "output": [-2, -6]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "38f4e5a51d154b85"} |
vcr_cf23d9bf77e3 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-17, 7, -8, -3, -5, 0, 19], "k": 20}, "output": [7, -8, -3, -5, 0, 19, -17]}, {"input": {"nums": [-6, 9, -6], "k": 20}, "output": [9, -6, -6]}, {"input": {"nums": [4, 3, -10, 8, -5, -10, 4], "k": 0}, "output": [4, 3, -10, 8, -5, -10, 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": "408cb306f9244b66"} |
vcr_70e1f039569d | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 2, 3, 7, 7, 8, 9, 10, 11, 13]}, "output": [0, 2, 3, 7, 8, 9, 10, 11, 13]}, {"input": {"nums": [1, 1, 2, 5, 8, 8, 9, 10, 10, 12]}, "output": [1, 2, 5, 8, 9, 10, 12]}, {"input": {"nums": [0, 3, 5]}, "output": [0, 3, 5]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "a1539dfc4533a1d4"} |
vcr_a9e694993d03 | 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": "abdbbbbbdba"}, "output": true}, {"input": {"s": "aabaccc"}, "output": false}, {"input": {"s": "cbcacca"}, "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": "71e8a0152443f3ae"} |
vcr_2b095219ac3f | 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": [46, 20, 41, 28, 5, 57]}, "output": 52}, {"input": {"prices": [21, 36, 32, 10, 27, 36, 15, 5, 22, 10]}, "output": 26}, {"input": {"prices": [8, 37, 1, 10, 27]}, "output": 29}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "c56820a22249ea42"} |
vcr_dbd8152bb3f5 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [14, -8, -16, -6, 56, 67, 31, 3, 60, -19, 64, 21, -2], "target": 74}, "output": [0, 8]}, {"input": {"nums": [-28, -12, -29, 31, -26, 17, 39], "target": 19}, "output": [1, 3]}, {"input": {"nums": [17, 44, 25, 4, 27, -21], "target": 71}, "output": [1, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "aa19dcdb4f85797a"} |
vcr_3cd1e666be09 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [25, -3, -10, -24, 32, 32, 26, -24, 27, 6, -7, -14]}, "output": [25, 22, 12, -12, 20, 52, 78, 54, 81, 87, 80, 66]}, {"input": {"nums": [10, 9, -1, 1, 11, 8]}, "output": [10, 19, 18, 19, 30, 38]}, {"input": {"nums": [7, -4, -2, 1, 9, 11, 6, 16, -2, 15]}, "output": [7, 3, 1, 2, 11, 22, 28, 44, 42, 57]... | 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": "09ba55000bd2eb98"} |
vcr_1c2a31420d30 | 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": [7, -15, -4, -9, -18, 1, -17]}, "output": 7}, {"input": {"nums": [-10, -7, -11, 5, -11, -7, -5, 8, -4, 8]}, "output": 12}, {"input": {"nums": [3, 3, -7, 3, -12, 4, -5, 6, -12, 5, 7]}, "output": 12}] | 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": "6645c93ed270769b"} |
vcr_680236dafdd6 | 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": "aaa cbaee ababad"}, "output": "ababad cbaee aaa"}, {"input": {"s": "aab ab aa"}, "output": "aa ab aab"}, {"input": {"s": "abb aba bab bb"}, "output": "bb bab aba abb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "3314c93391050d12"} |
vcr_f73230c447bd | 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": "ecbbd dad bdcc"}, "output": "bdcc dad ecbbd"}, {"input": {"s": "ba bba bb aaab aab"}, "output": "aab aaab bb bba ba"}, {"input": {"s": "abba ab b"}, "output": "b ab abba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "e4d303fa65e1fed6"} |
vcr_1cb025b98d9e | 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, 29, 30, 31, 40, 47], "target": 5}, "output": 1}, {"input": {"nums": [2, 6, 11, 12, 13, 17, 21, 23, 26, 30, 33], "target": 27}, "output": 9}, {"input": {"nums": [0, 9, 22, 23, 24, 26, 29, 32, 34, 37, 38], "target": 40}, "output": 11}] | 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": "3d8c3d71f0e0f2a0"} |
vcr_a519fb3b8d0f | 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, 0, 27, 33, 40], "target": 40}, "output": [1, 4]}, {"input": {"nums": [-20, -15, -9, -3, 3, 13, 16, 25, 27, 28], "target": 5}, "output": [0, 7]}, {"input": {"nums": [-18, -17, -12, -9, 1, 35, 37, 38], "target": -11}, "output": [2, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "60962a3d59181239"} |
vcr_cbda2013f41a | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-23, -19, 7, 9, 18, 31, 34], "target": -16}, "output": [0, 2]}, {"input": {"nums": [-12, -9, 2, 5, 23, 36], "target": 14}, "output": [1, 4]}, {"input": {"nums": [5, 11, 13, 16], "target": 16}, "output": [0, 1]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "5a0e56800d4d42d2"} |
vcr_cadd5dbe7c70 | medium | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-17, 74, 43, 49, -36, -30, -35, 9, 0, 10, -37, 61, -9], "target": 39}, "output": [1, 6]}, {"input": {"nums": [-18, -25, 41, -2, 24, 29, 21, 44], "target": 73}, "output": [5, 7]}, {"input": {"nums": [44, 21, 4, 16, 28], "target": 65}, "output": [0, 1]}] | 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": "b54ef63a1344aed9"} |
vcr_a4e32e27a2f8 | 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, 3, 7, 4, 4, 5, 7, 2]}, "output": [23520, 31360, 13440, 23520, 23520, 18816, 13440, 47040]}, {"input": {"nums": [3, 5, 1, 1, 3]}, "output": [15, 9, 45, 45, 15]}, {"input": {"nums": [4, 2, 5, 1, 3, 5, 4]}, "output": [600, 1200, 480, 2400, 800, 480, 600]}] | 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": "c4801ac2c6ecf2bf"} |
vcr_e11474e00072 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 1, 3, 5, 5, 5, 6, 10, 13, 13, 13]}, "output": [1, 3, 5, 6, 10, 13]}, {"input": {"nums": [1, 3, 6, 6, 12]}, "output": [1, 3, 6, 12]}, {"input": {"nums": [0, 1, 2, 3, 3, 5, 5, 9, 9, 10, 10]}, "output": [0, 1, 2, 3, 5, 9, 10]}] | 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": "3fac28b1948aa691"} |
vcr_baf9b9bb1841 | 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, 7, 12, 15, 18, 20, 23, 30, 47], "target": 7}, "output": 1}, {"input": {"nums": [7, 12, 21, 30, 34, 39], "target": 31}, "output": 4}, {"input": {"nums": [3, 6, 10, 11, 25, 34, 35, 36, 37], "target": 21}, "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": "5ec4d045f6fe7be9"} |
vcr_34fae3d7bd01 | 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": [46, 144, 0, 98, 190, 169, 10, 4, 93, 97, 26, 84, 51, 180, 65, 127]}, "output": false}, {"input": {"nums": [2, 8, 13, 9, 0]}, "output": false}, {"input": {"nums": [12, 6, 6, 2, 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": "e4b0bee9bc20f227"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.