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_641e724d8c49 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-24, 14, 21, 29, 41, 43, 45], "target": 19}, "output": [0, 5]}, {"input": {"nums": [-20, -13, -3, -1, 27, 30], "target": 7}, "output": [0, 4]}, {"input": {"nums": [-5, -2, 5, 16, 19, 32], "target": -7}, "output": [0, 1]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "84a8492ceb19d7ab"} |
vcr_354134473990 | hard | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "bbddbdbddbb"}, "output": true}, {"input": {"s": "aabbb"}, "output": false}, {"input": {"s": "bcac"}, "output": false}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "9f0077e4775f44aa"} |
vcr_59f202b2ace1 | 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": [46, 14, 34, 40, 1, 20, 24, 27, 29, 2, 59, 56, 48, 52, 24, 18]}, "output": 58}, {"input": {"prices": [12, 18, 25, 35, 12]}, "output": 23}, {"input": {"prices": [17, 22, 31, 29, 32, 35, 3, 10]}, "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": "acc2547769e45dc1"} |
vcr_b11d4b9a65cb | 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": 21283}, "output": 7}, {"input": {"n": 15256}, "output": 8}, {"input": {"n": 5677}, "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": "00ae9c60249b8122"} |
vcr_156306d4a6c2 | 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": [1, 1, 4, 14, 38, 20, 77, 20, 4, 38, 14, 12, 12]}, "output": 77}, {"input": {"nums": [10, 53, 15, 3, 25, 15, 25, 10, 3]}, "output": 53}, {"input": {"nums": [3, 23, 53, 23, 3]}, "output": 53}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "c604845f671616f2"} |
vcr_6bb6576d565e | 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, 21, 14, 17, 19, 7, 10, 3, 26, 18]}, "output": 81}, {"input": {"nums": [16, 1, 16, 14, 14, 3, 9, 14, 8, 2]}, "output": 63}, {"input": {"nums": [14, 1, 5, 5]}, "output": 19}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "f0d3f2fb416cc82e"} |
vcr_5d0d79e3e45a | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [17, 33, 14, 6, 10, 16, -1, -16]}, "output": [17, 50, 64, 70, 80, 96, 95, 79]}, {"input": {"nums": [10, 15, 12, -11, -11, 11]}, "output": [10, 25, 37, 26, 15, 26]}, {"input": {"nums": [3, 5, 8, -6, -7, -9]}, "output": [3, 8, 16, 10, 3, -6]}] | 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": "3e1e993ebfee3cb6"} |
vcr_c1d69cd14ebf | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 8, 10, 35, 41], "target": 20}, "output": 3}, {"input": {"nums": [0, 3, 7, 17, 20, 28, 36], "target": 23}, "output": 5}, {"input": {"nums": [5, 19, 28, 39], "target": 10}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "a6df833a6913491a"} |
vcr_cce782ef87d8 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 4, 6, 8, 8, 9, 9, 12, 14, 15]}, "output": [0, 2, 4, 6, 8, 9, 12, 14, 15]}, {"input": {"nums": [0, 0, 8, 12]}, "output": [0, 8, 12]}, {"input": {"nums": [5, 5, 5, 6, 7, 7, 8, 8, 9, 10, 11]}, "output": [5, 6, 7, 8, 9, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "e0a80649c6feaeb9"} |
vcr_2ef43d795aae | 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, -17, -5, -3, 19, 10, -13], "k": 3}, "output": [19, 10, -13, -8, -17, -5, -3]}, {"input": {"nums": [7, 7, 6, -5, -2, 6, -8, 2], "k": 18}, "output": [-8, 2, 7, 7, 6, -5, -2, 6]}, {"input": {"nums": [1, 10, -10, -2, -6, 4, -9, 6, -9, 9], "k": 20}, "output": [1, 10, -10, -2, -6, 4, -9, 6, -9, 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": "fd81cf764b74f177"} |
vcr_8959601cf217 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-15, -7, 7, 4, 3, 18, -6, -13, -17, -11, 11, -8, 9]}, "output": 32}, {"input": {"nums": [-11, 13, 2]}, "output": 15}, {"input": {"nums": [-1, 7, -12, -1, -12, 4, 6, 10]}, "output": 20}] | 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": "f7fad26f69dbb2ce"} |
vcr_97f78b580aff | 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": [-14, -12, -5, 0, 7, 9, 13, 29, 42, 43, 46, 49], "target": 29}, "output": [0, 9]}, {"input": {"nums": [-13, -3, 2, 5, 12, 15, 21, 22, 25, 26], "target": 37}, "output": [4, 8]}, {"input": {"nums": [-19, -16, -4, -2, 22, 23, 26, 29, 39], "target": 37}, "output": [3, 8]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "dc929f02b26c84c7"} |
vcr_1bd27034423f | 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": [-2, 4, 13, 16, 28, 42, 49], "target": 53}, "output": [1, 6]}, {"input": {"nums": [-12, -10, -1, 5, 21, 22, 24, 26, 32, 34], "target": 45}, "output": [4, 6]}, {"input": {"nums": [-20, -12, -9, 8, 9, 11, 31, 38], "target": -32}, "output": [0, 1]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "fbb0ab5e0b93cae6"} |
vcr_cb7cc0f1da46 | 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": "eacbc bdbd"}, "output": "bdbd eacbc"}, {"input": {"s": "a ab baaa ab"}, "output": "ab baaa ab a"}, {"input": {"s": "b aa"}, "output": "aa 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": "5544d3c18207e277"} |
vcr_ad22a07aff33 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [9, 15, 16, 18, 22, 28, 29, 43], "target": 43}, "output": 7}, {"input": {"nums": [1, 2, 7, 8, 10, 13, 22, 23, 30, 33, 39], "target": 32}, "output": 9}, {"input": {"nums": [3, 8, 12, 13, 14, 15, 16, 17, 24, 28, 29, 32], "target": 39}, "output": 12}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "a98a2a14780ae571"} |
vcr_c781511f717d | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 13, 16, 23, 25, 26, 31, 36, 37, 44], "target": 46}, "output": 10}, {"input": {"nums": [3, 4, 29], "target": 18}, "output": 2}, {"input": {"nums": [0, 3, 5, 16, 20, 24, 35], "target": 39}, "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": "102a1af0b9547f2d"} |
vcr_db790c8285d9 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [1, 4, 4, 4, 4, 3, 3, 4, 0]}, "output": true}, {"input": {"nums": [2, 0, 1, 3, 2, 0, 1]}, "output": true}, {"input": {"nums": [3, 3, 2, 0, 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": "08d1f7ad3d2cc9c4"} |
vcr_c7ca80a8f359 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 2, 2, 3, 7, 7, 8, 8, 10, 11, 12, 14]}, "output": [1, 2, 3, 7, 8, 10, 11, 12, 14]}, {"input": {"nums": [4, 7, 8, 9, 11]}, "output": [4, 7, 8, 9, 11]}, {"input": {"nums": [1, 1, 7, 8, 10, 11]}, "output": [1, 7, 8, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "b54c270cd5969abe"} |
vcr_1a12ad032167 | 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": [0, 2, 18, -15, 7, 18, 5, 2, 1, -7, -17, 4, 10, -2]}, "output": 38}, {"input": {"nums": [2, 13, 12, 3]}, "output": 30}, {"input": {"nums": [-12, -10, 14, -11, -1, -2, -4, 13, 13]}, "output": 26}] | 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": "0d9d4e8892cdfcec"} |
vcr_f904466dbd65 | 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, 9, 34, 35, 44, 48], "target": 0}, "output": 0}, {"input": {"nums": [2, 3, 6, 12, 23, 24, 32], "target": 35}, "output": 7}, {"input": {"nums": [0, 3, 10, 24, 36, 39], "target": 15}, "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": "a7acc93eb05dfe65"} |
vcr_02858e063a53 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 7, 6, 0, 6, 5]}, "output": [0, 0, 0, 6300, 0, 0]}, {"input": {"nums": [3, 3, 1, 3, 4]}, "output": [36, 36, 108, 36, 27]}, {"input": {"nums": [4, 2, 2]}, "output": [4, 8, 8]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "c45989bd0b0796af"} |
vcr_5acb15318823 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-6, 19, -4, -15], "k": 11}, "output": [19, -4, -15, -6]}, {"input": {"nums": [9, -10, 2, 6], "k": 1}, "output": [6, 9, -10, 2]}, {"input": {"nums": [-3, 8, 1, -7, 8, -8], "k": 5}, "output": [8, 1, -7, 8, -8, -3]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "b9ff482739e3eb3d"} |
vcr_c81998ec37fb | 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": [63, 35, 9, 37, 9, 8, 8, 14, 33, 35, 11, 14, 37, 33, 31, 31, 11]}, "output": 63}, {"input": {"nums": [56, 3, 28, 28, 12, 12, 3]}, "output": 56}, {"input": {"nums": [3, 28, 20, 21, 42, 21, 27, 28, 20, 27, 3]}, "output": 42}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "a0b148a38b23ab84"} |
vcr_bc8974d46e9b | 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, 0, 0, 1, 1, 1, 3, 5, 9, 11, 13, 13, 14]}, "output": [0, 1, 3, 5, 9, 11, 13, 14]}, {"input": {"nums": [2, 3, 4, 5, 5, 6, 8, 11, 11, 11, 12]}, "output": [2, 3, 4, 5, 6, 8, 11, 12]}, {"input": {"nums": [0, 4, 6, 6, 6, 11, 12]}, "output": [0, 4, 6, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "7cbb6f0015b3d3ff"} |
vcr_12b012fca644 | 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", "aca", "bbac", "acb", "cac", "bab"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "aaa", "aaa", "abb", "aaa", "aba", "bba"]}, "output": [2, 2, 3]}, {"input": {"strs": ["bba", "abb", "abb", "bbb", "bab", "aba"]}, "output": [1, 1, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "91065fd85de01011"} |
vcr_d57ec290e0ec | 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": "dcade eebda"}, "output": "eebda dcade"}, {"input": {"s": "aaa b aba aba aba"}, "output": "aba aba aba b aaa"}, {"input": {"s": "abb aa bbb bbb a"}, "output": "a bbb bbb aa abb"}] | 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": "bb444721169af886"} |
vcr_481affce0f1a | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["cb", "caba", "cc", "abaa", "cac", "bc", "acb", "bcc"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["abb", "aba", "bba", "bba", "abb", "baa", "bbb"]}, "output": [1, 2, 4]}, {"input": {"strs": ["aba", "baa", "bba", "abb", "baa", "aaa", "aab"]}, "output": [1, 2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "f1fdf4c44c14c5d4"} |
vcr_bb1ffaf33a28 | 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": [-24, 11, 9, 14, -3]}, "output": [-24, -13, -4, 10, 7]}, {"input": {"nums": [-7, 16, 9, -12, 6, 13, -9, -9, 9]}, "output": [-7, 9, 18, 6, 12, 25, 16, 7, 16]}, {"input": {"nums": [-11, 2, 16]}, "output": [-11, -9, 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": "9473b56145c45787"} |
vcr_6cb74547ca53 | 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": ["cabb", "accc", "ba", "bb", "aa", "bbc", "aab", "caba"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bba", "bba"]}, "output": [1, 2]}, {"input": {"strs": ["aab", "bbb", "bab"]}, "output": [1, 1, 1]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "ddbefe4dfb002ab2"} |
vcr_3d0c94ac0832 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 7, 13, 14, 34, 38, 44, 49], "target": 1}, "output": 0}, {"input": {"nums": [7, 13, 18, 19, 25, 28], "target": 36}, "output": 6}, {"input": {"nums": [0, 3, 8, 9, 10, 23, 26, 33, 38], "target": 3}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "e45d71eff5843da5"} |
vcr_fca2fa865711 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [11, 2, 6, 10, -5, -6]}, "output": 29}, {"input": {"nums": [3, 2, -2]}, "output": 5}, {"input": {"nums": [-6, 14, 0, 9]}, "output": 23}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "53abfd7ba729504d"} |
vcr_3cdc9b520242 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [10, 27, 10, 9, 29]}, "output": 56}, {"input": {"nums": [13, 9, 17, 7, 15, 5, 14, 9, 3, 19]}, "output": 78}, {"input": {"nums": [6, 5, 5, 18, 13, 2, 15, 4, 4, 11]}, "output": 50}] | 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": "b4eb4d5cb7002554"} |
vcr_1259e4030cc8 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [9, 65, 32, 34, 35, 22, 19, 9, 19, 6, 13, 32, 13, 6, 22, 34, 35]}, "output": 65}, {"input": {"nums": [14, 49, 14, 7, 7, 21, 24, 21, 24]}, "output": 49}, {"input": {"nums": [1, 11, 25, 25, 11, 1, 47]}, "output": 47}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "b25eafabcefa37b4"} |
vcr_6063053d5f0c | 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": 234, "b": 56}, "output": 2}, {"input": {"a": 133, "b": 76}, "output": 19}, {"input": {"a": 94, "b": 146}, "output": 2}] | 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": "4b2a1dccb6017065"} |
vcr_2139931347eb | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [1, 1, 2, 2, 0, 1, 2, 0, 0]}, "output": true}, {"input": {"nums": [3, 3, 0, 3, 0, 0, 3, 2]}, "output": true}, {"input": {"nums": [0, 3, 3, 2, 3, 1, 1, 1, 2]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "85e48faae148d1a6"} |
vcr_bfa518338a6e | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 4, 6, 6, 7, 11, 13, 14, 15]}, "output": [2, 4, 6, 7, 11, 13, 14, 15]}, {"input": {"nums": [1, 4, 5, 11, 12]}, "output": [1, 4, 5, 11, 12]}, {"input": {"nums": [0, 0, 3, 5, 5, 6, 7, 9, 9, 10, 11, 12]}, "output": [0, 3, 5, 6, 7, 9, 10, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "cfde810057c4edf2"} |
vcr_4018b76b8ce5 | 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": [39, 54, 3, 47, 40, 29, 10, 14, 30, 45, 58, 52]}, "output": 55}, {"input": {"prices": [15, 26, 26]}, "output": 11}, {"input": {"prices": [2, 34, 26, 7]}, "output": 32}] | 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": "11314f6969cf9cd5"} |
vcr_2e44fc5ee4fd | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 2, 3, 13, 13, 14]}, "output": [1, 2, 3, 13, 14]}, {"input": {"nums": [2, 3, 4, 4, 6, 8, 9, 11]}, "output": [2, 3, 4, 6, 8, 9, 11]}, {"input": {"nums": [2, 4, 5, 5, 9]}, "output": [2, 4, 5, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "2babc559d8c1e0bd"} |
vcr_8e00c8b3283c | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [19, 16, 29, 11, 57, 26, 5, 59, 11, 26]}, "output": 54}, {"input": {"prices": [4, 40, 8, 25, 31, 39, 37, 23, 39, 30, 10]}, "output": 36}, {"input": {"prices": [14, 12, 18, 32, 31, 12, 27]}, "output": 20}] | 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": "a9d8ca92f09ede36"} |
vcr_40e82f671937 | 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": "abbdab"}, "output": false}, {"input": {"s": "ac"}, "output": false}, {"input": {"s": "bacbaa"}, "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": "1a5bfe95f147aba1"} |
vcr_ee17b992352d | 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": 113, "b": 33}, "output": 1}, {"input": {"a": 67, "b": 126}, "output": 1}, {"input": {"a": 148, "b": 73}, "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": "b3dab5348b818c09"} |
vcr_15d38800a16f | 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": 196, "b": 73}, "output": 1}, {"input": {"a": 69, "b": 153}, "output": 3}, {"input": {"a": 132, "b": 99}, "output": 33}] | 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": "d51496b9a3ab13cd"} |
vcr_97fb9c3a4ad3 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [25, 23, 9, 17, 23, 27, 21]}, "output": 78}, {"input": {"nums": [7, 20, 7, 8, 3, 11, 5, 12, 16]}, "output": 55}, {"input": {"nums": [13, 4, 14]}, "output": 27}] | 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": "a508ed43282fd98a"} |
vcr_54f655be9216 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [9, 26, 31, 41], "target": 39}, "output": 3}, {"input": {"nums": [0, 12, 15, 17, 19, 20, 24, 30, 31, 33], "target": 40}, "output": 10}, {"input": {"nums": [6, 7, 8, 10, 11, 23, 35, 36], "target": 9}, "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": "907a75c5b26cd2f7"} |
vcr_77f18654ae37 | 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": [14, -39, -36, -28], "target": -25}, "output": [0, 1]}, {"input": {"nums": [20, 36, -12, -10, -25, 2, 6, 23, 18, 13, -23], "target": 25}, "output": [5, 7]}, {"input": {"nums": [-12, -27, 42, 5], "target": -22}, "output": [1, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "a33025e6d8128dcf"} |
vcr_9944a0dfc757 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [15, -6, 16, 4, -3, 11, -11]}, "output": 37}, {"input": {"nums": [-8, 9, -3, 10, -5, 14, 9, 4, 8, -11]}, "output": 46}, {"input": {"nums": [12, 5, -10, 12, 9, -1, -12, 10]}, "output": 28}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "20a59231c24806f6"} |
vcr_c30457f43e24 | 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, 4, 46, 26, 24]}, "output": true}, {"input": {"nums": [18, 21, 24, 21]}, "output": true}, {"input": {"nums": [2, 5, 24, 13]}, "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": "4e2a01fe33d9d8a1"} |
vcr_3d1633b3a0cb | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [36, 35, 32, 32, 36, 35, 14, 14, 8, 34, 34, 38, 38, 53, 8]}, "output": 53}, {"input": {"nums": [26, 26, 15, 11, 25, 11, 25, 56, 15]}, "output": 56}, {"input": {"nums": [17, 17, 3, 24, 14, 14, 24, 59, 3]}, "output": 59}] | 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": "aaf73d286a143ac3"} |
vcr_c3e3709c515a | 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": [49, 13, 49, 2, 14, 27, 23, 13]}, "output": 36}, {"input": {"prices": [22, 40, 38, 27, 34]}, "output": 18}, {"input": {"prices": [7, 2, 8, 6, 40]}, "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": "96171305d3fd360a"} |
vcr_6cf310939ab8 | 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": ["ba", "acca", "cac", "acb", "ccc", "aabb", "ab", "bb"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "bab", "aba"]}, "output": [1, 2]}, {"input": {"strs": ["aab", "aab", "baa", "aaa", "aab", "aaa", "aba"]}, "output": [2, 5]}] | 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": "e9748a39a480eedc"} |
vcr_00439870a36c | 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": [-19, -11, -3, 0, 2, 4, 12, 25, 27, 34], "target": -15}, "output": [0, 5]}, {"input": {"nums": [-13, -9, 14, 24], "target": 5}, "output": [1, 2]}, {"input": {"nums": [-14, 17, 18, 19, 30], "target": 16}, "output": [0, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "c6ab468768a008dd"} |
vcr_bde300663055 | 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": ["ac", "cbbc", "bcaa", "cb", "cba", "abcc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "aba", "baa", "aab", "baa"]}, "output": [5]}, {"input": {"strs": ["baa", "aab", "bba", "bba", "bab", "bab"]}, "output": [2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "40e4e3c6512b9aa5"} |
vcr_1113e30c4589 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [10, -5, 14, -8, 8, 1, -13, 2, 6, 0, 4]}, "output": 20}, {"input": {"nums": [-4, 0, -9, 6, -3, 13, -10, -9, -6, -9]}, "output": 16}, {"input": {"nums": [-6, 1, 11, 7, 11, 6, 13, 8, 1, 2]}, "output": 60}] | 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": "cf4d71ef4f8f55d8"} |
vcr_eaa64f06171c | 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": 57107}, "output": 10}, {"input": {"n": 5857}, "output": 7}, {"input": {"n": 11165}, "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": "b6823ea96edd1d89"} |
vcr_dc22ce018240 | 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, -12, 21, 20, -13, -10, -15, 12, -6, 22, -7, -2, 12, 22, -16]}, "output": 56}, {"input": {"nums": [-11, -4, -7, -8, -8, 5, 14, 9, 11]}, "output": 39}, {"input": {"nums": [4, 2, 7, 10, -9, -1, -9]}, "output": 23}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "a46331c6cd9c8ea3"} |
vcr_32eeb005bead | 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": [0, -15, -7, 16], "k": 8}, "output": [0, -15, -7, 16]}, {"input": {"nums": [-1, -5, -7, 4, 8, 5, -1, -2], "k": 11}, "output": [5, -1, -2, -1, -5, -7, 4, 8]}, {"input": {"nums": [-5, 3, 6, 4, -10, 8], "k": 5}, "output": [3, 6, 4, -10, 8, -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": "0c15b21fee4bafdd"} |
vcr_e97e8f6c133f | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 5, 5, 6, 12, 12, 13, 13]}, "output": [2, 5, 6, 12, 13]}, {"input": {"nums": [1, 3, 5, 12]}, "output": [1, 3, 5, 12]}, {"input": {"nums": [2, 7, 9, 10]}, "output": [2, 7, 9, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "9b342bfa557ed6f2"} |
vcr_7eef466948a6 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 13, 22, 25], "target": 26}, "output": 4}, {"input": {"nums": [3, 8, 33], "target": 26}, "output": 2}, {"input": {"nums": [5, 6, 20, 36], "target": 22}, "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": "f423e43165b25cc1"} |
vcr_9aa5ed5a4010 | 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, 14, 7, -13, -18, -1, -16, 18]}, "output": 21}, {"input": {"nums": [10, -2, 7, -11, -2, -1, 1, 12, 2]}, "output": 16}, {"input": {"nums": [-1, 7, -11, -2, 14]}, "output": 14}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "d03e72f4817e5329"} |
vcr_6d381a28e9f6 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 5, 11, 21, 26, 43, 46, 49], "target": 0}, "output": 0}, {"input": {"nums": [12, 13, 16, 25, 32], "target": 30}, "output": 4}, {"input": {"nums": [5, 9, 14, 16, 19, 20, 21, 24, 30, 33, 38, 39], "target": 8}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "2443efb7afc1fcdf"} |
vcr_1c4bd8a8abe9 | 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": 200, "b": 161}, "output": 1}, {"input": {"a": 21, "b": 97}, "output": 1}, {"input": {"a": 133, "b": 90}, "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": "654e45cdddfccdff"} |
vcr_9e3781b7c341 | 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": 14911}, "output": 10}, {"input": {"n": 82}, "output": 3}, {"input": {"n": 13733}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "8cf064461f33358e"} |
vcr_2fb788a3c241 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, -22, 2, 6, 19], "target": -24}, "output": [0, 3]}, {"input": {"nums": [-13, -10, -8, -6, 22, 30, 35], "target": 12}, "output": [1, 4]}, {"input": {"nums": [-14, -1, 7, 20], "target": -7}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "e460b09f292c4bfd"} |
vcr_9b763d77373a | 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": [7, 8, 14, 1, -25, -17, 21, -21]}, "output": [7, 15, 29, 30, 5, -12, 9, -12]}, {"input": {"nums": [16, -9, -9]}, "output": [16, 7, -2]}, {"input": {"nums": [2, 15, 9]}, "output": [2, 17, 26]}] | 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": "d243cfe2a5da7b7e"} |
vcr_c6e830a72944 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["baca", "bbbb", "ccac", "bc", "acac"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["baa", "abb", "bab", "baa", "aab", "aab", "bba"]}, "output": [3, 4]}, {"input": {"strs": ["aab", "bab", "bbb", "aaa", "aab", "abb", "bab"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "5f1f94b19a868863"} |
vcr_e24459b4efab | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 1, 1, 8, 9, 12, 13, 15]}, "output": [1, 8, 9, 12, 13, 15]}, {"input": {"nums": [0, 3, 5, 10, 11, 12, 12]}, "output": [0, 3, 5, 10, 11, 12]}, {"input": {"nums": [2, 4, 6, 7, 9]}, "output": [2, 4, 6, 7, 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": "205af22337037830"} |
vcr_b9a221cf4ed1 | 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": 121, "b": 94}, "output": 1}, {"input": {"a": 86, "b": 126}, "output": 2}, {"input": {"a": 83, "b": 48}, "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": "c148f4987e19f5e4"} |
vcr_3d23a3a7a3f5 | 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": "ddeaa eadb aeaa eee ebabb ad"}, "output": "ad ebabb eee aeaa eadb ddeaa"}, {"input": {"s": "babb ab abb"}, "output": "abb ab babb"}, {"input": {"s": "aab a b abaa b"}, "output": "b abaa b a aab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "0748800ee74fb10f"} |
vcr_04cea29e3f4c | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [17, 13, -16, 1, 2, -18, -3], "k": 20}, "output": [13, -16, 1, 2, -18, -3, 17]}, {"input": {"nums": [-4, 10, 9, -10, -9, 6, -3, -9, -8, -3], "k": 10}, "output": [-4, 10, 9, -10, -9, 6, -3, -9, -8, -3]}, {"input": {"nums": [-7, 8, 10, -5, -2, 2], "k": 13}, "output": [2, -7, 8, 10, -5, -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": "99712eba83cba921"} |
vcr_35c6aa8adf31 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-11, -19, -15, -4, 3, -20, -17, 33, 15, 16, 30]}, "output": [-11, -30, -45, -49, -46, -66, -83, -50, -35, -19, 11]}, {"input": {"nums": [-2, -9, -5, 14, 13, 5, 1, -3, 12, 1]}, "output": [-2, -11, -16, -2, 11, 16, 17, 14, 26, 27]}, {"input": {"nums": [-4, -1, 13, 5, 3, -1, -5, 6, 11, 3]}, "output": ... | 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": "2794ad7823790ebe"} |
vcr_2694cdc79573 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [1, 0, 0, 4, 3, 0, 2, 3, 0, 0]}, "output": false}, {"input": {"nums": [0, 2, 3, 3, 0, 2, 2]}, "output": false}, {"input": {"nums": [3, 1, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "dabf651dbd86c19a"} |
vcr_43613f99f575 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [21, 24, 28, 16, 18]}, "output": 67}, {"input": {"nums": [16, 14, 13, 13, 12, 17, 1, 8, 17]}, "output": 63}, {"input": {"nums": [3, 7, 4, 20, 18, 2, 11, 13]}, "output": 42}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "acb9b4f287044137"} |
vcr_fbd271849a4d | 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": ["bc", "cca", "ab", "cbab", "aaa", "ccca", "ba", "cca", "ca"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["bab", "baa", "bab", "aba", "aaa", "abb", "aab"]}, "output": [1, 3, 3]}, {"input": {"strs": ["bba", "aaa", "baa", "bba", "bba"]}, "output": [1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "7de31e92eb8aabe6"} |
vcr_359b5af7ae7e | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 4, 4, 3, 0, 0, 1, 3, 4, 0]}, "output": true}, {"input": {"nums": [3, 1, 1, 3, 3, 3, 1]}, "output": true}, {"input": {"nums": [3, 3, 3, 2, 3, 1, 2, 3]}, "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": "e5c61a1465e2c79d"} |
vcr_b6b0e69d0942 | 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, 24, 7, 10, 7]}, "output": 41}, {"input": {"nums": [1, 14, 10, 2, 15, 16, 14, 10]}, "output": 43}, {"input": {"nums": [11, 8, 1, 16]}, "output": 27}] | 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": "a6108d437d28ac6b"} |
vcr_f58695d5d9a0 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 4, 1, 3, 3, 0, 0]}, "output": true}, {"input": {"nums": [1, 2, 3, 3]}, "output": true}, {"input": {"nums": [2, 0, 2, 1, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "ad2ffdddaf207b4d"} |
vcr_f4b6804f5d17 | 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": [8, 0, 24, 19, 10, 11, 13, 0, 14, 22, 8]}, "output": 77}, {"input": {"nums": [15, 19, 14, 15]}, "output": 34}, {"input": {"nums": [1, 7, 1, 16, 7, 9, 1, 1]}, "output": 33}] | 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": "24438f131304ece4"} |
vcr_f3c751489cb2 | 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, 6, 18, 28, 30], "target": 8}, "output": 2}, {"input": {"nums": [6, 14, 37], "target": 12}, "output": 1}, {"input": {"nums": [0, 23, 28, 36, 37], "target": 9}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "8fd7d1541fc96171"} |
vcr_51938a0d2132 | 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": "cabdbcc"}, "output": false}, {"input": {"s": "cb"}, "output": false}, {"input": {"s": "aacc"}, "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": "a1836ae8011c8ee0"} |
vcr_bedf16e4282a | 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": 227, "b": 74}, "output": 1}, {"input": {"a": 136, "b": 28}, "output": 4}, {"input": {"a": 122, "b": 99}, "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": "244f761a1e9a7b16"} |
vcr_f1f7b42bb3ec | 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": [3, 19, 56, 47, 23, 58, 8, 47]}, "output": 55}, {"input": {"prices": [6, 19, 25, 39]}, "output": 33}, {"input": {"prices": [6, 28, 20, 6, 27, 18, 29, 29, 1, 5, 20]}, "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": "fff7ec13344889aa"} |
vcr_e39496528cd3 | 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": [43, 7, 16, 16, 18, 28, 48, 43, 5, 10, 46, 8, 9, 28, 36, 20, 19]}, "output": true}, {"input": {"nums": [15, 16, 25, 6, 7, 8, 22, 19]}, "output": false}, {"input": {"nums": [10, 14, 10, 8, 15]}, "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": "24af81a389780cd9"} |
vcr_6679ba312a41 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["acc", "caac", "ac", "acb", "ac", "ba"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["baa", "abb", "abb", "bab"]}, "output": [1, 3]}, {"input": {"strs": ["aba", "aab", "bab", "bbb"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "456377ae55b0c207"} |
vcr_772da641864c | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [2, 9, 10, 12, 8, 0, 23, 8, 3, 10, 27, 13]}, "output": 74}, {"input": {"nums": [2, 9, 7, 19, 15, 16]}, "output": 44}, {"input": {"nums": [19, 8, 8]}, "output": 27}] | 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": "873c825f7b85a8d9"} |
vcr_d1d7bd98b385 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 1, 7, 9, 10, 22, 23, 24, 27, 37, 46, 49], "target": 2}, "output": 2}, {"input": {"nums": [18, 25, 26], "target": 5}, "output": 0}, {"input": {"nums": [8, 15, 17, 25], "target": 18}, "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": "6a36abdd6234ab92"} |
vcr_1e2a6fc3e763 | 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": ["ac", "ba", "ab", "ccc", "cbaa", "bb", "aab", "ca", "abc"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["aaa", "bab", "bba", "aab", "aab", "aaa"]}, "output": [2, 2, 2]}, {"input": {"strs": ["bbb", "bbb", "bba", "aba", "aba", "bab"]}, "output": [2, 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": "f1045b18323ebaef"} |
vcr_ccb58f4493ff | hard | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "cabaabac"}, "output": true}, {"input": {"s": "bc"}, "output": false}, {"input": {"s": "bbccaac"}, "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": "55cabb07a4309d3c"} |
vcr_733395f6a4ce | 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": [11, 16, 9, 2]}, "output": 20}, {"input": {"nums": [16, 12, 13, 0, 3]}, "output": 32}, {"input": {"nums": [18, 10, 0, 20, 10, 3, 5, 7, 16]}, "output": 59}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "fffdd77d3dfac84d"} |
vcr_705ea01e5b8f | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 1, 4, 10, 12, 18, 21, 24, 38, 39, 42, 43], "target": 42}, "output": 10}, {"input": {"nums": [0, 4, 7, 10, 11, 15, 18, 25, 30, 38], "target": 39}, "output": 10}, {"input": {"nums": [1, 5, 8, 16, 22, 37], "target": 21}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "55cddbf38a86a0c5"} |
vcr_aad8a2d2ec34 | 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": ["ccb", "bbb", "ccbc", "cc", "baab", "acaa", "abab"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "baa", "abb"]}, "output": [1, 2]}, {"input": {"strs": ["bbb", "bab", "aab", "bba", "aab", "abb", "abb"]}, "output": [1, 2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "e55be8803bf370ba"} |
vcr_24206c3be3b1 | 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": [7, 20, 19, 29, 36, 45, 25, 49, 38, 4, 33, 35, 5, 14, 10, 49]}, "output": true}, {"input": {"nums": [25, 5, 17, 19, 0, 3]}, "output": false}, {"input": {"nums": [15, 16, 0, 25]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "d7f59554059f1dcb"} |
vcr_3218f04d9696 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [9, 11, 12, 15, 19, 23, 30, 35, 37, 42, 44, 45, 48], "target": 29}, "output": 6}, {"input": {"nums": [4, 6, 13, 14, 20, 29, 35], "target": 32}, "output": 6}, {"input": {"nums": [0, 2, 12, 17, 22, 23, 33], "target": 14}, "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": "24eeea35b763c0b6"} |
vcr_c0fd8004da5e | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 2, 3, 17, 26, 29, 32, 34], "target": 1}, "output": 1}, {"input": {"nums": [1, 2, 4, 6, 7, 12, 16, 18, 23, 26, 28, 36], "target": 37}, "output": 12}, {"input": {"nums": [3, 6, 17, 22, 23, 28, 35], "target": 35}, "output": 6}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "3abbb8cd0f4e7b95"} |
vcr_0c341d2c880c | 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": "edba dcbcdd dcb a deb daca cebd"}, "output": "cebd daca deb a dcb dcbcdd edba"}, {"input": {"s": "bbbb aabb bb babb"}, "output": "babb bb aabb bbbb"}, {"input": {"s": "aab abba bbb bb"}, "output": "bb bbb abba aab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "f4c18e2f990c789f"} |
vcr_805a8df3458f | 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": "abcaaaabbc"}, "output": 6}, {"input": {"s": "ccbcacaac"}, "output": 7}, {"input": {"s": "aaacbaabcc"}, "output": 6}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "56e18c55a87a354e"} |
vcr_2e4b7d71b1f2 | medium | binary_search | Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing. | def first_bad_version(n: int, bad: int) -> int: | Lower-bound binary search on the is_bad predicate. | def first_bad_version(n: int, bad: int) -> int:
def is_bad(v):
return v >= bad
lo, hi = 1, n
while lo < hi:
mid = (lo + hi) // 2
if is_bad(mid):
hi = mid
else:
lo = mid + 1
return lo
| [{"input": {"n": 40, "bad": 1}, "output": 1}, {"input": {"n": 35, "bad": 10}, "output": 10}, {"input": {"n": 5, "bad": 2}, "output": 2}] | true | python | 73591920a83cf64d | ### Problem
Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.
### Reasoning
Lower-bound binary search on the is_bad predicate.
### Solution
```python
def first_bad_version(n: int, bad: int) -> int:
def is_bad(v):
return v >= bad
lo, hi = 1, ... | {"v": "ok", "content_hash": "18ee95b0607f6647"} |
vcr_fe03fe6ff28b | 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": 179, "b": 223}, "output": 1}, {"input": {"a": 50, "b": 132}, "output": 2}, {"input": {"a": 176, "b": 51}, "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": "1a017a52f78e4872"} |
vcr_0aeb2cf1cade | 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": 19875}, "output": 8}, {"input": {"n": 6776}, "output": 7}, {"input": {"n": 11314}, "output": 6}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "307e2c7341b225fc"} |
vcr_d1c4957c5a2d | 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": [-14, -8, -7, -4, 2, 19, 21, 23, 29, 31, 46, 48], "target": 23}, "output": [1, 9]}, {"input": {"nums": [-18, -15, -12, -2, -1, 3, 4, 19, 32], "target": -16}, "output": [1, 4]}, {"input": {"nums": [-18, 22, 29, 38], "target": 51}, "output": [1, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "5a4373ed0a1615d6"} |
vcr_00d78f4e016c | 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": "e abb aaceae deeaad aebac"}, "output": "aebac deeaad aaceae abb e"}, {"input": {"s": "aa bbb ab a"}, "output": "a ab bbb aa"}, {"input": {"s": "b b bba abb abab"}, "output": "abab abb bba b 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": "bd95c5dafdd6072f"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.