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_5aed58f20c6a | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [9, 21, 22, 23, 39, 41, 43, 48], "target": 21}, "output": 1}, {"input": {"nums": [6, 14, 15, 19, 25, 34, 36], "target": 2}, "output": 0}, {"input": {"nums": [8, 18, 24, 37, 39], "target": 27}, "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": "b42a9dea3d1caeee"} |
vcr_1add20b3e9b3 | 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": "aecdb ddac dbcdaa aa ecae"}, "output": "ecae aa dbcdaa ddac aecdb"}, {"input": {"s": "abb bbba bab"}, "output": "bab bbba abb"}, {"input": {"s": "aaba b abaa"}, "output": "abaa b 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": "738cdb39417a6dd7"} |
vcr_413efdbdeea6 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 4, 0, 3, 2, 4, 3, 0, 0]}, "output": true}, {"input": {"nums": [2, 1, 2, 2, 3, 2, 3]}, "output": true}, {"input": {"nums": [0, 1, 0, 0, 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": "96de927f06eff6aa"} |
vcr_b55619134099 | 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": [49, -31, 18, 56, 23, 1], "target": -13}, "output": [1, 2]}, {"input": {"nums": [-16, 49, 27, -17, -13, 25], "target": -29}, "output": [0, 4]}, {"input": {"nums": [34, -9, 48, -29, 41, -21], "target": 82}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "668117e87cce774f"} |
vcr_937e62555fae | 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": 160, "b": 84}, "output": 4}, {"input": {"a": 149, "b": 130}, "output": 1}, {"input": {"a": 121, "b": 95}, "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": "cb9001af9129623c"} |
vcr_e2253a078c7e | 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, -17, -12, 7, 28, 37], "target": 4}, "output": [0, 4]}, {"input": {"nums": [-19, -11, -3, -1, 31, 36], "target": 25}, "output": [1, 5]}, {"input": {"nums": [-15, 3, 22, 30], "target": -12}, "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": "e1c9948ce7e938fa"} |
vcr_d7e3ebc4b1ba | 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": 133, "b": 165}, "output": 1}, {"input": {"a": 152, "b": 63}, "output": 1}, {"input": {"a": 112, "b": 91}, "output": 7}] | 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": "64b590bfef1753fd"} |
vcr_050b88343e45 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [5, 5, 6, 21, 6, 27, 23, 10, 0]}, "output": 63}, {"input": {"nums": [9, 15, 14, 6, 10, 12, 10]}, "output": 43}, {"input": {"nums": [15, 17, 15, 5]}, "output": 30}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "a1c3e417ac3a78cd"} |
vcr_e66f068b34dc | 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, -23, -17, -14, -11, -10, 4, 30, 38], "target": -21}, "output": [4, 5]}, {"input": {"nums": [-19, -11, -6, -5, 1, 10, 19, 20, 21, 24], "target": 29}, "output": [5, 6]}, {"input": {"nums": [-8, 7, 8, 13, 20, 21, 25], "target": 45}, "output": [4, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "a7db873ef1303cf7"} |
vcr_0ab72939c86c | 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": "cbcccacacccbc"}, "output": true}, {"input": {"s": "accbabc"}, "output": false}, {"input": {"s": "ac"}, "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": "4a86e2226bb74a4f"} |
vcr_c32ff2258d51 | 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": ["aa", "aaa", "cb", "ac", "baa", "aabc", "bc"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "aba", "bba", "baa", "aba", "baa"]}, "output": [1, 5]}, {"input": {"strs": ["baa", "bbb", "aaa"]}, "output": [1, 1, 1]}] | 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": "6e2cff174c065946"} |
vcr_821f72a7034d | 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": [8, 177, 126, 58, 188]}, "output": false}, {"input": {"nums": [24, 3, 17, 25, 10, 4, 16, 1, 18, 5, 6, 23]}, "output": false}, {"input": {"nums": [8, 13, 19]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "84d1917c794113db"} |
vcr_938a9007d794 | 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": "cebb cdbaeb decbb c abebc b"}, "output": "b abebc c decbb cdbaeb cebb"}, {"input": {"s": "aa abb abba ab a"}, "output": "a ab abba abb aa"}, {"input": {"s": "aa aba"}, "output": "aba aa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "3b3cf2846d93e49a"} |
vcr_b45c51fa9549 | 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": [5, 8, 9, 10, 12, 13, 14, 14, 14, 15, 15]}, "output": [5, 8, 9, 10, 12, 13, 14, 15]}, {"input": {"nums": [0, 1, 5, 6, 7, 8, 8, 9, 10, 11, 12]}, "output": [0, 1, 5, 6, 7, 8, 9, 10, 11, 12]}, {"input": {"nums": [1, 1, 2, 2, 3, 4, 6, 8, 9, 12]}, "output": [1, 2, 3, 4, 6, 8, 9, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "825c80abcba8d2c9"} |
vcr_32e5f7eccaf2 | 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": [13, 15, 18, 22, 26, 33, 40, 43, 44, 45, 48], "target": 44}, "output": 8}, {"input": {"nums": [8, 9, 29, 38], "target": 16}, "output": 2}, {"input": {"nums": [4, 12, 22, 24], "target": 30}, "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": "66d327fa64f014bc"} |
vcr_a3156eb5a50e | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [31, 46, 60, 37, 20, 14, 57, 41, 57]}, "output": 43}, {"input": {"prices": [32, 5, 38, 16, 9]}, "output": 33}, {"input": {"prices": [10, 40, 20, 12, 11]}, "output": 30}] | 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": "8e11dcf3e9ea5fd3"} |
vcr_50d990a4e7f9 | 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": [12, 12, 10, 28, 16, 24, 1, 6, 29, 29, 23, 11]}, "output": 116}, {"input": {"nums": [6, 13, 5, 6, 7]}, "output": 20}, {"input": {"nums": [14, 20]}, "output": 20}] | 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": "2a31dd1aff5a81fc"} |
vcr_0a71a21bb5c3 | 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": [13, 18, 7, 30, 11]}, "output": 48}, {"input": {"nums": [18, 8, 6, 9]}, "output": 27}, {"input": {"nums": [4, 10, 18, 5, 17, 20, 6, 14]}, "output": 56}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "5c337f92e8bc0153"} |
vcr_c0a45ffc9a9c | 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": 6, "b": 40}, "output": 2}, {"input": {"a": 93, "b": 98}, "output": 1}, {"input": {"a": 61, "b": 173}, "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": "026f73fdda7cb79a"} |
vcr_a0f021b320ba | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-11, -11, 15, 22, 16, -16, 15, 20]}, "output": 72}, {"input": {"nums": [-7, 11, -4, -3, 12, -11]}, "output": 16}, {"input": {"nums": [-6, 1, 12, 10, -10, -3, -10]}, "output": 23}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "e34fc2d218ecd530"} |
vcr_4f6dffc722b1 | 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": [2, 26, 47, 33, 41, 50, 2]}, "output": true}, {"input": {"nums": [0, 16, 19, 11, 10, 19, 17, 2, 25, 17]}, "output": true}, {"input": {"nums": [23, 8, 24, 11, 10, 6, 11]}, "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": "3a22276adb1c8c14"} |
vcr_db01b37b5c81 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-11, 8, 11, 9, 21, 4, -11, -18, 22, 17, -3, -11, 22, 8]}, "output": 79}, {"input": {"nums": [-1, -12, -2, -5, 2, 0, -12]}, "output": 2}, {"input": {"nums": [5, 11, -2, 5, -10, 7, 12, 10, 7, 14, -8]}, "output": 59}] | 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": "b667456da8dbab34"} |
vcr_a16cbe6881ab | 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": 69, "b": 111}, "output": 3}, {"input": {"a": 72, "b": 166}, "output": 2}, {"input": {"a": 12, "b": 174}, "output": 6}] | 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": "bf83a579165bdf78"} |
vcr_acc0cceae2b1 | 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, 13, 2, 13, 1, 7, 29, 11, 4]}, "output": 73}, {"input": {"nums": [10, 4, 13, 6, 20, 10]}, "output": 43}, {"input": {"nums": [11, 13, 17, 3, 2, 16, 3, 9, 19, 2]}, "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": "daec423b772cc44d"} |
vcr_5fd4d860a4ea | 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": [9, 20, 8]}, "output": 20}, {"input": {"nums": [7, 8, 12, 0, 9, 7, 15]}, "output": 43}, {"input": {"nums": [12, 19]}, "output": 19}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "4b6e0c7010c764d8"} |
vcr_89974865f606 | 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, 1, 48, 4, 14, 6, 5, 31, 44]}, "output": 47}, {"input": {"prices": [17, 18, 24, 19, 7]}, "output": 7}, {"input": {"prices": [36, 36, 6, 22, 37, 40, 3]}, "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": "5f9f0a022f5c9498"} |
vcr_ec77acbdf575 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 0, 2, 3, 1, 0, 0, 1, 0]}, "output": false}, {"input": {"nums": [1, 2, 1, 0, 3, 3, 2]}, "output": false}, {"input": {"nums": [1, 0, 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": "bdfc60ac1d12b1bd"} |
vcr_0c37375a540e | 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": [1, 7, 21, 20, 23, -5, 1, 27]}, "output": [1, 8, 29, 49, 72, 67, 68, 95]}, {"input": {"nums": [15, -4, -1]}, "output": [15, 11, 10]}, {"input": {"nums": [-3, -9, 1, 5, -3, 17, -1, 6]}, "output": [-3, -12, -11, -6, -9, 8, 7, 13]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "5e369a16a97469d0"} |
vcr_d1a2903bc523 | 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": [67, 17, 4, 61, -4], "target": 84}, "output": [0, 1]}, {"input": {"nums": [45, -4, -15, 18, 12, -1, 0, 33], "target": -3}, "output": [2, 4]}, {"input": {"nums": [11, -28, -12, 44, -26], "target": -40}, "output": [1, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "662a62b6921a375f"} |
vcr_8af75c824398 | 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": ["acba", "bcc", "ba", "cca"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["abb", "baa", "abb", "baa", "abb", "bbb"]}, "output": [1, 2, 3]}, {"input": {"strs": ["aaa", "aaa", "aba"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "c8247c323f5532fd"} |
vcr_916a3d8d09f1 | 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": 63311}, "output": 12}, {"input": {"n": 1452}, "output": 6}, {"input": {"n": 11567}, "output": 9}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "321d43d0204e75ef"} |
vcr_2b879bdc097e | 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": "dcdacdddcbdabc"}, "output": false}, {"input": {"s": "acabcaaaaa"}, "output": false}, {"input": {"s": "cbbaaabbc"}, "output": true}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "5422511a76d7ff3a"} |
vcr_3b2043fc55b9 | 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": ["cbac", "bb", "ca", "ba", "aa"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bab", "aba", "baa", "aba"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aba", "aab", "abb"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "97f6d4b767e4bf94"} |
vcr_b5f9cf3ac6c8 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [3, 2, 4, 5, 2, 5, 5]}, "output": [2000, 3000, 1500, 1200, 3000, 1200, 1200]}, {"input": {"nums": [4, 4, 4]}, "output": [16, 16, 16]}, {"input": {"nums": [3, 5, 5, 3, 5, 3, 2]}, "output": [2250, 1350, 1350, 2250, 1350, 2250, 3375]}] | 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": "97eccedb54872f63"} |
vcr_0337f1862785 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 4, 3, 5, 4, 1, 5]}, "output": [1200, 2100, 2800, 1680, 2100, 8400, 1680]}, {"input": {"nums": [1, 1, 4, 3, 4, 1, 2]}, "output": [96, 96, 24, 32, 24, 96, 48]}, {"input": {"nums": [3, 3, 2, 2, 3, 4, 2]}, "output": [288, 288, 432, 432, 288, 216, 432]}] | 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": "6b49de191aa29c6b"} |
vcr_f3c8cfad40c8 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [16, 4, 35, 68, 4, 35, 16]}, "output": 68}, {"input": {"nums": [41, 12, 24, 24, 2, 2, 1, 12, 1]}, "output": 41}, {"input": {"nums": [22, 27, 22, 11, 3, 3, 11, 43, 28, 28, 29, 27, 29]}, "output": 43}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "384720fd269af39e"} |
vcr_e8a4541f5d8d | 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": [52, 30, 24, 57, 12, 17, 44, 52, 30, 21, 58]}, "output": 46}, {"input": {"prices": [34, 7, 16, 17, 14, 21, 11, 15, 38, 15, 1, 40]}, "output": 39}, {"input": {"prices": [25, 3, 17, 30, 13, 16, 34, 19, 18]}, "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": "1245916bf32918cf"} |
vcr_c3a85a5be660 | 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": [56, 59, 45, 43, 17, 41, 55, 28, 11, 19, 2, 57, 56, 7, 40]}, "output": 55}, {"input": {"prices": [3, 2, 34, 20, 39, 34]}, "output": 37}, {"input": {"prices": [1, 29, 15, 1, 21]}, "output": 28}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "ecf6419605c1ac50"} |
vcr_17fe71988447 | 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, 13, 15, 16, 29, 30, 31, 32, 35, 42, 47, 49], "target": 48}, "output": 12}, {"input": {"nums": [1, 9, 10, 19, 21, 23, 25, 26, 27, 30, 34], "target": 30}, "output": 9}, {"input": {"nums": [5, 7, 14, 15, 27, 30], "target": 40}, "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": "82d54ba7ca01776d"} |
vcr_86262efbbeda | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-10, 4, 13, -15, 27, 28, 1, -2, 15, 6, 23]}, "output": [-10, -6, 7, -8, 19, 47, 48, 46, 61, 67, 90]}, {"input": {"nums": [-1, -6, -10, 14, -3, -5, 18]}, "output": [-1, -7, -17, -3, -6, -11, 7]}, {"input": {"nums": [2, -1, 11, -1]}, "output": [2, 1, 12, 11]}] | 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": "86c03284995b0229"} |
vcr_64f56972c1d7 | 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": "bbbcbcbdb"}, "output": false}, {"input": {"s": "baa"}, "output": false}, {"input": {"s": "bcac"}, "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": "a74eec4529129ab0"} |
vcr_f0ce09affe2e | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [9, 3, 1, 1, -7, 3], "k": 1}, "output": [3, 9, 3, 1, 1, -7]}, {"input": {"nums": [-9, -4, 7, -10, 2, -7, -5, 8, -6], "k": 17}, "output": [-4, 7, -10, 2, -7, -5, 8, -6, -9]}, {"input": {"nums": [-8, 1, -8, -10], "k": 16}, "output": [-8, 1, -8, -10]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "608063b0bff3c691"} |
vcr_2182aab693ee | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-4, 0, -1, 22, 17, -9, 1, 2]}, "output": 39}, {"input": {"nums": [8, 11, -11, 3, 1]}, "output": 19}, {"input": {"nums": [-6, 12, -7, 10, -9, -11, 8]}, "output": 15}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "024553fd391f5d70"} |
vcr_d4dca208b61a | 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": [12, 14, 23, 57, 6, 5, 52, 13, 34, 5, 26, 15, 16, 2, 37]}, "output": 47}, {"input": {"prices": [19, 17, 39, 31, 28, 38, 39, 18, 23, 35, 10]}, "output": 22}, {"input": {"prices": [20, 17, 6, 30, 25, 3, 12, 23]}, "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": "93e05c87c46b18cf"} |
vcr_e928fd3ad3fa | 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": 43042}, "output": 5}, {"input": {"n": 11627}, "output": 9}, {"input": {"n": 9790}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "a5aedf87c79a35ae"} |
vcr_d2b6bb6132a7 | 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": [9, 18, 3, 36, 9, 55, 3, 35, 35, 36, 18]}, "output": 55}, {"input": {"nums": [4, 42, 6, 4, 6]}, "output": 42}, {"input": {"nums": [14, 48, 26, 25, 23, 25, 26, 14, 3, 23, 3]}, "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": "0fe438fa78f4f3fe"} |
vcr_3c5541237428 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [33, 19, 16, 34, 15, 10, 35, 15, 20, -9, 17, 34]}, "output": [33, 52, 68, 102, 117, 127, 162, 177, 197, 188, 205, 239]}, {"input": {"nums": [-7, 18]}, "output": [-7, 11]}, {"input": {"nums": [9, -9, -4, 16, 17, 6]}, "output": [9, 0, -4, 12, 29, 35]}] | 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": "65d125d29640a1b7"} |
vcr_ceaea3d7043f | 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, 2, 20, 2, 16, 22, -4, 6, -15, -18, 16, -13, 4, 0]}, "output": 80}, {"input": {"nums": [4, 14, 10, 13, -4]}, "output": 41}, {"input": {"nums": [10, 1, 6, 11, 14, -2]}, "output": 42}] | 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": "4076265cdc589c8f"} |
vcr_88fdc53acb6f | 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": [21, -6, 11, -16, 12, -4, -13, 20, -2, -8, -12, 22, -1, 15, 18]}, "output": 57}, {"input": {"nums": [2, 9, 0]}, "output": 11}, {"input": {"nums": [-2, 10, 4, -4, 12, -11, 13, -2, 13]}, "output": 35}] | 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": "82b2dbc71e431042"} |
vcr_dd79f04c9583 | 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": [184, 131, 186, 147, 42, 76, 136, 164, 46, 130]}, "output": false}, {"input": {"nums": [4, 25, 3, 4, 5, 23, 20, 7]}, "output": true}, {"input": {"nums": [5, 24, 2, 20, 9, 6]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "34c61f6358e3f709"} |
vcr_77d1fde9559b | 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": "aabbdddacbd"}, "output": false}, {"input": {"s": "aaa"}, "output": true}, {"input": {"s": "aabaac"}, "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": "5794e63d7a15b62e"} |
vcr_692bd7f05efa | 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": [29, 6, 31, 10, 23, 34, 11, 35]}, "output": [29, 35, 66, 76, 99, 133, 144, 179]}, {"input": {"nums": [-7, 7]}, "output": [-7, 0]}, {"input": {"nums": [-9, 8, -3, -6, -11, 16, -9]}, "output": [-9, -1, -4, -10, -21, -5, -14]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "0ba5b71839027897"} |
vcr_d9f7831cc1d6 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 4, 5, 6, 3, 2, 2]}, "output": [1440, 360, 288, 240, 480, 720, 720]}, {"input": {"nums": [1, 2, 1, 2]}, "output": [4, 2, 4, 2]}, {"input": {"nums": [4, 5, 2, 2, 5, 3, 3]}, "output": [900, 720, 1800, 1800, 720, 1200, 1200]}] | 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": "57d7958971c43d46"} |
vcr_04f6e6cc6016 | 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, 7, 13, 14, 15, 24, 38, 49], "target": 45}, "output": [1, 6]}, {"input": {"nums": [-17, -12, -5, 0, 16, 17, 18, 23, 28, 37], "target": 16}, "output": [1, 8]}, {"input": {"nums": [5, 10, 13, 17, 18, 19, 20, 26, 29, 37], "target": 47}, "output": [1, 9]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "b008e884796e064a"} |
vcr_0b76684a406e | 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": [30, 53, 13, 35, 30]}, "output": 23}, {"input": {"prices": [15, 3, 13, 9, 15, 25, 37, 24, 38]}, "output": 35}, {"input": {"prices": [26, 30, 26, 13]}, "output": 4}] | 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": "a1ed348e895126ad"} |
vcr_782205112427 | 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": [63, 22, 77, 79, 2, -19, 41, 45, 58, -7, 7, 17, 61], "target": 62}, "output": [7, 11]}, {"input": {"nums": [48, 46, -3, 12, -26], "target": 58}, "output": [1, 3]}, {"input": {"nums": [32, 43, -18, 10, 27, 41, -6], "target": 14}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "27de01873972e9a0"} |
vcr_649876193c12 | 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, 19, 21, 28, 29], "target": 24}, "output": 3}, {"input": {"nums": [1, 7, 8, 13, 14, 17, 24, 25, 35], "target": 7}, "output": 1}, {"input": {"nums": [1, 4, 8, 14, 17, 18, 21, 28, 37], "target": 11}, "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": "0d9c6926b8616d20"} |
vcr_7846535336c6 | 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": [149, 178, 115, 134, 182, 107, 151, 49, 147, 127, 155, 185, 44, 148, 68, 64, 20, 114]}, "output": false}, {"input": {"nums": [9, 9, 20, 0, 6, 20, 0, 4]}, "output": true}, {"input": {"nums": [23, 22, 2, 3, 7, 14, 5, 18, 25]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "88b0f98efeeebf10"} |
vcr_3253974f1258 | 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": ["cccc", "bc", "aa", "ccc", "aaca"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "baa", "aaa", "aaa"]}, "output": [1, 3]}, {"input": {"strs": ["bba", "aba", "abb"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "6eadadaf08c2353a"} |
vcr_819a125705c7 | 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": [30, 28, 24, 28, 18, 4, 23, 21, 26]}, "output": 121}, {"input": {"nums": [14, 13, 19, 18, 10, 19, 18, 8, 8, 12]}, "output": 73}, {"input": {"nums": [6, 5, 7, 16, 8, 9, 13, 18, 17]}, "output": 52}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "b4bfc40588517ac5"} |
vcr_e97f7594085a | 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": [-14, -11, 13, 3, 20, -19, -15, -14, -17], "k": 10}, "output": [-17, -14, -11, 13, 3, 20, -19, -15, -14]}, {"input": {"nums": [-10, 4, -8, 1, 9, 8, -9, -7], "k": 20}, "output": [9, 8, -9, -7, -10, 4, -8, 1]}, {"input": {"nums": [9, 10, 8, -1, -1], "k": 13}, "output": [8, -1, -1, 9, 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": "0900d963e1ecc0d5"} |
vcr_ad6b6b529e33 | 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, 8, 10, 20, 22, 24, 37, 38, 41, 42, 46, 47], "target": 20}, "output": 4}, {"input": {"nums": [0, 2, 3, 15, 18, 19, 26, 31], "target": 16}, "output": 4}, {"input": {"nums": [1, 3, 4, 6, 12, 17, 29, 33, 37], "target": 36}, "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": "ce90190363b4f99e"} |
vcr_04b009738d21 | 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": "dacbbcbcbada"}, "output": false}, {"input": {"s": "cabb"}, "output": false}, {"input": {"s": "bcacbcbacc"}, "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": "b9b7b3d6c0dbada4"} |
vcr_2eef8c1434df | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-20, -14, 2, 3, 8, 37, 38, 41, 49], "target": -6}, "output": [1, 4]}, {"input": {"nums": [-10, -6, 7, 9, 15, 24, 35], "target": 9}, "output": [1, 4]}, {"input": {"nums": [-20, -9, 16, 19, 22, 37, 39], "target": 59}, "output": [4, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "c5095a76a2a8b1f3"} |
vcr_c6b81292b358 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [21, 30, 5, 13, 24, 12, 18, 13]}, "output": 72}, {"input": {"nums": [8, 13, 19, 19, 9, 0, 19]}, "output": 55}, {"input": {"nums": [1, 14, 8, 20, 15, 1, 8]}, "output": 42}] | 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": "b84f1a0d1b51bda6"} |
vcr_48af6f2ab388 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 0, 3, 4, 2, 1, 0]}, "output": true}, {"input": {"nums": [2, 1, 3, 1, 1, 0, 2, 3, 0]}, "output": false}, {"input": {"nums": [3, 3, 0, 3, 1, 0, 3, 1, 0]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "bf8fd057f95ea9c7"} |
vcr_62658892abdc | 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, 8, 0, 8, 4, 18, 10, 1, 23]}, "output": 68}, {"input": {"nums": [3, 20, 16, 9, 19, 6, 2, 12, 10]}, "output": 51}, {"input": {"nums": [6, 2, 0, 11, 11, 11, 9, 18, 16, 18]}, "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": "8e5c83daa3a5bcb3"} |
vcr_c5e2e48ccaec | 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, 4, 9, 21, 25, 27, 31, 34, 42, 47, 49], "target": 40}, "output": 8}, {"input": {"nums": [3, 7, 12, 15, 17, 21, 24, 30, 34, 36, 37, 39], "target": 34}, "output": 8}, {"input": {"nums": [7, 15, 31, 33], "target": 18}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "986ca30efcf1cedf"} |
vcr_8e6adc63427a | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 3, 3, 4, 5, 11, 13, 13]}, "output": [0, 2, 3, 4, 5, 11, 13]}, {"input": {"nums": [2, 2, 5, 5, 6, 6, 8, 8, 10, 10]}, "output": [2, 5, 6, 8, 10]}, {"input": {"nums": [1, 1, 3, 3, 8, 9, 11]}, "output": [1, 3, 8, 9, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "a744f51135f81346"} |
vcr_90f57ad2da0d | 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, 13, 14, 15, 16, 29, 40, 46], "target": 5}, "output": 1}, {"input": {"nums": [1, 4, 20], "target": 22}, "output": 3}, {"input": {"nums": [3, 6, 7, 10, 18, 20, 24, 28, 36, 39], "target": 8}, "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": "401f78872e7ce2b8"} |
vcr_14651bb4b109 | 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", "cb", "ccaa", "bcac", "bc", "acb", "aaa"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["abb", "bba", "aab", "bbb", "baa", "aaa", "bba"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["bab", "baa", "aba", "bbb", "aaa", "bab"]}, "output": [1, 1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "0a976375e664b1b1"} |
vcr_58bc1e8fdd6e | 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": ["bcc", "ab", "cccc", "aaa", "cabb", "aca", "cb"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "aab", "abb", "baa", "bba"]}, "output": [2, 3]}, {"input": {"strs": ["bab", "aab", "baa", "abb", "bbb", "abb", "abb"]}, "output": [1, 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": "3d11351ce11179c1"} |
vcr_6a60d4744ef2 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [17, 23, 24, 30, 13, 19, 23, 10, 7, 19, 21]}, "output": 105}, {"input": {"nums": [11, 12, 18, 3, 5, 5, 20, 17, 13, 8]}, "output": 67}, {"input": {"nums": [9, 12, 16, 6, 7, 5, 20, 12, 8]}, "output": 60}] | 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": "da919ed2d9213f30"} |
vcr_96428b80f7aa | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [33, 6, 37, 62, 6, 33, 37]}, "output": 62}, {"input": {"nums": [12, 3, 5, 22, 17, 12, 22, 3, 55, 17, 16, 5, 16]}, "output": 55}, {"input": {"nums": [47, 12, 16, 16, 12]}, "output": 47}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "848ab8a29bf6e32c"} |
vcr_e264369edcea | 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": [-6, 1, 11, 7, 15, 19, -14, -3]}, "output": 53}, {"input": {"nums": [12, -5, -4, 11, 1, -11, -8, -1]}, "output": 15}, {"input": {"nums": [13, 4, 5, 2, 12, -9, 1, -12, 8]}, "output": 36}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "b2b5a34682ea45e1"} |
vcr_63d10c0325e8 | 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": "cac bcabdb eaee"}, "output": "eaee bcabdb cac"}, {"input": {"s": "ba a b aaa abb"}, "output": "abb aaa b a ba"}, {"input": {"s": "a b"}, "output": "b a"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "8e4308b80e6e6962"} |
vcr_d70d81bf96fc | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-22, -19, -12, -6, -4, -2, 3, 8, 9, 20, 45, 46], "target": 66}, "output": [9, 11]}, {"input": {"nums": [-17, -12, -9, 2, 3, 10, 15, 19, 20], "target": -7}, "output": [0, 5]}, {"input": {"nums": [-19, -16, -7, 7, 25, 26, 28, 34], "target": 9}, "output": [0, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "e5eb7f43c103c45d"} |
vcr_2128d26c2c37 | 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": [60, 39, 47, 6, 19]}, "output": 13}, {"input": {"prices": [34, 5, 28, 14, 11, 3]}, "output": 23}, {"input": {"prices": [9, 20, 11, 33, 30, 13, 22]}, "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": "579f8e492961942e"} |
vcr_26c721d2e87c | 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, 20, 1, 28, 12, 1, 17, 4]}, "output": 65}, {"input": {"nums": [12, 8]}, "output": 12}, {"input": {"nums": [10, 10, 3, 5, 8]}, "output": 21}] | 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": "1c8c72af2e6bf45d"} |
vcr_2fe75470d279 | 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, 18, 14, -12, 3, 15, 10, 3, 10, 14, -15, -15, 5, 2, -10]}, "output": 89}, {"input": {"nums": [-1, 1, -6, -3, -5, 4, -10]}, "output": 4}, {"input": {"nums": [-7, 13, 9]}, "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": "32d2ab197c1fead0"} |
vcr_ea93196044f7 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [9, 10, 16, 17, 18, 31, 34, 44], "target": 42}, "output": 7}, {"input": {"nums": [3, 4, 7, 16, 18, 20, 23], "target": 6}, "output": 2}, {"input": {"nums": [2, 5, 18, 19, 22, 23, 25, 26, 28, 29, 30, 31], "target": 7}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "dae48d895d143f2b"} |
vcr_996ba4799659 | 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": 152, "b": 169}, "output": 1}, {"input": {"a": 87, "b": 49}, "output": 1}, {"input": {"a": 31, "b": 151}, "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": "d4a1894f2aded053"} |
vcr_dc5a59ba4113 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 4, 6, 7, 0, 6, 5, 1]}, "output": [0, 0, 0, 0, 5040, 0, 0, 0]}, {"input": {"nums": [4, 1, 2, 4]}, "output": [8, 32, 16, 8]}, {"input": {"nums": [5, 2, 3, 1, 3, 2, 5]}, "output": [180, 450, 300, 900, 300, 450, 180]}] | 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": "ac100d74b7bd40f2"} |
vcr_6e7c5c6cd8f9 | 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, 7, -12, -10], "k": 6}, "output": [-10, -10, 4, 7, -12]}, {"input": {"nums": [6, -5, -8, 4, -9], "k": 18}, "output": [-8, 4, -9, 6, -5]}, {"input": {"nums": [-8, 2, 9, 7, -2, 1], "k": 16}, "output": [9, 7, -2, 1, -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": "0fb2c103b2783958"} |
vcr_9bd696dc922f | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [35, 27, 21, 34, 14, 24, 51, 54, 51, 12, 6, 59]}, "output": 53}, {"input": {"prices": [32, 32, 29, 18, 10, 7, 38]}, "output": 31}, {"input": {"prices": [20, 16, 1, 10, 13, 7, 1]}, "output": 12}] | 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": "a94482cb5607f547"} |
vcr_7a4f9f77fd63 | 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, 1, 26, 34, 44], "target": 28}, "output": 3}, {"input": {"nums": [17, 20, 22, 23, 39], "target": 25}, "output": 4}, {"input": {"nums": [0, 1, 3, 7, 12, 27, 34, 36, 37, 39], "target": 8}, "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": "a1516b785241acdd"} |
vcr_506a08728b48 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bbb", "ac", "bbba", "aa", "bcab", "aba"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "aab", "bba", "bbb", "aba"]}, "output": [1, 2, 2]}, {"input": {"strs": ["bba", "bbb", "bab", "aaa", "bbb"]}, "output": [1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "e1b141e5a209f71f"} |
vcr_0e85e84c73a0 | 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": [50, 74, 58, -10, -31, 7, 72, -2, -19], "target": 79}, "output": [5, 6]}, {"input": {"nums": [39, -23, -7, 19, -12, 34, -19, 26, -13, 18], "target": 53}, "output": [3, 5]}, {"input": {"nums": [35, 18, 20, -15, -7, 8, -28], "target": 5}, "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": "acc9e18e8e4e512b"} |
vcr_c3d21bcb480c | 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, -1, 1, 5, -11, 12, -9]}, "output": 28}, {"input": {"nums": [-7, 10, 2, -1, 7, 3, 13, -8, 0, 7, -9]}, "output": 34}, {"input": {"nums": [-2, 7, -7, 14, 14]}, "output": 28}] | 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": "e36640ac31c283c1"} |
vcr_6412fb8f20a7 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 10, 12, 13, 18, 22, 28, 29, 30, 32, 35, 36, 37, 41], "target": 17}, "output": 4}, {"input": {"nums": [0, 1, 10, 12, 18, 28, 30, 34, 37, 38, 39], "target": 14}, "output": 4}, {"input": {"nums": [2, 9, 20, 24, 32, 37], "target": 14}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "6929828a055bed5f"} |
vcr_3ee180b5c98a | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-12, -3, 21, 4, 12, -8, -13, 11, 16, 10, 3, 17, -10]}, "output": 73}, {"input": {"nums": [10, 13, 11, 6, 13]}, "output": 53}, {"input": {"nums": [11, 11, 12]}, "output": 34}] | 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": "705d2aa5b132a5a2"} |
vcr_2c51cd8fe694 | 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": ["aaba", "cb", "aac", "cbaa", "cbc", "aaba", "aaba", "bcc", "caac"]}, "output": [1, 1, 1, 1, 2, 3]}, {"input": {"strs": ["bba", "aaa", "aaa", "aaa", "bba", "aba"]}, "output": [1, 2, 3]}, {"input": {"strs": ["abb", "bba", "bba", "bba", "aaa", "abb", "abb"]}, "output": [1, 6]}] | 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": "f07bf920fe5e2d8e"} |
vcr_6d0af029c429 | 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": [51, 16, 48, 70, -4, 31, 33, -11, 45, 29, -16, 68, 8, 78], "target": 76}, "output": [5, 8]}, {"input": {"nums": [45, 49, -13, 21, -29, 33, 19, -22, -26, 2, 8], "target": -35}, "output": [2, 7]}, {"input": {"nums": [-24, 43, 25, -7, 14, -9, 0, 8, -1, -14], "target": -1}, "output": [5, 7]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "d1a2bd4e9fb29472"} |
vcr_1f0584068897 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [15, -9, 7, 10, -12, -1, 21, 4, 9, -17, -6, -17, -5, 4, 19]}, "output": 44}, {"input": {"nums": [2, 9, -6, 12, 14, 0, -1]}, "output": 31}, {"input": {"nums": [6, 2, 7, -4]}, "output": 15}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "73b10517fad380b9"} |
vcr_8e9613f0d2b4 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 1, 2, 3, 4, 4, 5, 5, 8, 8, 13, 13, 15]}, "output": [1, 2, 3, 4, 5, 8, 13, 15]}, {"input": {"nums": [0, 3, 6, 6, 7, 8, 8, 9, 11, 11]}, "output": [0, 3, 6, 7, 8, 9, 11]}, {"input": {"nums": [1, 6, 7, 8, 9]}, "output": [1, 6, 7, 8, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "eea68af1a1338e78"} |
vcr_15710b2e5f46 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [16, 36, 9, 18, 14, 16, 25, 76, 36, 9, 18, 26, 14, 25, 26]}, "output": 76}, {"input": {"nums": [41, 5, 7, 21, 24, 24, 5, 7, 21, 6, 6]}, "output": 41}, {"input": {"nums": [17, 26, 19, 19, 17, 18, 12, 13, 13, 12, 26, 18, 56]}, "output": 56}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "5f0d0a1a67f03d5c"} |
vcr_bbc994cdefa8 | 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, -10, 1, -5, -4, -11, -18, 16, 12, -12, -13, -9, -7, 7, 14]}, "output": 28}, {"input": {"nums": [-5, 11, 5, 12, -9]}, "output": 28}, {"input": {"nums": [14, -2, 2, 9, -10, -7, -5, 7]}, "output": 23}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "8b3ae2097c8e4bdb"} |
vcr_58a9adacca21 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 1, 2, 4, 4, 4, 4, 0]}, "output": false}, {"input": {"nums": [0, 3, 2]}, "output": false}, {"input": {"nums": [1, 3, 2, 1, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "17790e5af0d93e77"} |
vcr_1e46434e0050 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [14, -23, 33, 18]}, "output": [14, -9, 24, 42]}, {"input": {"nums": [-6, 3, 1]}, "output": [-6, -3, -2]}, {"input": {"nums": [7, -1, -12, 7, 3, 15, -9, 17]}, "output": [7, 6, -6, 1, 4, 19, 10, 27]}] | 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": "f311e0d655b5f780"} |
vcr_1a158042bae1 | 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": [50, 10, 1, 17, 10, 17, 1]}, "output": 50}, {"input": {"nums": [25, 21, 21, 25, 24, 24, 48]}, "output": 48}, {"input": {"nums": [6, 46, 20, 22, 2, 5, 24, 5, 22, 20, 6, 24, 2]}, "output": 46}] | 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": "18df2c7d5cb13634"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.