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_e3ba79560d8e | 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, 16, -14, -6, 19, 7], "k": 11}, "output": [16, -14, -6, 19, 7, 10]}, {"input": {"nums": [5, 2, -7], "k": 9}, "output": [5, 2, -7]}, {"input": {"nums": [5, -7, -8, 5, -3], "k": 18}, "output": [-8, 5, -3, 5, -7]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "fbfa18b848d41831"} |
vcr_d53f40326651 | 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": 180, "b": 65}, "output": 5}, {"input": {"a": 90, "b": 126}, "output": 18}, {"input": {"a": 36, "b": 151}, "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": "223bbfb21b393abc"} |
vcr_c7c6d992d6f7 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [1, -19, 19, -5, 4, 18, -12, -20, 0], "k": 8}, "output": [-19, 19, -5, 4, 18, -12, -20, 0, 1]}, {"input": {"nums": [6, 6, -8, 1, 4, 6, -9], "k": 4}, "output": [1, 4, 6, -9, 6, 6, -8]}, {"input": {"nums": [-3, 6, 2, 5, 8, -8], "k": 0}, "output": [-3, 6, 2, 5, 8, -8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "6158f6b2d3d91a77"} |
vcr_0084b8c919c0 | 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": "cbccbbaababb"}, "output": 8}, {"input": {"s": "bbcbc"}, "output": 4}, {"input": {"s": "bcac"}, "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": "70b282079cd39498"} |
vcr_66b8476b51ff | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [3, 3, 4, 4, 6, 7, 8, 8, 9, 13]}, "output": [3, 4, 6, 7, 8, 9, 13]}, {"input": {"nums": [0, 0, 1, 3, 4, 7, 11]}, "output": [0, 1, 3, 4, 7, 11]}, {"input": {"nums": [0, 3, 3, 3, 6, 8, 9, 12]}, "output": [0, 3, 6, 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": "a64aa4c56cc9a4c6"} |
vcr_481510795d1d | 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": 35, "b": 82}, "output": 1}, {"input": {"a": 73, "b": 170}, "output": 1}, {"input": {"a": 170, "b": 3}, "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": "037d5f2286123acd"} |
vcr_44929fa53246 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [15, -11, -4, 6, 6, 22, -13, 1]}, "output": 34}, {"input": {"nums": [3, 4, 7, 14, 3, 12, -2]}, "output": 43}, {"input": {"nums": [6, 3, -9, 10, 0]}, "output": 10}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "2961c1099de77eac"} |
vcr_a929def9fbf3 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [22, 7, 26, 28, 15, 16, 17, 18, 5]}, "output": 85}, {"input": {"nums": [2, 1, 7, 3, 7, 3]}, "output": 16}, {"input": {"nums": [5, 3, 4, 13, 0, 12, 11, 6]}, "output": 36}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "138894abc44e1801"} |
vcr_1d01df7c5b1e | 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, -23, 30, 49, 52, -34, 62, -25, 66], "target": -2}, "output": [0, 2]}, {"input": {"nums": [21, 14, 27, 18, 41, 19, -7], "target": 48}, "output": [0, 2]}, {"input": {"nums": [45, 36, 33, 17, 40, 49], "target": 62}, "output": [0, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "6a4d842495d812c8"} |
vcr_de97a8a0ece4 | 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, -20, -11, -5, -2, 9, 10, 13, 19, 34, 35, 47], "target": -1}, "output": [1, 8]}, {"input": {"nums": [-19, -15, -4, 4, 10, 13, 24, 25], "target": 0}, "output": [2, 3]}, {"input": {"nums": [-19, -8, -2, 2, 12, 14, 17, 22, 36], "target": 53}, "output": [6, 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": "fd9d6f7228c948fa"} |
vcr_80593dac9f65 | 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, 13, 14, 7, 0]}, "output": 40}, {"input": {"nums": [16, 7]}, "output": 16}, {"input": {"nums": [14, 17, 8, 11]}, "output": 28}] | 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": "3f74a628327961c0"} |
vcr_3399dd21814b | 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": 5, "b": 220}, "output": 5}, {"input": {"a": 175, "b": 6}, "output": 1}, {"input": {"a": 124, "b": 151}, "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": "5146d883e515099e"} |
vcr_cf41d1778170 | 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": 40888}, "output": 10}, {"input": {"n": 13545}, "output": 8}, {"input": {"n": 2504}, "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": "a0016a61444f58b7"} |
vcr_50e27d6bbe06 | 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, 17, 21, -9, 10, -14, 20, -6, 16, -16, -15, 6, 18, 6, -18]}, "output": 71}, {"input": {"nums": [-10, -10, -3]}, "output": -3}, {"input": {"nums": [-6, 6, 8, -11, 3, 1, -8, 3, 14, 1, -2]}, "output": 18}] | 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": "6f78540aa10d5335"} |
vcr_bde813044a4d | 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": [55, 37, 32, 35, -26, -11, 28, 56, 54, 8, -30, 33], "target": 41}, "output": [9, 11]}, {"input": {"nums": [38, 1, 0, 3, 12], "target": 4}, "output": [1, 3]}, {"input": {"nums": [4, 14, 33, -24, 15], "target": -10}, "output": [1, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "523f0a4e11e51246"} |
vcr_4687256585ff | 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": [-28, -23, -20, -1, 3, 4, 13, 15, 27], "target": -13}, "output": [0, 7]}, {"input": {"nums": [-18, -14, -10, -6, -5, 3, 10, 11, 23, 26, 28], "target": 10}, "output": [0, 10]}, {"input": {"nums": [-11, -10, -3, 8, 9, 20, 21, 23], "target": -2}, "output": [0, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "6c61e981d37ebeed"} |
vcr_3af37a818fde | 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": [-2, -29, 4, 61, -18, 73, 74], "target": -20}, "output": [0, 4]}, {"input": {"nums": [-14, -27, 28, 36, 33, 46, 29, 23, -28, -9, 6], "target": 27}, "output": [3, 9]}, {"input": {"nums": [25, -8, 11, 4, 10, 0, 16], "target": 25}, "output": [0, 5]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "1aceb2c37b739b07"} |
vcr_94844348ec09 | medium | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "badcacaac"}, "output": false}, {"input": {"s": "cabbc"}, "output": false}, {"input": {"s": "ccbacccbba"}, "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": "4a9eee8abc8bc929"} |
vcr_390f1f53d1a5 | 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": [19, 27, 19, 8, 20, 5, 27, 22, 28, 28, 2, 3]}, "output": 116}, {"input": {"nums": [20, 14, 13, 14, 1, 0, 15, 2]}, "output": 49}, {"input": {"nums": [1, 16, 2, 20, 20, 6, 1, 16]}, "output": 58}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "520a15652cd53cb0"} |
vcr_4f0885b8ff13 | 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": "cbbbc aabd a d"}, "output": "d a aabd cbbbc"}, {"input": {"s": "aaa a aa bbbb"}, "output": "bbbb aa a aaa"}, {"input": {"s": "aa ab baa a"}, "output": "a baa ab 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": "9dce42249ddd42b3"} |
vcr_ada4e03d9cc9 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bc", "cbac", "cabc", "acb", "cbaa", "aba", "cbb"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "aaa", "bab", "aaa", "bba", "baa"]}, "output": [1, 2, 3]}, {"input": {"strs": ["aab", "aaa", "baa"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "9663fd92ad26c41e"} |
vcr_973450fd510b | 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": 3265}, "output": 5}, {"input": {"n": 541}, "output": 5}, {"input": {"n": 12192}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "f5a946db4d445ec3"} |
vcr_5c3ab71bc3e1 | 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": [29, 22, 33, 13, 36, 23, 33, 9, 36, 9, 13, 5, 29, 23, 5, 71, 22]}, "output": 71}, {"input": {"nums": [11, 2, 23, 29, 23, 29, 56, 9, 22, 11, 22, 9, 2]}, "output": 56}, {"input": {"nums": [1, 13, 14, 4, 1, 54, 14, 4, 13]}, "output": 54}] | 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": "a8478751b26113ad"} |
vcr_02963f3c0290 | 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": ["cbb", "cc", "ac", "bbc"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bbb", "abb", "aaa", "aab", "bab"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aba", "bab", "bbb", "bab", "aab", "aaa"]}, "output": [1, 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": "a67bdef46c62b7ac"} |
vcr_9e00ea6477f2 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, -19, -14, 5, 11, 25, 32, 48], "target": 11}, "output": [2, 5]}, {"input": {"nums": [-18, -17, -14, -7, -3, 13, 22, 29, 32, 35], "target": 28}, "output": [3, 9]}, {"input": {"nums": [-20, -17, -14, -7, 0, 3, 36, 38], "target": 74}, "output": [6, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "e1a5c941630d05ed"} |
vcr_3f61044dac62 | 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": [1, 1, 5, 4, 24]}, "output": 30}, {"input": {"nums": [18, 5, 5, 10, 0, 17]}, "output": 45}, {"input": {"nums": [14, 15, 6]}, "output": 20}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "55bc744d6b2d5622"} |
vcr_96eadf81b594 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bb", "ccb", "acb", "acb", "baab"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aaa", "baa", "abb", "aab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "aaa", "bbb"]}, "output": [1, 1, 1]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "62e5c3a75f3a8d94"} |
vcr_b7f69a7d0c44 | 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": "accabd bceb cdcbe a caba dcacc"}, "output": "dcacc caba a cdcbe bceb accabd"}, {"input": {"s": "ab bb abab baaa"}, "output": "baaa abab bb ab"}, {"input": {"s": "bb abba baab"}, "output": "baab abba bb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "3e6fd607a584bfe1"} |
vcr_a089dc9d99a9 | 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": [18, 18, 25, 43, 32, 28, 15, 10, 31, 27, 26, 24, 31]}, "output": 25}, {"input": {"prices": [18, 26, 27, 4, 28, 35, 39]}, "output": 35}, {"input": {"prices": [31, 31, 2, 24, 14, 12]}, "output": 22}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "abec9534f1103999"} |
vcr_ac9eabef333a | 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": [0, 41, 73, -1, 42, 2, 39, 34, 1, 78, -11, -24], "target": 72}, "output": [2, 3]}, {"input": {"nums": [-12, 20, -24, 22, -11, -3, 14, 1, 30, 11, -4], "target": 9}, "output": [1, 4]}, {"input": {"nums": [49, -10, -28, -14, -1, 17, -21, 10], "target": -15}, "output": [3, 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": "2da1e3f812d47302"} |
vcr_53a426474360 | easy | math | Return the greatest common divisor of positive integers a and b. | def gcd(a: int, b: int) -> int: | Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. | def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
| [{"input": {"a": 179, "b": 135}, "output": 1}, {"input": {"a": 138, "b": 93}, "output": 3}, {"input": {"a": 134, "b": 179}, "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": "390af94c389cd37b"} |
vcr_9bdece9700b7 | 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": "eba ccb aea ade dccea ca cba"}, "output": "cba ca dccea ade aea ccb eba"}, {"input": {"s": "ab a aaa"}, "output": "aaa a ab"}, {"input": {"s": "aba bba"}, "output": "bba aba"}] | 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": "b59bceeceba1232b"} |
vcr_c6154330b307 | 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": "ebb aadd ecaa bbe dadabc d cded"}, "output": "cded d dadabc bbe ecaa aadd ebb"}, {"input": {"s": "abb baba bab"}, "output": "bab baba abb"}, {"input": {"s": "bbb baba ab bab abba"}, "output": "abba bab ab baba bbb"}] | 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": "4b5c462c9b1b6993"} |
vcr_89ee8dc09e8f | 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": [21, -9, 2, 18, -7, -3, -12, -9, -14, 6, -15, -15]}, "output": 32}, {"input": {"nums": [13, -10, 8, -11, 9, 13, -2, 13]}, "output": 33}, {"input": {"nums": [-8, 3, 14, 9, -7, 0, 5, -6, 9]}, "output": 27}] | 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": "b5359b1405db2037"} |
vcr_22deb1da8eb7 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 4, 4, 6, 8, 8, 9, 11, 13, 13, 14]}, "output": [1, 4, 6, 8, 9, 11, 13, 14]}, {"input": {"nums": [2, 4, 10, 12, 12]}, "output": [2, 4, 10, 12]}, {"input": {"nums": [2, 5, 8, 10]}, "output": [2, 5, 8, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "09e7a0ce0d6eea71"} |
vcr_2fa0511c6b4d | 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": ["ca", "acb", "ab", "cccb", "ca", "bcaa", "aa"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "abb", "baa", "bba", "bab", "bab", "baa"]}, "output": [3, 4]}, {"input": {"strs": ["aab", "bab", "aaa"]}, "output": [1, 1, 1]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "b46d40fb537dffc8"} |
vcr_eed906c87ff9 | 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": [54, 13, 164, 199, 35, 87, 30, 58]}, "output": false}, {"input": {"nums": [9, 25, 23, 20, 20, 0, 0]}, "output": true}, {"input": {"nums": [0, 22, 21, 2, 14]}, "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": "ae062671ad93ba60"} |
vcr_7417b922c3f7 | 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": [57, -24, 63, 42, 16], "target": 39}, "output": [1, 2]}, {"input": {"nums": [-28, -9, -29, 25, 0, 34, 21, 45, -6, -10], "target": -4}, "output": [2, 3]}, {"input": {"nums": [-15, 21, 1, 16, 6, 39, 22], "target": 1}, "output": [0, 3]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "531b98098396c0f0"} |
vcr_507cd43eda97 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-19, -17, -12, -11, 10, 17, 19, 20, 25, 41, 45], "target": 86}, "output": [9, 10]}, {"input": {"nums": [-12, -4, 7, 33, 35], "target": 3}, "output": [1, 2]}, {"input": {"nums": [-16, -14, -13, -9, -2, 4, 7, 13, 14, 15, 23], "target": 11}, "output": [4, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "1c95ba27dcbeabd5"} |
vcr_5372b986d87d | 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": [4, 22, 42, 46, 21, 34, 47, 16, 38, 40, 42, 18, 32, 10, 27, 23, 43, 35]}, "output": true}, {"input": {"nums": [1, 22, 17]}, "output": false}, {"input": {"nums": [2, 1, 3, 20]}, "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": "8bf0999179f81598"} |
vcr_e60a2b83a452 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [27, 44, 57, 38, 6, 54, 2, 36, 24, 6, 42, 9, 45, 9, 31, 13]}, "output": 48}, {"input": {"prices": [5, 39, 40, 4, 30, 32, 35, 29, 6, 28]}, "output": 35}, {"input": {"prices": [23, 20, 20, 10, 27]}, "output": 17}] | 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": "9ad35ffaddb651f5"} |
vcr_d65f6e3dd3f3 | 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", "acc", "acb", "abca", "ccbb", "cbab"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bbb", "aba"]}, "output": [1, 2]}, {"input": {"strs": ["baa", "baa", "bba"]}, "output": [1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "dd994c9d7108f0c9"} |
vcr_b9d098337161 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["bc", "abca", "caca", "cc", "cbc", "ca", "babb"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "abb", "aba", "bba", "aab", "bba", "aab"]}, "output": [1, 3, 3]}, {"input": {"strs": ["bbb", "bab", "aab", "abb", "aab", "abb", "aab"]}, "output": [1, 3, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "bbbfe3f049a80627"} |
vcr_38add0ea2f7c | 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": [5, 6, 7, 7, 8, 8, 10, 10, 12, 13, 13, 15, 15]}, "output": [5, 6, 7, 8, 10, 12, 13, 15]}, {"input": {"nums": [1, 6, 7]}, "output": [1, 6, 7]}, {"input": {"nums": [3, 9, 9]}, "output": [3, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "e06e0c66eb383e02"} |
vcr_641b91c3e1f0 | 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": [2, -28, -13, 21, -14, 57, 51], "target": 78}, "output": [3, 5]}, {"input": {"nums": [38, -10, 46, -21, 6, 47, 45, -18, 37, 18, 21], "target": 82}, "output": [6, 8]}, {"input": {"nums": [26, 14, 16, 30, -15, 10], "target": 1}, "output": [2, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "1889272679ccb5a6"} |
vcr_967cf873691f | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [12, 20, 20, 27, 22]}, "output": 54}, {"input": {"nums": [12, 4, 6]}, "output": 18}, {"input": {"nums": [4, 14, 9, 12, 0, 12, 5, 14]}, "output": 52}] | 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": "f0f8b1240acd9c15"} |
vcr_f5e3ac32f3a1 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [2, 5, 0, 2, 3]}, "output": [0, 0, 60, 0, 0]}, {"input": {"nums": [1, 4, 5, 1, 2]}, "output": [40, 10, 8, 40, 20]}, {"input": {"nums": [4, 3, 3, 3, 2, 1, 3]}, "output": [162, 216, 216, 216, 324, 648, 216]}] | 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": "5ac50074965eac78"} |
vcr_f5a2304a4f7a | 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": "cebaa adbec cbdc adcdcc ddbca"}, "output": "ddbca adcdcc cbdc adbec cebaa"}, {"input": {"s": "bbab b"}, "output": "b bbab"}, {"input": {"s": "bba ab babb aaa bb"}, "output": "bb aaa babb ab bba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "62bf41a80f954845"} |
vcr_2dbbb9a60364 | 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": [37, 48, 19, 27, 45, 40, 43, 29, 6, 26, 37]}, "output": true}, {"input": {"nums": [3, 17, 22, 15, 5, 8, 16, 4]}, "output": false}, {"input": {"nums": [3, 1, 22]}, "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": "61cf42c22323cb9d"} |
vcr_1332ce218bb3 | 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, 7, -4, -8, -17, -11, 2, -12, -13, -8, 1], "k": 3}, "output": [-13, -8, 1, 6, 7, -4, -8, -17, -11, 2, -12]}, {"input": {"nums": [4, 5, -3, 0, 7], "k": 1}, "output": [7, 4, 5, -3, 0]}, {"input": {"nums": [-9, -6, 9, -3, 4, -4, 5, -7, -1], "k": 16}, "output": [9, -3, 4, -4, 5, -7, -1, -9, -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": "99ef4e43766d568a"} |
vcr_32473fbadd75 | 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": "abbccacbabb"}, "output": 8}, {"input": {"s": "ccaabbab"}, "output": 5}, {"input": {"s": "abccbacbbccb"}, "output": 9}] | 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": "dc7ea9fac25c1815"} |
vcr_3f21aa27b71c | 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": [-19, -2, 43, 41, 8, -31, -36, -39, 29, -33, 26], "target": -58}, "output": [0, 7]}, {"input": {"nums": [-11, -23, 9, -27, 23, -19, 19, -29], "target": -4}, "output": [3, 4]}, {"input": {"nums": [6, 29, -24, -7, 3, -21, 27, 39, -25], "target": -22}, "output": [4, 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": "a7fe0514d1950cc1"} |
vcr_d4725e7602dd | 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": [20, 21, 29, 0, 30]}, "output": 79}, {"input": {"nums": [9, 15, 3, 1, 19]}, "output": 34}, {"input": {"nums": [13, 4, 11, 16, 3, 11, 19, 13, 3]}, "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": "0146451b2c5833d1"} |
vcr_c36c92c5a8de | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [2, 14, 3, -20, -12, 20, -13, 11, 9], "k": 1}, "output": [9, 2, 14, 3, -20, -12, 20, -13, 11]}, {"input": {"nums": [-10, 7, 8, 9, 4], "k": 17}, "output": [9, 4, -10, 7, 8]}, {"input": {"nums": [10, -6, -1, -9, 5, 0, -10, 7, -7, -7], "k": 14}, "output": [-10, 7, -7, -7, 10, -6, -1, -9, 5, 0]}] | 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": "b3ff7311e7c6a8ce"} |
vcr_16e63b9b205a | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [13, 13, 0, 0, -20], "k": 5}, "output": [13, 13, 0, 0, -20]}, {"input": {"nums": [10, -8, -7, -9, -1, -9, 2, 2], "k": 18}, "output": [2, 2, 10, -8, -7, -9, -1, -9]}, {"input": {"nums": [7, 10, 10, -4, -4, -8, -8, 3, -2], "k": 0}, "output": [7, 10, 10, -4, -4, -8, -8, 3, -2]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "cbdd361c1164a76d"} |
vcr_9443f121a0f9 | 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": [7, 16, 11, 12, 29, 51, 27, 3, 24, 22, 3, 14, 49, 6, 35, 12]}, "output": 46}, {"input": {"prices": [7, 28, 6, 13, 29, 1, 31, 6, 18, 31]}, "output": 30}, {"input": {"prices": [17, 25, 32, 40, 1, 17, 1, 17, 34, 36, 24]}, "output": 35}] | 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": "cc7d8d2527f2aef0"} |
vcr_5d8f400e9a68 | 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": ["cacc", "caac", "aacb", "caba", "cb", "caba"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["aab", "bab", "aba", "bbb", "aab"]}, "output": [1, 1, 3]}, {"input": {"strs": ["abb", "baa", "bab", "aab"]}, "output": [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": "ba0763026a6c5de8"} |
vcr_9824bd2be485 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [8, 7, 14, 3, 2, 30, 27, 12, 3, 4]}, "output": 68}, {"input": {"nums": [8, 15, 15, 9, 10, 18, 14, 13]}, "output": 55}, {"input": {"nums": [7, 7, 5]}, "output": 12}] | 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": "797531c81a14a631"} |
vcr_b9e0058d059f | 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, 9, 10, 11, 12, 14, 15, 29, 31, 36, 39, 41, 46, 49], "target": 13}, "output": 5}, {"input": {"nums": [13, 16, 28, 33], "target": 11}, "output": 0}, {"input": {"nums": [17, 18, 25, 27, 32], "target": 12}, "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": "4047d2a482827f19"} |
vcr_d6b26d75a5a4 | 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": "bbbaaacbabb"}, "output": 6}, {"input": {"s": "acabcac"}, "output": 7}, {"input": {"s": "abaacbbbcaaa"}, "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": "b282f5b3c42decc1"} |
vcr_4f758b2f7661 | 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": [23, 2, 7, 28, 1, 16, 5, 4, 25, 25]}, "output": 96}, {"input": {"nums": [4, 20, 13, 18, 2, 11]}, "output": 49}, {"input": {"nums": [3, 16, 6, 4, 5, 1, 14, 15, 11, 0]}, "output": 46}] | 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": "7a99f7cb3c102f21"} |
vcr_3418b65f9bc4 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 2, 2, 3, 4, 5, 5, 7, 11, 12]}, "output": [1, 2, 3, 4, 5, 7, 11, 12]}, {"input": {"nums": [0, 2, 5, 5, 12]}, "output": [0, 2, 5, 12]}, {"input": {"nums": [0, 2, 6, 9, 9, 9, 10, 11]}, "output": [0, 2, 6, 9, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "392db2f3172f66f7"} |
vcr_5f77ce0f018d | 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, 3, 6, 12, 15, 27, 36, 39, 42, 46, 47, 48, 49], "target": 10}, "output": 3}, {"input": {"nums": [0, 1, 4, 9, 11, 13, 20, 35], "target": 17}, "output": 6}, {"input": {"nums": [18, 21, 39], "target": 9}, "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": "668f6bf87b408916"} |
vcr_6c3e3f06dc06 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-11, -15, 17, 8, -18]}, "output": 25}, {"input": {"nums": [7, -5, 6, 2, 10, 1, 9]}, "output": 30}, {"input": {"nums": [1, 2, -6, -11, -11, -9, -9, -1, 0, 5, -8]}, "output": 5}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "6392c07ba83e38ca"} |
vcr_aa1fecc11915 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["aa", "aac", "bab", "aba", "bbcb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "baa", "aaa", "bba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["abb", "baa", "abb", "abb"]}, "output": [1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "b482a9560051c8c8"} |
vcr_5f9d2d472d86 | 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": "bb eb ce ebeab cbb c cebdb"}, "output": "cebdb c cbb ebeab ce eb bb"}, {"input": {"s": "b b bbba"}, "output": "bbba b b"}, {"input": {"s": "aa bb aa bbb abbb"}, "output": "abbb bbb aa bb 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": "e6a8a0adf90219b9"} |
vcr_1e8912f55290 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 9, 12, 18, 19, 22, 32, 37, 46], "target": 4}, "output": 1}, {"input": {"nums": [6, 8, 9, 12, 14, 17, 19, 23, 32, 35], "target": 16}, "output": 5}, {"input": {"nums": [6, 10, 14, 22, 23, 27, 31, 38], "target": 37}, "output": 7}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "9c850dc4603e7753"} |
vcr_7ac7ec21e8d6 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 4, 4, 1, 3, 1, 4, 2, 0]}, "output": true}, {"input": {"nums": [3, 1, 0, 1, 0, 2, 1, 3, 1]}, "output": false}, {"input": {"nums": [3, 3, 2, 1, 1, 1, 0, 0]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "c0c6367634ddeed8"} |
vcr_9c6d0216d447 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [4, 9, 14, 40, 41], "target": 54}, "output": [2, 3]}, {"input": {"nums": [1, 8, 13, 19, 28, 31], "target": 27}, "output": [1, 3]}, {"input": {"nums": [-13, 19, 22, 26, 29, 32, 36], "target": 48}, "output": [1, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "c7cb598de06abd6f"} |
vcr_4d98990cc73e | 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, 9, 34, 38, 39, 49], "target": 34}, "output": 2}, {"input": {"nums": [6, 11, 16, 27], "target": 10}, "output": 1}, {"input": {"nums": [8, 9, 14, 18, 19, 25, 26], "target": 37}, "output": 7}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "ea34e6b6b8e67d8c"} |
vcr_47d167baddd8 | 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, 27, 27, 13, 60, 34, 28, 28, 13, 34, 26]}, "output": 60}, {"input": {"nums": [28, 15, 22, 15, 48, 6, 28, 22, 6]}, "output": 48}, {"input": {"nums": [24, 9, 20, 9, 7, 7, 21, 8, 21, 24, 57, 8, 20]}, "output": 57}] | 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": "a9c22671413f2cf3"} |
vcr_b3bf30b55fa7 | 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": ["bca", "cbac", "abbb", "bab", "acc", "cccc", "aabb", "acbc"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "aaa", "aaa", "bab", "baa", "bab", "abb"]}, "output": [1, 2, 4]}, {"input": {"strs": ["aab", "bbb", "bab", "aaa", "baa", "aba"]}, "output": [1, 1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "8f826f8ea2fc0da9"} |
vcr_4fc5167981cd | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [7, 9, 17, 21, 24, 30, 38, 45, 49], "target": 45}, "output": 7}, {"input": {"nums": [0, 11, 19, 37], "target": 40}, "output": 4}, {"input": {"nums": [3, 4, 17, 18], "target": 27}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "cdc9e0e3444253cc"} |
vcr_d4a52143b429 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 1, 3, 3, 4, 5, 5, 6, 7, 7, 9, 13, 13]}, "output": [0, 1, 3, 4, 5, 6, 7, 9, 13]}, {"input": {"nums": [1, 1, 2, 2, 3, 6, 8]}, "output": [1, 2, 3, 6, 8]}, {"input": {"nums": [0, 1, 2, 3, 6, 6, 7, 8, 9, 9, 9, 11]}, "output": [0, 1, 2, 3, 6, 7, 8, 9, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "249a158e6502810d"} |
vcr_855820f85fc6 | 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, 3, 1, 1, 3, 3, 1, 1, 0]}, "output": false}, {"input": {"nums": [3, 3, 0, 0, 1, 2, 1, 0]}, "output": true}, {"input": {"nums": [1, 1, 2, 0, 1, 0, 3, 3, 0]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "8c9299c671c646f7"} |
vcr_16bc009d1296 | 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, 19, 20, -7, 11, 15, 19, -5, 13, 17, 21, 1, 0, 21]}, "output": 145}, {"input": {"nums": [0, -7, 11, -8]}, "output": 11}, {"input": {"nums": [9, -2, -4, -1, 11, 5, -2, -4, -12, -7]}, "output": 18}] | 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": "c2518e821c375e2b"} |
vcr_0f2d18ca5bae | 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": 19695}, "output": 10}, {"input": {"n": 375}, "output": 7}, {"input": {"n": 9329}, "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": "1c8950b8581db5ef"} |
vcr_5cda6bd051f4 | 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, 0, 3, 4, 0, 3, 4, 0]}, "output": false}, {"input": {"nums": [3, 3, 2]}, "output": true}, {"input": {"nums": [3, 0, 0, 0, 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": "a6a54b4e05e04026"} |
vcr_24d8f0875048 | 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, 5, 24, 24, 29, 22, 14]}, "output": 95}, {"input": {"nums": [0, 17, 16, 15, 18]}, "output": 35}, {"input": {"nums": [8, 17, 4, 13, 19, 3, 6, 9, 16]}, "output": 58}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "0b30737ab66b18ff"} |
vcr_90dab4112f2a | 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, 10, 13, 18, 23, 25, 28, 30, 33, 35, 43, 47, 48], "target": 18}, "output": 3}, {"input": {"nums": [4, 11, 18, 24, 31, 36, 39], "target": 20}, "output": 3}, {"input": {"nums": [0, 1, 24, 33, 38], "target": 40}, "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": "1040e36be73b951c"} |
vcr_f8a78ea2ea26 | 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, 6, 6, 7, 7, 8, 11, 15]}, "output": [4, 6, 7, 8, 11, 15]}, {"input": {"nums": [2, 3, 4, 6, 6]}, "output": [2, 3, 4, 6]}, {"input": {"nums": [4, 6, 8, 9, 9, 11, 12, 12]}, "output": [4, 6, 8, 9, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "2eae7252d405ef10"} |
vcr_6d3aa50a807b | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-20, 19, -14, -8, -11], "k": 15}, "output": [-20, 19, -14, -8, -11]}, {"input": {"nums": [-10, 9, 3, 5, 3], "k": 3}, "output": [3, 5, 3, -10, 9]}, {"input": {"nums": [-7, -6, -4, 1, 6, 0, 10, -2], "k": 5}, "output": [1, 6, 0, 10, -2, -7, -6, -4]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "70389f7217673210"} |
vcr_35c57e49c989 | 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, 7, 2, 20, 15, 6, 8]}, "output": 58}, {"input": {"nums": [-1, 10, -5, 1, 7, -3, 10, 13, -8, 1]}, "output": 33}, {"input": {"nums": [-9, -8, 8, 14, 2, 4, 13, -10, 1, 1]}, "output": 41}] | 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": "599e20958128cac8"} |
vcr_0824f6df859c | 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, 4, 14, -17, 17, -18, 17]}, "output": 18}, {"input": {"nums": [-4, -10, -11]}, "output": -4}, {"input": {"nums": [9, 8, 12, -1]}, "output": 29}] | 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": "3ffe99a25e4411a0"} |
vcr_9128bef56b86 | 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": 124, "b": 157}, "output": 1}, {"input": {"a": 131, "b": 11}, "output": 1}, {"input": {"a": 35, "b": 149}, "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": "44c5f367ada94a62"} |
vcr_7f38d1257d92 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-21, -10, -7, 7, 23, 29, 37, 39, 43, 44], "target": 76}, "output": [6, 7]}, {"input": {"nums": [-19, -17, -12, -10, -4, 6, 15, 16, 17, 26, 38], "target": 14}, "output": [2, 9]}, {"input": {"nums": [-19, -11, -7, 9, 10, 21, 22, 23, 33, 34, 38], "target": -26}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "cdae9c3abd5138ea"} |
vcr_bca2eeff0dec | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [2, 2, 3, 2, 3, 7]}, "output": [252, 252, 168, 252, 168, 72]}, {"input": {"nums": [2, 5, 2]}, "output": [10, 4, 10]}, {"input": {"nums": [1, 5, 3]}, "output": [15, 3, 5]}] | 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": "0df4bd78825a6d15"} |
vcr_d2f5d740fd92 | 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": [183, 40, 149, 194, 103, 154, 29]}, "output": false}, {"input": {"nums": [3, 6, 25, 23, 16, 1, 5]}, "output": false}, {"input": {"nums": [19, 2, 21, 10]}, "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": "a7096dceb1e70fda"} |
vcr_6b4c0e6b2739 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 3, 6, 6, 7, 7, 12, 13, 14, 15]}, "output": [0, 2, 3, 6, 7, 12, 13, 14, 15]}, {"input": {"nums": [4, 6, 12]}, "output": [4, 6, 12]}, {"input": {"nums": [0, 2, 5, 5, 6, 6, 8, 8, 9, 11]}, "output": [0, 2, 5, 6, 8, 9, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "b791da00800b8613"} |
vcr_3c110fec65df | 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": [131, 11, 125, 188, 168, 165, 20, 118, 43, 191]}, "output": false}, {"input": {"nums": [16, 0, 8, 11, 12]}, "output": false}, {"input": {"nums": [16, 16, 12, 12, 17, 19, 11, 4, 15, 18, 24]}, "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": "75bff36eec33103d"} |
vcr_e647a3b67893 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [31, -11, 23, 16, 6, 29, -20, -23, 19, -11, 28, 12]}, "output": [31, 20, 43, 59, 65, 94, 74, 51, 70, 59, 87, 99]}, {"input": {"nums": [14, -1, 17]}, "output": [14, 13, 30]}, {"input": {"nums": [-7, -11, -5, -1, 7]}, "output": [-7, -18, -23, -24, -17]}] | 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": "fa6b2c60cc4a4ccb"} |
vcr_5b00e2a025db | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [6, 22, 31, 32, 37, 46], "target": 30}, "output": 2}, {"input": {"nums": [6, 10, 13, 14, 17, 18, 20, 22, 24, 31], "target": 16}, "output": 4}, {"input": {"nums": [2, 9, 17, 18, 20, 25, 28, 36, 37, 38], "target": 10}, "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": "128213fa4f634bc0"} |
vcr_2fe36209e77d | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 1, 4, 0, 3, 0, 1, 1, 0]}, "output": true}, {"input": {"nums": [1, 2, 1]}, "output": true}, {"input": {"nums": [0, 2, 2, 3, 0, 0, 2, 2, 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": "e062880d99191e96"} |
vcr_7d696b45eb53 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [13, -3, 1, 8, -13], "k": 14}, "output": [-3, 1, 8, -13, 13]}, {"input": {"nums": [-2, -8, 7, 6, 8, -3, 8, -4, 7, 1], "k": 7}, "output": [6, 8, -3, 8, -4, 7, 1, -2, -8, 7]}, {"input": {"nums": [8, -4, -4, -7, -9, -2], "k": 19}, "output": [-2, 8, -4, -4, -7, -9]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "a7c576a67b6fc15a"} |
vcr_355e24456940 | 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": "aaaccababbbaba"}, "output": 9}, {"input": {"s": "caacacccaca"}, "output": 8}, {"input": {"s": "bcbaabbab"}, "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": "f41d986402d65831"} |
vcr_4ccc9b2d62fc | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["ac", "ab", "cc", "bacc", "baba", "ab", "cbc", "bcb", "caa"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "bbb", "baa", "bbb", "aab", "aba", "aaa"]}, "output": [2, 2, 3]}, {"input": {"strs": ["abb", "aab", "bab", "abb", "aba"]}, "output": [2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "1c68a060e96bdb0b"} |
vcr_0942143d144e | 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": [124, 7, 71, 164, 15, 51, 62, 10, 35, 68]}, "output": false}, {"input": {"nums": [17, 5, 17, 5, 14, 18, 25, 24, 6]}, "output": true}, {"input": {"nums": [24, 22, 3, 11, 0, 13, 11, 22, 1, 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": "14543b9082487986"} |
vcr_b5acba0002e2 | 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": 28863}, "output": 10}, {"input": {"n": 1226}, "output": 5}, {"input": {"n": 5358}, "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": "9e67def8111ed498"} |
vcr_4098a9156764 | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "cb cdabcb da abca"}, "output": "abca da cdabcb cb"}, {"input": {"s": "ab aa"}, "output": "aa ab"}, {"input": {"s": "a aba"}, "output": "aba 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": "f9961c4361fda110"} |
vcr_59031a716518 | 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": [54, 38, 28, 29, 25, 32, 29, 32, 38, 28, 25]}, "output": 54}, {"input": {"nums": [24, 9, 9, 28, 24, 28, 58]}, "output": 58}, {"input": {"nums": [16, 26, 27, 16, 8, 26, 3, 12, 3, 27, 12, 59, 8]}, "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": "743e584d597826ed"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.