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_53780752ad82 | 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": [29, 30, 31, 47], "target": 45}, "output": 3}, {"input": {"nums": [1, 3, 7, 12, 25, 31, 34, 36, 37], "target": 24}, "output": 4}, {"input": {"nums": [7, 8, 18, 28, 29, 34, 35, 36], "target": 40}, "output": 8}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "633b108ba5a77e59"} |
vcr_d0cfe88d54b1 | 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, 13, 7, 7, 30, 2, 6, 2]}, "output": 49}, {"input": {"nums": [7, 16, 5, 18, 20, 15, 2, 12]}, "output": 61}, {"input": {"nums": [12, 16, 13, 0, 5, 14, 13, 6, 16]}, "output": 59}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "cc386dee831e6716"} |
vcr_0daabc94482a | 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": [44, 41, 7, 41, 8, 35, 21, 8, 8]}, "output": true}, {"input": {"nums": [4, 18, 7, 18, 19]}, "output": true}, {"input": {"nums": [21, 21, 16, 13, 14, 17]}, "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": "4bf5030bd60a4d31"} |
vcr_4a4bf30efccc | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 5, 7, 8, 10, 15]}, "output": [2, 5, 7, 8, 10, 15]}, {"input": {"nums": [1, 1, 3, 7, 9, 11]}, "output": [1, 3, 7, 9, 11]}, {"input": {"nums": [1, 1, 2, 3, 5, 8]}, "output": [1, 2, 3, 5, 8]}] | 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": "7e4cf0e701420d1e"} |
vcr_838e041f470c | 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": [28, 24, 7, 25]}, "output": [28, 52, 59, 84]}, {"input": {"nums": [16, 9, -6, 8, -12, 3, -12, 0, -10]}, "output": [16, 25, 19, 27, 15, 18, 6, 6, -4]}, {"input": {"nums": [-8, 6, 9, -4, 18, -8, 7, 8, 3, 15]}, "output": [-8, -2, 7, 3, 21, 13, 20, 28, 31, 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": "2522e447709e0d42"} |
vcr_7f91ba818e6a | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [29, 9, -7]}, "output": [29, 38, 31]}, {"input": {"nums": [-5, 16, 13, 18, 1, -8, 5]}, "output": [-5, 11, 24, 42, 43, 35, 40]}, {"input": {"nums": [4, 2, 10, -10, 11, 9, 15, 2, 12]}, "output": [4, 6, 16, 6, 17, 26, 41, 43, 55]}] | 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": "f0a8be8f74779fba"} |
vcr_bcc358e6e522 | 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, 6, 14, 17, 24, 26, 30, 34, 39, 43], "target": 37}, "output": 8}, {"input": {"nums": [0, 2, 4, 6, 10, 12, 20, 21, 34], "target": 35}, "output": 9}, {"input": {"nums": [6, 24, 32], "target": 31}, "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": "dfa2115c6e06a629"} |
vcr_7dbb259a55da | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bb", "cba", "ba", "ab", "cc", "cb", "bcc", "baa"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "aab", "bbb", "aaa", "abb", "bbb", "bbb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["aaa", "bba", "abb", "aab", "bbb", "bab"]}, "output": [1, 1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "318bfa604a1eb998"} |
vcr_af0d6f37b5df | 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": "caabcacaaacbb"}, "output": 9}, {"input": {"s": "bcbcaac"}, "output": 6}, {"input": {"s": "bacaa"}, "output": 4}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "e3bff292be9f8479"} |
vcr_41a014a49f38 | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "aabbdcddddcdbbaa"}, "output": true}, {"input": {"s": "bcabcacb"}, "output": false}, {"input": {"s": "aabaabac"}, "output": false}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "c2cd579847d9de96"} |
vcr_f34e19e2b778 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [35, 18, 30, 50, 16, 51, 36, 47, 41, 2, 55, 47, 5, 50, 14, 41]}, "output": 53}, {"input": {"prices": [21, 35, 7]}, "output": 14}, {"input": {"prices": [27, 26, 28, 1, 26, 29, 17, 29, 13, 32, 23, 38]}, "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": "f41c6480a260b678"} |
vcr_16bedbe2a2ec | 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": [-11, 43, 7, -5, 28, 65], "target": 35}, "output": [2, 4]}, {"input": {"nums": [10, 6, 1, 35, 8], "target": 18}, "output": [0, 4]}, {"input": {"nums": [28, 4, 6, 12], "target": 34}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "3add57f6ca49cdfa"} |
vcr_4530b28fa37a | 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": "aaada daabea aacc aaadc"}, "output": "aaadc aacc daabea aaada"}, {"input": {"s": "bbb bbbb"}, "output": "bbbb bbb"}, {"input": {"s": "baa aba a abb b"}, "output": "b abb a aba 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": "969c8eea932adda2"} |
vcr_9b7a742a2bac | 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": ["bac", "cbab", "acc", "bc"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bbb", "aba", "abb"]}, "output": [1, 1, 1]}, {"input": {"strs": ["bab", "aba", "aba"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "6c1d9ae6963d7159"} |
vcr_1d8b19191f60 | 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": [-3, 12, 17, 35, 49], "target": 84}, "output": [3, 4]}, {"input": {"nums": [-12, -8, 9, 15, 37], "target": 52}, "output": [3, 4]}, {"input": {"nums": [-18, -17, -16, -14, -13, -7, 15, 17, 19, 39], "target": -1}, "output": [0, 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": "a0b13cc3be168710"} |
vcr_5faf1816be8b | 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": 36803}, "output": 9}, {"input": {"n": 1128}, "output": 4}, {"input": {"n": 4643}, "output": 5}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "f3b1d5471317b492"} |
vcr_8cf7a6a2fdc5 | 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, 3, 8, 10, 14]}, "output": [0, 3, 8, 10, 14]}, {"input": {"nums": [0, 0, 5, 11, 12]}, "output": [0, 5, 11, 12]}, {"input": {"nums": [2, 3, 3, 6, 9, 10, 11]}, "output": [2, 3, 6, 9, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "a62fc8c78fe1cd8b"} |
vcr_ac323a853702 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 1, 3, 4, 0, 0, 1, 4, 3, 0]}, "output": true}, {"input": {"nums": [2, 1, 2]}, "output": true}, {"input": {"nums": [2, 3, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "1667d4a2b3d3e89b"} |
vcr_99e2ad3e8b1c | 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": ["bcba", "bbab", "cbcb", "ba", "caab"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "aab", "aba", "aaa", "bba", "aab"]}, "output": [1, 1, 4]}, {"input": {"strs": ["bbb", "abb", "aaa", "bab", "bba", "aab", "aaa"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "16c227eefbbcab62"} |
vcr_510ab47a041a | 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": "edcca a abce"}, "output": "abce a edcca"}, {"input": {"s": "baab abb abb"}, "output": "abb abb baab"}, {"input": {"s": "baa ab"}, "output": "ab 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": "c4289e925028de6a"} |
vcr_2531e6988f37 | 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": "edecb badec dac"}, "output": "dac badec edecb"}, {"input": {"s": "abb aaa"}, "output": "aaa abb"}, {"input": {"s": "bba b"}, "output": "b bba"}] | 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": "c7d0bc47867fab1e"} |
vcr_628fa62fdb1c | 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": [58, 13, 11, 34, 20, 9, 13, 34, 39, 20, 11, 9, 39, 5, 5]}, "output": 58}, {"input": {"nums": [27, 9, 28, 18, 14, 12, 18, 27, 9, 28, 14, 12, 44]}, "output": 44}, {"input": {"nums": [15, 25, 15, 9, 25, 9, 40]}, "output": 40}] | 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": "e7c358f3423d2fad"} |
vcr_b0d045eaf8aa | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 4, 0, 3, 3, 3, 4, 0]}, "output": true}, {"input": {"nums": [2, 3, 3, 3, 2, 0, 3, 1, 1]}, "output": true}, {"input": {"nums": [0, 1, 0, 0, 2, 1, 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": "5b02c2e5fa66d1fb"} |
vcr_fa14db516136 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-26, -15, 6, 9, 11, 14, 16, 19, 43], "target": 20}, "output": [2, 5]}, {"input": {"nums": [-16, -15, -8, 9, 19, 21, 25, 26, 29, 32], "target": 38}, "output": [3, 8]}, {"input": {"nums": [-20, -14, 2, 11, 14, 21], "target": 0}, "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": "96bbc88efd3199b6"} |
vcr_4eec3ab7a226 | 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": [-1, 7, 20, 17, 19, -10, -4, 13, 12, 8, 18]}, "output": 100}, {"input": {"nums": [-10, 13, 3, 7, -12, -5, 4, -3]}, "output": 23}, {"input": {"nums": [-9, 0, 14, -6, -2, -6, -7, 13, -9]}, "output": 14}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "421995f678de05ca"} |
vcr_e0b529d92537 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-20, 18, 13, 12, -15], "k": 6}, "output": [-15, -20, 18, 13, 12]}, {"input": {"nums": [-6, -7, 1, 8, -6, -3, 8, -9, -5], "k": 20}, "output": [-9, -5, -6, -7, 1, 8, -6, -3, 8]}, {"input": {"nums": [10, -1, -10, -2, 1], "k": 12}, "output": [-2, 1, 10, -1, -10]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "c67eaa459af1434e"} |
vcr_77bc970f9df4 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-27, -19, -12, -4, 23, 29, 33, 49], "target": -16}, "output": [2, 3]}, {"input": {"nums": [-10, 5, 6, 11, 16, 21, 31], "target": 22}, "output": [2, 4]}, {"input": {"nums": [-20, -18, -5, -3, -2, 11, 14, 21, 29, 32, 33], "target": -38}, "output": [0, 1]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "c288cf53b471cf5b"} |
vcr_7939b8fe72b1 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-17, 10, 27, 2, -4, 27, -20, -14, 22]}, "output": [-17, -7, 20, 22, 18, 45, 25, 11, 33]}, {"input": {"nums": [2, 10, 4, 11, -10, 13]}, "output": [2, 12, 16, 27, 17, 30]}, {"input": {"nums": [5, 7, 18, -9, 13, 8, -9, -6, 4]}, "output": [5, 12, 30, 21, 34, 42, 33, 27, 31]}] | 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": "d21fe3f3b906ab50"} |
vcr_6a847b075939 | 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, 2, -18, 8, -6, -5, -8], "k": 3}, "output": [-6, -5, -8, 9, 2, -18, 8]}, {"input": {"nums": [8, 2, 7, 4, 10, -6, 9], "k": 4}, "output": [4, 10, -6, 9, 8, 2, 7]}, {"input": {"nums": [9, 2, 6, -8, -4], "k": 8}, "output": [6, -8, -4, 9, 2]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "10302da444772c80"} |
vcr_2793072ad64d | 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": [37, -22, 1, 50, -5, 65, 48, -31, -21, 21, 4], "target": 19}, "output": [3, 7]}, {"input": {"nums": [4, 10, 29, -9, -10], "target": 20}, "output": [2, 3]}, {"input": {"nums": [4, 7, 35, 3, 44, 28, -29, -28, 48], "target": 6}, "output": [2, 6]}] | 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": "d522fb46a32cc80e"} |
vcr_4029dc94d0db | 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": [-17, -7, 18, 16, -16, -7]}, "output": 34}, {"input": {"nums": [-11, 5, 6, -1, -12, -9, -10, -9]}, "output": 11}, {"input": {"nums": [6, -1, 6]}, "output": 11}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "56750fd920f42853"} |
vcr_a3422aca5c26 | 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, 6, -9, 0, 10]}, "output": [-5, 1, -8, -8, 2]}, {"input": {"nums": [-12, 1, -2, -12, -3, 18, 13]}, "output": [-12, -11, -13, -25, -28, -10, 3]}, {"input": {"nums": [-11, 16, 6]}, "output": [-11, 5, 11]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "602fd5f40e47ad39"} |
vcr_e13a70e8c68f | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [7, 7, 21, 21, 21, 12, 20, 9, 0, 22, 13, 21]}, "output": 112}, {"input": {"nums": [17, 18]}, "output": 18}, {"input": {"nums": [10, 6, 18, 8, 16, 17]}, "output": 45}] | 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": "597b52262e2f44cf"} |
vcr_446aa695f40a | 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, 4, 3, 4, 2, 4, 0, 0]}, "output": true}, {"input": {"nums": [0, 0, 3, 1, 2, 3]}, "output": false}, {"input": {"nums": [3, 0, 2, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "b5a39ddd20d98296"} |
vcr_d069026982b5 | 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": [17, 34, 41, 42], "target": 23}, "output": 1}, {"input": {"nums": [0, 8, 9, 12, 26, 28, 31, 32, 35, 37, 38], "target": 31}, "output": 6}, {"input": {"nums": [9, 19, 21, 28], "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": "9ee824df1c42a172"} |
vcr_17035c2fa160 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [11, 25, 41, 43], "target": 3}, "output": 0}, {"input": {"nums": [0, 2, 4, 10, 13, 16, 17, 21, 23, 28, 31, 38], "target": 22}, "output": 8}, {"input": {"nums": [1, 2, 3, 5, 8, 11, 16, 20, 38], "target": 15}, "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": "d3dff97835d7112f"} |
vcr_786291f02e84 | hard | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "adcdccabad"}, "output": false}, {"input": {"s": "ba"}, "output": false}, {"input": {"s": "cbbacb"}, "output": false}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "92fc69ff801c1c0f"} |
vcr_5661dc3d0ed2 | 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, 3, 0, 3, 0, 4, 1, 2, 0]}, "output": true}, {"input": {"nums": [3, 1, 3, 3, 2, 0, 0]}, "output": true}, {"input": {"nums": [2, 3, 2, 2, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "bdc52833fb8d9b6f"} |
vcr_4759a9f4a396 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-17, -11, 0, -17, -12, -11, 3, 17], "k": 22}, "output": [0, -17, -12, -11, 3, 17, -17, -11]}, {"input": {"nums": [-5, 1, 5, 7, 4, -9], "k": 15}, "output": [7, 4, -9, -5, 1, 5]}, {"input": {"nums": [0, 9, -4, 7], "k": 14}, "output": [-4, 7, 0, 9]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "a65919a2cea57f26"} |
vcr_b9834bf19d4b | 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": [-1, -6, -16, -5, -11, -3]}, "output": -1}, {"input": {"nums": [12, -11, -7]}, "output": 12}, {"input": {"nums": [-1, -2, 4, 12, -11, 10, -12, -3, -4, -12]}, "output": 16}] | 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": "403b9a657ddf7af3"} |
vcr_3872fca787ee | 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": "bbbabccbcacb"}, "output": 9}, {"input": {"s": "aabbbbaac"}, "output": 4}, {"input": {"s": "aabbccbbaca"}, "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": "0f34c23571ab990f"} |
vcr_1dbbb0e6d968 | 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": [42, 92, 108, 94, 69, 116, 33]}, "output": false}, {"input": {"nums": [13, 24, 11, 24]}, "output": true}, {"input": {"nums": [0, 9, 19, 15, 2, 22, 22, 3, 22]}, "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": "3b3b4c0a79122725"} |
vcr_a8febad759dd | 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": [6, 24, 25, 31], "target": 21}, "output": 1}, {"input": {"nums": [2, 24, 30], "target": 1}, "output": 0}, {"input": {"nums": [0, 5, 10, 13, 17, 37, 39], "target": 6}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "221165fc62e76d96"} |
vcr_7c5a08b94e11 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [22, 30, 16, 18]}, "output": 48}, {"input": {"nums": [10, 10, 1, 5, 14, 16, 19, 8, 9]}, "output": 53}, {"input": {"nums": [2, 11]}, "output": 11}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "cb1ef44c4e4915d5"} |
vcr_ac7b4b5b5511 | 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": [20, 2, 23, 45, 37, 49, 35, 30, 20, 3]}, "output": true}, {"input": {"nums": [5, 11, 6, 16, 5, 16, 11, 23, 24, 2, 8]}, "output": true}, {"input": {"nums": [12, 16, 5, 22, 3, 2, 9, 5, 12]}, "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": "7f23a4cbbe015569"} |
vcr_cd6f7006d8ad | 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": [2, 18, 45, -25, -9, 60, -37, 36, -31, 57], "target": 78}, "output": [1, 5]}, {"input": {"nums": [-17, 12, 44, 7, -3, 10, 16, 36, -10, 47, 48], "target": 4}, "output": [3, 4]}, {"input": {"nums": [0, 11, -20, 13], "target": -7}, "output": [2, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "6d0dce5f7839bc8c"} |
vcr_2d988953d684 | 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": [-9, -2, 13, 1], "target": 14}, "output": [2, 3]}, {"input": {"nums": [29, -25, -14, -17, -28, 20], "target": -8}, "output": [4, 5]}, {"input": {"nums": [-25, 46, 35, 29, 8, -19, 14, 37, -22, 41], "target": 4}, "output": [0, 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": "9bf00ca7f2230e87"} |
vcr_2d2b4659f97b | 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": [16, 11, -15, -4, 20, 13, 3, -7, 20, 15, -10, 9], "k": 34}, "output": [-15, -4, 20, 13, 3, -7, 20, 15, -10, 9, 16, 11]}, {"input": {"nums": [5, 0, -9], "k": 4}, "output": [-9, 5, 0]}, {"input": {"nums": [-6, 4, -10, 0, -5, -2, 10], "k": 14}, "output": [-6, 4, -10, 0, -5, -2, 10]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "5f63cded99831002"} |
vcr_d077b7675431 | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "cacdaacacbad"}, "output": false}, {"input": {"s": "abaccac"}, "output": false}, {"input": {"s": "accacbaba"}, "output": false}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "26315707904d266e"} |
vcr_41e93786982c | 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": "ea c dba ceced abbecd"}, "output": "abbecd ceced dba c ea"}, {"input": {"s": "baab baaa baa ba"}, "output": "ba baa baaa baab"}, {"input": {"s": "aaaa ba abab a"}, "output": "a abab ba aaaa"}] | 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": "aa24e68c2bbfc60b"} |
vcr_57e2123ff4cf | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [9, 15, 2, 2, 21, 35, 38, 15, 21, 77, 35, 32, 38, 9, 32]}, "output": 77}, {"input": {"nums": [23, 12, 22, 23, 12, 56, 22]}, "output": 56}, {"input": {"nums": [41, 16, 20, 20, 16]}, "output": 41}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "e60901643c0828e6"} |
vcr_d78c8fb07b39 | 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": [4, 20, 30, 6, 3, 26]}, "output": 60}, {"input": {"nums": [5, 17, 10, 9, 14, 3, 5]}, "output": 36}, {"input": {"nums": [13, 4, 4, 17, 20, 17]}, "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": "82ab797b22c25ea1"} |
vcr_071327cd6410 | 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": [4, 11, 24, 55, 17]}, "output": 51}, {"input": {"prices": [22, 34, 40, 2, 4, 36, 17, 14]}, "output": 34}, {"input": {"prices": [36, 15, 39, 33, 24, 27, 16, 10, 36]}, "output": 26}] | 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": "b7c8eb41d8b2764a"} |
vcr_82e4aaac51cc | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 1, 7, 6, 1, 4, 5]}, "output": [840, 4200, 600, 700, 4200, 1050, 840]}, {"input": {"nums": [2, 3, 3, 1, 3]}, "output": [27, 18, 18, 54, 18]}, {"input": {"nums": [1, 4, 4, 1, 2]}, "output": [32, 8, 8, 32, 16]}] | 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": "7da6d8a02944a993"} |
vcr_6099c5d3fd38 | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "bdcaacbbcd"}, "output": false}, {"input": {"s": "abaaa"}, "output": false}, {"input": {"s": "cbaab"}, "output": false}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "fa10090635e4d119"} |
vcr_21171e533f21 | 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": [44, 5, 28, 21, 44]}, "output": true}, {"input": {"nums": [23, 3, 13, 18, 17, 9, 1, 21, 1, 10, 16, 4]}, "output": true}, {"input": {"nums": [19, 6, 18, 8, 1, 23]}, "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": "4652f5b6794b5be2"} |
vcr_f8a38958953e | 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": [-17, 79, 4, -7, 61, 42, -38], "target": 65}, "output": [2, 4]}, {"input": {"nums": [16, 14, 39, -1, 46, 21, 0], "target": 67}, "output": [4, 5]}, {"input": {"nums": [-5, -3, -7, 40, 20, -21, -14, -20], "target": 26}, "output": [3, 6]}] | 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": "07f922038f012b9f"} |
vcr_62e7c149d209 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [22, 18, -2, -8, 19, 3, 4, 3, 8]}, "output": 67}, {"input": {"nums": [-4, 2, 10, -5, 2, -10]}, "output": 12}, {"input": {"nums": [-5, 10, 3, -7]}, "output": 13}] | 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": "309f60308a476500"} |
vcr_1b8b59d90fcc | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [36, 26, 21, 39, 39, 16, 22, 11, 22, 26, 63, 16, 36, 11, 21]}, "output": 63}, {"input": {"nums": [6, 27, 6, 16, 27, 16, 26, 19, 26, 52, 19]}, "output": 52}, {"input": {"nums": [10, 27, 51, 12, 8, 8, 19, 10, 27, 25, 12, 19, 25]}, "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": "481526183debea1c"} |
vcr_b95ba3a83170 | 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": [36, 133, 34, 148, 129, 162, 96, 167, 97, 68, 89]}, "output": false}, {"input": {"nums": [19, 11, 8, 3, 15, 23, 25]}, "output": false}, {"input": {"nums": [21, 22, 4, 13, 23, 5, 4, 14, 17, 3, 1, 25]}, "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": "95b438cf4774d6ac"} |
vcr_7054abbbe081 | 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": [-4, 19, -10, -5, -7, -17, -4, 17, 0, -12, 18, 2, 17, -21]}, "output": [-4, 15, 5, 0, -7, -24, -28, -11, -11, -23, -5, -3, 14, -7]}, {"input": {"nums": [15, -10, 4, 5, 17, -3, -3]}, "output": [15, 5, 9, 14, 31, 28, 25]}, {"input": {"nums": [-4, -9, -11, -5, -8]}, "output": [-4, -13, -24, -29, -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": "7f037f97b9cf7aa4"} |
vcr_0536e33a60b7 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-17, 28, 22, 3, -7, 16, 18, -12, 30]}, "output": [-17, 11, 33, 36, 29, 45, 63, 51, 81]}, {"input": {"nums": [11, 1]}, "output": [11, 12]}, {"input": {"nums": [14, 2, 6, 13]}, "output": [14, 16, 22, 35]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "d651fee3670a9045"} |
vcr_298a12b2e47d | 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": [12, 16, 40, 44, 10, 12]}, "output": true}, {"input": {"nums": [7, 4, 7, 10, 19, 18, 1, 16]}, "output": true}, {"input": {"nums": [20, 20, 15, 10, 4, 25]}, "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": "4634fc6d4b2442f2"} |
vcr_0773fa68623d | 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, -18, -17, -14, -2, -1, 8, 22, 48], "target": 30}, "output": [1, 8]}, {"input": {"nums": [-17, -12, -5, -2, 8, 10, 20, 23], "target": 33}, "output": [5, 7]}, {"input": {"nums": [-6, -1, 17, 18], "target": 17}, "output": [1, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "8d4e04816168ac25"} |
vcr_2828d612228a | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "d ddcbb bbdaea bbdc"}, "output": "bbdc bbdaea ddcbb d"}, {"input": {"s": "aa bbb"}, "output": "bbb aa"}, {"input": {"s": "aaba b abab"}, "output": "abab b aaba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "21da1a7320642ace"} |
vcr_8cc5330e7d61 | 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": [-1, 5, -18, 22, 13, -9, 19, 20, -4, 4, 5, 16, -5, 17]}, "output": 98}, {"input": {"nums": [-1, -9, 2, 0, -5, -6, 10, -2, 6]}, "output": 14}, {"input": {"nums": [11, 0, 2, 5, 13, 9, 6, -4, 7, 13]}, "output": 62}] | 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": "4b5daca27771de8c"} |
vcr_450768e192eb | 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": [19, 5, -4, -12, 15, 0, 8, -12, 21, 7, 2, 1, 18, -13, -11]}, "output": 68}, {"input": {"nums": [13, 3, 0, 14]}, "output": 30}, {"input": {"nums": [13, 1, 5, 10, 6, -2, 11, -2, -10]}, "output": 44}] | 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": "ef6e3b88a2ebb184"} |
vcr_1bf3f779b1fd | 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": 230, "b": 179}, "output": 1}, {"input": {"a": 86, "b": 125}, "output": 1}, {"input": {"a": 47, "b": 43}, "output": 1}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "b2ff743122c38ba1"} |
vcr_60a68d09c6d3 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [21, 32, 21, 38, 28, 22, 12, 55, 12, 37, 37, 28, 38, 22, 32]}, "output": 55}, {"input": {"nums": [42, 29, 7, 7, 29]}, "output": 42}, {"input": {"nums": [4, 16, 16, 4, 18, 18, 55, 27, 27]}, "output": 55}] | 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": "0f3b2d3bbe4879f0"} |
vcr_7327df66c84c | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [4, 7, 11, 21, 32, 33, 37, 44, 49], "target": 81}, "output": [4, 8]}, {"input": {"nums": [-17, 6, 10, 13, 27, 33, 37], "target": 46}, "output": [3, 5]}, {"input": {"nums": [-13, 30, 31, 37], "target": 17}, "output": [0, 1]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "a746ebc714e2659c"} |
vcr_848409e8ce96 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-18, -15, -16, 6, 4, 16, -10, 20, -3, -2], "k": 3}, "output": [20, -3, -2, -18, -15, -16, 6, 4, 16, -10]}, {"input": {"nums": [-10, -3, -1, 4, 3], "k": 13}, "output": [-1, 4, 3, -10, -3]}, {"input": {"nums": [0, -5, -5, -3, -2, -2, -2, -9, 0], "k": 7}, "output": [-5, -3, -2, -2, -2, -9, 0, 0, -5]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "3583a51714658dd1"} |
vcr_80b5038057e9 | 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": [-11, 9, 15, 22, 29, 30], "target": 18}, "output": [0, 4]}, {"input": {"nums": [-16, -10, -8, -5, -4, 21, 22], "target": -24}, "output": [0, 2]}, {"input": {"nums": [-18, -17, -3, 6, 13, 14, 17, 20], "target": 17}, "output": [2, 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": "3e3d6a85c4a309f7"} |
vcr_d0fad380f440 | 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": [-6, 26, 34, 36, 47], "target": 62}, "output": [1, 3]}, {"input": {"nums": [-8, 2, 7, 14, 17, 21, 27, 36], "target": 35}, "output": [3, 5]}, {"input": {"nums": [-19, -13, 5, 6, 16, 18, 23, 28, 39], "target": 10}, "output": [1, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "e21134d32b292f42"} |
vcr_e64084165522 | 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": ["bbab", "abab", "bc", "cc", "baa", "ca"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bba", "aaa", "aba", "bab", "bab", "baa"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["bba", "aab", "aaa", "aaa"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "3b287e4d539b60c2"} |
vcr_0091c7d83067 | 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, 5, 13, 13, 14]}, "output": [4, 5, 13, 14]}, {"input": {"nums": [6, 7, 8]}, "output": [6, 7, 8]}, {"input": {"nums": [0, 1, 3, 5, 5, 5, 6, 7, 9, 10, 10]}, "output": [0, 1, 3, 5, 6, 7, 9, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "fbac8cbb4134fa89"} |
vcr_897f48431127 | 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, 6, 8, 9, 9, 9, 12, 13, 13, 15]}, "output": [1, 2, 6, 8, 9, 12, 13, 15]}, {"input": {"nums": [1, 1, 2, 4, 9, 9, 10, 12]}, "output": [1, 2, 4, 9, 10, 12]}, {"input": {"nums": [0, 0, 1, 2, 4, 6, 6, 8, 9, 10, 12, 12]}, "output": [0, 1, 2, 4, 6, 8, 9, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "0acc060691a48791"} |
vcr_87f0106bcd0e | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 1, 2, 0, 3, 0, 4, 0, 0]}, "output": true}, {"input": {"nums": [3, 1, 1, 2, 3]}, "output": true}, {"input": {"nums": [1, 1, 3, 1, 0, 0, 2, 0, 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": "b6c668a2f42ee970"} |
vcr_2975eebaadd1 | 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, 8, 25, 28, 34, 43], "target": 3}, "output": 1}, {"input": {"nums": [1, 17, 38], "target": 17}, "output": 1}, {"input": {"nums": [1, 4, 15, 27, 37], "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": "bf055c6c8f604926"} |
vcr_d082be2d5f9a | 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, 13, 12, 2, 24, 17, 14, 14, 3, 6, 20]}, "output": 78}, {"input": {"nums": [13, 8, 6, 17, 4]}, "output": 30}, {"input": {"nums": [6, 8]}, "output": 8}] | 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": "ed2b7f6c3ef2f42f"} |
vcr_280e965ec70e | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [5, -1, -6, -6, -6, -16, 20, 9, -3, 18], "k": 24}, "output": [20, 9, -3, 18, 5, -1, -6, -6, -6, -16]}, {"input": {"nums": [4, 8, -6, 3, -5], "k": 0}, "output": [4, 8, -6, 3, -5]}, {"input": {"nums": [-5, 6, -3, 0, 9, -2, -6, 1, -10], "k": 13}, "output": [-2, -6, 1, -10, -5, 6, -3, 0, 9]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "444ea5aef67d66be"} |
vcr_7dbbf2f81d0f | 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": [44, 17, 34, 57, 24, 48, 20, 4, 9, 51, 51, 42, 42, 19, 23, 48]}, "output": 47}, {"input": {"prices": [14, 27, 20, 37, 16, 20, 27, 34]}, "output": 23}, {"input": {"prices": [26, 26, 7, 15, 5]}, "output": 8}] | 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": "6b910b973b0a3fe0"} |
vcr_dc1be9b8eb65 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [11, 16, 0, -12, 15, -18, 6, -20, -19, 10], "k": 7}, "output": [-12, 15, -18, 6, -20, -19, 10, 11, 16, 0]}, {"input": {"nums": [-10, 3, -7], "k": 4}, "output": [-7, -10, 3]}, {"input": {"nums": [0, -5, -2, -2, 1], "k": 3}, "output": [-2, -2, 1, 0, -5]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "c2f08242a72ba109"} |
vcr_c6a55ac41be4 | 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": 55, "b": 37}, "output": 1}, {"input": {"a": 98, "b": 50}, "output": 2}, {"input": {"a": 124, "b": 47}, "output": 1}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "833f40b1a218dfb4"} |
vcr_8b0597b40aef | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-7, -16, 9, -9, -10, -12, -10], "k": 7}, "output": [-7, -16, 9, -9, -10, -12, -10]}, {"input": {"nums": [-5, -5, -10, 8, 0, 9, 2, 9, 7], "k": 20}, "output": [9, 7, -5, -5, -10, 8, 0, 9, 2]}, {"input": {"nums": [6, 7, -7, 7, 3], "k": 5}, "output": [6, 7, -7, 7, 3]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "bc264262d3ee1468"} |
vcr_09395e828c95 | 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, -18, 4, -9, 14, -12], "k": 12}, "output": [12, -18, 4, -9, 14, -12]}, {"input": {"nums": [1, 8, 0], "k": 5}, "output": [8, 0, 1]}, {"input": {"nums": [-1, 6, 4, 6, 1, -7, 7, 9, 7, 7], "k": 7}, "output": [6, 1, -7, 7, 9, 7, 7, -1, 6, 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": "412546e3300efe9e"} |
vcr_bb6109dcf3b7 | 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": [61, 18, -34, 74, 21, 45, 20, -6, 17, 59, 40, 54], "target": 57}, "output": [8, 10]}, {"input": {"nums": [0, 43, 40, -24, -23, -1, 20, -19], "target": -19}, "output": [0, 7]}, {"input": {"nums": [42, 26, -17, -20, 48, 23, 11, 43, -7], "target": 66}, "output": [5, 7]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "452c3df25cefda14"} |
vcr_1ec180886bc1 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 1, 3, 3, 6, 6, 6, 10, 13, 13, 14, 14, 14, 15]}, "output": [1, 3, 6, 10, 13, 14, 15]}, {"input": {"nums": [0, 0, 2, 3, 4, 6, 8, 9, 9, 10, 11, 12]}, "output": [0, 2, 3, 4, 6, 8, 9, 10, 11, 12]}, {"input": {"nums": [4, 4, 5, 8, 9, 9, 9, 9, 12]}, "output": [4, 5, 8, 9, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "2c2c428dfc8d10a3"} |
vcr_53aa45009c6b | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [4, 1, 6, 1, 5, 1]}, "output": [30, 120, 20, 120, 24, 120]}, {"input": {"nums": [4, 3, 4, 3, 4, 2, 4]}, "output": [1152, 1536, 1152, 1536, 1152, 2304, 1152]}, {"input": {"nums": [5, 1, 1, 2, 3, 4]}, "output": [24, 120, 120, 60, 40, 30]}] | 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": "3c788cddb9e3edc6"} |
vcr_07eea9cbe9a5 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [18, -1, -9, 15, 16, 0, -9, -3, 9, 8]}, "output": 44}, {"input": {"nums": [4, 14, 2, 13]}, "output": 33}, {"input": {"nums": [1, 8, -4, -7, 3, -1, 1, 2, -7]}, "output": 9}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "18506d5bcbacab83"} |
vcr_b55adf62f379 | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "dabccbaaacca"}, "output": false}, {"input": {"s": "bbccb"}, "output": false}, {"input": {"s": "acba"}, "output": false}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "5a9bb10d87504370"} |
vcr_5b9b4484e5dd | 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": "ddddea a eba ddaa a"}, "output": "a ddaa eba a ddddea"}, {"input": {"s": "aba ba abb"}, "output": "abb ba aba"}, {"input": {"s": "aa baba aaa"}, "output": "aaa baba 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": "ed5503d60b0143ff"} |
vcr_8fcb5bcff059 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["ccbc", "cbc", "abaa", "cab", "bc", "ccab", "cab", "bc", "bbcb"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["bba", "bbb", "aba"]}, "output": [1, 1, 1]}, {"input": {"strs": ["abb", "bbb", "abb", "aba", "abb"]}, "output": [1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "c0db7ce7b4299e95"} |
vcr_493be67585f6 | 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": [135, 64, 120, 25, 97, 26, 103, 199, 189, 86, 35]}, "output": false}, {"input": {"nums": [15, 25, 19, 10, 2, 6, 23, 15, 18, 25, 10, 23]}, "output": true}, {"input": {"nums": [23, 25, 18, 23, 21, 7, 9, 19]}, "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": "04b3cb77c1b6272f"} |
vcr_50af82bb5c18 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-4, 6, 7, 32, 36, 38, 43], "target": 28}, "output": [0, 3]}, {"input": {"nums": [-18, -13, -10, 2, 21, 32, 33], "target": -16}, "output": [0, 3]}, {"input": {"nums": [-14, -6, -4, 0, 1, 7, 19, 31, 33], "target": 20}, "output": [4, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "9032469343a69685"} |
vcr_7d31d7c47d89 | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "bbccbddccddbccbb"}, "output": true}, {"input": {"s": "cbccbbbb"}, "output": false}, {"input": {"s": "bbaacccc"}, "output": false}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "c545c255a89881fe"} |
vcr_0e2af46cba13 | 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": [24, 5, 25, 15, 1, 3, 2]}, "output": 52}, {"input": {"nums": [5, 14, 3, 2, 16, 11, 5, 11, 1, 16]}, "output": 57}, {"input": {"nums": [4, 4, 11, 1, 1, 17, 9]}, "output": 32}] | 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": "1d32ac30a797c989"} |
vcr_a14fb3a1ca1e | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [26, 20, 23, 58, 53, 53, 21, 6, 33]}, "output": 38}, {"input": {"prices": [35, 25, 39, 8]}, "output": 14}, {"input": {"prices": [28, 36, 33, 9, 37, 33, 23, 22, 25, 4, 12]}, "output": 28}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "9f4f380294a3507d"} |
vcr_c37a5c0bba64 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 12, 22, 23, 24, 33, 47, 49], "target": 26}, "output": 5}, {"input": {"nums": [0, 6, 22, 24, 27, 28, 37], "target": 20}, "output": 2}, {"input": {"nums": [3, 18, 36, 38], "target": 20}, "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": "44a608481ba4ac91"} |
vcr_f419da5a3c13 | 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": [-14, -2, 39, 30, 23, 5, 68, 63, 20], "target": 35}, "output": [3, 5]}, {"input": {"nums": [7, 31, 43, 17, 9], "target": 16}, "output": [0, 4]}, {"input": {"nums": [-21, 22, -24, 41, -15, 28], "target": 13}, "output": [4, 5]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "495f13253c7b21ef"} |
vcr_5e76fb28edb0 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-5, -7, 0, -2, 19, -18], "k": 14}, "output": [19, -18, -5, -7, 0, -2]}, {"input": {"nums": [-3, 4, -9, 7, 3, -10, -2, 0, 3, 3], "k": 11}, "output": [3, -3, 4, -9, 7, 3, -10, -2, 0, 3]}, {"input": {"nums": [3, -4, 3, -2, 8, 0, -10, 7], "k": 8}, "output": [3, -4, 3, -2, 8, 0, -10, 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": "d0abc2ef4c94f857"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.