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_35a410920eca | 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": "aee ecceae caedce dbcac adcc c"}, "output": "c adcc dbcac caedce ecceae aee"}, {"input": {"s": "bbab b"}, "output": "b bbab"}, {"input": {"s": "baa b"}, "output": "b baa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "20b708122c6bd3f4"} |
vcr_ca6bdca64115 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 1, 4, 8]}, "output": [0, 1, 4, 8]}, {"input": {"nums": [0, 0, 1, 2, 3, 5, 7, 8, 11, 11, 12]}, "output": [0, 1, 2, 3, 5, 7, 8, 11, 12]}, {"input": {"nums": [0, 4, 8, 11, 12, 12]}, "output": [0, 4, 8, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "3ca4d6e6f8de6941"} |
vcr_e713c77f2c63 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-1, -3, -5, -10, 3, -16, -6, 14], "k": 8}, "output": [-1, -3, -5, -10, 3, -16, -6, 14]}, {"input": {"nums": [9, -7, -5, 4, 3, 9, 2], "k": 0}, "output": [9, -7, -5, 4, 3, 9, 2]}, {"input": {"nums": [-5, 9, -3, 2, 6, 1], "k": 1}, "output": [1, -5, 9, -3, 2, 6]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "4cc6fe3f27005320"} |
vcr_0cd51e8f1844 | 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": "daab badea e ccccdc dce dedeb e"}, "output": "e dedeb dce ccccdc e badea daab"}, {"input": {"s": "bb aaba ab ba"}, "output": "ba ab aaba bb"}, {"input": {"s": "aab b ab aa"}, "output": "aa ab b aab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "ba306dbc65d409e6"} |
vcr_f76bbc9482ce | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [1, 0, 3, 2, 3, 1, 0]}, "output": false}, {"input": {"nums": [3, 0, 1]}, "output": true}, {"input": {"nums": [3, 1, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "d070ab889e6167ab"} |
vcr_1ef8781c10db | easy | string | Count contiguous groups of identical characters (example: aabbc -> 3). | def count_char_groups(s: str) -> int: | Walk the string and increment whenever the character changes. | def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[i - 1]:
groups += 1
return groups
| [{"input": {"s": "abacaaabba"}, "output": 7}, {"input": {"s": "cbbcabba"}, "output": 6}, {"input": {"s": "bbacbbaaaaac"}, "output": 6}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "db4771d7128b8dea"} |
vcr_cc2c8b5dcffe | 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": "de ea b"}, "output": "b ea de"}, {"input": {"s": "bba aaba aba aa"}, "output": "aa aba aaba bba"}, {"input": {"s": "bb bb aaab b"}, "output": "b aaab bb bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "fbbaf8ce8e1a7859"} |
vcr_3092ae3d1bf1 | 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, -3, 9], "k": 7}, "output": [3, -3, 9, 9]}, {"input": {"nums": [-10, 8, -8, 8, -10, -10, 3, 1, -7, -2], "k": 15}, "output": [-10, 3, 1, -7, -2, -10, 8, -8, 8, -10]}, {"input": {"nums": [-9, -2, 5, 5, 4, 5, 7, 10], "k": 17}, "output": [10, -9, -2, 5, 5, 4, 5, 7]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "da597664359d91e3"} |
vcr_4579ed8a8370 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["cca", "baab", "caaa", "bba", "bccb", "ccab"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bbb", "aaa"]}, "output": [1, 2]}, {"input": {"strs": ["bba", "bba", "bba"]}, "output": [3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "c8763fbe84e8c65e"} |
vcr_0df1dd627ce6 | easy | string | Count contiguous groups of identical characters (example: aabbc -> 3). | def count_char_groups(s: str) -> int: | Walk the string and increment whenever the character changes. | def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[i - 1]:
groups += 1
return groups
| [{"input": {"s": "bbbbbbaccb"}, "output": 4}, {"input": {"s": "bba"}, "output": 2}, {"input": {"s": "abcaabccbc"}, "output": 8}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "83524255ab0885c9"} |
vcr_50caa21bf7c1 | 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, 0, 3, 1, 1, 3, 0, 0]}, "output": true}, {"input": {"nums": [1, 3, 0, 3, 3, 2]}, "output": true}, {"input": {"nums": [3, 3, 1, 2, 2, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "d91627dca77681aa"} |
vcr_2b2bbf002063 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [36, 59, 18, 3, 57, 27, 51, 47]}, "output": 54}, {"input": {"prices": [16, 31, 35]}, "output": 19}, {"input": {"prices": [8, 32, 23, 16, 25]}, "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": "294fec100c5dcafc"} |
vcr_94ce37beecf0 | 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": [35, -9, -3, 30, 8, 1, -16, 16, 21, -14, 2, 21, -12, 19]}, "output": [35, 26, 23, 53, 61, 62, 46, 62, 83, 69, 71, 92, 80, 99]}, {"input": {"nums": [11, 8, 13, -9, 4, 1, -6, -8, 17]}, "output": [11, 19, 32, 23, 27, 28, 22, 14, 31]}, {"input": {"nums": [8, 4, 16, -10, -10, -9, 4, 3]}, "output": [8, 12... | 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": "21fd5186d98d735c"} |
vcr_cf584f698e5c | 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": [18, 10, 5, 29, 29, 4, 14, 23, 0, 25]}, "output": 100}, {"input": {"nums": [6, 1, 20, 8, 12]}, "output": 38}, {"input": {"nums": [15, 8, 14, 9, 6, 12, 19]}, "output": 54}] | 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": "13e32ffaf839bad9"} |
vcr_7798dba9ed71 | easy | string | Count contiguous groups of identical characters (example: aabbc -> 3). | def count_char_groups(s: str) -> int: | Walk the string and increment whenever the character changes. | def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[i - 1]:
groups += 1
return groups
| [{"input": {"s": "baabcbcaabbaab"}, "output": 10}, {"input": {"s": "caccbccbcc"}, "output": 7}, {"input": {"s": "ccacbabaa"}, "output": 7}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "9a87284ed8267a1b"} |
vcr_63c0cdd1ad93 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 2, 5, 10, 11, 12, 13, 14, 14, 15]}, "output": [1, 2, 5, 10, 11, 12, 13, 14, 15]}, {"input": {"nums": [2, 3, 3, 4, 4, 5, 6, 7, 9, 10]}, "output": [2, 3, 4, 5, 6, 7, 9, 10]}, {"input": {"nums": [2, 5, 6, 7, 10]}, "output": [2, 5, 6, 7, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "8e130138442e929d"} |
vcr_59337531324e | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [10, 11, 15, 18, 21, 24, 27, 34, 37, 39, 41, 42, 47], "target": 5}, "output": 0}, {"input": {"nums": [3, 4, 5, 9, 13, 15, 16, 20, 32, 39], "target": 33}, "output": 9}, {"input": {"nums": [7, 12, 18], "target": 28}, "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": "2a70759d302155fa"} |
vcr_afec62ffb0c6 | 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": ["acbc", "ccc", "aa", "aaa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "bbb", "aaa"]}, "output": [1, 1, 1]}, {"input": {"strs": ["bbb", "bab", "bba", "bbb", "bbb", "bab"]}, "output": [3, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "a5b1d072e4af2703"} |
vcr_198aa7688884 | 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": [120, 8, 17, 98, 60, 36, 136, 27, 82, 163]}, "output": false}, {"input": {"nums": [24, 25, 2, 13, 6, 15, 17, 1, 25, 20, 1, 4]}, "output": true}, {"input": {"nums": [2, 18, 5, 8, 19, 8, 12, 9]}, "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": "8b94f190f815c964"} |
vcr_530bce9a3153 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [10, 9, 29, 16, 22, 7, 12, 21]}, "output": 82}, {"input": {"nums": [1, 13, 10, 6, 6, 15, 18]}, "output": 37}, {"input": {"nums": [19, 2, 10, 8, 18, 13, 16, 10, 14, 11]}, "output": 77}] | 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": "3e9d1a69c20b56e0"} |
vcr_4680805c84a0 | hard | 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": [8, -8, -30, -16, 11], "target": -22}, "output": [0, 2]}, {"input": {"nums": [-4, 19, 17, 38, 22, -6, 49, 34], "target": 57}, "output": [1, 3]}, {"input": {"nums": [7, -6, 0, 42, 46, -17, 15, -14, 11, 9], "target": 53}, "output": [0, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "b811347a9fc7a014"} |
vcr_587689e8bfe0 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [25, 28, 21, 2, -18, 30, 12, -8, 6, -2, -9, 22, 3]}, "output": [25, 53, 74, 76, 58, 88, 100, 92, 98, 96, 87, 109, 112]}, {"input": {"nums": [6, 14, 9, 8, -9, -4, 7, 6]}, "output": [6, 20, 29, 37, 28, 24, 31, 37]}, {"input": {"nums": [17, -7, 11, 0]}, "output": [17, 10, 21, 21]}] | 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": "37b08c978aeded1b"} |
vcr_a1cea2f8e3fa | 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, 2, 22, 26, 22, 24]}, "output": 69}, {"input": {"nums": [16, 2, 6, 19, 5, 15, 19, 3, 19, 15]}, "output": 73}, {"input": {"nums": [15, 8]}, "output": 15}] | 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": "d782dd323a191392"} |
vcr_56596998b318 | 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, 7, 19, 20, 21, 22, 23, 24, 37, 39, 44, 45], "target": 23}, "output": 6}, {"input": {"nums": [2, 4, 5, 8, 9, 14, 20, 24, 25, 31, 35, 38], "target": 18}, "output": 6}, {"input": {"nums": [1, 13, 14, 16, 18, 24, 31, 32, 34, 36], "target": 5}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "dff5086dc2e7eb2d"} |
vcr_4d70af44b4e5 | 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": [3, -13, 2, -14, 17], "k": 2}, "output": [-14, 17, 3, -13, 2]}, {"input": {"nums": [8, 2, 10, 5, -3], "k": 13}, "output": [10, 5, -3, 8, 2]}, {"input": {"nums": [2, -8, 1, -3, -9, 0], "k": 12}, "output": [2, -8, 1, -3, -9, 0]}] | 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": "4f26d9a8b4bbf48d"} |
vcr_f12a5a38bc68 | 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": ["abab", "acc", "bbc", "caa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "aab", "bab"]}, "output": [1, 2]}, {"input": {"strs": ["bbb", "baa", "baa", "bbb", "aaa", "bbb", "bba"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "cf25057379a233f6"} |
vcr_332ec5fd7e23 | 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, 3, 4, 8, 8, 12, 15, 15]}, "output": [1, 3, 4, 8, 12, 15]}, {"input": {"nums": [1, 2, 2, 4, 6, 10, 11, 12, 12]}, "output": [1, 2, 4, 6, 10, 11, 12]}, {"input": {"nums": [2, 2, 5, 12, 12]}, "output": [2, 5, 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": "9c1a22cf6b54c529"} |
vcr_41eda979bc24 | 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": [11, 24, 36, 29, 57, 57, 50, 42]}, "output": 46}, {"input": {"prices": [32, 4, 21, 20, 10, 14, 11, 11, 18, 37, 18]}, "output": 33}, {"input": {"prices": [11, 17, 14]}, "output": 6}] | 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": "0df32e6d2a2c730c"} |
vcr_968d8f74b52c | easy | string | Count contiguous groups of identical characters (example: aabbc -> 3). | def count_char_groups(s: str) -> int: | Walk the string and increment whenever the character changes. | def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[i - 1]:
groups += 1
return groups
| [{"input": {"s": "abaabbcbabcba"}, "output": 11}, {"input": {"s": "ababbbcc"}, "output": 5}, {"input": {"s": "baccacba"}, "output": 7}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "286a2aa4e4923700"} |
vcr_4c5de820bbff | 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, 11, 19, 39], "target": 10}, "output": 1}, {"input": {"nums": [2, 4, 6, 7, 13, 21, 28, 38, 39], "target": 15}, "output": 5}, {"input": {"nums": [4, 8, 11, 26, 33], "target": 37}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "45c4894275b23e9b"} |
vcr_1216b4e64b03 | 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, 6, 3, 7, 5]}, "output": [630, 105, 210, 90, 126]}, {"input": {"nums": [3, 2, 5, 3]}, "output": [30, 45, 18, 30]}, {"input": {"nums": [2, 1, 4, 4]}, "output": [16, 32, 8, 8]}] | 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": "cfb178c6ff1e1060"} |
vcr_ec677e69034b | 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": [23, 30, 18, 15, 11, 30]}, "output": 75}, {"input": {"nums": [13, 19, 11, 1, 1, 14]}, "output": 38}, {"input": {"nums": [19, 3, 11, 19, 4, 7, 11, 16, 4]}, "output": 61}] | 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": "360a6ed10dfd2cd3"} |
vcr_16a160d55be1 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 5, 13, 37, 41, 43], "target": 38}, "output": 4}, {"input": {"nums": [3, 5, 7, 9, 10, 17, 19, 21, 26, 33, 37], "target": 3}, "output": 0}, {"input": {"nums": [3, 13, 31], "target": 37}, "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": "11ba83aa4ac585e9"} |
vcr_a88c1cbfdeeb | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [16, 16, 4, -14, 17, -13, -24, -10, -5]}, "output": [16, 32, 36, 22, 39, 26, 2, -8, -13]}, {"input": {"nums": [18, 16, -9, 16, 14, -5, -2, -7]}, "output": [18, 34, 25, 41, 55, 50, 48, 41]}, {"input": {"nums": [0, 2, 6]}, "output": [0, 2, 8]}] | 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": "3c3c32e4b7b36a15"} |
vcr_6e93c14e7a7f | 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": "eddb cbdee adbcc"}, "output": "adbcc cbdee eddb"}, {"input": {"s": "aa aaab baa"}, "output": "baa aaab aa"}, {"input": {"s": "baa aa"}, "output": "aa baa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "917d029306c60443"} |
vcr_712f988e4faf | 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": [73, 16, 20, 10, 33, 10, 20, 16, 33]}, "output": 73}, {"input": {"nums": [19, 19, 24, 7, 54, 7, 24]}, "output": 54}, {"input": {"nums": [24, 24, 45, 7, 4, 4, 3, 3, 7]}, "output": 45}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "02cfd21550edbd71"} |
vcr_38f7fe1f8f3b | 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": [-22, 23, 22, 21, -12, -16, 30, 28, 10, -15]}, "output": [-22, 1, 23, 44, 32, 16, 46, 74, 84, 69]}, {"input": {"nums": [17, -3]}, "output": [17, 14]}, {"input": {"nums": [-8, 7, 16, 3, -9, 15, 4, -2, -2]}, "output": [-8, -1, 15, 18, 9, 24, 28, 26, 24]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "6a3e4bbec7d5fb2b"} |
vcr_fa8ff13f65e2 | 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": ["cb", "cbb", "cbca", "ca", "bbc"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["baa", "abb", "aaa", "aba", "bbb"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["bba", "baa", "aab", "bbb", "aba", "aba", "bbb"]}, "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": "e92eddc126e1d66c"} |
vcr_ebeea58bab4d | 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, 4, 7, 8, 9, 14, 21, 23, 24, 28, 32, 33, 36], "target": 41}, "output": 14}, {"input": {"nums": [3, 7, 15], "target": 11}, "output": 2}, {"input": {"nums": [12, 16, 28, 30, 33], "target": 24}, "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": "0bfc55e56158e0a1"} |
vcr_da93b098a9bd | 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, 5, 5, 5, 7, 9, 9, 10, 11, 11]}, "output": [1, 5, 7, 9, 10, 11]}, {"input": {"nums": [2, 2, 4, 4, 5, 6, 10, 10, 11, 12]}, "output": [2, 4, 5, 6, 10, 11, 12]}, {"input": {"nums": [4, 5, 7]}, "output": [4, 5, 7]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "749ed84b05020069"} |
vcr_5f03f35cc0a8 | 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": ["acc", "bc", "cc", "ab"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["abb", "bba", "aab", "baa", "bba", "bab"]}, "output": [2, 4]}, {"input": {"strs": ["bbb", "aab", "bbb", "bbb", "aaa", "baa", "bba"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "9144382176369ad3"} |
vcr_fb813c738b1f | 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, -22, -18, -15, 16, 24, 41, 46], "target": 16}, "output": [0, 7]}, {"input": {"nums": [-14, -3, 1, 15, 29, 32, 36, 37], "target": 29}, "output": [1, 5]}, {"input": {"nums": [-19, -15, 0, 5, 35], "target": -19}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "405841c1e16ddd01"} |
vcr_05001e602e34 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [2, 9, 13, 22, 19, 4, 11, 6, 6, 29, 23]}, "output": 74}, {"input": {"nums": [6, 0, 11, 0, 11, 14, 0]}, "output": 31}, {"input": {"nums": [0, 10]}, "output": 10}] | 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": "8130dd30ec8b866b"} |
vcr_616cd4bd5dff | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [8, 12, 17, 21, 25, 27, 28, 29, 41, 44], "target": 20}, "output": 3}, {"input": {"nums": [0, 3, 6, 7, 11, 19, 20, 26, 27, 33, 36, 39], "target": 24}, "output": 7}, {"input": {"nums": [14, 16, 23, 25, 33], "target": 25}, "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": "367bbae25bb2908f"} |
vcr_c0c3d56ede2f | 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": [5, 16, -14, 35, 24, 0, 18, -15]}, "output": [5, 21, 7, 42, 66, 66, 84, 69]}, {"input": {"nums": [10, 16, -4, 17]}, "output": [10, 26, 22, 39]}, {"input": {"nums": [17, 18, -8, -5, 18]}, "output": [17, 35, 27, 22, 40]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "cbcab11c61417cf0"} |
vcr_0788c0004c4d | 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": [30, 24, 28, 8, 24, 19, 33, 18, 19, 33, 28, 18, 8, 20, 53, 20, 30]}, "output": 53}, {"input": {"nums": [45, 23, 27, 23, 27]}, "output": 45}, {"input": {"nums": [7, 7, 20, 10, 2, 23, 23, 19, 2, 19, 20, 10, 51]}, "output": 51}] | 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": "a99c1fb7e02bae62"} |
vcr_418b1e03aeed | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 2, 4, 1, 4, 3, 2, 3, 1, 0]}, "output": true}, {"input": {"nums": [2, 3, 0, 0, 0, 0, 2]}, "output": false}, {"input": {"nums": [0, 2, 1, 0, 1, 1, 1]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "9890f5cc6d184895"} |
vcr_993bcde48157 | 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": [129, 62, 182, 47, 188, 172, 1, 159, 187]}, "output": false}, {"input": {"nums": [2, 13, 22, 9, 19]}, "output": false}, {"input": {"nums": [13, 0, 13, 7, 11, 18, 23, 10, 14, 15, 14]}, "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": "d9609e3d853373d4"} |
vcr_6bd49a00f4bd | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [20, 70, 10, 10, 17, 20, 12, 26, 17, 16, 26, 15, 12, 16, 15]}, "output": 70}, {"input": {"nums": [17, 5, 26, 46, 17, 26, 5]}, "output": 46}, {"input": {"nums": [22, 28, 3, 3, 28, 10, 10, 22, 53]}, "output": 53}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "55741ce64645e1c4"} |
vcr_3a280cec9679 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-12, 2, -2, -16, -10], "k": 1}, "output": [-10, -12, 2, -2, -16]}, {"input": {"nums": [0, 2, 9, -7, -10, -6, 4, 4], "k": 4}, "output": [-10, -6, 4, 4, 0, 2, 9, -7]}, {"input": {"nums": [6, -5, -10, -7, -10, -4, 0, 7], "k": 18}, "output": [0, 7, 6, -5, -10, -7, -10, -4]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "73b6cdc04d0b06a5"} |
vcr_dd22e878e781 | 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": "cba acbaa abb adecd dbc"}, "output": "dbc adecd abb acbaa cba"}, {"input": {"s": "abb ab"}, "output": "ab abb"}, {"input": {"s": "bab b b ab"}, "output": "ab b b bab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "82104c5a3f1c5ea6"} |
vcr_7f63176c6f64 | 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, 4, 4, 8, 10, 12, 13]}, "output": [0, 4, 8, 10, 12, 13]}, {"input": {"nums": [0, 0, 4, 5, 10]}, "output": [0, 4, 5, 10]}, {"input": {"nums": [2, 8, 11]}, "output": [2, 8, 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": "4521153b730921f2"} |
vcr_6bc95065056c | 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, 30, 30, 15, 22, 17, 10, 6, 18]}, "output": 85}, {"input": {"nums": [9, 20, 14, 9, 0, 18, 7, 4, 2]}, "output": 51}, {"input": {"nums": [11, 19, 19, 0, 7, 15, 1, 2]}, "output": 47}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "6f32a430f9a308d5"} |
vcr_abf91f1974b3 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [25, 18, 24, 30, 16, 21, 6, 10, 26, 14, 17, 7]}, "output": 119}, {"input": {"nums": [0, 19, 14, 19, 3, 8, 1, 15, 17]}, "output": 63}, {"input": {"nums": [20, 3, 12, 18, 6, 9, 7, 7, 17]}, "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": "d9c6984ed652b93e"} |
vcr_b800f149297b | 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": [197, 164, 62, 63, 184, 86, 56, 145]}, "output": false}, {"input": {"nums": [7, 21, 10, 3]}, "output": false}, {"input": {"nums": [11, 14, 0, 4, 16, 20, 14, 3, 1, 1]}, "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": "4a751046ef0af15f"} |
vcr_6838a8a783e9 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 1, 3, 0, 0, 1, 3, 0, 0, 0]}, "output": true}, {"input": {"nums": [3, 0, 3, 0, 3, 2, 0, 3, 0]}, "output": true}, {"input": {"nums": [3, 2, 2, 3, 3, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "c5c82c96f664d968"} |
vcr_3c9611633b3d | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-2, -1, 7, 20, 16, -11]}, "output": 43}, {"input": {"nums": [-7, 9, 6, 0, -12, 3]}, "output": 15}, {"input": {"nums": [-8, -10, -5, 4, -2, -12, 6, 6, -9, 8, -1]}, "output": 12}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "ca7e0511c8471b57"} |
vcr_ebb2b6b93031 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 0, 3, 1, 4, 2, 2, 0, 1, 0]}, "output": true}, {"input": {"nums": [2, 2, 1, 3, 0]}, "output": true}, {"input": {"nums": [0, 0, 3, 1, 0, 2, 2, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "83003d7e9f32b705"} |
vcr_1beb4e0c9398 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-18, 14, 19, 26, 44, 48], "target": 62}, "output": [1, 5]}, {"input": {"nums": [-17, -9, 0, 1, 5, 8, 11, 13, 19, 27, 39], "target": 44}, "output": [4, 10]}, {"input": {"nums": [-18, -13, 6, 10, 16, 17, 27], "target": 3}, "output": [1, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "20cc43ba617660bb"} |
vcr_58b8e993fd67 | 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": [4, 7, 8, 10, 13, 15]}, "output": [4, 7, 8, 10, 13, 15]}, {"input": {"nums": [0, 8, 11]}, "output": [0, 8, 11]}, {"input": {"nums": [3, 4, 6, 6, 10]}, "output": [3, 4, 6, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "7605b20af7873c6d"} |
vcr_9919dde97dcc | 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, 2, 4, 3, 0, 4, 4, 0, 0]}, "output": true}, {"input": {"nums": [0, 2, 1, 2, 0, 3]}, "output": false}, {"input": {"nums": [3, 2, 3, 3, 0, 1, 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": "1bb4e142bc829994"} |
vcr_77e83f9c9441 | 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": "dbec ba cb e bedabe"}, "output": "bedabe e cb ba dbec"}, {"input": {"s": "ba ab a aabb aab"}, "output": "aab aabb a ab ba"}, {"input": {"s": "a baab bbbb ab"}, "output": "ab bbbb baab 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": "e6b864400989d8e0"} |
vcr_d6a321bc1953 | easy | string | Count contiguous groups of identical characters (example: aabbc -> 3). | def count_char_groups(s: str) -> int: | Walk the string and increment whenever the character changes. | def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[i - 1]:
groups += 1
return groups
| [{"input": {"s": "bccbbabacac"}, "output": 9}, {"input": {"s": "bcabb"}, "output": 4}, {"input": {"s": "acabcacaaab"}, "output": 9}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "7ea4c76ed8b6f315"} |
vcr_1ccc593db6a4 | 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": [26, -33, 74, 55, 73], "target": 100}, "output": [0, 2]}, {"input": {"nums": [32, 22, 11, 19, -3, 29, -21, 26], "target": 45}, "output": [3, 7]}, {"input": {"nums": [17, -16, 10, -13], "target": -29}, "output": [1, 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": "b449b10100ef693c"} |
vcr_68f077f7751c | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [25, 16, 28, 30]}, "output": 55}, {"input": {"nums": [9, 1, 16, 0]}, "output": 25}, {"input": {"nums": [10, 9, 0, 17, 17, 2, 12, 13]}, "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": "b612105019c69977"} |
vcr_282529b02139 | 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": "db aeccd bbeed cbcdc cdcec cbbce accbe"}, "output": "accbe cbbce cdcec cbcdc bbeed aeccd db"}, {"input": {"s": "b aba ba"}, "output": "ba aba b"}, {"input": {"s": "ab bbbb ba ab"}, "output": "ab ba bbbb ab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "a892ee43b9b48666"} |
vcr_79835304a3eb | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [29, 9, 1, 12]}, "output": 41}, {"input": {"nums": [13, 16]}, "output": 16}, {"input": {"nums": [15, 20, 10, 0]}, "output": 25}] | 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": "fc687ad282c1df61"} |
vcr_0b8b94190aa7 | 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": "addaad eabee e cbd"}, "output": "cbd e eabee addaad"}, {"input": {"s": "bbbb abb baaa aabb"}, "output": "aabb baaa abb bbbb"}, {"input": {"s": "aa ab ab aaa bb"}, "output": "bb aaa ab ab 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": "c3ce630f0b7a4c20"} |
vcr_e7626a84718a | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [6, -19, -2, 13, 18, -11, 11, -12, -7, -18], "k": 11}, "output": [-18, 6, -19, -2, 13, 18, -11, 11, -12, -7]}, {"input": {"nums": [-1, -6, -6], "k": 6}, "output": [-1, -6, -6]}, {"input": {"nums": [-6, -6, 7, -9, -2], "k": 17}, "output": [-9, -2, -6, -6, 7]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "d49072a726e3ded5"} |
vcr_b87285775e0f | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 14, 16, 17, 21, 24, 31, 38, 39, 40, 44, 46, 47, 48], "target": 22}, "output": 5}, {"input": {"nums": [4, 15, 35, 39], "target": 9}, "output": 1}, {"input": {"nums": [1, 6, 11, 12, 15, 16, 20, 27, 29, 30, 31, 35], "target": 17}, "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": "162edb7427446b92"} |
vcr_cf02bd58a9f5 | 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": [-11, 29, 34, -23, 2, 30, -13, -22, 21, -16, 8, -20, 15]}, "output": [-11, 18, 52, 29, 31, 61, 48, 26, 47, 31, 39, 19, 34]}, {"input": {"nums": [11, -4, 2, 6, -8]}, "output": [11, 7, 9, 15, 7]}, {"input": {"nums": [-6, 12, -9]}, "output": [-6, 6, -3]}] | 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": "e948955c2c5843f2"} |
vcr_53fdfb2dc29e | 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, 3, 1, 1, 4, 3, 3, 4, 1, 0]}, "output": true}, {"input": {"nums": [0, 0, 0, 3, 2, 2, 0, 2]}, "output": false}, {"input": {"nums": [0, 3, 1, 2, 0, 0, 1, 2]}, "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": "119f37d9e22e33a0"} |
vcr_899ca7cefabc | 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": [-22, -13, 1, -3, 1, 30, -7, 15, 5, -5]}, "output": [-22, -35, -34, -37, -36, -6, -13, 2, 7, 2]}, {"input": {"nums": [4, -3, 14, 0, -8, 10, -1, 8, -10, -9]}, "output": [4, 1, 15, 15, 7, 17, 16, 24, 14, 5]}, {"input": {"nums": [16, 9, 7, 2, 16, -7, 3]}, "output": [16, 25, 32, 34, 50, 43, 46]}] | 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": "da5dcbda501cdae8"} |
vcr_3c0931bc101f | 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": [91, 178, 144, 106, 161, 57, 99, 143, 70, 104, 124, 103, 132, 154, 2, 25, 84]}, "output": false}, {"input": {"nums": [9, 13, 16, 25, 3, 4, 21, 23, 20, 25, 22, 15]}, "output": true}, {"input": {"nums": [11, 4, 5, 16, 7, 21]}, "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": "bf07fc388c477388"} |
vcr_8a6cfd6e3eaa | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 2, 6, 7, 11, 13, 13, 14, 15]}, "output": [0, 1, 2, 6, 7, 11, 13, 14, 15]}, {"input": {"nums": [2, 2, 5, 5, 7, 10, 11, 12, 12]}, "output": [2, 5, 7, 10, 11, 12]}, {"input": {"nums": [1, 4, 7, 8, 11, 12]}, "output": [1, 4, 7, 8, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "938f2561a41d8543"} |
vcr_5fc1f627a5da | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 2, 6, 7, 12, 15, 21, 22, 23, 28, 30, 33, 39, 48], "target": 32}, "output": 11}, {"input": {"nums": [1, 6, 15, 16, 18, 20, 22, 29, 33, 34, 38], "target": 14}, "output": 2}, {"input": {"nums": [8, 25, 28, 39], "target": 2}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "96c3a6289e936e30"} |
vcr_b3d49335ab12 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 5, 6, 9, 18, 21, 25, 30, 35, 37, 47, 48, 49], "target": 49}, "output": 12}, {"input": {"nums": [10, 14, 19, 26, 30, 33, 34, 37], "target": 23}, "output": 3}, {"input": {"nums": [8, 31, 33, 36], "target": 8}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "b231d4b20bf4a01e"} |
vcr_16b1ac1e95fa | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [6, 12, 23, 14, 25, 15, 2]}, "output": 56}, {"input": {"nums": [11, 17, 3, 12, 7, 4, 3, 10]}, "output": 43}, {"input": {"nums": [9, 2, 5, 10, 10, 10, 10]}, "output": 34}] | 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": "d082cc4095481200"} |
vcr_bb33b70ebf56 | 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": [8, 42, 20, 28, 49, 11, 11, 45, 51, 51, 44, 39, 59]}, "output": 51}, {"input": {"prices": [33, 15, 39, 39, 18]}, "output": 24}, {"input": {"prices": [30, 24, 2, 2, 18, 29, 40, 17, 38, 4, 27, 15]}, "output": 38}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "8099fe085f77290f"} |
vcr_8017352f7784 | 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": 174, "b": 64}, "output": 2}, {"input": {"a": 38, "b": 60}, "output": 2}, {"input": {"a": 120, "b": 165}, "output": 15}] | 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": "dbee0f7113125d58"} |
vcr_d1a851e1c728 | 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": [10, 10, 11, 15, 15]}, "output": [10, 11, 15]}, {"input": {"nums": [1, 3, 5, 5, 6, 6, 8, 10, 11, 11]}, "output": [1, 3, 5, 6, 8, 10, 11]}, {"input": {"nums": [3, 5, 6, 6, 12]}, "output": [3, 5, 6, 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": "f24221ffb4a085fb"} |
vcr_289b0a90f4cc | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [13, -11, -9, 19, -1, -18, 2, -13], "k": 3}, "output": [-18, 2, -13, 13, -11, -9, 19, -1]}, {"input": {"nums": [2, -9, -9, 8, -10, -7, 9, -6, -2, -3], "k": 11}, "output": [-3, 2, -9, -9, 8, -10, -7, 9, -6, -2]}, {"input": {"nums": [-10, -6, -3, 5, -10, 5, -1, -5, -7, -2], "k": 9}, "output": [-6, -3,... | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "2315a1283f934558"} |
vcr_ce76ca2917f4 | 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, 1, 4, 3, 7, 7, 6]}, "output": [3528, 24696, 6174, 8232, 3528, 3528, 4116]}, {"input": {"nums": [5, 3, 1, 2, 2, 4]}, "output": [48, 80, 240, 120, 120, 60]}, {"input": {"nums": [3, 5, 4, 1, 4, 4]}, "output": [320, 192, 240, 960, 240, 240]}] | 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": "861d7994576a77a1"} |
vcr_547f5c945c8e | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-19, -6, 8, -12, 20, 10, 18, -15, -11, -2, -4], "k": 15}, "output": [-15, -11, -2, -4, -19, -6, 8, -12, 20, 10, 18]}, {"input": {"nums": [-6, -1, -6, -1, -3, 3, 5, -10, 9], "k": 2}, "output": [-10, 9, -6, -1, -6, -1, -3, 3, 5]}, {"input": {"nums": [8, -1, 2], "k": 17}, "output": [-1, 2, 8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "8f388d491c332ec5"} |
vcr_135a7fc02f39 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-23, -22, -17, -13, 2, 3, 11, 17, 24, 25], "target": -12}, "output": [0, 6]}, {"input": {"nums": [-16, -14, 5, 8, 11, 15, 18, 24, 26, 34], "target": 12}, "output": [1, 8]}, {"input": {"nums": [-11, 3, 7, 13, 17, 25, 29, 30], "target": 33}, "output": [1, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "285da4dffca8ce44"} |
vcr_b7f81a1eb2c3 | 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, 6, 10, 14, 16, 18, 19, 22, 23, 24, 29, 31, 35, 46], "target": 17}, "output": 5}, {"input": {"nums": [1, 3, 7, 21, 22, 28, 29, 39], "target": 1}, "output": 0}, {"input": {"nums": [2, 5, 23, 34, 38], "target": 21}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "d9fa331944a92002"} |
vcr_a21d79709cbf | 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": [70, 31, -6, 51, 53, 67], "target": 64}, "output": [0, 2]}, {"input": {"nums": [49, 11, -13, 42, -2, 6, 20, -15], "target": 91}, "output": [0, 3]}, {"input": {"nums": [36, -15, 25, 39, 46, -8, 43, -21], "target": 10}, "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": "89db80b5ae55edf5"} |
vcr_f6f66bbdaba8 | easy | string | Count contiguous groups of identical characters (example: aabbc -> 3). | def count_char_groups(s: str) -> int: | Walk the string and increment whenever the character changes. | def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[i - 1]:
groups += 1
return groups
| [{"input": {"s": "bbcbbaccabcbab"}, "output": 11}, {"input": {"s": "accbcbbcbbb"}, "output": 7}, {"input": {"s": "aaabbcabbcbc"}, "output": 8}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "85b01a1d7365feb0"} |
vcr_e475d540c8c1 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [20, 6, 26, 12, 6, 26, 33, 18, 16, 18, 12, 20, 27, 33, 70, 16, 27]}, "output": 70}, {"input": {"nums": [20, 21, 27, 25, 27, 40, 25, 9, 21, 9, 20]}, "output": 40}, {"input": {"nums": [26, 15, 7, 9, 7, 26, 15, 46, 5, 5, 9]}, "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": "901aa2b020fc497a"} |
vcr_8845021fc2b2 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-20, -14, -25, 5, -15, -21, 33, 20, 7, -21, -21, 4]}, "output": [-20, -34, -59, -54, -69, -90, -57, -37, -30, -51, -72, -68]}, {"input": {"nums": [14, -4, 12, 11, -5]}, "output": [14, 10, 22, 33, 28]}, {"input": {"nums": [5, 8, -9, 11, 7]}, "output": [5, 13, 4, 15, 22]}] | 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": "4867a4bf9a14802b"} |
vcr_48090f0f6c21 | 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, 2, 4, 6, 7, 10, 22, 25, 33, 36, 46], "target": 16}, "output": 6}, {"input": {"nums": [3, 5, 10, 16, 25, 28, 29, 30, 31, 36], "target": 24}, "output": 4}, {"input": {"nums": [5, 13, 14, 17, 24, 27, 30, 32, 33, 35, 37, 38], "target": 40}, "output": 12}] | 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": "9e70475ab159eb3e"} |
vcr_22c12b16b8b0 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 5, 12, 13, 17, 23, 31, 35, 39, 41, 42], "target": 18}, "output": 5}, {"input": {"nums": [4, 14, 21, 23, 25, 32, 34], "target": 37}, "output": 7}, {"input": {"nums": [5, 6, 11, 12, 16, 19, 22, 23, 26, 29, 32], "target": 11}, "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": "755a04f0561bdf4d"} |
vcr_a044d07ceee8 | 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": "cdcc ebe ccddea cda"}, "output": "cda ccddea ebe cdcc"}, {"input": {"s": "aa b abb"}, "output": "abb b aa"}, {"input": {"s": "a a ab"}, "output": "ab a 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": "115db97876c197c0"} |
vcr_3572a0b292de | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [13, -2, -14, 14, -5, 12, -8, 5, 10, -15, -8, -11, -8, 21]}, "output": 28}, {"input": {"nums": [14, -11, -2]}, "output": 14}, {"input": {"nums": [13, -10, 14]}, "output": 17}] | 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": "e62769f3b1e4113e"} |
vcr_1921a2016258 | 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, 4, 8, 9, 11, 19, 21, 28, 29, 36, 38, 39, 44, 47], "target": 12}, "output": 5}, {"input": {"nums": [2, 3, 7, 11, 12, 13, 15, 19, 26, 28, 29], "target": 11}, "output": 3}, {"input": {"nums": [2, 3, 5, 13, 18, 19, 22, 23, 32, 36], "target": 12}, "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": "06dbe75138140a91"} |
vcr_12ab6606def8 | 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": 24381}, "output": 11}, {"input": {"n": 764}, "output": 7}, {"input": {"n": 4535}, "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": "206a5c60c0852908"} |
vcr_8d43690f0b7c | 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": 60552}, "output": 7}, {"input": {"n": 3955}, "output": 9}, {"input": {"n": 10482}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "538cb4b7c01695e8"} |
vcr_d502deeef8dd | 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": [18, -4, 6, 0]}, "output": [18, 14, 20, 20]}, {"input": {"nums": [-9, 11, 1, 11, -3, 17, 3]}, "output": [-9, 2, 3, 14, 11, 28, 31]}, {"input": {"nums": [14, 6, 13, -6, 14, -4]}, "output": [14, 20, 33, 27, 41, 37]}] | 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": "4a6e84e8cc7a6c74"} |
vcr_f3943a15f470 | 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": 18705}, "output": 5}, {"input": {"n": 6673}, "output": 5}, {"input": {"n": 11140}, "output": 6}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "b2cb1ee3dfc3fd0e"} |
vcr_be715ccf91fa | 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": [41, 24, 21, 58, 23, 28, 4, 35, 15, 24]}, "output": 37}, {"input": {"prices": [12, 13, 39, 27, 34, 35, 17, 22, 33, 6, 27, 28]}, "output": 27}, {"input": {"prices": [2, 6, 6, 4, 39, 24]}, "output": 37}] | 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": "0afc6cadc6cc6f83"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.