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_0c001402e0f7 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [27, 25, 10]}, "output": 37}, {"input": {"nums": [7, 5, 3, 12, 4, 17, 13, 13, 14]}, "output": 50}, {"input": {"nums": [17, 8, 4]}, "output": 21}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "6bfa721a6781663c"} |
vcr_b7fe51f32faf | 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": "cb dcacb a bbd ac ea"}, "output": "ea ac bbd a dcacb cb"}, {"input": {"s": "aaa b aab a"}, "output": "a aab b aaa"}, {"input": {"s": "bbba b baab bbba aba"}, "output": "aba bbba baab b bbba"}] | 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": "3a1f09e3efffcd40"} |
vcr_cc443194c714 | 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": [16, 27, 24, 9, 4, 26, 5, 15, 4, 26]}, "output": 107}, {"input": {"nums": [18, 18, 11, 1]}, "output": 29}, {"input": {"nums": [11, 2, 10]}, "output": 21}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "017585d835a01f01"} |
vcr_274e6ed5f505 | 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": [2, 1, 2, 1, 4, 3]}, "output": [24, 48, 24, 48, 12, 16]}, {"input": {"nums": [1, 3, 3, 2, 3]}, "output": [54, 18, 18, 27, 18]}, {"input": {"nums": [4, 1, 1, 1]}, "output": [1, 4, 4, 4]}] | 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": "0d65885eace8524a"} |
vcr_6d85071f1dde | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-1, 10, -8, -12, -16, -4, -9, -17, -2, 13, -4, -15, -8, -10, -7]}, "output": 13}, {"input": {"nums": [5, -8, 11, 4, -10]}, "output": 15}, {"input": {"nums": [2, -7, -9, -6, -12, 10, -3, -6]}, "output": 10}] | 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": "64897f19796dcfc2"} |
vcr_4c3d2ea0f52e | 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": "bbccad"}, "output": false}, {"input": {"s": "accccaabc"}, "output": false}, {"input": {"s": "aaab"}, "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": "ee5ece2ed2bbe58e"} |
vcr_939e420c8ae5 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [19, 5, 0, 19, 16, -2, -12, -18, 5]}, "output": 59}, {"input": {"nums": [10, -1, -8, -5, -7, 7, -12, -12, -6, 4]}, "output": 10}, {"input": {"nums": [-3, -9, -11, -6, 13, -4, -4, 1]}, "output": 13}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "cc06205be7ba14ab"} |
vcr_caa9b21a2b10 | 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": "c dabe bed cebea ececd"}, "output": "ececd cebea bed dabe c"}, {"input": {"s": "a ab abab aaa ba"}, "output": "ba aaa abab ab a"}, {"input": {"s": "aaba abba aa ab"}, "output": "ab aa abba aaba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "a63cce8adcd65f2a"} |
vcr_25de6f91ffef | 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": 25513}, "output": 8}, {"input": {"n": 12347}, "output": 7}, {"input": {"n": 14119}, "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": "6b3d2f0d23dae82d"} |
vcr_1f8c2f65b619 | 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, 17, 22, 24, 25, 41, 47], "target": 31}, "output": 5}, {"input": {"nums": [1, 6, 21, 27], "target": 22}, "output": 3}, {"input": {"nums": [3, 16, 20, 22, 24, 25, 27, 32], "target": 32}, "output": 7}] | 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": "f44daa6d0a24f6a7"} |
vcr_14cf38011e6e | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 4, 9, 17, 23, 27, 30, 37, 38, 44, 49], "target": 2}, "output": 0}, {"input": {"nums": [1, 2, 4, 14, 18], "target": 23}, "output": 5}, {"input": {"nums": [14, 15, 20], "target": 31}, "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": "62e64ea82809e78a"} |
vcr_90f2d719e06e | 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": [-10, -14, 49, 2], "target": 51}, "output": [2, 3]}, {"input": {"nums": [11, -1, 9, 35, -15, -22, 19, 28, -10, 0, -24], "target": -37}, "output": [4, 5]}, {"input": {"nums": [-13, -26, 33, -16, -6, 8, 1, -28], "target": -54}, "output": [1, 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": "0e34599549d71108"} |
vcr_7b9a467671c4 | 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": "dbabccdadcadabd"}, "output": false}, {"input": {"s": "acccc"}, "output": false}, {"input": {"s": "abbccabc"}, "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": "75928fe8e132864b"} |
vcr_54c188e578aa | 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": "adeeca dadae eedacd bbed db cdae dabbee"}, "output": "dabbee cdae db bbed eedacd dadae adeeca"}, {"input": {"s": "aba abab bbb"}, "output": "bbb abab aba"}, {"input": {"s": "abba bb abb"}, "output": "abb bb abba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "ef0fb79e9eafbdac"} |
vcr_e0e7e42e2185 | easy | math | Return the greatest common divisor of positive integers a and b. | def gcd(a: int, b: int) -> int: | Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. | def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
| [{"input": {"a": 237, "b": 177}, "output": 3}, {"input": {"a": 130, "b": 43}, "output": 1}, {"input": {"a": 42, "b": 25}, "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": "d1b23abec3042d81"} |
vcr_eed62e3563c7 | 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": [53, 37, 57, 17, -33, -17, 7, 65, 12, 62, 38, 16, 36, -21], "target": 5}, "output": [4, 10]}, {"input": {"nums": [7, -21, 12, -5, 46, 10, 48, 21, 37, 2], "target": 53}, "output": [0, 4]}, {"input": {"nums": [48, 1, -24, -20], "target": 24}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "c4ca5c2c2ee8961c"} |
vcr_92d49ef96b45 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 4, 21, 22, 26, 32, 35, 49], "target": 20}, "output": 2}, {"input": {"nums": [3, 13, 14, 15, 29, 35], "target": 38}, "output": 6}, {"input": {"nums": [1, 9, 15, 18, 19, 23, 25, 37], "target": 38}, "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": "ccb8de9b831803a5"} |
vcr_d3acdecb452d | 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, 11, -8, 4, 2, 11, 3, 21, -17, -14, -10, -10, 10, 2]}, "output": 58}, {"input": {"nums": [-12, 7, 12, -8, -7, -4, -9]}, "output": 19}, {"input": {"nums": [2, -12, -12, -7, 5]}, "output": 5}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "942571b010f48ba6"} |
vcr_e6fd34c42835 | 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, 9, 13, 32, 35, 37, 43, 44], "target": 25}, "output": 5}, {"input": {"nums": [26, 30, 32, 35, 37], "target": 10}, "output": 0}, {"input": {"nums": [1, 2, 4, 10, 16, 17, 18, 21, 22, 26, 30], "target": 5}, "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": "68545689fe2f7004"} |
vcr_2ee9489672b8 | 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, -7, -5, 0, 7, 14, 30, 48], "target": 4}, "output": [0, 6]}, {"input": {"nums": [-20, 8, 14, 18, 20], "target": 26}, "output": [1, 3]}, {"input": {"nums": [-10, -8, 6, 23, 27, 31], "target": 15}, "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": "49989a205a33b87d"} |
vcr_036385d33da9 | 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": [62, -19, 17, 54, -31], "target": 35}, "output": [1, 3]}, {"input": {"nums": [0, 21, -16, 43, -30, 9, -14], "target": 29}, "output": [3, 6]}, {"input": {"nums": [45, 13, -4, -16, 26, 34, -6, 47, 21], "target": 29}, "output": [0, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "b05246507fe80104"} |
vcr_9b383d56f4bd | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [19, -5, -5, 19, -16, 5, 8, -20], "k": 24}, "output": [19, -5, -5, 19, -16, 5, 8, -20]}, {"input": {"nums": [6, -4, 7, -7, 5, 9], "k": 10}, "output": [7, -7, 5, 9, 6, -4]}, {"input": {"nums": [-9, 5, 8, 7, -3, 6], "k": 1}, "output": [6, -9, 5, 8, 7, -3]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "c819ffa83c938cd7"} |
vcr_4ed470acd5e9 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [30, 16, 23, 8]}, "output": 53}, {"input": {"nums": [13, 16, 9, 18, 7, 10, 3, 2]}, "output": 46}, {"input": {"nums": [14, 6, 14, 16, 9, 14]}, "output": 44}] | 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": "339b9094553a1178"} |
vcr_89af2457d425 | 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": [51, 33, 60, 35, 52, 20, 1, 55, 60, 20, 38, 24, 43, 59, 16]}, "output": 59}, {"input": {"prices": [3, 22, 24, 6]}, "output": 21}, {"input": {"prices": [25, 16, 24, 27, 19, 1, 35, 10, 8]}, "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": "211f0049044a2e66"} |
vcr_a0fa863c59c6 | 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": [95, 41, 78, 64, 23, 87]}, "output": false}, {"input": {"nums": [1, 6, 2, 9, 22, 18]}, "output": false}, {"input": {"nums": [0, 22, 13, 21, 15, 14, 21]}, "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": "9ab0c7184697748f"} |
vcr_6de7429a38b2 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["acbc", "cccc", "ba", "abcc"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "bba", "bbb", "aba", "bbb", "abb", "aab"]}, "output": [2, 2, 3]}, {"input": {"strs": ["bbb", "bab", "aba", "aba", "abb", "bba"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "c9a3dacd2a69f65f"} |
vcr_230dc0b05690 | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [46, 23, 14, 29, 20, 17, 33, 22, 24, 48, 29, 35, 9, 0, 35]}, "output": true}, {"input": {"nums": [14, 11, 2, 20, 25, 18, 15, 5, 10]}, "output": false}, {"input": {"nums": [24, 24, 24, 5, 7, 0, 22, 8, 0]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "3773dff3c79dbc9c"} |
vcr_9a6b7da6d129 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-18, 16, 13, 19, -3, -6, 7, -16, -15], "k": 7}, "output": [13, 19, -3, -6, 7, -16, -15, -18, 16]}, {"input": {"nums": [10, 10, -9, 10, 9, -3, -2, -5, -8, 7], "k": 1}, "output": [7, 10, 10, -9, 10, 9, -3, -2, -5, -8]}, {"input": {"nums": [3, -2, 8, 2, -3, -4, 7, -7], "k": 15}, "output": [-2, 8, 2, -... | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "045026fc73bcdc9c"} |
vcr_b3f91dec78b2 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [27, 8, 30, 25, 16, 13, 23]}, "output": 96}, {"input": {"nums": [7, 14, 2, 12, 8, 16, 14, 18, 14, 14]}, "output": 74}, {"input": {"nums": [8, 0, 7, 16, 18, 8, 19, 11, 18, 11]}, "output": 70}] | 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": "3852cd6daadfab65"} |
vcr_2957d0548a58 | 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": [23, 44, 34, 67], "target": 67}, "output": [0, 1]}, {"input": {"nums": [26, 42, 9, -9, 37], "target": 17}, "output": [0, 3]}, {"input": {"nums": [-18, 30, 23, -26, 13, 6, 21, 37], "target": 19}, "output": [4, 5]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "11a5c54b3fe47075"} |
vcr_8a4fa1e1d716 | 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": [50, 49, 35, 5, 50, 36, 10, 31, 50, 47, 5, 42, 29, 23, 21, 40]}, "output": true}, {"input": {"nums": [8, 16, 7, 25, 20]}, "output": false}, {"input": {"nums": [3, 25, 1, 17, 7, 11, 23]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "32469efb3135cd0e"} |
vcr_3f988828e129 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [28, 21, -7, 33, 3, -22, 29, 10, -12, 18, 14, -23, -21]}, "output": [28, 49, 42, 75, 78, 56, 85, 95, 83, 101, 115, 92, 71]}, {"input": {"nums": [-11, 12, -11, 12, 13]}, "output": [-11, 1, -10, 2, 15]}, {"input": {"nums": [1, -6, 9]}, "output": [1, -5, 4]}] | 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": "17032de70ecb66fb"} |
vcr_7c4d5fb8bef9 | 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": "cabaddbcbadb"}, "output": false}, {"input": {"s": "cbaabaab"}, "output": false}, {"input": {"s": "accbccab"}, "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": "a03ebc962872371c"} |
vcr_670be7dec3fe | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 3, 2, 2, 2, 1, 4, 0]}, "output": true}, {"input": {"nums": [1, 1, 3, 2, 2, 3, 2, 0, 0]}, "output": true}, {"input": {"nums": [3, 1, 0, 3, 2, 3, 3, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "3432aa7a483cfd03"} |
vcr_5fbb1ea95e7e | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-13, -14, 8, 8, 11, 8], "k": 15}, "output": [8, 11, 8, -13, -14, 8]}, {"input": {"nums": [1, 2, 2, -7, -1, -7], "k": 1}, "output": [-7, 1, 2, 2, -7, -1]}, {"input": {"nums": [8, 2, -5, -7, -8, 2], "k": 2}, "output": [-8, 2, 8, 2, -5, -7]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "9f465711b2ffc1a3"} |
vcr_550450108b93 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [18, 7, 0]}, "output": 18}, {"input": {"nums": [0, 3]}, "output": 3}, {"input": {"nums": [4, 20, 2]}, "output": 20}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "a1443c354b848a34"} |
vcr_03bb28901b0a | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [17, 3, 23, 22, 8, 4, 2, 11]}, "output": 59}, {"input": {"nums": [17, 6]}, "output": 17}, {"input": {"nums": [19, 9, 13, 8, 10, 11]}, "output": 43}] | 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": "b3e2839febb80f56"} |
vcr_73769e088a58 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-4, -20, -5, 15, 32, -1, -17, -5, -13, 19, 35, 25, -4]}, "output": [-4, -24, -29, -14, 18, 17, 0, -5, -18, 1, 36, 61, 57]}, {"input": {"nums": [0, 16, 14]}, "output": [0, 16, 30]}, {"input": {"nums": [-5, -3, 4, 15, 6, -10, 17]}, "output": [-5, -8, -4, 11, 17, 7, 24]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "38b8d86678cab0f6"} |
vcr_82152a3460ff | 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": [34, 9, -21, -4, 28, 25, -12]}, "output": [34, 43, 22, 18, 46, 71, 59]}, {"input": {"nums": [6, 16, -1, 17, -1, -4, 10, 6, -3]}, "output": [6, 22, 21, 38, 37, 33, 43, 49, 46]}, {"input": {"nums": [10, -3, 16, 9, -5]}, "output": [10, 7, 23, 32, 27]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "81d48df27c08e57d"} |
vcr_e6dd74c03b1c | 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": [-8, -18, -20, 1, 18, 19, -15], "k": 4}, "output": [1, 18, 19, -15, -8, -18, -20]}, {"input": {"nums": [4, -1, 7, 2, -4, 8], "k": 11}, "output": [-1, 7, 2, -4, 8, 4]}, {"input": {"nums": [8, 5, -8, 8, -10, -6, 9, -7, 9, -5], "k": 9}, "output": [5, -8, 8, -10, -6, 9, -7, 9, -5, 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": "ef3c1fc05481f7f3"} |
vcr_24f4dbab8e91 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-23, -20, -12, -7, 0, 14, 18, 28, 39], "target": 16}, "output": [0, 8]}, {"input": {"nums": [-14, -11, -1, 6, 7, 9, 19, 20, 24, 28, 37], "target": 28}, "output": [5, 6]}, {"input": {"nums": [-18, -9, -8, -7, -1, 8, 12, 14, 28], "target": 26}, "output": [6, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "95ff0b2b2612b33c"} |
vcr_362b0e8c4d20 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-16, 30, 11]}, "output": [-16, 14, 25]}, {"input": {"nums": [-2, -2, 2, 8, 8, -6, 6]}, "output": [-2, -4, -2, 6, 14, 8, 14]}, {"input": {"nums": [9, 17, 17, -3, 16, 9, 10, 8, -6, -2]}, "output": [9, 26, 43, 40, 56, 65, 75, 83, 77, 75]}] | 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": "228ee7bd10c9e31b"} |
vcr_030b2b0fd5d9 | 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": [24, 29, 12, 19, 9, 27, 5, 49, 9]}, "output": true}, {"input": {"nums": [3, 16, 11, 25, 17, 20, 22, 7, 12, 11]}, "output": true}, {"input": {"nums": [4, 12, 15, 13, 0, 18, 21, 17, 2, 1, 12]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "a1781d68ed296f3d"} |
vcr_3c6e431cb0c0 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-2, 5, 5, -19, 8], "k": 12}, "output": [-19, 8, -2, 5, 5]}, {"input": {"nums": [10, 9, -2, 6, 6, -6, 9], "k": 19}, "output": [-2, 6, 6, -6, 9, 10, 9]}, {"input": {"nums": [-8, -10, 4, 1, -8], "k": 0}, "output": [-8, -10, 4, 1, -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": "fc5313b623fda6cf"} |
vcr_bebc14f4896c | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 3, 3, 3, 10, 11, 13]}, "output": [1, 3, 10, 11, 13]}, {"input": {"nums": [1, 4, 5, 8, 10, 12, 12, 12]}, "output": [1, 4, 5, 8, 10, 12]}, {"input": {"nums": [1, 2, 3, 6, 8]}, "output": [1, 2, 3, 6, 8]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "5c7eca39432eda60"} |
vcr_8792b386695c | 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": "dadcacdcdcacdad"}, "output": true}, {"input": {"s": "bbab"}, "output": false}, {"input": {"s": "ccacacb"}, "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": "7393b63fba1e90d0"} |
vcr_5d8f51fdb90d | 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": 12831}, "output": 8}, {"input": {"n": 3938}, "output": 7}, {"input": {"n": 13582}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "3af85d2fc9be8361"} |
vcr_1c013439eb9b | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [5, -17, 10, -15, -1, -9, 14, 11, -6], "k": 16}, "output": [10, -15, -1, -9, 14, 11, -6, 5, -17]}, {"input": {"nums": [4, 3, 5], "k": 4}, "output": [5, 4, 3]}, {"input": {"nums": [4, 9, -7, 8, -2, 6, -1], "k": 0}, "output": [4, 9, -7, 8, -2, 6, -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": "aca9663a6561b9ff"} |
vcr_74e24279a51c | 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": "abcdaabdccbac"}, "output": false}, {"input": {"s": "acabaaab"}, "output": false}, {"input": {"s": "bcc"}, "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": "9b80c037b9453a51"} |
vcr_928093e42557 | 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": [-21, -20, -17, -8, -1, 5, 14, 17], "target": 13}, "output": [4, 6]}, {"input": {"nums": [-18, -14, -10, -9, 18, 38], "target": 29}, "output": [3, 5]}, {"input": {"nums": [-19, -17, -11, -10, -7, -4, 1, 36, 38, 39], "target": 17}, "output": [0, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "00f7f00fc29f4b1f"} |
vcr_cb6f766b8465 | 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": [48, 57, 21, 23, 25, 33, 39, 56, 57, 13]}, "output": 36}, {"input": {"prices": [15, 27, 16, 31, 34, 2, 17]}, "output": 19}, {"input": {"prices": [37, 28, 35, 18, 8, 13, 30]}, "output": 22}] | 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": "022f9b003b8176bc"} |
vcr_f4e0cf3b00cf | 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": "adde e cc"}, "output": "cc e adde"}, {"input": {"s": "bbab bb bba bb b"}, "output": "b bb bba bb bbab"}, {"input": {"s": "b bb bba ba ba"}, "output": "ba ba bba bb b"}] | 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": "724fc169df95a357"} |
vcr_3d66cda2b7c8 | 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": "aededa eec bccac"}, "output": "bccac eec aededa"}, {"input": {"s": "bbba bba a"}, "output": "a bba bbba"}, {"input": {"s": "b ba aaab bab"}, "output": "bab aaab ba b"}] | 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": "a3288ad87ab296ec"} |
vcr_a9baea990a83 | 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": "bcb dadcca"}, "output": "dadcca bcb"}, {"input": {"s": "abb ba aba"}, "output": "aba ba abb"}, {"input": {"s": "bb a bba ab aaba"}, "output": "aaba ab bba a 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": "bfc03231e84ca875"} |
vcr_7d8080bb1de6 | 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": [7, 11, 13, 24, 27], "target": 19}, "output": 3}, {"input": {"nums": [3, 7, 8, 13, 17, 25, 28, 29, 37, 39], "target": 3}, "output": 0}, {"input": {"nums": [2, 3, 11, 12, 13, 20, 27, 28, 29, 30, 32, 38], "target": 32}, "output": 10}] | 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": "871f853aa35871fb"} |
vcr_056819ba8a28 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-6, 8, 19, 6, 0, -3]}, "output": 33}, {"input": {"nums": [3, 2, -5, 10, 9]}, "output": 19}, {"input": {"nums": [11, 9, 4, -8, 11, 11, -2, -7]}, "output": 38}] | 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": "3457c64d9d4f8789"} |
vcr_9c440f86f7f4 | 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": [3, 20, 3, 16, 26, 6, 8, 6, 17, 17, 78, 8, 26, 39, 20, 39, 16]}, "output": 78}, {"input": {"nums": [27, 19, 4, 22, 27, 3, 19, 53, 22, 4, 3]}, "output": 53}, {"input": {"nums": [6, 21, 48, 4, 21, 29, 4, 6, 29]}, "output": 48}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "ee5172c5fe655059"} |
vcr_1e47923c8d1c | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [4, 9, 23, 14, 56, 58, 36, 43, 25, 7, 35]}, "output": 54}, {"input": {"prices": [33, 33, 19, 9, 7, 35]}, "output": 28}, {"input": {"prices": [17, 38, 18, 3, 28, 39, 9, 38, 20, 12, 2, 24]}, "output": 36}] | 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": "a0dd71577e42190b"} |
vcr_fcacf266be00 | 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": "eadbbb bcbe bbda"}, "output": "bbda bcbe eadbbb"}, {"input": {"s": "ba b a bb"}, "output": "bb a b ba"}, {"input": {"s": "ab bba"}, "output": "bba ab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "e0d7caee120d31e3"} |
vcr_d894be035b89 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-1, -11, -8, -10, -8], "k": 1}, "output": [-8, -1, -11, -8, -10]}, {"input": {"nums": [5, -1, 0, 2, -2, 2, -2, 4], "k": 1}, "output": [4, 5, -1, 0, 2, -2, 2, -2]}, {"input": {"nums": [6, 7, -2, 1, 8, -3, -5, -9], "k": 13}, "output": [1, 8, -3, -5, -9, 6, 7, -2]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "05c9a1543d8ddd16"} |
vcr_6eabe968777b | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [2, 23, 22, 13]}, "output": 36}, {"input": {"nums": [20, 10, 7, 4, 15, 11, 15, 4, 2]}, "output": 59}, {"input": {"nums": [0, 5, 3, 19, 11, 17, 17, 17, 3]}, "output": 58}] | 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": "13f2f29f64fea66c"} |
vcr_076531c80a24 | 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, 3, 3, 4, 4, 5, 10, 11, 11, 12, 14]}, "output": [0, 3, 4, 5, 10, 11, 12, 14]}, {"input": {"nums": [1, 3, 7]}, "output": [1, 3, 7]}, {"input": {"nums": [1, 4, 4, 9, 10, 12, 12]}, "output": [1, 4, 9, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "3d3e1953520aa80a"} |
vcr_03658086013d | 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": [3, 3, 14, 4, 25, 25, 4, 26, 26, 71, 14, 29, 29]}, "output": 71}, {"input": {"nums": [9, 13, 54, 13, 5, 9, 5]}, "output": 54}, {"input": {"nums": [23, 2, 22, 14, 22, 13, 13, 14, 2, 46, 23]}, "output": 46}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "357f57f972646034"} |
vcr_98461e1c03c8 | 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": [2, 2, 6, 6, 0, 5, 7]}, "output": [0, 0, 0, 0, 5040, 0, 0]}, {"input": {"nums": [3, 4, 3, 5, 3]}, "output": [180, 135, 180, 108, 180]}, {"input": {"nums": [1, 2, 2, 3, 2, 5, 5]}, "output": [600, 300, 300, 200, 300, 120, 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": "ad06dba6adb27b17"} |
vcr_34dbe3ecef1f | 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": [176, 79, 6, 7, 15, 11, 62, 33, 18, 52, 24, 151]}, "output": false}, {"input": {"nums": [7, 13, 20, 7, 14, 4]}, "output": true}, {"input": {"nums": [18, 0, 4, 20, 6, 5, 14, 0]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "8cec6ec18af9658c"} |
vcr_131b5672e188 | 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": [-9, -2, 20, -19, 34, 31, 26, -14, -21]}, "output": [-9, -11, 9, -10, 24, 55, 81, 67, 46]}, {"input": {"nums": [16, 5]}, "output": [16, 21]}, {"input": {"nums": [5, 4, -3, -8, 15, 7, 0, 15, 3]}, "output": [5, 9, 6, -2, 13, 20, 20, 35, 38]}] | 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": "8c68f7c3e773435f"} |
vcr_79d398febb5d | 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": "eeee ad aadcd e"}, "output": "e aadcd ad eeee"}, {"input": {"s": "aabb b"}, "output": "b aabb"}, {"input": {"s": "abab a bbba aaa"}, "output": "aaa bbba a abab"}] | 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": "d80fd3eff8fe5ff7"} |
vcr_0f8c4bfd88de | 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": "cadecd baab db"}, "output": "db baab cadecd"}, {"input": {"s": "b bb bab aa"}, "output": "aa bab bb b"}, {"input": {"s": "a aab ab"}, "output": "ab aab a"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "c9d2267e562ed281"} |
vcr_cdc25e0b374d | 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, 7, 10, 14, 15]}, "output": [0, 3, 4, 7, 10, 14, 15]}, {"input": {"nums": [4, 5, 7, 9, 9, 11]}, "output": [4, 5, 7, 9, 11]}, {"input": {"nums": [2, 2, 3, 6, 8, 8, 8, 9]}, "output": [2, 3, 6, 8, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "b61f234c8ddbdcee"} |
vcr_22f08cbd24ae | 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": [23, 9, -3, -8, 6, -17, 21, 26, -15, 33, 7, 28, 3, -16]}, "output": [23, 32, 29, 21, 27, 10, 31, 57, 42, 75, 82, 110, 113, 97]}, {"input": {"nums": [10, 7, 1, -9, -9, 18, -1, -5, 0]}, "output": [10, 17, 18, 9, 0, 18, 17, 12, 12]}, {"input": {"nums": [-7, -5, 6, 16, 4, 13, -2, 8, -1]}, "output": [-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": "b68b112dc327518a"} |
vcr_654d3f3de820 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [12, 25, 29]}, "output": 41}, {"input": {"nums": [1, 18, 1]}, "output": 18}, {"input": {"nums": [19, 13, 8, 17, 2]}, "output": 36}] | 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": "a102195f04867fe4"} |
vcr_36c886f564e4 | 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": ["abc", "acbc", "cb", "aaa", "bba", "ccca", "bcb"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "baa", "baa", "abb", "abb"]}, "output": [1, 2, 2]}, {"input": {"strs": ["baa", "aab", "bab"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "2786351206033dae"} |
vcr_f99b7459f624 | 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, 7, 6, 4, 2, 4]}, "output": [1344, 768, 896, 1344, 2688, 1344]}, {"input": {"nums": [3, 5, 3, 4]}, "output": [60, 36, 60, 45]}, {"input": {"nums": [4, 4, 2, 5, 4, 1]}, "output": [160, 160, 320, 128, 160, 640]}] | 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": "7edf8a496730ebe9"} |
vcr_e9d5d879baef | 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": "dcaa bbbb"}, "output": "bbbb dcaa"}, {"input": {"s": "aa bbbb aaa bba"}, "output": "bba aaa bbbb aa"}, {"input": {"s": "ba bbbb b a"}, "output": "a b bbbb ba"}] | 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": "da490a0ee270ab61"} |
vcr_28e7ffee4176 | 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": [42, 59, 46, 6, 17, 46, 40, 5, 39, 2, 54]}, "output": 52}, {"input": {"prices": [4, 9, 33, 32, 25, 20, 38, 19]}, "output": 34}, {"input": {"prices": [12, 1, 31, 10, 29, 39]}, "output": 38}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "96f57cd12630e7cb"} |
vcr_fef096c3a849 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [12, 12, 15, 9, -8, -15, 4, -16, -2, 3, 5]}, "output": 48}, {"input": {"nums": [8, -1, 4, 14, 12, -1]}, "output": 37}, {"input": {"nums": [-8, -10, -5, -4, -2]}, "output": -2}] | 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": "a36fa255ad0eec90"} |
vcr_4597c95ed2bd | 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, -19, -12, 17, -12, 14, -7, 13, 9, 14], "k": 21}, "output": [7, -19, -12, 17, -12, 14, -7, 13, 9, 14, 10]}, {"input": {"nums": [10, 9, -5, 3, -4, -3, -1], "k": 6}, "output": [9, -5, 3, -4, -3, -1, 10]}, {"input": {"nums": [6, 9, -2, 5, 5], "k": 20}, "output": [6, 9, -2, 5, 5]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "7de3a8a86f3b55eb"} |
vcr_1d7332756eda | 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": "bbaabaabbc"}, "output": 6}, {"input": {"s": "abca"}, "output": 4}, {"input": {"s": "abcbccabb"}, "output": 7}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "bcf5d72835a84754"} |
vcr_a2c7b84ba0db | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["aa", "cbab", "abba", "accb", "aa", "ab"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["baa", "aab", "baa", "baa", "aaa"]}, "output": [1, 4]}, {"input": {"strs": ["aba", "bab", "bba", "aaa", "aba"]}, "output": [1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "0bcb36212ed87b5a"} |
vcr_fbd08e393371 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-17, -15, 21, 19, 10, 0, 14, 3, -4, -7, 13, -17, 2]}, "output": 69}, {"input": {"nums": [3, -8, 0, -2, 0, 10, -9]}, "output": 10}, {"input": {"nums": [7, -1, 13, 12, -9, 13, 9, -3, -8, 9]}, "output": 44}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "6af271e404c4fb5e"} |
vcr_19b8b7364040 | 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": [38, 1, 15, 25, 31, 59]}, "output": 58}, {"input": {"prices": [16, 32, 27]}, "output": 16}, {"input": {"prices": [6, 5, 23]}, "output": 18}] | 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": "8b9d1a807cd3f706"} |
vcr_017c87928b0d | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-13, -5, 5, 18, 4, 12, -12, 17, 12], "k": 11}, "output": [17, 12, -13, -5, 5, 18, 4, 12, -12]}, {"input": {"nums": [9, 1, -3], "k": 10}, "output": [-3, 9, 1]}, {"input": {"nums": [-4, 9, 8], "k": 1}, "output": [8, -4, 9]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "eb3d0036613feba4"} |
vcr_9330156061a7 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 5, 10, 12, 15, 16, 18, 28, 31, 34, 39, 48], "target": 2}, "output": 1}, {"input": {"nums": [2, 5, 13, 35], "target": 38}, "output": 4}, {"input": {"nums": [9, 14, 16, 21, 24, 25, 29, 31, 39], "target": 31}, "output": 7}] | 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": "ec6eb7c67814f0de"} |
vcr_a2e2ddb15df0 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 2, 1, 3, 2, 3, 3, 1, 0]}, "output": true}, {"input": {"nums": [2, 3, 0, 3, 2, 1]}, "output": true}, {"input": {"nums": [0, 1, 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": "5b0355204c3ec906"} |
vcr_00904f6f20e0 | 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, 5, 26, 29, -8, 16, -10, 15]}, "output": [8, 13, 39, 68, 60, 76, 66, 81]}, {"input": {"nums": [16, 5, 16]}, "output": [16, 21, 37]}, {"input": {"nums": [17, -9, 12, -6, -7, 18, 2]}, "output": [17, 8, 20, 14, 7, 25, 27]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "79def29e420e2adb"} |
vcr_c7c7c437e0ca | 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": [3, 3, 3, 4, 9, 11, 12, 12, 13, 13]}, "output": [3, 4, 9, 11, 12, 13]}, {"input": {"nums": [1, 3, 4, 8]}, "output": [1, 3, 4, 8]}, {"input": {"nums": [0, 1, 3, 5, 7]}, "output": [0, 1, 3, 5, 7]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "6b8e8fb42f347f19"} |
vcr_91ad4f95af7c | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "ea bdd ab dcc"}, "output": "dcc ab bdd ea"}, {"input": {"s": "b bba abaa"}, "output": "abaa bba b"}, {"input": {"s": "a a"}, "output": "a a"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "abe29790eadc8c68"} |
vcr_79a79dea4bb4 | 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": "bcbdadbaabdadbcb"}, "output": true}, {"input": {"s": "cccbccbc"}, "output": false}, {"input": {"s": "bcbb"}, "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": "4ec377398500afd8"} |
vcr_7590aa8a1533 | 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": "bcbea bccbd eec"}, "output": "eec bccbd bcbea"}, {"input": {"s": "bab bb bab"}, "output": "bab bb bab"}, {"input": {"s": "baa abba a"}, "output": "a abba 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": "f960de5f7e67d0fb"} |
vcr_59524cd6b2b1 | 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, 20, 12, 6, 6, 12, 20, 26, 25, 25, 7, 55, 26]}, "output": 55}, {"input": {"nums": [13, 11, 44, 22, 17, 17, 22, 13, 11]}, "output": 44}, {"input": {"nums": [11, 11, 17, 51, 17, 29, 29]}, "output": 51}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "f65049554c509996"} |
vcr_89f7e96768e1 | 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": [17, 17, 27, 7, 38, 39, 1, 39, 1, 7, 53, 38, 27]}, "output": 53}, {"input": {"nums": [3, 27, 7, 7, 15, 27, 15, 45, 3]}, "output": 45}, {"input": {"nums": [18, 4, 4, 43, 14, 17, 14, 17, 18]}, "output": 43}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "7668c15b21fa306c"} |
vcr_50f9ddc6aa21 | 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": 11111}, "output": 9}, {"input": {"n": 4842}, "output": 7}, {"input": {"n": 15319}, "output": 11}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "b8e9ebecd4a59b14"} |
vcr_b0cd204c335a | 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": [21, 15, -27, -22, 68, 11, 1, 9, 42, 69], "target": -21}, "output": [3, 6]}, {"input": {"nums": [37, 28, -9, 45], "target": 28}, "output": [0, 2]}, {"input": {"nums": [-24, 15, 18, 23, 37], "target": 55}, "output": [2, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "4db72f6fb966dba3"} |
vcr_bec1ec9979d0 | 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, 29, 23, 22, 18]}, "output": 63}, {"input": {"nums": [20, 10, 13]}, "output": 33}, {"input": {"nums": [11, 18, 20, 6, 3, 18]}, "output": 49}] | 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": "2f93cbb8f57ff130"} |
vcr_ae359d07a50b | 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": [25, 2, 18, 28, 28]}, "output": 26}, {"input": {"prices": [40, 7, 21]}, "output": 14}, {"input": {"prices": [3, 11, 15, 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": "275102a3ef064a98"} |
vcr_75fec0ef05f1 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 1, 4, 6, 8, 9, 11, 11, 11, 12, 14, 14, 15]}, "output": [0, 1, 4, 6, 8, 9, 11, 12, 14, 15]}, {"input": {"nums": [3, 5, 5, 6, 6, 7, 7, 10, 11]}, "output": [3, 5, 6, 7, 10, 11]}, {"input": {"nums": [0, 1, 1, 4, 5, 10, 12, 12]}, "output": [0, 1, 4, 5, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "44444be5461eede0"} |
vcr_a0320867971f | 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, 6, 2, 6, 20, 18, 25, 23, 8, 30, 2]}, "output": 99}, {"input": {"nums": [18, 9, 13, 19]}, "output": 37}, {"input": {"nums": [7, 6, 18, 20, 5, 16, 18, 13]}, "output": 56}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "264607e2dfa08ca7"} |
vcr_5dd5b83205e2 | 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": 26486}, "output": 10}, {"input": {"n": 1773}, "output": 8}, {"input": {"n": 6315}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "917c2f04064436e1"} |
vcr_5bad4b9e6b9a | 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": 44515}, "output": 10}, {"input": {"n": 4966}, "output": 7}, {"input": {"n": 14299}, "output": 11}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "eb8ff25b0460c8a7"} |
vcr_388fd057273c | 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, 6, 16, 2, 23, 10, 20, 29, 28, 8, 29, 8]}, "output": 145}, {"input": {"nums": [3, 11, 16, 3, 11, 17]}, "output": 36}, {"input": {"nums": [5, 13, 1, 18, 7, 17, 0, 0, 20, 16]}, "output": 68}] | 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": "5aae767004a1120a"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.