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_8582f9496a54 | 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": [37, 1, 36, 13, 71, 37, 13, 1, 36]}, "output": 71}, {"input": {"nums": [15, 22, 41, 15, 22]}, "output": 41}, {"input": {"nums": [25, 57, 25, 18, 16, 16, 18]}, "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": "5e5469950a1dc875"} |
vcr_c9d0289bcecd | 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": [56, 103, 82, 68, 64, 151, 13, 69, 57, 186, 100, 67, 187, 27]}, "output": false}, {"input": {"nums": [4, 14, 18, 11, 6, 0, 7, 9, 12]}, "output": false}, {"input": {"nums": [6, 10, 13, 6, 9, 20, 25]}, "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": "a12322b82793bbcb"} |
vcr_2dadf6d8315b | 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, -27, -24, -7, 5, 20], "target": -34}, "output": [1, 3]}, {"input": {"nums": [-18, -16, -10, -6, -1, 3, 9, 11, 27, 29], "target": -3}, "output": [3, 5]}, {"input": {"nums": [-20, -18, -8, 14, 15, 20, 29, 30], "target": 2}, "output": [1, 5]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "7de8959743ec99e7"} |
vcr_0c532e6705b6 | 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": [21, 78, 35, 3, 8, 5, 31, 20, 3, 35, 8, 5, 20, 31, 21]}, "output": 78}, {"input": {"nums": [13, 13, 26, 23, 9, 11, 11, 42, 26, 9, 23]}, "output": 42}, {"input": {"nums": [2, 11, 47, 5, 11, 2, 5]}, "output": 47}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "d62a42f0d27b4771"} |
vcr_45c04c90f9e8 | 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": [26, 20, 9, 2, 19, -5, 6, 33]}, "output": [26, 46, 55, 57, 76, 71, 77, 110]}, {"input": {"nums": [18, -12, -10]}, "output": [18, 6, -4]}, {"input": {"nums": [-6, 18, 11, 5, 10, 5]}, "output": [-6, 12, 23, 28, 38, 43]}] | 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": "8adbb57c17d91568"} |
vcr_43516534a600 | 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, 3, 0, 20, 2, 11, 21, 18, 3, 20, -11, 0]}, "output": 98}, {"input": {"nums": [-2, -2, 9, 11, 6]}, "output": 26}, {"input": {"nums": [7, -6, 7, 9, -10, 7, 12, -10, 5]}, "output": 26}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "c22de8a2b1614183"} |
vcr_eea61d32e046 | 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": "baaaacbbc"}, "output": 5}, {"input": {"s": "aaab"}, "output": 2}, {"input": {"s": "bbaabaabccaa"}, "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": "68a730a6df451501"} |
vcr_2389dc979a99 | 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": 184, "b": 134}, "output": 2}, {"input": {"a": 38, "b": 170}, "output": 2}, {"input": {"a": 76, "b": 47}, "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": "ad8f33b908c8195f"} |
vcr_302714d27da5 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 1, 2, 7, 9, 10, 12, 14]}, "output": [0, 1, 2, 7, 9, 10, 12, 14]}, {"input": {"nums": [1, 4, 4, 7, 9, 10, 10]}, "output": [1, 4, 7, 9, 10]}, {"input": {"nums": [3, 4, 8, 8, 11, 11, 11, 12, 12]}, "output": [3, 4, 8, 11, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "a01f97d18459c282"} |
vcr_9a8f783ba4df | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "e ccda b eb b ccdab db"}, "output": "db ccdab b eb b ccda e"}, {"input": {"s": "a aa bbab"}, "output": "bbab aa a"}, {"input": {"s": "b b"}, "output": "b b"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "de4250f93c9edc8b"} |
vcr_6e56d57bc777 | 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": [42, 68, 47, -25, 50, 24, -2, 77, -10, 29, 43, -34, 4], "target": 16}, "output": [4, 11]}, {"input": {"nums": [44, 39, 12, 28, 25, -21], "target": 7}, "output": [3, 5]}, {"input": {"nums": [43, 20, 24, 45, 17, -13], "target": 30}, "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": "2762e2aa817e065e"} |
vcr_757b7c23f59e | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [36, 20, 36, 16, 16, 58, 20]}, "output": 58}, {"input": {"nums": [15, 20, 11, 57, 20, 15, 19, 19, 11]}, "output": 57}, {"input": {"nums": [3, 3, 27, 17, 8, 2, 17, 2, 58, 8, 10, 27, 10]}, "output": 58}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "f0aff0c039554c81"} |
vcr_fb558f97b8e9 | 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, 4, 5, 7, 7, 6, 6]}, "output": [35280, 17640, 14112, 10080, 10080, 11760, 11760]}, {"input": {"nums": [5, 4, 1, 1, 2, 4]}, "output": [32, 40, 160, 160, 80, 40]}, {"input": {"nums": [3, 3, 4, 3, 1, 1]}, "output": [36, 36, 27, 36, 108, 108]}] | 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": "947c9c994daf0ea0"} |
vcr_18b01f20eaa2 | 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": "bbaded aabdbb cbdca badb"}, "output": "badb cbdca aabdbb bbaded"}, {"input": {"s": "ab ba baa"}, "output": "baa ba ab"}, {"input": {"s": "aaa aaaa bb"}, "output": "bb aaaa aaa"}] | 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": "b31131e6d1e564d9"} |
vcr_47b0f263999f | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-24, -9, -5, 6, 7, 25, 26, 29], "target": 33}, "output": [4, 6]}, {"input": {"nums": [-17, -5, -3, 0, 16, 17, 20, 22, 24, 31], "target": 51}, "output": [6, 9]}, {"input": {"nums": [4, 8, 18, 27, 28, 33], "target": 12}, "output": [0, 1]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "65fc4b15313cf4f6"} |
vcr_9579e6cca0c6 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [5, -4, 15, -20, 11], "k": 11}, "output": [11, 5, -4, 15, -20]}, {"input": {"nums": [9, -6, -6], "k": 16}, "output": [-6, 9, -6]}, {"input": {"nums": [3, 5, 9, 1, -8, 5, 2, 1, -1], "k": 10}, "output": [-1, 3, 5, 9, 1, -8, 5, 2, 1]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "739e37678068c64d"} |
vcr_722891a2c758 | 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": "ddbacc ca"}, "output": "ca ddbacc"}, {"input": {"s": "abb ba"}, "output": "ba abb"}, {"input": {"s": "aaa bb abaa bb"}, "output": "bb abaa bb aaa"}] | 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": "eb12982b8d4416c0"} |
vcr_9b07d46bc69c | 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": [18, 9, 35, 29, 12, 8, 18, 29, 8, 35, 37, 9, 32, 12, 50, 37, 32]}, "output": 50}, {"input": {"nums": [13, 43, 4, 4, 29, 7, 12, 12, 6, 7, 6, 29, 13]}, "output": 43}, {"input": {"nums": [56, 28, 28, 16, 16, 12, 12, 9, 13, 13, 9]}, "output": 56}] | 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": "7f146b70674fb4a3"} |
vcr_8fb883fe0ccd | 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": [-33, 6, -19, 16, -1, -11, 62, -12], "target": -13}, "output": [1, 2]}, {"input": {"nums": [36, 23, 15, 29, -8, -7, 10], "target": 46}, "output": [0, 6]}, {"input": {"nums": [-11, -1, 32, -21, -14], "target": -15}, "output": [1, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "9b2c9545e651606c"} |
vcr_33fe67a0d69a | 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": "ac da eebeed"}, "output": "eebeed da ac"}, {"input": {"s": "bb b b bbba"}, "output": "bbba b b bb"}, {"input": {"s": "babb a bba aab bbab"}, "output": "bbab aab bba a babb"}] | 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": "bf9c551c32837022"} |
vcr_286f24718670 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [11, 2, 4, 27, 11, 10]}, "output": 48}, {"input": {"nums": [12, 1, 10, 7, 13, 19, 5, 2]}, "output": 43}, {"input": {"nums": [2, 2, 11, 10]}, "output": 13}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "982b27918fe1949c"} |
vcr_c828a7595bb3 | 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": [23, 7, 36, 7, 66, 35, 36, 23, 34, 13, 13, 35, 34]}, "output": 66}, {"input": {"nums": [1, 25, 25, 19, 19, 1, 46]}, "output": 46}, {"input": {"nums": [9, 28, 9, 10, 42, 10, 28]}, "output": 42}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "dc85a1c541d9ac29"} |
vcr_32101e150259 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 1, 21, 38], "target": 44}, "output": 4}, {"input": {"nums": [17, 20, 25, 28, 37, 39], "target": 25}, "output": 2}, {"input": {"nums": [4, 7, 10, 13, 19, 35, 37], "target": 31}, "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": "a606167968e5e9d3"} |
vcr_88578129f762 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 4, 4, 8, 8, 9, 9, 14, 14]}, "output": [0, 2, 4, 8, 9, 14]}, {"input": {"nums": [2, 2, 2, 3, 5, 7, 12, 12]}, "output": [2, 3, 5, 7, 12]}, {"input": {"nums": [4, 8, 8]}, "output": [4, 8]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "dd3eacbf1e393f8c"} |
vcr_ba3ff18e803f | 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, -6, -4, 48, 49], "target": 42}, "output": [1, 3]}, {"input": {"nums": [-10, -6, 8, 13, 21, 38], "target": 34}, "output": [3, 4]}, {"input": {"nums": [-15, -11, -10, 5, 8, 23, 26, 32, 37, 38], "target": 17}, "output": [0, 7]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "863787842c575e9a"} |
vcr_d388e041f126 | 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": "eacbec caa dcc cad"}, "output": "cad dcc caa eacbec"}, {"input": {"s": "bbaa baab bbab bbb"}, "output": "bbb bbab baab bbaa"}, {"input": {"s": "bb b bbaa aaab"}, "output": "aaab bbaa b 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": "53caae97f4905ab1"} |
vcr_14f9543cea71 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [3, 3, 13]}, "output": 16}, {"input": {"nums": [7, 0, 9, 13, 7]}, "output": 23}, {"input": {"nums": [12, 12]}, "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": "6c92f73c8d8edf8a"} |
vcr_bc2ae004aef4 | 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, 16, 30, 13, 9, 0, 28, 13, 0, 3, 14, 8]}, "output": 82}, {"input": {"nums": [10, 14, 20, 1]}, "output": 30}, {"input": {"nums": [2, 9, 2, 20, 13, 12, 13, 2, 19, 3]}, "output": 61}] | 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": "7a2498dd8849d03f"} |
vcr_60c6a286f757 | 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": [31, 75, -14, 52, 41, 3, 0, 5, -16, 40, -7], "target": 115}, "output": [1, 9]}, {"input": {"nums": [-21, -30, -18, 34, 49, 14], "target": 31}, "output": [2, 4]}, {"input": {"nums": [2, -28, 32, -12, -27, -30, -8, -7, 27, 37, 0], "target": -40}, "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": "110a95107301e880"} |
vcr_ce8bba6e6bfd | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-11, -17, -3, -12, 0, -20, 11, 7, -11, -15, -9], "k": 0}, "output": [-11, -17, -3, -12, 0, -20, 11, 7, -11, -15, -9]}, {"input": {"nums": [3, -3, -3, -2, 4, -8, -6], "k": 12}, "output": [-3, -2, 4, -8, -6, 3, -3]}, {"input": {"nums": [2, 6, 3, -4, 0, -7], "k": 18}, "output": [2, 6, 3, -4, 0, -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": "b181f5b35da49a4f"} |
vcr_58d23d98df57 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-3, 16, 6, -18, 1, -14, 0, 2, 1, -11], "k": 12}, "output": [1, -11, -3, 16, 6, -18, 1, -14, 0, 2]}, {"input": {"nums": [-3, 2, 3, -10, -1], "k": 17}, "output": [-10, -1, -3, 2, 3]}, {"input": {"nums": [-6, 6, 9, 7], "k": 13}, "output": [7, -6, 6, 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": "e7c07afff632a1af"} |
vcr_3e9428a6c58d | 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": [66, 9, 12, 9, 29, 29, 12]}, "output": 66}, {"input": {"nums": [46, 23, 8, 23, 4, 11, 4, 8, 11]}, "output": 46}, {"input": {"nums": [28, 26, 17, 18, 19, 8, 17, 18, 8, 19, 28, 26, 47]}, "output": 47}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "797d4ced0e81b8d9"} |
vcr_de72519b0f27 | 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": [18, 19, 32, 36, 49], "target": 49}, "output": 4}, {"input": {"nums": [2, 10, 18, 23, 30, 34, 35, 39], "target": 28}, "output": 4}, {"input": {"nums": [28, 33, 35], "target": 25}, "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": "7772c87cd83084db"} |
vcr_268b8f9f2f84 | 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": [14, 24, 6, -21, 23, -22, -25, -8, -2, 29]}, "output": [14, 38, 44, 23, 46, 24, -1, -9, -11, 18]}, {"input": {"nums": [-3, 9, 15, 17, -11, -12, 3, -11, -9]}, "output": [-3, 6, 21, 38, 27, 15, 18, 7, -2]}, {"input": {"nums": [-9, 11, 13, 17]}, "output": [-9, 2, 15, 32]}] | 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": "0bcfc11832e588c2"} |
vcr_e6e74123af3e | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-4, -16, -1, -6, -14, -4, 27, 19, 6]}, "output": [-4, -20, -21, -27, -41, -45, -18, 1, 7]}, {"input": {"nums": [-4, 15, 10, 0, 4, -5, 2, -5, 7, 12]}, "output": [-4, 11, 21, 21, 25, 20, 22, 17, 24, 36]}, {"input": {"nums": [16, 0, 15, 18, 5, -9, 6, 0, -8]}, "output": [16, 16, 31, 49, 54, 45, 51, 51,... | 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": "e3efbb59c17a9f18"} |
vcr_7adaa9bd17e7 | 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": "daabddabcdadc"}, "output": false}, {"input": {"s": "cccbacaca"}, "output": false}, {"input": {"s": "abbccba"}, "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": "9801d3448a60bdb3"} |
vcr_237cf91f5897 | 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": 181, "b": 93}, "output": 1}, {"input": {"a": 53, "b": 65}, "output": 1}, {"input": {"a": 151, "b": 116}, "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": "02a55663e7860bd3"} |
vcr_4600dd75ff2d | 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": ["acab", "abac", "acaa", "abb", "bc", "bb"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bbb", "aba", "bab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "aba", "abb"]}, "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": "1e4cc8c74bc4973c"} |
vcr_27e20db9974b | 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": "baacababbbb"}, "output": 7}, {"input": {"s": "cbaaabbac"}, "output": 6}, {"input": {"s": "acab"}, "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": "ad9011439c1decea"} |
vcr_fcff6941be2c | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 10, 11, 16, 19, 20, 38, 40, 44], "target": 44}, "output": 8}, {"input": {"nums": [3, 4, 5, 6, 8, 9, 10, 11, 14, 32, 33, 38], "target": 23}, "output": 9}, {"input": {"nums": [2, 5, 6, 7, 8, 12, 25, 27, 28, 36], "target": 3}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "f52c5276d12d383d"} |
vcr_73f21eb09bf9 | 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": [-14, 16, 19, 19, -7], "k": 10}, "output": [-14, 16, 19, 19, -7]}, {"input": {"nums": [7, 9, -4, 8, -6], "k": 18}, "output": [-4, 8, -6, 7, 9]}, {"input": {"nums": [8, 4, -5, 0], "k": 3}, "output": [4, -5, 0, 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": "d08aca5ec264988b"} |
vcr_23ff71953126 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [6, 5, 7, 0, 2]}, "output": [0, 0, 0, 420, 0]}, {"input": {"nums": [5, 2, 5, 4]}, "output": [40, 100, 40, 50]}, {"input": {"nums": [1, 5, 3, 1, 3, 5, 5]}, "output": [1125, 225, 375, 1125, 375, 225, 225]}] | 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": "d5331bef48ad5640"} |
vcr_f4150162b2d1 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [10, -1, -4, 7, -9, 20, -18, 18, 17, 7, -12, -18, 20]}, "output": 47}, {"input": {"nums": [3, -12, -12, 10, 2, -8, 14, 2, 13, -10]}, "output": 33}, {"input": {"nums": [-3, -10, 5, 11, 2, -2, -10, 11, 6]}, "output": 23}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "5aaf84a276987c54"} |
vcr_6bb7e7bbc9ad | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [20, 0, 1, 8, 32, -2, 5, 21, 28]}, "output": [20, 20, 21, 29, 61, 59, 64, 85, 113]}, {"input": {"nums": [8, -12, -10, -9]}, "output": [8, -4, -14, -23]}, {"input": {"nums": [14, -10, 14, 3, 12, 10, -11, -12, 11, 18]}, "output": [14, 4, 18, 21, 33, 43, 32, 20, 31, 49]}] | 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": "ab1f538ec9b32890"} |
vcr_3d613c9d4e5b | 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, 14, 15, 24, 17, 10]}, "output": 53}, {"input": {"nums": [15, 0, 10, 7, 5, 13, 13, 19]}, "output": 57}, {"input": {"nums": [10, 13, 10, 1, 0, 19, 8, 7]}, "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": "d177e23ff2a26d22"} |
vcr_274c685528a6 | 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": [20, 27, 59, 7, 18, 27, 20, 7, 18]}, "output": 59}, {"input": {"nums": [22, 17, 17, 22, 43]}, "output": 43}, {"input": {"nums": [23, 6, 15, 2, 23, 15, 6, 2, 49]}, "output": 49}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "66dd2dc9a4faf123"} |
vcr_4046d2bb4fef | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["ccac", "acac", "acab", "aaac", "bac", "cb", "caa", "bcb", "abb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bab", "abb", "bbb"]}, "output": [1, 2]}, {"input": {"strs": ["abb", "bba", "aba", "abb", "bbb", "aba"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "7841fae417cf40ef"} |
vcr_31e7dc95f9a6 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 23, 35, 48, 49], "target": 29}, "output": 2}, {"input": {"nums": [2, 6, 7, 9, 14, 22, 32, 33, 37], "target": 13}, "output": 4}, {"input": {"nums": [2, 11, 16, 31, 38, 39], "target": 18}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "d79fa84042704f30"} |
vcr_cc68c9f6eb54 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [18, -13, -24, 10, 21, 29, -5]}, "output": [18, 5, -19, -9, 12, 41, 36]}, {"input": {"nums": [12, -4, 11, 13, 14]}, "output": [12, 8, 19, 32, 46]}, {"input": {"nums": [-3, 3, 4, 15]}, "output": [-3, 0, 4, 19]}] | 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": "2fa20f311c875880"} |
vcr_057d058aff85 | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "daacaccbddd"}, "output": false}, {"input": {"s": "abcbba"}, "output": false}, {"input": {"s": "bb"}, "output": true}] | true | python | 9742b2c402b3548d | ### Problem
Return True if lowercase string s is a palindrome.
### Reasoning
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
### Solution
```python
def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += ... | {"v": "ok", "content_hash": "99d92f0ece4ce8e3"} |
vcr_66bd0b8de5ae | 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": [33, 57, 36, 11, 10]}, "output": 24}, {"input": {"prices": [2, 8, 6, 22, 27, 32, 17, 17, 10, 8, 36, 19]}, "output": 34}, {"input": {"prices": [33, 24, 7, 5, 36, 17, 4]}, "output": 31}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "6bc6147dcb9f5bf4"} |
vcr_bb00df32bbe3 | 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": 47563}, "output": 10}, {"input": {"n": 4612}, "output": 3}, {"input": {"n": 4792}, "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": "562effc53be21ec6"} |
vcr_83b5b247312a | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "e cab ebedb dccbdc caaabd bbebcd c"}, "output": "c bbebcd caaabd dccbdc ebedb cab e"}, {"input": {"s": "ab abbb ab a"}, "output": "a ab abbb ab"}, {"input": {"s": "bba abbb ba aaa"}, "output": "aaa ba abbb 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": "880c3b28d42fa41b"} |
vcr_cd4bcda121b0 | 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", "ab", "cccb", "baab", "cb", "ac", "abc"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "abb", "aba", "bab", "aab", "abb", "bba"]}, "output": [1, 2, 4]}, {"input": {"strs": ["abb", "abb", "aaa", "bbb"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "1da9fc6a57ea034e"} |
vcr_43ddad09f735 | 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": [5, 10, 4, 16, 22, 28, 30]}, "output": 62}, {"input": {"nums": [6, 18, 10, 6]}, "output": 24}, {"input": {"nums": [13, 3, 20, 6, 0]}, "output": 33}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "80574a9e492dbcde"} |
vcr_2c55fe321bc3 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-10, 20, -11, 33, 5, 19, 26]}, "output": [-10, 10, -1, 32, 37, 56, 82]}, {"input": {"nums": [5, 2, 15, 6, 13, -2]}, "output": [5, 7, 22, 28, 41, 39]}, {"input": {"nums": [9, 10, 17, 16]}, "output": [9, 19, 36, 52]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "9658c68e1e84648e"} |
vcr_59e0553a9a72 | 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": [7, -20, 34, 20, -22, 22], "target": -2}, "output": [3, 4]}, {"input": {"nums": [7, 14, 44, -5], "target": 58}, "output": [1, 2]}, {"input": {"nums": [-16, 41, 0, 42, 5, -27, -11], "target": 15}, "output": [3, 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": "9882d2048a302568"} |
vcr_b5d261f0e9de | 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, 3, 4, 4, 5, 6, 8, 8, 10, 10, 10, 12]}, "output": [0, 3, 4, 5, 6, 8, 10, 12]}, {"input": {"nums": [0, 1, 8, 9, 10]}, "output": [0, 1, 8, 9, 10]}, {"input": {"nums": [0, 2, 3, 5, 6, 7, 7, 9, 11]}, "output": [0, 2, 3, 5, 6, 7, 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": "9171408141aa8539"} |
vcr_b4ea69ec9b23 | 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": "caccabacadab"}, "output": false}, {"input": {"s": "bbc"}, "output": false}, {"input": {"s": "bbcaccb"}, "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": "c0a173dfa3bbc6ac"} |
vcr_9299d3725018 | 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": [-15, -11, -9, -8, -3, 11], "target": -24}, "output": [0, 2]}, {"input": {"nums": [-3, 4, 10, 11, 12, 30, 39], "target": 8}, "output": [0, 3]}, {"input": {"nums": [-17, -16, -11, -1, 1, 10, 18, 26, 37, 39], "target": 28}, "output": [2, 9]}] | 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": "b4baeb72ddb0e2d8"} |
vcr_f3474867ceae | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [38, 42, 55, 41, 51, 15, 29, 4, 54, 58, 45, 15, 53]}, "output": 54}, {"input": {"prices": [5, 17, 16, 29]}, "output": 24}, {"input": {"prices": [36, 23, 40, 32, 10]}, "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": "4dac17032def6999"} |
vcr_116bfb37b89b | 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": [4, 7, 5, 17, 4, 29, 14, 26, 28, 27, 2]}, "output": 106}, {"input": {"nums": [13, 9, 4, 19, 4, 5, 10]}, "output": 42}, {"input": {"nums": [8, 9, 4, 5, 8, 1]}, "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": "06cf0369511bcfdb"} |
vcr_fefe8b4917fd | 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": ["bcba", "bcaa", "cb", "bcb", "aacc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "baa", "bba"]}, "output": [1, 2]}, {"input": {"strs": ["aab", "bba", "abb", "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": "5f80026fe671b814"} |
vcr_fbc2829ea98f | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 6, 8, 13, 16, 18, 28, 29, 32, 33, 34, 39, 40], "target": 3}, "output": 0}, {"input": {"nums": [16, 28, 32, 38], "target": 17}, "output": 1}, {"input": {"nums": [4, 14, 24, 34], "target": 10}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "0c33dc35bbf9d37e"} |
vcr_b7c65080c91f | 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, 3, 1, 2, 7, 1, 4]}, "output": [168, 112, 336, 168, 48, 336, 84]}, {"input": {"nums": [3, 1, 1, 1, 3]}, "output": [3, 9, 9, 9, 3]}, {"input": {"nums": [5, 3, 4, 2, 5, 5]}, "output": [600, 1000, 750, 1500, 600, 600]}] | 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": "1e3e359107c0a6bb"} |
vcr_ac6e1820ddae | 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": 168, "b": 181}, "output": 1}, {"input": {"a": 72, "b": 45}, "output": 9}, {"input": {"a": 91, "b": 33}, "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": "379375307546d5df"} |
vcr_0054ac712746 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [17, 7, 30, 17]}, "output": 47}, {"input": {"nums": [10, 19, 11]}, "output": 21}, {"input": {"nums": [3, 18, 10, 0, 10, 7, 14, 5]}, "output": 42}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "b0251b14a7f842d4"} |
vcr_a2d5fccc933e | 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": [20, 35, 32, 10, 10, 32, 20, 59, 25, 35, 25]}, "output": 59}, {"input": {"nums": [19, 26, 20, 20, 26, 19, 11, 27, 3, 3, 27, 51, 11]}, "output": 51}, {"input": {"nums": [22, 25, 2, 49, 22, 25, 2, 27, 27]}, "output": 49}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "ba8c9054089acae4"} |
vcr_b29cc6172013 | 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": [32, 17, 3, 36, 33, 32, 25, 3, 10, 36, 33, 25, 61, 10, 17]}, "output": 61}, {"input": {"nums": [14, 48, 1, 3, 14, 1, 3]}, "output": 48}, {"input": {"nums": [7, 12, 42, 12, 7]}, "output": 42}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "a0c60b9281b772b3"} |
vcr_a705d7b0d499 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 10, 15, 39, 45], "target": 18}, "output": 3}, {"input": {"nums": [0, 13, 20, 25], "target": 33}, "output": 4}, {"input": {"nums": [18, 27, 28], "target": 22}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "bd163baef746fdd4"} |
vcr_3d7c686802e8 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 3, 3, 2, 0, 3, 3, 0]}, "output": true}, {"input": {"nums": [2, 0, 1, 1]}, "output": true}, {"input": {"nums": [0, 2, 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": "85527f43e0f4f11f"} |
vcr_a512451e3fd1 | 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": "bacbaaaacbbcaa"}, "output": 9}, {"input": {"s": "ccbac"}, "output": 4}, {"input": {"s": "bcacbcbbbbbc"}, "output": 8}] | 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": "80cd815a7ff6290a"} |
vcr_d9fe33d57e08 | 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": "aabcaabbbcaadd"}, "output": false}, {"input": {"s": "bacbbcab"}, "output": true}, {"input": {"s": "baccac"}, "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": "2dd278eb5431b5b5"} |
vcr_b7383f9f974a | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-24, -22, -16, -6, -2, 6, 9, 12, 15, 17, 22, 41], "target": 16}, "output": [3, 10]}, {"input": {"nums": [-12, 4, 9, 30, 34], "target": 39}, "output": [2, 3]}, {"input": {"nums": [-18, -17, -8, -3, -2, 8, 9, 18, 20], "target": -25}, "output": [1, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "02c1ea855412e637"} |
vcr_81126e8a0dd0 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-11, 10, -12, 17, 8, 18, -14], "k": 17}, "output": [8, 18, -14, -11, 10, -12, 17]}, {"input": {"nums": [-1, 1, 3], "k": 7}, "output": [3, -1, 1]}, {"input": {"nums": [-4, 1, 4], "k": 20}, "output": [1, 4, -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": "80ae45557081d84d"} |
vcr_24321f83358e | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 4, 0, 0, 1, 1, 4, 0]}, "output": true}, {"input": {"nums": [1, 0, 2, 0, 3, 0, 0]}, "output": false}, {"input": {"nums": [3, 0, 3, 3]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "afab8756e283bc98"} |
vcr_b69a8ea1701c | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 3, 6, 8, 8, 8, 11, 14]}, "output": [0, 3, 6, 8, 11, 14]}, {"input": {"nums": [6, 9, 10]}, "output": [6, 9, 10]}, {"input": {"nums": [2, 4, 7, 7, 12]}, "output": [2, 4, 7, 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": "d0c0a1d11065faa0"} |
vcr_f998b7596220 | 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": [16, 48, -17, 13, -38, 49], "target": -4}, "output": [2, 3]}, {"input": {"nums": [6, 8, -9, -10, -15, 31], "target": -24}, "output": [2, 4]}, {"input": {"nums": [1, -16, -11, -8, -1, 31, -6, 30, -10, 38], "target": -14}, "output": [3, 6]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "21b231021ec98f45"} |
vcr_38dd02bc9fa4 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 7, 11, 16, 18, 20, 21, 29, 30, 33, 45, 46, 48], "target": 1}, "output": 0}, {"input": {"nums": [5, 6, 9, 10, 22, 24, 25, 35], "target": 18}, "output": 4}, {"input": {"nums": [15, 16, 31, 34, 37], "target": 11}, "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": "ea607927101d51d6"} |
vcr_81c85267a203 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [3, 3, 3, 3, 4, 7, 7, 8, 10, 11, 11, 13]}, "output": [3, 4, 7, 8, 10, 11, 13]}, {"input": {"nums": [1, 3, 3, 3, 4, 5, 5, 8, 12]}, "output": [1, 3, 4, 5, 8, 12]}, {"input": {"nums": [1, 3, 4]}, "output": [1, 3, 4]}] | 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": "3d245a32ee455613"} |
vcr_9b1f094d3b3d | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [3, 9, 3, 24]}, "output": 33}, {"input": {"nums": [19, 14, 6, 10]}, "output": 29}, {"input": {"nums": [7, 3, 1, 1, 13, 10]}, "output": 21}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "ddb553f5ea785fcf"} |
vcr_96b4d95e76ff | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [17, 9, 22, 24, 1, 24]}, "output": 65}, {"input": {"nums": [3, 4, 11, 20, 16, 16, 6]}, "output": 40}, {"input": {"nums": [9, 4, 17, 20, 2, 14]}, "output": 43}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "dd22140030e78836"} |
vcr_e50a54d8b5b5 | 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": ["cba", "bc", "aa", "bbba", "aacc", "bbbb", "bbba", "bbab", "cac"]}, "output": [1, 1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bbb", "bab", "aba", "aab", "aba"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aba", "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": "5385906fd1172030"} |
vcr_1f64e9fc9b2d | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 23, 32, 33, 38, 41, 43, 44, 47], "target": 18}, "output": 1}, {"input": {"nums": [2, 3, 4, 6, 21, 23, 24, 25, 27, 38], "target": 10}, "output": 4}, {"input": {"nums": [7, 9, 11, 12, 15, 19, 20, 27, 29, 34], "target": 6}, "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": "43812022a176b843"} |
vcr_a3181c0b0368 | 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", "bbac", "cc", "cc", "ba"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["baa", "baa", "aaa", "bab", "baa", "aab"]}, "output": [1, 1, 4]}, {"input": {"strs": ["aba", "abb", "baa", "aab"]}, "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": "90ef22c54eb4fc9c"} |
vcr_d68267bc2104 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [8, 19, 8, 1, 14, 15, 12, -16, 4, 5, -3, -1]}, "output": 77}, {"input": {"nums": [-4, -3, 4, 4, -10, 14, 9, -3, 2]}, "output": 23}, {"input": {"nums": [13, 14, 14]}, "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": "627c99a95692d655"} |
vcr_04d93576fb75 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [16, 7, 2, 10, 3, 11, 26, 17, 5, 7, 26]}, "output": 83}, {"input": {"nums": [20, 6]}, "output": 20}, {"input": {"nums": [18, 11, 13, 17, 12, 3]}, "output": 43}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "ec444cb5fecd2ded"} |
vcr_c5c1525788c2 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [5, 4, -15, -8, -5, -9, 3], "k": 17}, "output": [-5, -9, 3, 5, 4, -15, -8]}, {"input": {"nums": [4, -7, 4, 8, -9, -3, 3, -6, 4, -2], "k": 17}, "output": [8, -9, -3, 3, -6, 4, -2, 4, -7, 4]}, {"input": {"nums": [-7, 7, -9, -6, -5, 4, 4, -5, -9, -10], "k": 19}, "output": [7, -9, -6, -5, 4, 4, -5, -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": "e792d495617ee2ac"} |
vcr_e4dcc58cf0ce | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [4, 38, 43, 30]}, "output": 39}, {"input": {"prices": [2, 38, 9, 34, 33, 18]}, "output": 36}, {"input": {"prices": [26, 33, 3, 23, 4, 35, 26, 17, 30, 32, 26]}, "output": 32}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "5ca5b38f3134e0eb"} |
vcr_0efb4ebafe1a | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 6, 7, 8, 9, 9, 12]}, "output": [0, 1, 6, 7, 8, 9, 12]}, {"input": {"nums": [0, 0, 5, 8, 11]}, "output": [0, 5, 8, 11]}, {"input": {"nums": [0, 0, 1, 2, 4, 5, 6, 7, 8, 8, 8, 10]}, "output": [0, 1, 2, 4, 5, 6, 7, 8, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "7847356c87c76b49"} |
vcr_263d4e596aea | 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": ["bcb", "abc", "bca", "ac", "bbbb"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["bab", "aaa", "aaa", "baa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bab", "bbb", "bbb", "bbb", "aaa", "aba", "aab"]}, "output": [1, 1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "074f0fb40033d4f3"} |
vcr_9e1c38fa3786 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 9, 15, 20, 25, 26, 32, 37, 38, 40, 45, 48], "target": 22}, "output": 4}, {"input": {"nums": [6, 9, 19, 30, 33, 38, 39], "target": 23}, "output": 3}, {"input": {"nums": [0, 3, 4, 10, 12, 20, 21, 28, 29, 37], "target": 26}, "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": "83e0bc2c3aaf9480"} |
vcr_6ccf527b0d6d | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [12, 13, 15, 16, 21, 23, 24, 30, 31, 32, 33, 36, 41, 42], "target": 28}, "output": 7}, {"input": {"nums": [7, 17, 18, 19, 20, 21, 22, 25, 27], "target": 20}, "output": 4}, {"input": {"nums": [2, 5, 6, 12, 14, 31, 32, 37, 38], "target": 8}, "output": 3}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "1ba07b04fec3e2dd"} |
vcr_b684c56805d8 | easy | matrix | Return the sum of all elements in a 2D matrix. | def matrix_sum(mat: list[list[int]]) -> int: | Sum each row, then sum those totals. | def matrix_sum(mat: list[list[int]]) -> int:
return sum(sum(row) for row in mat)
| [{"input": {"mat": [[4, 4, 9, 1, 4], [2, 2, 3, 4, 7]]}, "output": 40}, {"input": {"mat": [[0, 5, 7, 4], [1, 7, 3, 2]]}, "output": 29}, {"input": {"mat": [[5, 4, 4, 0], [7, 4, 1, 2]]}, "output": 27}] | true | python | 1b4635892b4bdc45 | ### Problem
Return the sum of all elements in a 2D matrix.
### Reasoning
Sum each row, then sum those totals.
### Solution
```python
def matrix_sum(mat: list[list[int]]) -> int:
return sum(sum(row) for row in mat)
``` | {"v": "ok", "content_hash": "9df5661670daf098"} |
vcr_6b91c0b87499 | 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": "baccccab"}, "output": 5}, {"input": {"s": "bbaaa"}, "output": 2}, {"input": {"s": "cbccbc"}, "output": 5}] | 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": "47d2f3a1d74e2b53"} |
vcr_a1b606076313 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [17, 15, 8, 13, 8, 2, 25, 25]}, "output": 58}, {"input": {"nums": [16, 4, 1, 2, 3, 8]}, "output": 26}, {"input": {"nums": [19, 13, 10, 17, 17, 6, 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": "1b10b964a9444dc7"} |
vcr_4bbc69f5ad91 | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "dabcbcdaddb"}, "output": false}, {"input": {"s": "ccaabbaba"}, "output": false}, {"input": {"s": "bcaacbba"}, "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": "b65ac3c803635ec8"} |
vcr_4f7ab5046db9 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 1, 2, 2, 6, 5]}, "output": [120, 600, 300, 300, 100, 120]}, {"input": {"nums": [2, 4, 2, 3, 3, 1, 3]}, "output": [216, 108, 216, 144, 144, 432, 144]}, {"input": {"nums": [4, 2, 5, 3]}, "output": [30, 60, 24, 40]}] | 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": "dc51ab00314c25b1"} |
vcr_a15d92aac151 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-24, -19, -10, 9, 23, 24, 30, 35, 45, 47], "target": 0}, "output": [0, 5]}, {"input": {"nums": [-18, -16, -7, 3, 9, 10, 13, 21, 25, 29, 35], "target": -25}, "output": [0, 2]}, {"input": {"nums": [-19, -14, -10, -1, 1, 24, 29, 31, 34, 35, 37], "target": 59}, "output": [5, 9]}] | 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": "1539cf5d248dcc1c"} |
vcr_0d2f1706decf | easy | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "bdbdabadbdb"}, "output": true}, {"input": {"s": "cacabaccbb"}, "output": false}, {"input": {"s": "abacc"}, "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": "42cf6bcdb9576e5d"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.