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_0d2c9cfe9e34 | 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": [-16, 2, 18, 10, 16, 18, -11, 11, 0, -1, -3, -11, -14, -1]}, "output": 64}, {"input": {"nums": [4, -7, 14, 10, -9, 0, -5, 1, -4, -2, 5]}, "output": 24}, {"input": {"nums": [-7, -11, -8, 2, -7, 7, 8]}, "output": 15}] | 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": "92867c655fee5607"} |
vcr_620de3f715be | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "bbbdccdccdbbb"}, "output": true}, {"input": {"s": "abbc"}, "output": false}, {"input": {"s": "bab"}, "output": true}] | 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": "dd2408511400e3da"} |
vcr_faadf62b5388 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-10, -12, -13, 20, 1, -15, 9, -14, 16, 6, 2], "k": 12}, "output": [2, -10, -12, -13, 20, 1, -15, 9, -14, 16, 6]}, {"input": {"nums": [5, -10, 2, 3, -6, 9, -2, 0, 5, -4], "k": 9}, "output": [-10, 2, 3, -6, 9, -2, 0, 5, -4, 5]}, {"input": {"nums": [3, 8, 3, -10, 6, -3, 7], "k": 0}, "output": [3, 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": "fd98fdae23e2b67b"} |
vcr_e40338e42cbe | 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": [50, 56, 31, 36, -30, -33, -28, 45, 62, 33, 17, -9, 37], "target": 28}, "output": [1, 6]}, {"input": {"nums": [37, -28, -18, 12, 33, -9, 40], "target": 77}, "output": [0, 6]}, {"input": {"nums": [27, 35, 6, 29], "target": 41}, "output": [1, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "ef8c73fc1561530a"} |
vcr_c1e19c109580 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [19, 15, -10, 9, -9, 7, 0, 11, -9, 14, 5, -12]}, "output": 52}, {"input": {"nums": [14, 4, 14, -8, -12, 12, 3, -7, -5]}, "output": 32}, {"input": {"nums": [1, 11, -1, -3, 13, 14]}, "output": 35}] | 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": "a1a176896f3e1c9c"} |
vcr_8cb3f9637c2b | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "aaaaaabbaaaaaa"}, "output": true}, {"input": {"s": "caabcb"}, "output": false}, {"input": {"s": "cb"}, "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": "7a7c09b80dfceab3"} |
vcr_babb141debce | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [4, 5, 2, 3, 0, 4, 2, 6]}, "output": [0, 0, 0, 0, 5760, 0, 0, 0]}, {"input": {"nums": [3, 3, 1, 1]}, "output": [3, 3, 9, 9]}, {"input": {"nums": [2, 1, 1, 3, 5]}, "output": [15, 30, 30, 10, 6]}] | 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": "70fe3ff1dd13d53b"} |
vcr_b500c5c8a2e8 | 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": ["baa", "bcc", "cba", "bca", "bb", "ca", "abca"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["abb", "aaa", "baa", "baa", "bba"]}, "output": [1, 2, 2]}, {"input": {"strs": ["bba", "bbb", "aab", "aba"]}, "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": "a650b9813f9619f2"} |
vcr_9e634321689a | 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": [32, 12, 14, -9, 25, -8, 19, -8, 35]}, "output": [32, 44, 58, 49, 74, 66, 85, 77, 112]}, {"input": {"nums": [17, 2, -5]}, "output": [17, 19, 14]}, {"input": {"nums": [7, -1, 8]}, "output": [7, 6, 14]}] | 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": "e64f241b34cfec5e"} |
vcr_63626a0b4484 | 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": 4914}, "output": 6}, {"input": {"n": 13360}, "output": 5}, {"input": {"n": 4880}, "output": 4}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "a9ef556a63e3b11c"} |
vcr_d03a61e4492e | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [16, -14, -2, -16, 9], "k": 7}, "output": [-16, 9, 16, -14, -2]}, {"input": {"nums": [6, 4, -1, 4], "k": 18}, "output": [-1, 4, 6, 4]}, {"input": {"nums": [10, 5, 6, -6], "k": 9}, "output": [-6, 10, 5, 6]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "aa525ef60b0b0e8d"} |
vcr_f66aa3f8d79b | 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": [0, 11, 32, 36, 38, 42, 45], "target": 36}, "output": [0, 3]}, {"input": {"nums": [5, 19, 20, 29], "target": 48}, "output": [1, 3]}, {"input": {"nums": [-19, -18, -14, -9, -4, -2, 6, 17, 22, 31, 36], "target": -27}, "output": [1, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "88bf9332fd72198d"} |
vcr_74e0b0abb307 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 5, 9, 10, 18, 22, 24, 27, 30, 32, 33, 35, 43], "target": 41}, "output": 12}, {"input": {"nums": [1, 3, 4, 6, 22, 24], "target": 27}, "output": 6}, {"input": {"nums": [7, 11, 16, 23], "target": 5}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "22d4565a5ecf7115"} |
vcr_19d9f608ecb4 | 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": [28, 22, 4, 3]}, "output": 32}, {"input": {"nums": [5, 7, 4, 9]}, "output": 16}, {"input": {"nums": [7, 13]}, "output": 13}] | 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": "7b675582518b25a6"} |
vcr_57edcbb9b5a8 | 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": [16, 58, 7, 12, 52]}, "output": 45}, {"input": {"prices": [26, 3, 31, 6]}, "output": 28}, {"input": {"prices": [15, 31, 34, 26, 5, 39, 33, 20]}, "output": 34}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "ae4371b7eddd5952"} |
vcr_58e794a89366 | 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": [16, 37, 26, 9, 28, 44, 28, 14, 9, 25]}, "output": 35}, {"input": {"prices": [19, 28, 12, 39, 20, 38, 39, 27, 17, 3, 24]}, "output": 27}, {"input": {"prices": [32, 32, 31]}, "output": 0}] | 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": "e15a514d9da12b36"} |
vcr_c740a42c6c6b | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 6, 16, 17, 22, 26, 30, 31, 32, 41, 48], "target": 0}, "output": 0}, {"input": {"nums": [14, 16, 17, 19, 29, 32], "target": 38}, "output": 6}, {"input": {"nums": [9, 10, 19, 29, 31, 35], "target": 0}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "576f290d896b4595"} |
vcr_ad1bf82bbe6d | 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": [162, 83, 150, 85, 72, 36, 87, 71]}, "output": false}, {"input": {"nums": [8, 25, 13, 1, 6]}, "output": false}, {"input": {"nums": [5, 9, 6, 18, 3, 8, 9, 21, 17]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "66e04f92d8a8d20d"} |
vcr_7f654b4089ef | 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": [4, 2, -7, -6, 21, 7, -11, 17, 10, -11, 4]}, "output": 44}, {"input": {"nums": [-10, -12, 5, -5, -8, 9, -2, 5, -2, 1]}, "output": 12}, {"input": {"nums": [-6, -7, 11, 2, 0, 8, 13]}, "output": 34}] | 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": "003eaf7b4092f28e"} |
vcr_5b32b75fce34 | 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": "aeaabd dcceeb eeb ab"}, "output": "ab eeb dcceeb aeaabd"}, {"input": {"s": "baab aba ab aaba bba"}, "output": "bba aaba ab aba baab"}, {"input": {"s": "baa aa bbb bb"}, "output": "bb bbb aa baa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "bad5b2ac2907231f"} |
vcr_70238ca3021a | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [32, 25, 75, 19, -18], "target": 44}, "output": [1, 3]}, {"input": {"nums": [-7, -30, 39, -14, -19, -25, 13, 25, 48], "target": -33}, "output": [3, 4]}, {"input": {"nums": [-23, 41, -10, -11], "target": -33}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "40a9fcf421f636e0"} |
vcr_7636f2fc78d9 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 5, 8, 10, 14, 15, 18, 41, 42], "target": 34}, "output": 7}, {"input": {"nums": [8, 10, 12, 21], "target": 28}, "output": 4}, {"input": {"nums": [2, 4, 5, 6, 8, 11, 15, 17, 18, 24, 34, 38], "target": 5}, "output": 2}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "263180e0147e63c3"} |
vcr_75e87a1a5fc4 | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [19, -16, 12, -19, 27, -24, 39, 7, 68, 21, -6, 41, -25, 33], "target": -13}, "output": [2, 12]}, {"input": {"nums": [16, 14, 33, -28, -22, -30, 13, 12, 19, -8], "target": -10}, "output": [4, 7]}, {"input": {"nums": [28, 43, -28, 5, -2, 12, 29, -13, 10, 8, 20], "target": 25}, "output": [3, 10]}] | 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": "1cc5264d7c4315d6"} |
vcr_0cc4e573ee5c | 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": "ecbbca eeeadd"}, "output": "eeeadd ecbbca"}, {"input": {"s": "aa bbab"}, "output": "bbab aa"}, {"input": {"s": "ba bbbb bb bbb"}, "output": "bbb bb bbbb ba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "710a069a27fe9e5d"} |
vcr_86b01747c3ee | 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, 2, 2, 4, 1, 2, 3, 0, 0]}, "output": true}, {"input": {"nums": [3, 1, 0, 0, 3, 3, 1, 0, 1]}, "output": false}, {"input": {"nums": [0, 1, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "61fce94021aed120"} |
vcr_59c8f3d4e78d | 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": [15, 17, 22, 5]}, "output": 37}, {"input": {"nums": [10, 14, 3, 1, 16, 11, 19, 14]}, "output": 49}, {"input": {"nums": [2, 7, 15, 15, 1]}, "output": 22}] | 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": "45d2185a06f3152e"} |
vcr_cc059799b440 | 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, 0, 1, 2, 3, 1, 4, 0]}, "output": false}, {"input": {"nums": [2, 2, 0, 3, 0, 1, 0, 0, 1]}, "output": false}, {"input": {"nums": [2, 0, 2, 0, 0, 2, 0, 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": "bf9f2d4967a2a00b"} |
vcr_b6dcbcec5054 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 4, 1, 3, 1, 2, 4, 0]}, "output": false}, {"input": {"nums": [1, 2, 2, 1, 1, 3, 1]}, "output": true}, {"input": {"nums": [0, 2, 0, 1]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "e4716ee1e1238e3e"} |
vcr_388f6b2bdb9b | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [8, 13, 19, 45], "target": 29}, "output": 3}, {"input": {"nums": [5, 8, 9, 11, 20, 25, 30, 31, 34, 36, 37], "target": 6}, "output": 1}, {"input": {"nums": [2, 7, 12], "target": 28}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "534645fc7b75b7fa"} |
vcr_e297b00c6eff | 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, -9, 3, 17, 20, 18, -10, 3, 4, -5, -10], "k": 10}, "output": [-9, 3, 17, 20, 18, -10, 3, 4, -5, -10, 6]}, {"input": {"nums": [-10, 3, 0, 9, 7, -5, -7, 9], "k": 12}, "output": [7, -5, -7, 9, -10, 3, 0, 9]}, {"input": {"nums": [-9, 5, -2, -2], "k": 10}, "output": [-2, -2, -9, 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": "c09ccf2578f942fe"} |
vcr_0c6b5cc94a13 | 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": "babacaabbb"}, "output": 7}, {"input": {"s": "acbcacab"}, "output": 8}, {"input": {"s": "baaacaccbbaa"}, "output": 7}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "07760dc9309bcdd7"} |
vcr_c80dec0f742f | 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": [3, 6, 3, 7, 2, 2, 3]}, "output": [1512, 756, 1512, 648, 2268, 2268, 1512]}, {"input": {"nums": [2, 4, 4]}, "output": [16, 8, 8]}, {"input": {"nums": [5, 2, 4, 4, 5, 4, 3]}, "output": [1920, 4800, 2400, 2400, 1920, 2400, 3200]}] | 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": "6e8185d22de24067"} |
vcr_ddebf0c3f489 | 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": [-22, 14, 17, 20, 24, 37, 44], "target": 68}, "output": [4, 6]}, {"input": {"nums": [-11, -7, 23, 26, 34], "target": 15}, "output": [0, 3]}, {"input": {"nums": [-7, -3, -1, 0, 1, 15, 26, 34, 36], "target": 35}, "output": [2, 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": "189f184f3059a335"} |
vcr_51fe471b4c81 | 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": 65057}, "output": 9}, {"input": {"n": 13971}, "output": 8}, {"input": {"n": 9029}, "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": "ad5d54458d901c2b"} |
vcr_88b69796610c | 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": [-5, 8, 18, 6, 6, 1, 10, -2, -13, 22, 18, 14, -12, -17]}, "output": 88}, {"input": {"nums": [7, 9, -10, 13, 12, 4, -3, -12, -10, -10, -10]}, "output": 35}, {"input": {"nums": [-1, -2, -3, -10, 1, 9, -10, -5, 12]}, "output": 12}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "ea334c470fa6c774"} |
vcr_ab8adc1df5ff | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 3, 4, 4, 8, 8, 9, 10, 10, 10, 11, 12, 12]}, "output": [0, 3, 4, 8, 9, 10, 11, 12]}, {"input": {"nums": [0, 6, 10, 12, 12]}, "output": [0, 6, 10, 12]}, {"input": {"nums": [3, 7, 7, 10, 12]}, "output": [3, 7, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "a7fc79d876301d2a"} |
vcr_5a8b45e6da21 | 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": "caacbaaaacbba"}, "output": 8}, {"input": {"s": "abacbbabab"}, "output": 9}, {"input": {"s": "ccca"}, "output": 2}] | 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": "e7aa02c093b5c0a6"} |
vcr_7263a40516a9 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [13, -9, -9, 20, -4, 9, -1, 2, 5, -16, -17, 15, -14, 6, 22]}, "output": 31}, {"input": {"nums": [1, -6, 4, -10, -10, 1, -2, -10, 1, 8]}, "output": 9}, {"input": {"nums": [-12, -11, 3, 2, -10, -12, 3, -3, 8, -4]}, "output": 8}] | 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": "57f1afab052117c1"} |
vcr_9f9d7acbc54f | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [24, 38, 7, 38, 9, 37, 13, 16, 37, 7, 13, 9, 16, 67, 24]}, "output": 67}, {"input": {"nums": [27, 14, 54, 27, 7, 7, 14]}, "output": 54}, {"input": {"nums": [59, 16, 3, 21, 8, 19, 21, 16, 8, 10, 3, 10, 19]}, "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": "8a7768a9834df873"} |
vcr_df5e0018f0b5 | 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": [14, 29, 0, 25, 14, 28]}, "output": 82}, {"input": {"nums": [1, 14, 16, 10, 14, 7]}, "output": 31}, {"input": {"nums": [16, 8, 9, 12, 19, 3, 14, 14, 6]}, "output": 64}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "2344ae2f7b5a13f7"} |
vcr_79d3caccb316 | 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": "abead ba eeaaac bee"}, "output": "bee eeaaac ba abead"}, {"input": {"s": "abab b bbb baab aaa"}, "output": "aaa baab bbb b abab"}, {"input": {"s": "aa bba aab bbab a"}, "output": "a bbab aab bba aa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "4d8ccf39d820a64c"} |
vcr_a8e6b05db60a | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [30, 21, 25, 19, 3]}, "output": 58}, {"input": {"nums": [18, 6, 3, 14, 14, 6]}, "output": 38}, {"input": {"nums": [20, 3, 0, 19, 18, 8, 3]}, "output": 47}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "af68f3233feac7a5"} |
vcr_d39a9e202125 | 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": 26, "b": 38}, "output": 2}, {"input": {"a": 5, "b": 103}, "output": 1}, {"input": {"a": 70, "b": 109}, "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": "28ab234302814c7d"} |
vcr_ee7885a92030 | 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": 204, "b": 62}, "output": 2}, {"input": {"a": 125, "b": 60}, "output": 5}, {"input": {"a": 84, "b": 96}, "output": 12}] | 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": "13aff0c3e9db0aaf"} |
vcr_318049cdce4e | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [18, 3, -13, 18, 32, 28, -19, -22]}, "output": [18, 21, 8, 26, 58, 86, 67, 45]}, {"input": {"nums": [4, 6, 18, 18, 16, -9]}, "output": [4, 10, 28, 46, 62, 53]}, {"input": {"nums": [-8, 0, -5, -5, -9, 15]}, "output": [-8, -8, -13, -18, -27, -12]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "1fc84f8cedf9cef5"} |
vcr_d5d0fbc5c199 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 0, 2, 1, 2, 1, 4, 0]}, "output": true}, {"input": {"nums": [1, 3, 2]}, "output": true}, {"input": {"nums": [0, 0, 2, 3, 3, 3, 0, 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": "f21fa5da997a28c8"} |
vcr_1cfdf1f6fa33 | 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": "ccbbbaabbbca"}, "output": 6}, {"input": {"s": "bab"}, "output": 3}, {"input": {"s": "bacb"}, "output": 4}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "3d791ac772368ca5"} |
vcr_dfda451592fe | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [29, -20, 15, -5, -11, -24, -7, -16, -11, 15, -22, 7]}, "output": [29, 9, 24, 19, 8, -16, -23, -39, -50, -35, -57, -50]}, {"input": {"nums": [18, -6, 14]}, "output": [18, 12, 26]}, {"input": {"nums": [0, 6, 14, 8, -11, 0, 8, 13]}, "output": [0, 6, 20, 28, 17, 17, 25, 38]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "cb39be559a8d2449"} |
vcr_8086e3930e94 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [20, -3, 11, 21, -12, -7, 0, 2, 29]}, "output": [20, 17, 28, 49, 37, 30, 30, 32, 61]}, {"input": {"nums": [-4, -3, 6, 13, 15, 12, -12, 17]}, "output": [-4, -7, -1, 12, 27, 39, 27, 44]}, {"input": {"nums": [-2, -1, 14, -11, 1, 3]}, "output": [-2, -3, 11, 0, 1, 4]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "498b759a60068556"} |
vcr_9fb37762afa4 | 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, 3, 5, 7, 7, 9, 10, 11, 12, 12]}, "output": [2, 3, 5, 7, 9, 10, 11, 12]}, {"input": {"nums": [1, 2, 4, 4, 9, 11]}, "output": [1, 2, 4, 9, 11]}, {"input": {"nums": [0, 1, 2, 2, 3, 4, 6, 8, 10, 12]}, "output": [0, 1, 2, 3, 4, 6, 8, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "171916c661cdc063"} |
vcr_867a7d788b85 | 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": [-25, -21, -19, 1, 2, 6, 20, 23], "target": 3}, "output": [3, 4]}, {"input": {"nums": [5, 13, 25, 32], "target": 38}, "output": [1, 2]}, {"input": {"nums": [-15, -10, -7, 8, 16, 28, 32], "target": 48}, "output": [4, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "9fafaae8a2e1ab85"} |
vcr_652966bc0240 | 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": [3, 2, 5, 1, 2, 3, 2]}, "output": [120, 180, 72, 360, 180, 120, 180]}, {"input": {"nums": [5, 5, 4]}, "output": [20, 20, 25]}, {"input": {"nums": [3, 1, 5, 2, 3]}, "output": [30, 90, 18, 45, 30]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "89ec59785ac44f5f"} |
vcr_6f2d16083aca | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [11, -14, 3, 15, -4], "k": 11}, "output": [-4, 11, -14, 3, 15]}, {"input": {"nums": [-9, -8, -10, -9, 6], "k": 6}, "output": [6, -9, -8, -10, -9]}, {"input": {"nums": [-8, -7, 4, -8], "k": 17}, "output": [-8, -8, -7, 4]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "52075c41c7ab095b"} |
vcr_ab269b1c95a9 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [4, 5, 5, 6, 8, 9, 11, 11, 12, 13, 13, 14, 15, 15]}, "output": [4, 5, 6, 8, 9, 11, 12, 13, 14, 15]}, {"input": {"nums": [0, 1, 2, 4, 5, 7, 9, 11, 11]}, "output": [0, 1, 2, 4, 5, 7, 9, 11]}, {"input": {"nums": [0, 1, 7, 11]}, "output": [0, 1, 7, 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": "5339aaf2efa707a6"} |
vcr_1ae5b1b5c4fe | 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": [26, 8, 66, 27, 8, 6, 26, 27, 6]}, "output": 66}, {"input": {"nums": [27, 24, 12, 24, 10, 12, 42, 22, 22, 17, 10, 27, 17]}, "output": 42}, {"input": {"nums": [19, 15, 10, 4, 15, 4, 23, 50, 10, 23, 19]}, "output": 50}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "893937262d361dc0"} |
vcr_f4ed6ddf1e2d | 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": 18, "b": 97}, "output": 1}, {"input": {"a": 73, "b": 28}, "output": 1}, {"input": {"a": 154, "b": 14}, "output": 14}] | 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": "4bd079fa313661b4"} |
vcr_72df96a495b8 | 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": 62803}, "output": 10}, {"input": {"n": 4766}, "output": 7}, {"input": {"n": 748}, "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": "eb9769005a62a164"} |
vcr_4e76df4347b8 | 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": [9, 54, 16, 57, 36, 2, 47, 4, 24, 3, 25, 37, 16, 9, 45, 50]}, "output": 48}, {"input": {"prices": [29, 8, 17, 14, 6, 39, 4, 3, 30, 9, 13]}, "output": 33}, {"input": {"prices": [34, 38, 24, 30]}, "output": 6}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "87215eff8561c193"} |
vcr_20f1bd1fda53 | 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": [13, 5, 7, 7, 2, 2, 13, 5, 50]}, "output": 50}, {"input": {"nums": [29, 20, 29, 2, 2, 47, 12, 11, 5, 11, 20, 5, 12]}, "output": 47}, {"input": {"nums": [29, 10, 6, 9, 12, 10, 29, 26, 6, 9, 26, 43, 12]}, "output": 43}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "b5c16581fbed078b"} |
vcr_efe0c1494f00 | 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": [-2, 13, 23, 5, -2, -15, 13, -20]}, "output": [-2, 11, 34, 39, 37, 22, 35, 15]}, {"input": {"nums": [16, 2, 4]}, "output": [16, 18, 22]}, {"input": {"nums": [-7, 15]}, "output": [-7, 8]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "3708720f076f1d1b"} |
vcr_b60f4082238d | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [12, 8, 18, -19, 10], "k": 5}, "output": [12, 8, 18, -19, 10]}, {"input": {"nums": [-2, -2, -9, 0, 1, -6], "k": 16}, "output": [-9, 0, 1, -6, -2, -2]}, {"input": {"nums": [-4, 3, 10, 4, -6, -6, 1, -7], "k": 19}, "output": [-6, 1, -7, -4, 3, 10, 4, -6]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "15250a3626eeca63"} |
vcr_682c031755ab | 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": [30, 7, 24, 53, 42, 54, 33, 13, 12, 3, 21]}, "output": 47}, {"input": {"prices": [10, 6, 16, 23, 37, 37, 23, 12, 3]}, "output": 31}, {"input": {"prices": [2, 34, 32, 36]}, "output": 34}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "97e450e0676315be"} |
vcr_b9411205f24c | 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": 34327}, "output": 7}, {"input": {"n": 324}, "output": 3}, {"input": {"n": 15313}, "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": "65adde9bb31872e2"} |
vcr_5cd6ca131d4f | 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": [174, 193, 52, 149, 84, 23, 32, 67, 146, 122, 47, 50, 160, 196, 116, 126]}, "output": false}, {"input": {"nums": [1, 11, 2, 24, 16, 21, 2, 19, 3, 2]}, "output": true}, {"input": {"nums": [23, 12, 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": "d576d33a9984f2e9"} |
vcr_2eefe0f18d5a | 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": 29361}, "output": 8}, {"input": {"n": 8676}, "output": 6}, {"input": {"n": 10061}, "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": "69e0e7df05f4f229"} |
vcr_2063769af963 | 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": "babcabcababaa"}, "output": 12}, {"input": {"s": "aaaaaabbba"}, "output": 3}, {"input": {"s": "acbbba"}, "output": 4}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "8da9244bab52e129"} |
vcr_b5880df9961e | 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, 18, 12, 1, 30, 15, 11, 15]}, "output": 63}, {"input": {"nums": [18, 13, 15, 5, 9]}, "output": 42}, {"input": {"nums": [18, 14, 1, 12, 7]}, "output": 30}] | 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": "bf905f52655e64bf"} |
vcr_60e424d14f1f | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [74, 13, 31, -24, 45, 70, 46], "target": 87}, "output": [0, 1]}, {"input": {"nums": [-17, -3, -19, 6, 22, -6, 34, 21, 48, -2], "target": 82}, "output": [6, 8]}, {"input": {"nums": [-7, -30, 29, 18, 1, 5, 23, 42, 24, -27], "target": 29}, "output": [5, 8]}] | 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": "2006fec91fce6712"} |
vcr_83fccf5f9f3b | 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": [6, 8, 1, 35, 6, 11, 8, 58, 11, 35, 1]}, "output": 58}, {"input": {"nums": [27, 27, 12, 14, 12, 2, 2, 44, 3, 3, 14]}, "output": 44}, {"input": {"nums": [3, 7, 51, 7, 3, 10, 10]}, "output": 51}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "e61f265f97fe03ac"} |
vcr_259b3d6ee3e3 | 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": [29, 45, 9, 51, 36, 46, 26, 34, 33, 26]}, "output": 42}, {"input": {"prices": [38, 28, 18, 2, 20, 39, 11, 17, 11, 37, 17, 3]}, "output": 37}, {"input": {"prices": [29, 7, 4, 36, 26, 3, 35]}, "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": "3f2689defcd25691"} |
vcr_59f9543d31ec | 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, 9, 4, 12, 30, 13, 24, 1, 1, 22]}, "output": 91}, {"input": {"nums": [19, 20]}, "output": 20}, {"input": {"nums": [18, 5]}, "output": 18}] | 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": "0b4aab932d93d9e1"} |
vcr_08b9ce9b6ad3 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 8, 10, 11, 16, 19, 20, 33, 38, 42, 44, 47, 49], "target": 5}, "output": 1}, {"input": {"nums": [0, 1, 2, 13, 18, 23, 34], "target": 21}, "output": 5}, {"input": {"nums": [1, 3, 6, 26, 30], "target": 25}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "2894c6607236cc02"} |
vcr_bd51e6b6f2cc | 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": [3, 13, 4, 28, 10, 8, 20, 28, 6, 4, 23]}, "output": 100}, {"input": {"nums": [1, 6, 20, 5, 17, 19, 7, 0, 16, 18]}, "output": 63}, {"input": {"nums": [13, 20, 12, 10, 9, 12, 4, 11, 9]}, "output": 53}] | 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": "504aa35b3efb75ad"} |
vcr_fe73aa283bd0 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 7, 8, 19, 23, 27, 29, 31, 34, 38, 42, 43, 46], "target": 23}, "output": 4}, {"input": {"nums": [13, 28, 36], "target": 15}, "output": 1}, {"input": {"nums": [3, 10, 13, 17, 18, 19, 24, 26, 34, 35, 37], "target": 2}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "483eca3a3b274ef1"} |
vcr_d52a3d352287 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 2, 2, 4, 4, 4, 1, 0, 4, 0]}, "output": false}, {"input": {"nums": [0, 3, 3, 3, 1, 1, 2, 0, 0]}, "output": false}, {"input": {"nums": [2, 0, 0, 0, 3, 3, 3, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "394f04bcf31898d8"} |
vcr_bec9dfefa085 | 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": [8, 6, -9, -18, -6, -18, 18, 15]}, "output": 33}, {"input": {"nums": [-11, -9, 0, -3]}, "output": 0}, {"input": {"nums": [-11, -5, -8, 8, 8, 4, 10, -1, -8]}, "output": 30}] | 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": "c88da8fe84f6c034"} |
vcr_ad49d1fa3de6 | 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, 14, 27, 11]}, "output": 52}, {"input": {"nums": [3, 4, 16, 17, 10, 2, 18, 15, 4, 16]}, "output": 63}, {"input": {"nums": [2, 14, 9, 11, 18, 7]}, "output": 32}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "902efcff02fef9e5"} |
vcr_9ecaa9e6b2d1 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [4, 10, 10, 13, 14]}, "output": [4, 10, 13, 14]}, {"input": {"nums": [0, 5, 5, 6, 7, 7, 8, 9, 10]}, "output": [0, 5, 6, 7, 8, 9, 10]}, {"input": {"nums": [0, 0, 1, 5, 8, 9, 12]}, "output": [0, 1, 5, 8, 9, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "2f09844675d14865"} |
vcr_954ff453171a | 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": [-16, -1, 21, 11, -18, -12, -18, 5, 19, 15]}, "output": 39}, {"input": {"nums": [-10, 5, 14, 6, 1, -1, 0, -9, 7, -8, 2]}, "output": 26}, {"input": {"nums": [6, -11, -11, 12, 3]}, "output": 15}] | 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": "9c46dfe8640422f9"} |
vcr_4a6228ecee62 | 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": ["ab", "aacc", "baaa", "ac", "ac", "acc", "bbcc", "cbac", "bcbc"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["bba", "aba", "baa"]}, "output": [1, 2]}, {"input": {"strs": ["bba", "aaa", "aab", "bab", "bba", "bab"]}, "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": "501030ebf6f549ad"} |
vcr_9bdb4d36b6ab | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 4, 4, 5, 7, 11, 13, 14]}, "output": [0, 1, 4, 5, 7, 11, 13, 14]}, {"input": {"nums": [2, 2, 8]}, "output": [2, 8]}, {"input": {"nums": [1, 1, 1, 5, 6, 8, 12, 12]}, "output": [1, 5, 6, 8, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "ebb01ae9f3bfad0b"} |
vcr_a03002918763 | 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": 225, "b": 242}, "output": 1}, {"input": {"a": 36, "b": 83}, "output": 1}, {"input": {"a": 67, "b": 171}, "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": "c289bcb993e233ea"} |
vcr_f0ce84f67f9f | hard | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-21, 37, 47, -25, -27, -18, 59, 7, 3, 73, 40], "target": 80}, "output": [7, 9]}, {"input": {"nums": [41, -3, 40, 17, -10, 32, 37, 44, 42, -1], "target": 79}, "output": [6, 8]}, {"input": {"nums": [45, -25, 26, -16, 4, 44, 25, 6], "target": 49}, "output": [0, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "1d67d3589d8cc811"} |
vcr_236637b51023 | 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": "ceedcc ba d cbded dcb cbb"}, "output": "cbb dcb cbded d ba ceedcc"}, {"input": {"s": "b ba bbb a"}, "output": "a bbb ba b"}, {"input": {"s": "aab aab baa bab bb"}, "output": "bb bab baa aab 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": "efe6637e100afc1b"} |
vcr_6a793dffa4cc | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [53, 15, 62, -9], "target": 53}, "output": [2, 3]}, {"input": {"nums": [15, -6, 26, 27, 49, 37, -25, 19], "target": 12}, "output": [5, 6]}, {"input": {"nums": [-16, 16, -12, 10, 29, -30, -6, 22, 18, 26], "target": 28}, "output": [3, 8]}] | 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": "14f05a9d301a95f3"} |
vcr_d2707acfe9d0 | 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": "ababbbaaabb"}, "output": 6}, {"input": {"s": "aac"}, "output": 2}, {"input": {"s": "bbbcbc"}, "output": 4}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "bcb1d58477e69072"} |
vcr_7e0eaad31585 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [4, -23, -16, 30, 30, -7, -11, 31]}, "output": [4, -19, -35, -5, 25, 18, 7, 38]}, {"input": {"nums": [7, -1, -11, -12, -11, -12]}, "output": [7, 6, -5, -17, -28, -40]}, {"input": {"nums": [17, -1, -4, 7, -9, 14, 3, 12, 13]}, "output": [17, 16, 12, 19, 10, 24, 27, 39, 52]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "aa8a71a1a1b296d2"} |
vcr_3df25b1ac5bc | 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, 10, 15, 24, 29, 31, 34, 35, 44], "target": 7}, "output": 0}, {"input": {"nums": [0, 1, 7, 14, 17, 22, 28, 29, 35], "target": 15}, "output": 4}, {"input": {"nums": [6, 7, 12, 13, 16, 26, 27, 28, 30, 34, 38], "target": 22}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "a0e7226b815e30fd"} |
vcr_84e451996b49 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [4, 4, 6, 3, 3]}, "output": [216, 216, 144, 288, 288]}, {"input": {"nums": [4, 4, 5, 4, 4, 5, 5]}, "output": [8000, 8000, 6400, 8000, 8000, 6400, 6400]}, {"input": {"nums": [1, 5, 4, 4, 4, 3, 3]}, "output": [2880, 576, 720, 720, 720, 960, 960]}] | 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": "c4c97f5fa94054c4"} |
vcr_1997db6990ca | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "d cebe bcb"}, "output": "bcb cebe d"}, {"input": {"s": "bb aab"}, "output": "aab bb"}, {"input": {"s": "a baab abb aba bbb"}, "output": "bbb aba abb baab a"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "a44eaa2e62fc0be8"} |
vcr_16eda121a6d9 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["cca", "bcab", "ca", "bb", "acb", "bcca"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "baa", "bab"]}, "output": [1, 2]}, {"input": {"strs": ["aaa", "abb", "bab", "aba", "baa"]}, "output": [1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "c65120e91fcc232b"} |
vcr_32ceb334b1ec | easy | hashmap | Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. | def two_sum(nums: list[int], target: int) -> list[int]: | 1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space O(n). | def two_sum(nums: list[int], target: int) -> list[int]:
seen = {}
for i, x in enumerate(nums):
need = target - x
if need in seen:
a, b = seen[need], i
return [a, b] if a < b else [b, a]
seen[x] = i
return []
| [{"input": {"nums": [-2, -13, 44, -6, -31, 10, 75], "target": 54}, "output": [2, 5]}, {"input": {"nums": [27, 9, 31, 42, -19], "target": 36}, "output": [0, 1]}, {"input": {"nums": [3, 29, -30, -12, -25, 47, 18], "target": 4}, "output": [1, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "13e99c9e798f62da"} |
vcr_479a1ed0be6a | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-1, 12, -2, -11, -12, -2, 21, -7, 9, 1, 10, -11, -9, -17]}, "output": 34}, {"input": {"nums": [1, -11, 1, 4, 12, -8, 1]}, "output": 17}, {"input": {"nums": [-10, -2, 1]}, "output": 1}] | 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": "3cafbf5f79f1a5be"} |
vcr_babe40424b9d | 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": "cadabddabd"}, "output": false}, {"input": {"s": "ba"}, "output": false}, {"input": {"s": "ca"}, "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": "5889db7dd71cc335"} |
vcr_101b97a9a792 | 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": 9786}, "output": 7}, {"input": {"n": 4427}, "output": 6}, {"input": {"n": 2228}, "output": 5}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "d865d3de222b8664"} |
vcr_ee08bbb621ca | 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": [26, 3, 5, 0, 6, 15]}, "output": 46}, {"input": {"nums": [1, 14, 3]}, "output": 14}, {"input": {"nums": [12, 12, 17, 10, 8, 19, 4, 16, 5, 6]}, "output": 70}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "c8979acf68626ff0"} |
vcr_62c4c99981dc | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-23, -20, -15, -11, -9, -7, 9, 29, 31, 37, 39], "target": -43}, "output": [0, 1]}, {"input": {"nums": [-20, -1, 4, 9, 31, 33], "target": -21}, "output": [0, 1]}, {"input": {"nums": [-18, -17, -11, -5, -3, 5, 21, 34], "target": 10}, "output": [2, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "f90b5485731b1190"} |
vcr_2d23f9e76334 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -5, -4, 7, 13, 22, 43], "target": 39}, "output": [2, 6]}, {"input": {"nums": [-4, 0, 11, 12, 13, 21, 25, 27, 29, 32, 36], "target": 44}, "output": [3, 9]}, {"input": {"nums": [-11, -4, 9, 11, 35, 39], "target": 0}, "output": [0, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "375e22dae060c833"} |
vcr_9221865a7b58 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 0, 4, 0, 2, 4, 0]}, "output": true}, {"input": {"nums": [2, 0, 3, 1, 3]}, "output": true}, {"input": {"nums": [2, 2, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "8a63b7a7c489c982"} |
vcr_2c7acf5df62c | 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": [26, 2, 22, 28, 10, 24, 1, 23, 28, 29, 13, 20]}, "output": 150}, {"input": {"nums": [18, 11, 5]}, "output": 23}, {"input": {"nums": [9, 16, 14, 19, 8, 13, 17, 13, 19]}, "output": 71}] | 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": "632736b8bb374e28"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.