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_4e0d003af5fd | 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": [28, 9, 54, 4, 9, 28, 31, 4, 31]}, "output": 54}, {"input": {"nums": [17, 25, 19, 17, 42, 19, 25]}, "output": 42}, {"input": {"nums": [6, 20, 56, 6, 23, 20, 23]}, "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": "a0741d7684e5bcb7"} |
vcr_5ca3dba50633 | 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": ["ccbc", "ccac", "acb", "aca"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aaa", "aba", "bba", "bab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bba", "bbb", "aba", "abb", "baa"]}, "output": [1, 2, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "6ddebedbea5b3813"} |
vcr_9fad106051fd | 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, 1, 0, 3, 1, 3, 4, 1, 0]}, "output": false}, {"input": {"nums": [1, 2, 2, 1, 1, 0, 3, 0]}, "output": false}, {"input": {"nums": [1, 2, 1, 2, 0, 2, 1, 3, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "7901a419976daad2"} |
vcr_63e9c17f5f28 | 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": [7, 9, 11, -2, -18, -7, -15, 5, -13, -5, 17]}, "output": 27}, {"input": {"nums": [-5, -6, 11, 8]}, "output": 19}, {"input": {"nums": [5, -2, -5, -1]}, "output": 5}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "a1272e77260bb353"} |
vcr_e25319ed5889 | 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": [-6, 13, 23, 23, -11, 7, 2, -22, 18, -14, -14, 24, 35]}, "output": [-6, 7, 30, 53, 42, 49, 51, 29, 47, 33, 19, 43, 78]}, {"input": {"nums": [18, 15, -11, 8, 3, 4, 8, -9, 14]}, "output": [18, 33, 22, 30, 33, 37, 45, 36, 50]}, {"input": {"nums": [6, 2, -9]}, "output": [6, 8, -1]}] | 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": "ce3a69c39b346727"} |
vcr_9f3e33d2fa00 | 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": [15, 1, 23, 8, 46, 35, 15]}, "output": true}, {"input": {"nums": [19, 20, 13, 16]}, "output": false}, {"input": {"nums": [2, 12, 2, 11, 12, 1, 21, 8, 25, 8]}, "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": "022fecbe6fc168f4"} |
vcr_f30048198169 | 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": ["cbbc", "cac", "abb", "bbaa", "acc", "baab"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["aba", "baa", "baa", "aba", "baa", "aba"]}, "output": [6]}, {"input": {"strs": ["baa", "bba", "bba", "baa", "bab"]}, "output": [2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "566e3353354867b3"} |
vcr_0d1ba8b6f019 | 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, -10, 3, 15, 20, 14, 5, 9, 34, -21, 20]}, "output": [-18, -28, -25, -10, 10, 24, 29, 38, 72, 51, 71]}, {"input": {"nums": [17, -1, 13]}, "output": [17, 16, 29]}, {"input": {"nums": [13, -8, -7, -5, 14, 6, 14, -1, -1, 3]}, "output": [13, 5, -2, -7, 7, 13, 27, 26, 25, 28]}] | 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": "e5af01d2c2e0f18c"} |
vcr_9a9709d69fe0 | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "c daebe beee ebd ac"}, "output": "ac ebd beee daebe c"}, {"input": {"s": "bbab bbab"}, "output": "bbab bbab"}, {"input": {"s": "b a abba b"}, "output": "b abba a 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": "fadcfc219be05fcb"} |
vcr_83d6bc094526 | 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": [24, 14, 15, 15, 16, 13, 4]}, "output": 59}, {"input": {"nums": [16, 5, 13, 0, 4]}, "output": 33}, {"input": {"nums": [13, 0, 10, 16, 17, 13, 1, 9, 14]}, "output": 56}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "0c950c4b9de5cb09"} |
vcr_91f22927967f | 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": [-22, -11, -16, 28, 4, 13, 2, 22, -5, -11, 20]}, "output": [-22, -33, -49, -21, -17, -4, -2, 20, 15, 4, 24]}, {"input": {"nums": [14, 12]}, "output": [14, 26]}, {"input": {"nums": [-3, 5, -12, -12, -10]}, "output": [-3, 2, -10, -22, -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": "53b3e3017f4985d6"} |
vcr_60d52bf80f69 | 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": [14, 22, 31, 32, 33, 34, 40, 45, 48], "target": 30}, "output": 2}, {"input": {"nums": [5, 6, 8, 16, 17, 32, 36, 39], "target": 40}, "output": 8}, {"input": {"nums": [0, 2, 21, 25, 34, 35, 38], "target": 29}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "eaa33ecca88c227f"} |
vcr_494f60a1cc2c | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-8, 3, -1, -11, -14, 5, 3, -16, 6], "k": 22}, "output": [5, 3, -16, 6, -8, 3, -1, -11, -14]}, {"input": {"nums": [7, 4, 4, 1, 4, 7, -1, -4, -9], "k": 12}, "output": [-1, -4, -9, 7, 4, 4, 1, 4, 7]}, {"input": {"nums": [-2, -7, -3], "k": 17}, "output": [-7, -3, -2]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "bb07e4689abab9cf"} |
vcr_fe025dedba83 | 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, 38, 11, 38, 52, 11, 21]}, "output": 52}, {"input": {"nums": [28, 28, 22, 15, 22, 54, 15]}, "output": 54}, {"input": {"nums": [24, 24, 21, 10, 52, 10, 21, 25, 25]}, "output": 52}] | 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": "8aab1e26ed07c757"} |
vcr_7c0ae448adcc | 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, -2, 14, 13, 3, -11, -12, 16, 16, -16, -18, -7, -11]}, "output": 56}, {"input": {"nums": [7, -7, 12, -3, -10, 7, 9, 10, -6, 12]}, "output": 32}, {"input": {"nums": [2, -10, -3, 8, 5, -7, -3, 3, 11, 9, -9]}, "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": "020131c5073242bb"} |
vcr_590a4a685abb | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-26, -16, -9, -4, 17, 21, 35, 39, 44, 47], "target": -9}, "output": [0, 4]}, {"input": {"nums": [-14, -2, 0, 4, 9, 14, 22, 30, 33, 36], "target": -14}, "output": [0, 2]}, {"input": {"nums": [4, 8, 10, 14, 38], "target": 48}, "output": [2, 4]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "90f37481b1608cd7"} |
vcr_21b213e738bd | 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": [34, 154, 68, 98, 22, 0]}, "output": false}, {"input": {"nums": [6, 15, 16, 23, 6, 4, 15, 2, 5, 2, 14]}, "output": true}, {"input": {"nums": [22, 22, 21, 11]}, "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": "2b604ec11f91bbe0"} |
vcr_de7fe5fd2e9b | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [28, 12, 27, 27, 26, 20, 13, 1, 10, 3]}, "output": 104}, {"input": {"nums": [14, 14, 6]}, "output": 20}, {"input": {"nums": [0, 6, 3, 1, 10]}, "output": 16}] | 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": "52a6105d52ec5d2e"} |
vcr_cc1c280cd9c5 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [7, 8, 23, 26], "target": 48}, "output": 4}, {"input": {"nums": [2, 3, 4, 5, 12, 13, 18, 27, 28, 30, 36, 37], "target": 8}, "output": 4}, {"input": {"nums": [1, 2, 8, 22, 31], "target": 30}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "cba42cf46375afc1"} |
vcr_7c8417979acc | 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, 5, 9, 10, 11, 14, 15, 23, 24, 41, 43, 44], "target": 22}, "output": 8}, {"input": {"nums": [2, 14, 30, 33, 34, 37], "target": 4}, "output": 1}, {"input": {"nums": [4, 8, 16, 20, 23, 24, 28, 33, 38, 39], "target": 38}, "output": 8}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "cbe057d41074416e"} |
vcr_2dba59913bba | 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": "cabcccaaaacccbac"}, "output": true}, {"input": {"s": "abaaaca"}, "output": false}, {"input": {"s": "aaaaaaabbb"}, "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": "b8299aaf3b2f9fcc"} |
vcr_0ff8e6f67777 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [10, 2, 20]}, "output": 30}, {"input": {"nums": [11, 10, 18, 7, 3, 10, 17, 13, 15]}, "output": 64}, {"input": {"nums": [15, 14, 20, 18]}, "output": 35}] | 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": "31eed337f10cb0c7"} |
vcr_6d4aad12bec3 | 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, 3, 7, 5, 6, 7, 4, 4]}, "output": [70560, 117600, 50400, 70560, 58800, 50400, 88200, 88200]}, {"input": {"nums": [4, 4, 4, 3, 4]}, "output": [192, 192, 192, 256, 192]}, {"input": {"nums": [5, 2, 4, 5, 1, 3, 3]}, "output": [360, 900, 450, 360, 1800, 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": "d9eb52a38cc412d9"} |
vcr_36ca831d0dac | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [6, 12, 12, -19, -14, -10, -1, 1], "k": 5}, "output": [-19, -14, -10, -1, 1, 6, 12, 12]}, {"input": {"nums": [2, 6, 7, -7, -8, -1, 1, 3, -4, -2], "k": 4}, "output": [1, 3, -4, -2, 2, 6, 7, -7, -8, -1]}, {"input": {"nums": [3, 4, -8, -1, 5, -6, -6, 5], "k": 2}, "output": [-6, 5, 3, 4, -8, -1, 5, -6]}... | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "ceb3c2bd4406e14e"} |
vcr_cfccfbf29051 | 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": [31, 62, -12, -37, 6, 27, 38], "target": 65}, "output": [5, 6]}, {"input": {"nums": [35, -26, -10, -18, 29, 9, 10, 13], "target": 19}, "output": [2, 4]}, {"input": {"nums": [-23, -22, 14, 25, 49, 35, 10, -10, 9], "target": 4}, "output": [2, 7]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "576328ca5c2290f7"} |
vcr_09ef73d68f38 | 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": [-18, 17, -6, -4, -9, -1, 4, 18]}, "output": 22}, {"input": {"nums": [1, 3, -4, 3, 4, -2, 0, 4, -7]}, "output": 9}, {"input": {"nums": [9, 14, -5, -11]}, "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": "1421dc69d5db8127"} |
vcr_3a95716a2770 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [10, 20, 28, 14, 26, 24, 4, 28, 28]}, "output": 96}, {"input": {"nums": [4, 15, 20, 6, 9, 7, 9, 17, 16]}, "output": 58}, {"input": {"nums": [17, 17, 16, 13]}, "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": "de2184f3a5236715"} |
vcr_5146c6ae53b2 | 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": [40, 37, -30, 55, -10, 0, 11, 63, 8, -5, -25, -7, -28, -16], "target": -30}, "output": [2, 5]}, {"input": {"nums": [25, 9, 37, -2, 14, 47, -26, -14, -5, 26], "target": -5}, "output": [1, 7]}, {"input": {"nums": [38, 4, -17, -27, 2], "target": -23}, "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": "7721aa122c34c6e7"} |
vcr_fc0202338595 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-11, -12, -18, 1, 19, 12, 11]}, "output": [-11, -23, -41, -40, -21, -9, 2]}, {"input": {"nums": [5, 3, 17]}, "output": [5, 8, 25]}, {"input": {"nums": [7, -9, 1, -3, -12, -12, -4]}, "output": [7, -2, -1, -4, -16, -28, -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": "1a62ef81ac613bf5"} |
vcr_7889402a3da8 | 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, 14, 27, 28, 38, 44, 47, 48], "target": 40}, "output": 6}, {"input": {"nums": [1, 4, 20], "target": 0}, "output": 0}, {"input": {"nums": [0, 1, 12, 16, 20, 22, 23, 27, 28, 30, 38, 39], "target": 24}, "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": "9d174b466d008fa7"} |
vcr_ee8ff38da275 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 3, 5, 6, 6, 8, 8, 9, 10, 12, 13, 15, 15, 15]}, "output": [1, 3, 5, 6, 8, 9, 10, 12, 13, 15]}, {"input": {"nums": [0, 0, 1, 4, 8, 11]}, "output": [0, 1, 4, 8, 11]}, {"input": {"nums": [0, 2, 3, 4, 7, 7, 8, 9]}, "output": [0, 2, 3, 4, 7, 8, 9]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "309823a5f22e2a6e"} |
vcr_c7eeea1ca196 | 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": "bdba ceccb bc bbb"}, "output": "bbb bc ceccb bdba"}, {"input": {"s": "b baab"}, "output": "baab b"}, {"input": {"s": "b bb"}, "output": "bb b"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "8634c99c03ce6bcb"} |
vcr_9dc1f574e775 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [32, -13, -1, 31, -11, -18, -3]}, "output": [32, 19, 18, 49, 38, 20, 17]}, {"input": {"nums": [9, 4, 18, -4]}, "output": [9, 13, 31, 27]}, {"input": {"nums": [-7, -7, -6, -9]}, "output": [-7, -14, -20, -29]}] | 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": "6f48c42caa8faf54"} |
vcr_ba9bef8553dc | 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": "ab ddcdca"}, "output": "ddcdca ab"}, {"input": {"s": "aaba b aa"}, "output": "aa b aaba"}, {"input": {"s": "abba a aabb aa"}, "output": "aa aabb a abba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "cc2e7ce8421999cb"} |
vcr_0a9915b7a139 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-26, -20, -16, -6, -5, -4, 19, 20, 30, 41, 45], "target": 14}, "output": [2, 8]}, {"input": {"nums": [-2, 0, 12, 24, 27], "target": 10}, "output": [0, 2]}, {"input": {"nums": [-14, -13, 0, 7, 23, 30, 36], "target": 30}, "output": [2, 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": "0ba0e11ee6ef212e"} |
vcr_9e01a2cf7fe1 | 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": [28, 22, 18, 17, 25, 51, 47]}, "output": 34}, {"input": {"prices": [32, 3, 18, 15, 30, 13, 24]}, "output": 27}, {"input": {"prices": [37, 11, 6, 7, 28, 31, 4, 29]}, "output": 25}] | 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": "a1e2011ed802d4c5"} |
vcr_d18661da47ec | 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, 4, 11, 16, 20, 25, 32, 35, 48, 49], "target": 31}, "output": 6}, {"input": {"nums": [0, 2, 3, 5, 10, 13, 28, 35, 36, 38], "target": 39}, "output": 10}, {"input": {"nums": [1, 2, 3, 7, 8, 18, 22, 25, 30, 31, 33], "target": 7}, "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": "baf677f58dc399d9"} |
vcr_64f7dd9dc907 | 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": [-4, 76, 44, 7, 31], "target": 38}, "output": [3, 4]}, {"input": {"nums": [-18, -28, 40, -16, -26, 1, 34], "target": 22}, "output": [0, 2]}, {"input": {"nums": [44, -18, -9, 1, 29, -11, -4, 15], "target": 18}, "output": [4, 5]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "997378c3f9b8b648"} |
vcr_193cb52f4c56 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [13, 36, 36, 24, 17, 8, 13, 24, 17, 25, 75, 25, 8]}, "output": 75}, {"input": {"nums": [15, 10, 10, 8, 41, 23, 23, 15, 8]}, "output": 41}, {"input": {"nums": [14, 24, 42, 26, 26, 24, 14, 13, 13]}, "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": "cbee5ded9ccfaa17"} |
vcr_3038ee1435f3 | 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": [7, 4, 2, 2, 5, 0, 3]}, "output": [0, 0, 0, 0, 0, 1680, 0]}, {"input": {"nums": [4, 4, 4, 2, 5]}, "output": [160, 160, 160, 320, 128]}, {"input": {"nums": [3, 4, 2, 3, 1, 3, 3]}, "output": [216, 162, 324, 216, 648, 216, 216]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "d663f66aae46bcd7"} |
vcr_2e4f3170668b | 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": [-19, 13, 3, -23]}, "output": [-19, -6, -3, -26]}, {"input": {"nums": [-2, -6, 10, 16, -12]}, "output": [-2, -8, 2, 18, 6]}, {"input": {"nums": [-4, -1, -7, -1, 5, 9, -8, -10, -10]}, "output": [-4, -5, -12, -13, -8, 1, -7, -17, -27]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "0587403183e2d509"} |
vcr_44e69b3d6ebe | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [12, -4, -6, 18, 19, -17], "k": 5}, "output": [-4, -6, 18, 19, -17, 12]}, {"input": {"nums": [-1, -1, -4, -2, 3], "k": 14}, "output": [-1, -4, -2, 3, -1]}, {"input": {"nums": [8, -10, 9, 5, 0, 7], "k": 3}, "output": [5, 0, 7, 8, -10, 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": "05905f3a3fd59697"} |
vcr_190e3511481b | 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": [5, 17, 14, 29, 29, 72, 5, 17, 14]}, "output": 72}, {"input": {"nums": [26, 2, 6, 6, 2, 25, 25, 7, 22, 26, 7, 22, 40]}, "output": 40}, {"input": {"nums": [5, 28, 28, 7, 16, 5, 58, 7, 16]}, "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": "f7ad3fe16ff354de"} |
vcr_e94f03b4fccf | 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": [30, -31, 61, 72, 26], "target": -1}, "output": [0, 1]}, {"input": {"nums": [-25, 41, 36, 31, 48], "target": 79}, "output": [3, 4]}, {"input": {"nums": [18, -1, 1, -2, 0, 42, -27, 21, -23, -16], "target": -25}, "output": [3, 8]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "8a7e5e316ce9f5fd"} |
vcr_d2b85302fa54 | 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": "db c aeae abdab db"}, "output": "db abdab aeae c db"}, {"input": {"s": "bba bb aba baab bbba"}, "output": "bbba baab aba bb bba"}, {"input": {"s": "bb baa b"}, "output": "b baa 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": "6d880463eb17fb8a"} |
vcr_780e0a8865fb | 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": ["bac", "acaa", "bbc", "cb", "ac", "acbb"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "baa", "bab"]}, "output": [1, 2]}, {"input": {"strs": ["aba", "bba", "baa", "abb", "bbb", "baa"]}, "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": "a977ca1587c097d9"} |
vcr_af556c548b6b | 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, 4, 1, 1, 2, 0]}, "output": [0, 0, 0, 0, 0, 0, 240]}, {"input": {"nums": [2, 3, 2, 2, 5, 3]}, "output": [180, 120, 180, 180, 72, 120]}, {"input": {"nums": [2, 1, 4, 1]}, "output": [4, 8, 2, 8]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "7d4dbd55eb0299cc"} |
vcr_3bece0fb7d1e | 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": "acabbbcab"}, "output": 7}, {"input": {"s": "acbcaaaaaab"}, "output": 6}, {"input": {"s": "bacc"}, "output": 3}] | 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": "e85f2e70865a916b"} |
vcr_b68665ff8a8f | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [13, 13, 65, 31, 9, 9, 27, 11, 31, 11, 27]}, "output": 65}, {"input": {"nums": [2, 2, 23, 19, 23, 7, 7, 49, 22, 19, 18, 22, 18]}, "output": 49}, {"input": {"nums": [51, 21, 8, 11, 13, 21, 11, 8, 13]}, "output": 51}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "d91fe30745dd0ade"} |
vcr_4287b79679fb | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [3, 2, 2, 5, 3, 2, 3, 4]}, "output": [1440, 2160, 2160, 864, 1440, 2160, 1440, 1080]}, {"input": {"nums": [5, 1, 5, 2, 4]}, "output": [40, 200, 40, 100, 50]}, {"input": {"nums": [2, 2, 4]}, "output": [8, 8, 4]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "9aef4990ce5b40b2"} |
vcr_21f06fc1b74c | 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": 214, "b": 122}, "output": 2}, {"input": {"a": 135, "b": 110}, "output": 5}, {"input": {"a": 57, "b": 39}, "output": 3}] | 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": "0489de84d7a54366"} |
vcr_f335cb0db926 | 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": 24, "b": 215}, "output": 1}, {"input": {"a": 150, "b": 144}, "output": 6}, {"input": {"a": 76, "b": 112}, "output": 4}] | 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": "cdde78223374c581"} |
vcr_142874f5d076 | 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": [-9, 65, 66, 25, 14, -26, -18, 3, 34, -32, 37, 61], "target": 8}, "output": [5, 8]}, {"input": {"nums": [-18, -13, 20, 23], "target": 43}, "output": [2, 3]}, {"input": {"nums": [42, 48, -10, 16, -16, 38, 22, 20, 2, 5], "target": 53}, "output": [1, 9]}] | 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": "fc60c9f214bf5695"} |
vcr_c2dfba4185dd | 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": "bcacddacbdccdbb"}, "output": false}, {"input": {"s": "abaabcaacb"}, "output": false}, {"input": {"s": "ab"}, "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": "2a41e78d37f9eb9b"} |
vcr_57594e1ab5bd | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [13, 17, 11, -18, -15, -16, -18, 9, 2, 19, -1]}, "output": 41}, {"input": {"nums": [-3, 2, 4, 10, -12]}, "output": 16}, {"input": {"nums": [2, 8, -8, 13, 10, 13, -7, 6, -2, 11]}, "output": 46}] | 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": "1fbf93e13ae575fd"} |
vcr_2b99dfd8052e | 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": "bbbababbacb"}, "output": 8}, {"input": {"s": "cabccac"}, "output": 6}, {"input": {"s": "babbabbcbcc"}, "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": "9b7732e7d56dc83c"} |
vcr_2deba5a44e9f | 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": "babbbcbc"}, "output": 6}, {"input": {"s": "cbbacbc"}, "output": 6}, {"input": {"s": "cbcaabaaccbb"}, "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": "8eea0e7949c1393a"} |
vcr_023cc881a5f5 | 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": [-27, -19, 13, 17, 44], "target": 61}, "output": [3, 4]}, {"input": {"nums": [-17, -11, -10, 2, 5, 8, 16, 22], "target": -8}, "output": [2, 3]}, {"input": {"nums": [2, 34, 37, 39], "target": 36}, "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": "915fe07b909df7d7"} |
vcr_d71287c9d28a | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [13, -15, -2, 17, -5, -5, 14, -1, 18, -8], "k": 4}, "output": [14, -1, 18, -8, 13, -15, -2, 17, -5, -5]}, {"input": {"nums": [-3, -9, -10], "k": 15}, "output": [-3, -9, -10]}, {"input": {"nums": [-10, 1, 1, 2, -3, 3, -2, -4, -4, -6], "k": 4}, "output": [-2, -4, -4, -6, -10, 1, 1, 2, -3, 3]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "c6913f09ff6819a5"} |
vcr_a9296a910001 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-1, 4, -15, -10, 14, 9, -3, -16], "k": 6}, "output": [-15, -10, 14, 9, -3, -16, -1, 4]}, {"input": {"nums": [0, 2, 3, 0, -10, 5, -1], "k": 4}, "output": [0, -10, 5, -1, 0, 2, 3]}, {"input": {"nums": [-10, -1, 10, 9, 0], "k": 14}, "output": [-1, 10, 9, 0, -10]}] | 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": "dc85d81da372d7ff"} |
vcr_dd44ddbed8a1 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [29, 2, 12, 25, 21, 14, 30]}, "output": 92}, {"input": {"nums": [17, 4, 11]}, "output": 28}, {"input": {"nums": [19, 15, 4, 14, 9, 12, 0, 16, 18]}, "output": 63}] | 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": "70e72fbaf34ae201"} |
vcr_9f18f8417778 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-12, -5, -16, 6, -17], "k": 15}, "output": [-12, -5, -16, 6, -17]}, {"input": {"nums": [-1, -3, 6, -4, 9, -10, -2], "k": 20}, "output": [-3, 6, -4, 9, -10, -2, -1]}, {"input": {"nums": [-2, 10, -3, -4, 0, -5, 9, -5], "k": 14}, "output": [-3, -4, 0, -5, 9, -5, -2, 10]}] | 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": "146e83fbcbdfbbe4"} |
vcr_523133e61398 | 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, 4, 3, 2, 2, 2, 3, 2, 0]}, "output": true}, {"input": {"nums": [1, 3, 2]}, "output": true}, {"input": {"nums": [2, 0, 1, 2, 2]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "5007eb02674af971"} |
vcr_c1e0df7358a9 | 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": [42, 8, 136, 159, 190, 77, 126, 110, 186, 192, 23, 69, 32, 196, 157, 24, 75, 123]}, "output": false}, {"input": {"nums": [0, 11, 17, 15, 14, 13, 10]}, "output": false}, {"input": {"nums": [18, 0, 6, 12, 4, 25, 14, 6]}, "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": "c44cb4072e13c2e8"} |
vcr_8240751e44b1 | 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": "bcaccccacabc"}, "output": 9}, {"input": {"s": "bcabbbacbcca"}, "output": 9}, {"input": {"s": "bbbbcbba"}, "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": "e9ab0838edc0f321"} |
vcr_c4ad47ed50e5 | 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": ["acb", "ac", "abbc", "bab", "acc", "acca"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "baa", "baa", "aba"]}, "output": [4]}, {"input": {"strs": ["aba", "bbb", "baa", "abb", "aaa"]}, "output": [1, 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": "ec8eb5b12478dc14"} |
vcr_0337896b2c46 | 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 d daea ace"}, "output": "ace daea d e"}, {"input": {"s": "aaba ba"}, "output": "ba aaba"}, {"input": {"s": "abba aba aaab"}, "output": "aaab aba abba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "c703afd51a237d35"} |
vcr_89fbab8c338b | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [26, 3, 22, 1, 35, 27, 20, 3, 57, 20, 27, 1, 22, 35, 26]}, "output": 57}, {"input": {"nums": [24, 8, 9, 46, 27, 9, 8, 7, 24, 28, 28, 7, 27]}, "output": 46}, {"input": {"nums": [14, 19, 14, 59, 19]}, "output": 59}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "f596892880b83af6"} |
vcr_e89835fb4e16 | 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": [39, 20, 78, 32, 32, 27, 27, 39, 20]}, "output": 78}, {"input": {"nums": [13, 29, 13, 29, 51]}, "output": 51}, {"input": {"nums": [1, 1, 23, 5, 23, 3, 3, 5, 57]}, "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": "c8972886a9aae41c"} |
vcr_93d0d6638cb5 | 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": [29, 47, 36, 5, 49, 17, 41, 33, 18, 11, 10, 0, 5, 40, 33, 46, 6]}, "output": true}, {"input": {"nums": [10, 21, 21, 18, 2, 12, 6, 4]}, "output": true}, {"input": {"nums": [11, 24, 16, 16, 24, 25, 6, 18, 15]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "c91e5eec2bee27b7"} |
vcr_934216656796 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [18, 9, 26, 28, 8, 19, 19, 1, 0, 24, 26]}, "output": 97}, {"input": {"nums": [4, 11, 12, 5, 0]}, "output": 16}, {"input": {"nums": [13, 9, 18, 10, 13, 4, 19, 5, 20]}, "output": 83}] | 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": "697f1c77fec4c90e"} |
vcr_822d2daecebb | 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": "badddacddbaddb"}, "output": false}, {"input": {"s": "baaaab"}, "output": true}, {"input": {"s": "aca"}, "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": "87a91a9e898c75e8"} |
vcr_b6979823cb39 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [29, 31, 2, 13, 13]}, "output": [29, 60, 62, 75, 88]}, {"input": {"nums": [16, 8, 13, 8]}, "output": [16, 24, 37, 45]}, {"input": {"nums": [-11, 17, -8, 7, 2]}, "output": [-11, 6, -2, 5, 7]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "8b8cfe314c45b0e3"} |
vcr_b678c3561745 | 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": [38, 9, 38, 30, 36, 24, 6, 53, 23, 36, 24, 9, 23, 30, 6]}, "output": 53}, {"input": {"nums": [3, 3, 56, 13, 13]}, "output": 56}, {"input": {"nums": [27, 23, 3, 18, 12, 23, 27, 12, 18, 41, 3]}, "output": 41}] | 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": "928220b87c29a83a"} |
vcr_6dd025c8c1fb | 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": 74, "b": 62}, "output": 2}, {"input": {"a": 6, "b": 99}, "output": 3}, {"input": {"a": 122, "b": 13}, "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": "f44d9c412c7781f2"} |
vcr_c60fe2603f3b | 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": [-7, -4, 0, -13], "k": 5}, "output": [-13, -7, -4, 0]}, {"input": {"nums": [7, -10, 3, 3, 4, 5, -10], "k": 9}, "output": [5, -10, 7, -10, 3, 3, 4]}, {"input": {"nums": [-5, -9, 8, 3, 9, -8, -10, 10, 0], "k": 18}, "output": [-5, -9, 8, 3, 9, -8, -10, 10, 0]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "902397923aba0686"} |
vcr_0682cb8603d9 | 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": ["cc", "baa", "aba", "ca", "bcb", "aacb", "ca"]}, "output": [1, 1, 1, 2, 2]}, {"input": {"strs": ["baa", "bba", "baa"]}, "output": [1, 2]}, {"input": {"strs": ["bab", "aab", "aab"]}, "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": "b52394af89220b44"} |
vcr_486f2b8b0967 | 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": ["aaac", "aabc", "bca", "bab", "cbaa", "bbab"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "bba", "aaa", "bab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aaa", "bba", "aaa", "aab", "aab", "bba", "abb"]}, "output": [2, 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": "7ec1a803ef1858cf"} |
vcr_2b62489363cd | 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, 4, -2, -13, 11, 2, 18, 10]}, "output": [-10, -6, -8, -21, -10, -8, 10, 20]}, {"input": {"nums": [14, -3, 0, 3, -9]}, "output": [14, 11, 11, 14, 5]}, {"input": {"nums": [-5, -9, 18, -11, 8, 7]}, "output": [-5, -14, 4, -7, 1, 8]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "48a19a68e43fba9e"} |
vcr_76ef1b0c3218 | easy | math | Return the greatest common divisor of positive integers a and b. | def gcd(a: int, b: int) -> int: | Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. | def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
| [{"input": {"a": 180, "b": 13}, "output": 1}, {"input": {"a": 155, "b": 61}, "output": 1}, {"input": {"a": 108, "b": 118}, "output": 2}] | true | python | 0a8801b251631b1d | ### Problem
Return the greatest common divisor of positive integers a and b.
### Reasoning
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
### Solution
```python
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
``` | {"v": "ok", "content_hash": "20caea59296d4149"} |
vcr_64bdbc62589f | 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": [18, 6, -17, 0, -8, -13, 8, 18, -14, 7, 6, -8, 1, 3, 7]}, "output": 28}, {"input": {"nums": [9, 7, 10]}, "output": 26}, {"input": {"nums": [14, 7, 11, -9, 10, 2, 8, 7, -1, -10, 10]}, "output": 50}] | 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": "10990f8b3dbffa1e"} |
vcr_b2479dca0b12 | 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": "abbbddccddbbba"}, "output": true}, {"input": {"s": "bccacc"}, "output": false}, {"input": {"s": "aaaac"}, "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": "61f09e54e130d713"} |
vcr_b4dfdcd5021a | 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, 1, 0, 1, 3, 1, 0, 0, 0]}, "output": false}, {"input": {"nums": [2, 0, 2, 3, 0, 1, 1]}, "output": true}, {"input": {"nums": [3, 1, 2, 2, 3, 2, 0]}, "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": "a3aa6768ea1470e2"} |
vcr_22410b0904af | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [50, 30, 36, 1, 27, 21, 13, 11, 48, 50]}, "output": true}, {"input": {"nums": [1, 19, 6, 11, 0]}, "output": false}, {"input": {"nums": [20, 0, 18, 13, 18, 25, 11, 14, 2, 10, 25, 1]}, "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": "b3ffe149e8dcf412"} |
vcr_2d037987f22f | 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": [92, 84, 86, 32, 87, 178, 54]}, "output": false}, {"input": {"nums": [24, 9, 3, 19, 16, 18, 2]}, "output": false}, {"input": {"nums": [15, 2, 11, 4]}, "output": false}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "33d68287571ba40a"} |
vcr_acaac053bc11 | 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": 52417}, "output": 7}, {"input": {"n": 9757}, "output": 7}, {"input": {"n": 8270}, "output": 5}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "7f53efc675ffad47"} |
vcr_a9d715227b38 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-25, -15, -6, 35, 37, 38, 41, 43], "target": 23}, "output": [1, 5]}, {"input": {"nums": [-14, -7, -6, 19, 28, 29, 35, 36], "target": 64}, "output": [4, 7]}, {"input": {"nums": [-17, -16, 1, 11, 21], "target": -33}, "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": "33e9de4d69cd6d75"} |
vcr_0cdad8a07a76 | 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": 59117}, "output": 11}, {"input": {"n": 3750}, "output": 7}, {"input": {"n": 14072}, "output": 9}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "31bf1f90ad8225db"} |
vcr_7a21ed9fc860 | 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": "ecacc ddd e e edea d c"}, "output": "c d edea e e ddd ecacc"}, {"input": {"s": "aaba ba b b baa"}, "output": "baa b b ba aaba"}, {"input": {"s": "baba b b"}, "output": "b b baba"}] | 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": "78d23d2d1c0d2625"} |
vcr_6369405d70ab | 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": [25, 3, 70, 4, 25, 3, 12, 12, 24, 20, 24, 20, 4]}, "output": 70}, {"input": {"nums": [28, 27, 28, 55, 14, 7, 19, 14, 7, 27, 19]}, "output": 55}, {"input": {"nums": [22, 3, 54, 22, 3]}, "output": 54}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "2383af4ecc090957"} |
vcr_ad43624e9b3f | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [48, 38, 3, 9, 49, 50]}, "output": 47}, {"input": {"prices": [28, 14, 14, 34, 5, 25, 6, 5, 26, 5, 13, 16]}, "output": 21}, {"input": {"prices": [38, 10, 17]}, "output": 7}] | 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": "51b6a2461e78cad0"} |
vcr_72775990f643 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-23, -21, -24, 23, 22, 7, 4]}, "output": [-23, -44, -68, -45, -23, -16, -12]}, {"input": {"nums": [5, -4, 8, 2, 10, -8, 18, 11]}, "output": [5, 1, 9, 11, 21, 13, 31, 42]}, {"input": {"nums": [14, -3, 1, -2]}, "output": [14, 11, 12, 10]}] | 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": "dee864c5b5778dce"} |
vcr_74aecf49789d | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [3, 1, 2, 3, 4, 2, 4, 0]}, "output": true}, {"input": {"nums": [1, 1, 0, 2, 2, 3, 1, 0]}, "output": false}, {"input": {"nums": [2, 0, 2, 1, 1, 0, 2]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "ebde255331464d2f"} |
vcr_8be851b050de | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [10, -13, -7, 7, -20, -16, 7, -18, 10, -12, 9, 5], "k": 1}, "output": [5, 10, -13, -7, 7, -20, -16, 7, -18, 10, -12, 9]}, {"input": {"nums": [2, 7, -3, -8], "k": 13}, "output": [-8, 2, 7, -3]}, {"input": {"nums": [9, -5, -3, 8], "k": 16}, "output": [9, -5, -3, 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": "f6ec14050a630e6e"} |
vcr_3adf3c67d660 | 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, 76, 21, 33, 31, 31, 33]}, "output": 76}, {"input": {"nums": [26, 24, 52, 29, 24, 8, 26, 29, 8]}, "output": 52}, {"input": {"nums": [22, 11, 22, 11, 25, 25, 15, 5, 5, 53, 15]}, "output": 53}] | true | python | f76c1da3c46d529e | ### Problem
Every element appears twice except one. Find the single element using XOR.
### Reasoning
XOR cancels pairs. The remaining value is the single number.
### Solution
```python
def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
``` | {"v": "ok", "content_hash": "7a9881f60fcba9a2"} |
vcr_f9a0695f44c1 | 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": ["bbbc", "ac", "acab", "bcac", "bb", "cc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "aab", "aaa", "baa", "baa", "bbb"]}, "output": [1, 2, 3]}, {"input": {"strs": ["aaa", "bba", "bba", "aab"]}, "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": "ac0d80b75a69fbbd"} |
vcr_23f0597dcf07 | 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": [34, 43, 73, 5, -37, 60], "target": 39}, "output": [0, 3]}, {"input": {"nums": [-24, -1, 13, -11], "target": -12}, "output": [1, 3]}, {"input": {"nums": [32, 46, -22, 16, -14, 3, 4, 27, 20, -11], "target": 66}, "output": [1, 8]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "0c6726dedee05a56"} |
vcr_94e79e363a8a | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [14, 4, 7, 0, 15, 15, 23, 30, 28]}, "output": 87}, {"input": {"nums": [15, 19, 1, 0]}, "output": 19}, {"input": {"nums": [15, 13, 8, 7, 0, 7, 1, 14, 0, 8]}, "output": 52}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "0a32754a0d362cef"} |
vcr_4bf78b15db08 | 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": ["cc", "bcbc", "bb", "aa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "bbb", "bba", "aaa", "bba", "baa", "bba"]}, "output": [1, 1, 1, 4]}, {"input": {"strs": ["bba", "baa", "bba", "aab", "baa", "aba", "bbb"]}, "output": [1, 2, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "265ff03f1bc663ba"} |
vcr_5d16ad7d475b | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-29, -14, -12, 2, 8, 18, 19, 20, 32, 33, 39], "target": -41}, "output": [0, 2]}, {"input": {"nums": [-18, -16, -6, 3, 6, 11, 31, 33, 35, 36], "target": 17}, "output": [0, 8]}, {"input": {"nums": [-17, 0, 3, 11, 21, 22, 26, 27, 30, 36], "target": 29}, "output": [2, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "34c02a76bf21df95"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.