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_0a5b8f4504bf | 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": [19, 36, 52, 50, 10, 16]}, "output": 33}, {"input": {"prices": [25, 36, 10]}, "output": 11}, {"input": {"prices": [1, 13, 13, 14, 8]}, "output": 13}] | 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": "79400448cb401284"} |
vcr_9593eab6028d | 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": [59, 20, 31, 28, 28, 5, 5, 13, 13, 20, 32, 14, 31, 14, 32]}, "output": 59}, {"input": {"nums": [6, 29, 6, 50, 29]}, "output": 50}, {"input": {"nums": [9, 50, 23, 23, 9, 15, 15]}, "output": 50}] | 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": "bfe0a15182771b4d"} |
vcr_854881e247c9 | 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, -9, 4, 19, 29, 30], "target": -22}, "output": [0, 2]}, {"input": {"nums": [-10, -5, 19, 23, 36], "target": 59}, "output": [3, 4]}, {"input": {"nums": [-13, 4, 5, 9, 16, 19, 24, 33, 36], "target": 3}, "output": [0, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "2ad89d1b43754247"} |
vcr_8de091cdd4c1 | 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, 3, 4, 4, 8, 8, 11]}, "output": [0, 3, 4, 8, 11]}, {"input": {"nums": [1, 2, 2, 3, 3, 3, 4, 6, 8]}, "output": [1, 2, 3, 4, 6, 8]}, {"input": {"nums": [0, 0, 0, 1, 1, 3, 4, 4, 4, 8, 11, 12]}, "output": [0, 1, 3, 4, 8, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "2bc701cdce574d7d"} |
vcr_1fdc4cac1eef | 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": [15, 18, 35, 37], "target": 3}, "output": 0}, {"input": {"nums": [10, 12, 22, 30], "target": 40}, "output": 4}, {"input": {"nums": [1, 3, 4, 5, 6, 13, 16, 19, 25, 29, 31, 38], "target": 32}, "output": 11}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "23714346b8ca94b9"} |
vcr_8397e5eb4db9 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -25, -23, -6, -4, -2, 17, 18, 20, 25, 30], "target": 43}, "output": [7, 9]}, {"input": {"nums": [-17, -16, -6, 0, 2, 3, 18, 22, 25, 27, 30], "target": 48}, "output": [6, 10]}, {"input": {"nums": [-9, 0, 3, 17, 30, 37], "target": -6}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "cc8b24ea4424deba"} |
vcr_f92d1c15c7a2 | 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": [19, 173, 113, 188, 24, 178, 21, 13]}, "output": false}, {"input": {"nums": [2, 16, 10]}, "output": false}, {"input": {"nums": [1, 22, 2, 18, 16, 19, 9, 11, 2]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "030babc6dd7fc997"} |
vcr_1f18f255ffbb | 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": [24, 11, 22, 14, 21, 67, 11, 14, 24, 21, 22]}, "output": 67}, {"input": {"nums": [28, 12, 12, 7, 21, 49, 28, 21, 7]}, "output": 49}, {"input": {"nums": [17, 29, 58, 17, 29, 15, 15]}, "output": 58}] | 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": "186df5c2ef8770d0"} |
vcr_18819228941c | 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": ["ccac", "ca", "ba", "ab", "cbaa", "baa", "bbcb", "ccc"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "aaa", "aaa", "bba", "bbb", "bab", "bbb"]}, "output": [1, 2, 2, 2]}, {"input": {"strs": ["bba", "baa", "bba", "aba", "aba", "baa"]}, "output": [2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "45f04eaf6d1992aa"} |
vcr_46adf48a8ffa | 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, -24, -1, 11, 14, 20, 27], "target": 25}, "output": [3, 4]}, {"input": {"nums": [-1, 1, 5, 11, 13, 17, 18, 19, 23, 37, 39], "target": 57}, "output": [6, 10]}, {"input": {"nums": [-20, -17, -11, -9, -3, 0, 18, 38], "target": -14}, "output": [2, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "a207c0c6c03fd28c"} |
vcr_9959618d40d6 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [25, -13, -17, 2, 26, -13, 30, -12, -24, 16]}, "output": [25, 12, -5, -3, 23, 10, 40, 28, 4, 20]}, {"input": {"nums": [-12, 6, -4, -7, 6, 11, -2, 13, 5]}, "output": [-12, -6, -10, -17, -11, 0, -2, 11, 16]}, {"input": {"nums": [-1, 11, 18, 6, 11, 2, 5]}, "output": [-1, 10, 28, 34, 45, 47, 52]}] | 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": "2d3ca544d939c661"} |
vcr_5fc95d470995 | 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, 8, 15, 21]}, "output": 43}, {"input": {"nums": [9, 17, 15, 18, 0, 6, 13, 17, 15, 13]}, "output": 71}, {"input": {"nums": [11, 10, 11, 15, 20, 20, 17]}, "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": "6ae53da18bbde4e3"} |
vcr_77327b826285 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-10, 8, -1, 17, 10, 4, -3, 20, 14, 8, -6, 21]}, "output": [-10, -2, -3, 14, 24, 28, 25, 45, 59, 67, 61, 82]}, {"input": {"nums": [-9, 14, -9, -5, -9, 14, 6, -5, -11]}, "output": [-9, 5, -4, -9, -18, -4, 2, -3, -14]}, {"input": {"nums": [2, -4, 1]}, "output": [2, -2, -1]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "cc326f0c30ff70d5"} |
vcr_c9dabb990673 | 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": "ec aaabe dce"}, "output": "dce aaabe ec"}, {"input": {"s": "aa baaa"}, "output": "baaa aa"}, {"input": {"s": "baa aaa aaa aabb"}, "output": "aabb aaa aaa 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": "7c583ac92c979238"} |
vcr_9ac374dd549a | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 4, 6, 17, 23, 24, 28, 36, 40, 46], "target": 13}, "output": 3}, {"input": {"nums": [4, 5, 10, 13, 22, 27, 30, 34], "target": 1}, "output": 0}, {"input": {"nums": [8, 16, 25, 32], "target": 37}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "34570d49f8217f9e"} |
vcr_baebc181976c | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [10, 7, 16, -5], "k": 12}, "output": [10, 7, 16, -5]}, {"input": {"nums": [10, -10, 2, -10], "k": 4}, "output": [10, -10, 2, -10]}, {"input": {"nums": [-10, -10, 6, -3, -2], "k": 9}, "output": [-10, 6, -3, -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": "17757a6510eadafd"} |
vcr_f9a87d18dc51 | 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": 7389}, "output": 9}, {"input": {"n": 8291}, "output": 5}, {"input": {"n": 1347}, "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": "a40dd99a269bc870"} |
vcr_6211d7aaf667 | 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": "ccbdbbbbdbcc"}, "output": true}, {"input": {"s": "bbcaabbcc"}, "output": false}, {"input": {"s": "cacabcaa"}, "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": "30d81281d69a9507"} |
vcr_d55ec543f51a | 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": 19330}, "output": 6}, {"input": {"n": 16189}, "output": 11}, {"input": {"n": 9598}, "output": 9}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "89a44904a2f92c0a"} |
vcr_ad75436b60b8 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 1, 1, 2, 5, 8, 8, 8, 10, 14]}, "output": [0, 1, 2, 5, 8, 10, 14]}, {"input": {"nums": [0, 0, 2, 6, 6, 10]}, "output": [0, 2, 6, 10]}, {"input": {"nums": [0, 0, 3, 4, 6, 8, 12, 12]}, "output": [0, 3, 4, 6, 8, 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": "fefaabfe3ddbf7e7"} |
vcr_4d59a4f4fb13 | 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": "eebc ebb cdeac cddea bb cbeb"}, "output": "cbeb bb cddea cdeac ebb eebc"}, {"input": {"s": "b b abb bbb"}, "output": "bbb abb b b"}, {"input": {"s": "bb bbab b aab ab"}, "output": "ab aab b bbab bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "4bbc83719638a431"} |
vcr_67dd61a98d23 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 2, 3, 3, 3, 6, 8, 11, 12, 14]}, "output": [0, 2, 3, 6, 8, 11, 12, 14]}, {"input": {"nums": [5, 10, 10]}, "output": [5, 10]}, {"input": {"nums": [0, 2, 4, 4, 5, 5, 9]}, "output": [0, 2, 4, 5, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "bac428874dbaa094"} |
vcr_774d4d5aa69d | 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, -26, -19, -12, 11, 34], "target": -15}, "output": [1, 4]}, {"input": {"nums": [-4, 24, 30, 35, 39], "target": 69}, "output": [2, 4]}, {"input": {"nums": [-14, -7, -3, -1, 9, 19, 25, 29, 36], "target": 11}, "output": [0, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "55d357a6d710b2ec"} |
vcr_19179463f761 | 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": "cbbbbababbbbc"}, "output": true}, {"input": {"s": "ccabaacbab"}, "output": false}, {"input": {"s": "acc"}, "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": "cf9a3b7528fcfc0b"} |
vcr_db7d9f6f871f | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["cb", "aaaa", "accb", "ccc", "bcba", "ccb"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bba", "bab", "aab"]}, "output": [1, 3]}, {"input": {"strs": ["abb", "baa", "abb", "aba", "bba"]}, "output": [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": "88ff7933b575afcf"} |
vcr_e32e8cbc53b0 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-9, -3, 17, 9, 15, 11, -1, -1, -8, 1], "k": 29}, "output": [-3, 17, 9, 15, 11, -1, -1, -8, 1, -9]}, {"input": {"nums": [-7, -6, -5, -2, 1, -4], "k": 5}, "output": [-6, -5, -2, 1, -4, -7]}, {"input": {"nums": [10, -3, 2, -10, 8, 6, 2], "k": 3}, "output": [8, 6, 2, 10, -3, 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": "3db3839885c0854b"} |
vcr_c5c35e2685be | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [1, 0, 3, 4, 3, 5]}, "output": [0, 180, 0, 0, 0, 0]}, {"input": {"nums": [3, 2, 2, 1]}, "output": [4, 6, 6, 12]}, {"input": {"nums": [4, 5, 3, 2, 5]}, "output": [150, 120, 200, 300, 120]}] | 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": "62870ef7cf8f4c1a"} |
vcr_b135add09947 | 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": [-16, 1, 9, 14, 19, 25, 32, 34], "target": -2}, "output": [0, 3]}, {"input": {"nums": [-10, -9, 4, 21, 31], "target": -6}, "output": [0, 2]}, {"input": {"nums": [-11, -3, 4, 7, 12, 17, 24, 30, 35, 39], "target": 42}, "output": [3, 8]}] | 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": "abf5f88e2fbc9e18"} |
vcr_b49fc426162f | 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": [-9, -8, -5, 3, 4, 34], "target": -6}, "output": [0, 3]}, {"input": {"nums": [-14, -3, 8, 27], "target": 24}, "output": [1, 3]}, {"input": {"nums": [-17, -3, 2, 25, 33, 36], "target": 27}, "output": [2, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "45150be5e0fda4ff"} |
vcr_5dcbb143361c | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, -27, -22, -19, -11, -8, -7, 18, 20, 35, 40, 46], "target": -38}, "output": [0, 5]}, {"input": {"nums": [-16, -15, -12, -11, 7, 11, 25], "target": 32}, "output": [4, 6]}, {"input": {"nums": [-11, -3, 0, 4, 7, 11, 15], "target": 18}, "output": [4, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "a2c9de97674076d1"} |
vcr_6c8c1370b791 | 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": 55048}, "output": 7}, {"input": {"n": 15354}, "output": 11}, {"input": {"n": 14374}, "output": 6}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "386f484b623d9aab"} |
vcr_47526e5e6620 | 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": [1, -27, 73, 70, 45, 40], "target": 43}, "output": [1, 3]}, {"input": {"nums": [5, -9, 15, 27, 10, -7, 16, -18, 3, 45, 19], "target": 48}, "output": [8, 9]}, {"input": {"nums": [-10, 48, -20, -14, -2, 18, -8, 21, 26, 32], "target": 39}, "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": "cb8e33fc4fb0956c"} |
vcr_ac9f89347755 | 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": "adcbdbcaa"}, "output": false}, {"input": {"s": "acbacacbbc"}, "output": false}, {"input": {"s": "bbaababcab"}, "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": "1feb197334d38f4f"} |
vcr_2952ea34f332 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-3, -10, -8, -7, -3, -16, 16], "k": 15}, "output": [16, -3, -10, -8, -7, -3, -16]}, {"input": {"nums": [-1, 5, 3, 9], "k": 15}, "output": [5, 3, 9, -1]}, {"input": {"nums": [-8, 2, -9, -8, 8, 3, 7, 0], "k": 2}, "output": [7, 0, -8, 2, -9, -8, 8, 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": "5c6c674b3d5181c7"} |
vcr_65d177f57f79 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-25, 16, 31, 21, 18, 4, -6, 25, 34, 35, 9, 9, -15, -19]}, "output": [-25, -9, 22, 43, 61, 65, 59, 84, 118, 153, 162, 171, 156, 137]}, {"input": {"nums": [-10, 6, -4, -8, -7, -6, -8, -2, 12]}, "output": [-10, -4, -8, -16, -23, -29, -37, -39, -27]}, {"input": {"nums": [-10, -9, 8, 6, 17, -11, -2, 10]... | 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": "37e73a6c81143325"} |
vcr_1d891283dfb6 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 2, 0, 0, 3, 3, 3, 3, 0]}, "output": true}, {"input": {"nums": [0, 1, 2, 2, 0]}, "output": false}, {"input": {"nums": [1, 0, 0, 3, 0, 1, 0, 2, 0]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "d88ccab8dbaf79ca"} |
vcr_7ecd7aa73a8c | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 5, 7, 12, 18, 29, 32, 35, 36, 37, 41, 44, 47], "target": 36}, "output": 8}, {"input": {"nums": [1, 5, 26], "target": 35}, "output": 3}, {"input": {"nums": [7, 8, 14, 15, 25, 29, 30], "target": 26}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "bc47dcf78fc9d0b9"} |
vcr_8808632a2ad1 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [19, 20, -5, 5, -20, 13, -12, -12, 20, -5], "k": 9}, "output": [20, -5, 5, -20, 13, -12, -12, 20, -5, 19]}, {"input": {"nums": [0, -9, -1, 9], "k": 20}, "output": [0, -9, -1, 9]}, {"input": {"nums": [10, 4, -1, 8, -8, 10, 6, 3], "k": 3}, "output": [10, 6, 3, 10, 4, -1, 8, -8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "01612c0e28ee706e"} |
vcr_364cf85b5d9c | 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": [-8, 15, 6, -25, 3]}, "output": [-8, 7, 13, -12, -9]}, {"input": {"nums": [-10, -2, 17, -5, 8]}, "output": [-10, -12, 5, 0, 8]}, {"input": {"nums": [-4, 0, -8, -5, 13, 5, -10, 0]}, "output": [-4, -4, -12, -17, -4, 1, -9, -9]}] | 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": "a99ade5389c3ff0e"} |
vcr_0e2a3ec223cd | 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, 8, 19, 17, 29, 34, 31]}, "output": 26}, {"input": {"prices": [3, 17, 29, 4, 18, 10, 23, 10, 36, 18, 25, 21]}, "output": 33}, {"input": {"prices": [10, 26, 23, 2, 24, 15, 36]}, "output": 34}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "ce787f5fa52360a2"} |
vcr_dd97e530603c | 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": ["baca", "bbb", "cca", "caba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aaa", "aaa", "aaa", "baa", "baa", "bab"]}, "output": [1, 2, 3]}, {"input": {"strs": ["bba", "bab", "aab", "bba", "bba"]}, "output": [1, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "3ab5e63e9bb3c291"} |
vcr_b4362f34f311 | 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": "bbabacaaaaaaba"}, "output": 8}, {"input": {"s": "aabbcacba"}, "output": 7}, {"input": {"s": "bccca"}, "output": 3}] | 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": "ed24caabbe23e900"} |
vcr_b169700ace60 | 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, 3, 0, 3, 0, 0, 0]}, "output": true}, {"input": {"nums": [2, 3, 1, 0, 0, 3, 1]}, "output": false}, {"input": {"nums": [0, 0, 2, 0, 3, 1]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "67f83a58b79fb2a8"} |
vcr_bf8223c36565 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [18, -14, -15, -20, 3, 8]}, "output": [18, 4, -11, -31, -28, -20]}, {"input": {"nums": [-4, 2, -10, -11, 16]}, "output": [-4, -2, -12, -23, -7]}, {"input": {"nums": [-1, -10, 2, 1, -10, 10]}, "output": [-1, -11, -9, -8, -18, -8]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "c21da944d4dc5a72"} |
vcr_ada9f6514bd6 | 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": [6, 1, 5, 0, 7, 5, 4, 5]}, "output": [0, 0, 0, 21000, 0, 0, 0, 0]}, {"input": {"nums": [1, 5, 2, 4, 3, 1]}, "output": [120, 24, 60, 30, 40, 120]}, {"input": {"nums": [2, 2, 1, 2, 3]}, "output": [12, 12, 24, 12, 8]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "b16a3cc66d5222c4"} |
vcr_9c5f30a21c04 | 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": [12, 15, 19, 20, 21, 34, 40], "target": 44}, "output": 7}, {"input": {"nums": [2, 10, 24, 38], "target": 17}, "output": 2}, {"input": {"nums": [3, 4, 8, 12, 18, 20, 26, 30, 31, 36], "target": 12}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "c8eb4aca456b27ca"} |
vcr_6b61da3d7356 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 4, 3, 1, 3, 2, 4, 0, 2, 0]}, "output": false}, {"input": {"nums": [3, 2, 0, 0, 2]}, "output": false}, {"input": {"nums": [0, 3, 1, 2, 3, 1, 2, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "17ff0ac3fefe1490"} |
vcr_37b8d6c6b1fc | 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": 28241}, "output": 8}, {"input": {"n": 12112}, "output": 7}, {"input": {"n": 8784}, "output": 4}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "7c8b0b190b883fdf"} |
vcr_b9c3c7aad596 | 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, -7, -10, 4, -2, 13, 4, -11, 2], "k": 1}, "output": [2, -12, -7, -10, 4, -2, 13, 4, -11]}, {"input": {"nums": [-6, 5, -7, 7, 9, 2, -6, -5], "k": 1}, "output": [-5, -6, 5, -7, 7, 9, 2, -6]}, {"input": {"nums": [-1, -4, 10, -8, -5, 0], "k": 5}, "output": [-4, 10, -8, -5, 0, -1]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "b36c5649e62564fc"} |
vcr_ef5de720ee0b | 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, 4, 0, 4, 1, 2, 2, 1, 0]}, "output": true}, {"input": {"nums": [0, 0, 3, 1, 2, 0, 2, 3]}, "output": false}, {"input": {"nums": [0, 3, 2, 0, 2, 0, 1, 1]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "e5c2f27f07cd4be4"} |
vcr_c1bdec13153b | 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": [-10, 10, -5, 0, 20, -4, 8]}, "output": 29}, {"input": {"nums": [-1, -4, -7, -10, 3, 5]}, "output": 8}, {"input": {"nums": [11, 14, -11, -1, 7, -5, -9]}, "output": 25}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "4ee5d78b77943a61"} |
vcr_d86cac162104 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [25, 12, 24, 26]}, "output": 51}, {"input": {"nums": [7, 17, 10, 4, 2]}, "output": 21}, {"input": {"nums": [7, 20, 3, 11, 8, 3, 20]}, "output": 51}] | 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": "60b3ff5fd9486aca"} |
vcr_6668c94823b3 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [60, 60, 31, 53, 3, 2, 14, 58, 39, 56, 23, 20]}, "output": 56}, {"input": {"prices": [19, 15, 23, 38, 40]}, "output": 25}, {"input": {"prices": [27, 26, 38, 13, 20, 13, 39, 6, 16, 23, 15]}, "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": "bf8e7dd1ada6e370"} |
vcr_bbc9e2428fe9 | 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": ["cc", "bcb", "ccca", "caaa", "caac"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "abb", "aaa", "bba", "baa", "aaa", "aab"]}, "output": [2, 2, 3]}, {"input": {"strs": ["aaa", "aba", "bba"]}, "output": [1, 1, 1]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "885726e944f2220d"} |
vcr_ede83f2a8df9 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 15, 15]}, "output": [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 15]}, {"input": {"nums": [4, 10, 12]}, "output": [4, 10, 12]}, {"input": {"nums": [0, 3, 3, 4, 8, 8, 9, 11]}, "output": [0, 3, 4, 8, 9, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "9e52fa492f460fdb"} |
vcr_6693065b701b | 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, -12, 6, -14, 7, -1, -9, -7], "k": 8}, "output": [12, -12, 6, -14, 7, -1, -9, -7]}, {"input": {"nums": [9, -5, 2, 4, 5, -4], "k": 9}, "output": [4, 5, -4, 9, -5, 2]}, {"input": {"nums": [-6, 4, -10], "k": 1}, "output": [-10, -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": "bdea1e2235fd1241"} |
vcr_2ce952c9abff | 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": [47, 21, 41, 0, 1, 47]}, "output": true}, {"input": {"nums": [17, 1, 12, 20, 24, 20, 15, 13, 19, 8]}, "output": true}, {"input": {"nums": [2, 12, 16, 9, 10, 21, 8, 7, 14, 0]}, "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": "88ff43d5a2c6105a"} |
vcr_267c4557f709 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-22, 14, 32]}, "output": [-22, -8, 24]}, {"input": {"nums": [13, 11, 12]}, "output": [13, 24, 36]}, {"input": {"nums": [4, 0, -4, -6, -4]}, "output": [4, 4, 0, -6, -10]}] | 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": "026d2cb56ddd0958"} |
vcr_9b771ee6fc23 | 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": 51, "b": 27}, "output": 3}, {"input": {"a": 99, "b": 176}, "output": 11}, {"input": {"a": 69, "b": 82}, "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": "28ffc8be6eefd29a"} |
vcr_3707e8dbf820 | 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": [27, 42, 3, 6, 34, 59, 13, 36, 42, 22, 18, 19]}, "output": 56}, {"input": {"prices": [11, 11, 28, 26, 17, 8, 8]}, "output": 17}, {"input": {"prices": [38, 25, 22, 3, 21, 7, 4, 27, 8, 34]}, "output": 31}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "660e74a3580ddd04"} |
vcr_1bc15345dfc9 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [2, 1, 22, 39, 6, 22, 31, 19, 15, 44, 33, 7, 32, 28, 42]}, "output": true}, {"input": {"nums": [8, 8, 8, 16, 5, 0, 25, 0, 4]}, "output": true}, {"input": {"nums": [21, 6, 7]}, "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": "3a886ecd1a2d7bc7"} |
vcr_dbf91f0015fb | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [10, 11, 21, 23, 27, 46], "target": 17}, "output": 2}, {"input": {"nums": [5, 6, 11, 13, 23, 24, 31, 39], "target": 28}, "output": 6}, {"input": {"nums": [19, 21, 36], "target": 32}, "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": "0bd19e5e44f1b29a"} |
vcr_dda45909f697 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [120, 50, 169, 187, 63, 67, 12, 188, 58, 46, 89, 78, 66]}, "output": false}, {"input": {"nums": [15, 18, 16, 10, 13, 12, 24]}, "output": false}, {"input": {"nums": [6, 21, 6, 9]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "ebcc00d7d0674a24"} |
vcr_be34788b2169 | 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": [14, 4, 31, 50]}, "output": 46}, {"input": {"prices": [13, 30, 22, 9, 13]}, "output": 17}, {"input": {"prices": [35, 16, 17, 16, 21, 1, 27, 11]}, "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": "5556758e8c8e8521"} |
vcr_b630c015679f | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-14, -13, -5, 22, -3, 15, 13, -17, -13, 20, 2, 3, 8]}, "output": 50}, {"input": {"nums": [-9, 7, -5, 8, 4, -7, 9, 0, 5]}, "output": 21}, {"input": {"nums": [-7, -9, 14, 11, -9, -2, 3, -3]}, "output": 25}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "8d71ff50f8dc34b2"} |
vcr_0570d4148737 | 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": [123, 52, 36, 60, 156, 54, 112, 100, 41, 16, 166, 90]}, "output": false}, {"input": {"nums": [25, 0, 11, 21, 22, 14, 4, 6, 14]}, "output": true}, {"input": {"nums": [21, 23, 20, 17, 1, 9, 25, 15, 3, 24, 15, 7]}, "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": "f1374bd8ad5a68ef"} |
vcr_bcb434fb4e45 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [29, 20, 11, 16, 10, 12, 26, 23, 21, 29, 26, 29]}, "output": 138}, {"input": {"nums": [11, 7, 8, 20, 7, 19, 8]}, "output": 50}, {"input": {"nums": [11, 19, 12, 19, 1, 8, 16, 18, 10]}, "output": 64}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "5804ca2d4394a3c5"} |
vcr_74643d4706ad | 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": [39, -3, -32, 60, -28, 68, 43], "target": 99}, "output": [0, 3]}, {"input": {"nums": [-16, 46, 44, 30, 31, 18, -18, 19], "target": 13}, "output": [4, 6]}, {"input": {"nums": [-29, -10, -9, 14, 30, 38, -20], "target": 9}, "output": [0, 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": "5cb25327bec98d85"} |
vcr_5b1122fb11c6 | 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, 4, 4]}, "output": [16, 16, 16]}, {"input": {"nums": [4, 4, 2, 1, 2, 1]}, "output": [16, 16, 32, 64, 32, 64]}, {"input": {"nums": [1, 4, 5, 4]}, "output": [80, 20, 16, 20]}] | 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": "4a62d5f5edb28867"} |
vcr_3710fcf3012f | 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": "aaaabcaddababdc"}, "output": false}, {"input": {"s": "ccababac"}, "output": false}, {"input": {"s": "cabbacbaa"}, "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": "cb27abb1ab663d99"} |
vcr_fb0561576aad | 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": [40, 144, 26, 191, 85, 127, 49, 8]}, "output": false}, {"input": {"nums": [15, 15, 4, 6, 0, 14, 12, 11, 7, 17, 16, 20]}, "output": true}, {"input": {"nums": [20, 10, 16]}, "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": "61fd139fccd7fc41"} |
vcr_6717f33b68ea | 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": [31, 7, 14, 26, -11, -22, 30, -21, -6]}, "output": [31, 38, 52, 78, 67, 45, 75, 54, 48]}, {"input": {"nums": [2, 10, 17, 4, 14, 8, -5, -8, -9, 17]}, "output": [2, 12, 29, 33, 47, 55, 50, 42, 33, 50]}, {"input": {"nums": [11, 2, -12, 13, 2]}, "output": [11, 13, 1, 14, 16]}] | 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": "bde196d071485ae8"} |
vcr_147bd64ce148 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 2, 0, 4, 3, 4, 4, 0, 1, 0]}, "output": true}, {"input": {"nums": [2, 1, 2, 1, 2]}, "output": true}, {"input": {"nums": [3, 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": "ae6e04ae2bae0a9e"} |
vcr_6ffe05e7e700 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 1, 3, 8, 11]}, "output": [0, 1, 3, 8, 11]}, {"input": {"nums": [0, 1, 1, 1, 3, 4, 5, 6, 6, 7, 10, 12]}, "output": [0, 1, 3, 4, 5, 6, 7, 10, 12]}, {"input": {"nums": [0, 0, 7, 8, 10]}, "output": [0, 7, 8, 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": "5dad86056d466428"} |
vcr_a9626c433c9f | 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": [17, 50, 2, 52, 52, 6]}, "output": 50}, {"input": {"prices": [2, 28, 10, 16, 14, 34, 6]}, "output": 32}, {"input": {"prices": [12, 20, 16, 31, 8]}, "output": 19}] | 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": "8dfcf6305c2a367c"} |
vcr_fb77b0fff12a | 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, -3, -5, -7], "k": 10}, "output": [-5, -7, 12, -3]}, {"input": {"nums": [6, 10, 5, 0], "k": 16}, "output": [6, 10, 5, 0]}, {"input": {"nums": [9, 2, -10, 7], "k": 4}, "output": [9, 2, -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": "523cb4195523732a"} |
vcr_cd0c20208c75 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-25, -10, 26, 31, 19, 22, 16, -9]}, "output": [-25, -35, -9, 22, 41, 63, 79, 70]}, {"input": {"nums": [17, 5, 10, 0, 16]}, "output": [17, 22, 32, 32, 48]}, {"input": {"nums": [-11, 18]}, "output": [-11, 7]}] | 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": "700214a3f6483789"} |
vcr_4cbcb5d5f11a | 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": [-8, 0, 10, -20, -13, -24, 6, 22, -12, 14, 30, -6, 15]}, "output": [-8, -8, 2, -18, -31, -55, -49, -27, -39, -25, 5, -1, 14]}, {"input": {"nums": [15, -10, 8, 3, 16, -7]}, "output": [15, 5, 13, 16, 32, 25]}, {"input": {"nums": [10, -3, 1, 6, 6, 17]}, "output": [10, 7, 8, 14, 20, 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": "abfab8940664f1a5"} |
vcr_ac100427d6dd | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [7, 64, 72, 76, 41, 11, 13, 34, 6, 78, 65], "target": 72}, "output": [0, 10]}, {"input": {"nums": [4, -28, 16, 13, 39, 33, -30], "target": 49}, "output": [2, 5]}, {"input": {"nums": [-19, 12, 7, 49, -17, 48, 13], "target": -6}, "output": [0, 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": "52987d986c8b83ba"} |
vcr_efc53a1bacc6 | 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": [69, -31, -2, 68, -9, 48, 73, 63, 56, 46, 5, 0], "target": -31}, "output": [1, 11]}, {"input": {"nums": [36, 32, -13, 49, -8, -15, 25, -11], "target": -24}, "output": [2, 7]}, {"input": {"nums": [12, 42, -12, 9, -25, 35, 36, 41], "target": 47}, "output": [0, 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": "f18edf5ab6662467"} |
vcr_959253ac2a77 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-18, 22, -23]}, "output": [-18, 4, -19]}, {"input": {"nums": [-5, 5, 3]}, "output": [-5, 0, 3]}, {"input": {"nums": [16, -1, 1, -9]}, "output": [16, 15, 16, 7]}] | 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": "3e1a08772904a120"} |
vcr_28e03fbdfce9 | 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, 20, 29, 36, 37, 39, 41, 47, 48], "target": 7}, "output": 1}, {"input": {"nums": [2, 4, 6, 16, 20, 22, 24, 28, 31, 34], "target": 29}, "output": 8}, {"input": {"nums": [3, 5, 9, 15, 16, 18, 21, 33, 38, 39], "target": 36}, "output": 8}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "94d7591aafcb6c59"} |
vcr_f954f18d66f1 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [13, 16, 18, 19, 25, 33, 41, 47, 48], "target": 2}, "output": 0}, {"input": {"nums": [17, 24, 29, 32, 37, 39], "target": 34}, "output": 4}, {"input": {"nums": [2, 6, 9, 18], "target": 19}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "0604f79fcda7bd9d"} |
vcr_ec734323db1a | 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": [1, 3, 12, 41, 1]}, "output": true}, {"input": {"nums": [0, 3, 11, 16, 23, 17, 4, 6, 12, 8, 23, 21]}, "output": true}, {"input": {"nums": [10, 1, 13, 16, 23, 21, 14, 7]}, "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": "91177c64896728d2"} |
vcr_ac282ebac7df | 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": [29, 25, 81, 16, 152]}, "output": false}, {"input": {"nums": [1, 25, 13, 5, 9]}, "output": false}, {"input": {"nums": [23, 10, 23, 12, 25, 23, 20, 12, 23, 3, 11, 2]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "6a7485182e65fcc4"} |
vcr_75f4b8675b55 | 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": [14, 16, 3, 8, 15, 26, 7, 8, 3, 39]}, "output": true}, {"input": {"nums": [6, 25, 12, 7, 7, 2, 1, 9, 21, 11, 21, 17]}, "output": true}, {"input": {"nums": [5, 7, 1, 11, 3, 18]}, "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": "9a8f994548304fe0"} |
vcr_8f6f6096d311 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 7, 8, 14, 16, 17, 24, 29, 39], "target": 17}, "output": 5}, {"input": {"nums": [2, 9, 10, 12, 22, 27, 28, 32, 36], "target": 26}, "output": 5}, {"input": {"nums": [2, 5, 13, 14, 31, 39], "target": 28}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "4c589bf50335dac2"} |
vcr_77e4279efb9c | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-18, 4, 5, 9, 17, 40, 48], "target": 45}, "output": [2, 5]}, {"input": {"nums": [3, 5, 7, 18, 20, 24, 26, 28, 36], "target": 44}, "output": [3, 6]}, {"input": {"nums": [-17, -1, 31, 34], "target": 17}, "output": [0, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "8a49838238960eeb"} |
vcr_e36fa7773578 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [31, 53, 19, 47, 50, 30, 15, 53, 48, 35, 53, 31, 28, 1, 30, 40]}, "output": 39}, {"input": {"prices": [26, 3, 26, 25, 34, 26, 21, 16, 10, 40, 10, 28]}, "output": 37}, {"input": {"prices": [30, 3, 4, 26]}, "output": 23}] | 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": "1b12ae368969ead5"} |
vcr_01e551eaf0de | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 2, 8, 11, 13, 14, 15]}, "output": [0, 1, 2, 8, 11, 13, 14, 15]}, {"input": {"nums": [0, 2, 2, 4, 6, 12]}, "output": [0, 2, 4, 6, 12]}, {"input": {"nums": [2, 3, 3, 4, 4, 7, 7, 10, 10, 11, 11]}, "output": [2, 3, 4, 7, 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": "7a8b4bf67e7c819e"} |
vcr_a394908949c0 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 4, 5, 6, 12, 28, 30, 33, 37, 40, 42, 49], "target": 41}, "output": 10}, {"input": {"nums": [0, 6, 8, 10, 13, 17, 18, 19, 20, 27, 29, 35], "target": 2}, "output": 1}, {"input": {"nums": [2, 14, 18], "target": 35}, "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": "bd2ae33ef30a628b"} |
vcr_0ca2347bedcb | 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": "bbcabcbba"}, "output": 7}, {"input": {"s": "cbbbac"}, "output": 4}, {"input": {"s": "babaa"}, "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": "bb9facc3331406e9"} |
vcr_222f51127630 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [13, 16, 8, 30, 8]}, "output": 46}, {"input": {"nums": [7, 17, 2, 2, 3, 4, 17, 12, 19]}, "output": 56}, {"input": {"nums": [15, 0, 16, 15, 11, 11, 8, 14, 5, 3]}, "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": "209e53aea7333ddf"} |
vcr_7ce27b415965 | 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, -20, -19, 3, 7, 12, 15, 17, 24, 27, 29, 34], "target": 10}, "output": [2, 10]}, {"input": {"nums": [-20, -16, -8, -1, 4, 5, 12, 19, 24, 25, 27], "target": 51}, "output": [8, 10]}, {"input": {"nums": [-15, 4, 12, 20, 32], "target": 24}, "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": "79c9b51f5ffd6c8b"} |
vcr_111c2ad6407d | 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": [71, 38, 4, 7, 39, 4, 7, 39, 38]}, "output": 71}, {"input": {"nums": [18, 19, 51, 23, 23, 19, 18]}, "output": 51}, {"input": {"nums": [9, 20, 21, 5, 27, 6, 27, 42, 20, 5, 6, 9, 21]}, "output": 42}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "428fb8f03827d67a"} |
vcr_a031dc86bd83 | 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": [15, 20, -5]}, "output": [15, 35, 30]}, {"input": {"nums": [-9, -10, -12]}, "output": [-9, -19, -31]}, {"input": {"nums": [8, 7, 5, 18, -5, -2]}, "output": [8, 15, 20, 38, 33, 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": "4bd849b0fed08fa8"} |
vcr_2fe2fd36b060 | 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": [41, 12, 10, -15], "target": 51}, "output": [0, 2]}, {"input": {"nums": [-7, 38, -25, -17, -30, -14, 13, 7], "target": 13}, "output": [1, 2]}, {"input": {"nums": [34, 23, 33, -30, 10, -16, -14], "target": 57}, "output": [0, 1]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "0339eaf5184edc4d"} |
vcr_31fc8359aba5 | 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": [6, 6, 5, 1, 1, 4, 6]}, "output": [720, 720, 864, 4320, 4320, 1080, 720]}, {"input": {"nums": [5, 4, 2, 2, 3]}, "output": [48, 60, 120, 120, 80]}, {"input": {"nums": [5, 1, 4, 3]}, "output": [12, 60, 15, 20]}] | 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": "790436c240ed238e"} |
vcr_1f1c39e8db5e | 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": [7, 10, 10, 7, 1, 60, 1]}, "output": 60}, {"input": {"nums": [16, 4, 4, 9, 1, 47, 16, 22, 22, 24, 24, 1, 9]}, "output": 47}, {"input": {"nums": [3, 49, 29, 28, 7, 24, 28, 23, 3, 23, 29, 24, 7]}, "output": 49}] | 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": "1fe1797a5c7ae456"} |
vcr_1b0821fb0b7b | 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": [19, 14, 12, 17, 50, 56, 12, 50]}, "output": 44}, {"input": {"prices": [13, 1, 29, 31, 38]}, "output": 37}, {"input": {"prices": [16, 12, 8, 15, 5, 17, 34, 20, 39, 27, 12]}, "output": 34}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "a2aa9bedea8c81d0"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.