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_08c8ccf65bcf | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [15, 30, 17, 22, 8, 25, 12, 14, 29]}, "output": 106}, {"input": {"nums": [0, 1]}, "output": 1}, {"input": {"nums": [9, 16, 20, 7, 4, 17]}, "output": 46}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "eae85d359c3e5c88"} |
vcr_7e10d591e0ba | 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": "cc bb bddca e e"}, "output": "e e bddca bb cc"}, {"input": {"s": "ab ba"}, "output": "ba ab"}, {"input": {"s": "a bab bb"}, "output": "bb bab a"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "ec3882d9505986c6"} |
vcr_3fb9585bfddd | 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, 19, -17, 6, -7, 15, 17, -1, -4, -6, 19]}, "output": 59}, {"input": {"nums": [12, -12, -4, 4, 9, 11, 2]}, "output": 26}, {"input": {"nums": [6, -10, 14, -2]}, "output": 14}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "83e9afc29b30a98b"} |
vcr_a0689820e63b | 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": [13, 26, 1, 28, 22, 7, 19, 27, 13]}, "output": 88}, {"input": {"nums": [18, 6, 14, 12, 3, 10, 14, 7, 0]}, "output": 49}, {"input": {"nums": [1, 16, 18, 18]}, "output": 34}] | 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": "f4380af15d573eb6"} |
vcr_51eff6598e01 | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-14, -10, -2, 14, 20, 21, 27, 33, 36, 44], "target": 19}, "output": [0, 7]}, {"input": {"nums": [-15, -14, -13, -7, -1, 2, 4, 6, 15, 25, 35], "target": 41}, "output": [7, 10]}, {"input": {"nums": [-18, -5, 2, 3, 11, 13, 16, 25, 39], "target": 21}, "output": [0, 8]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "0960ae52fb7c45fc"} |
vcr_f53caa8f867e | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [22, 22, 25, 24, 30, 22, 26]}, "output": 103}, {"input": {"nums": [0, 9, 7, 0]}, "output": 9}, {"input": {"nums": [15, 1, 11, 9, 3, 14, 0, 16, 20, 9]}, "output": 65}] | 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": "81b02aa99d01e0a5"} |
vcr_218edab37eac | 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": [0, 24, 45, 37, 32, 1, 50, 34, 8, 14, 29, 38, 32, 13, 39, 43]}, "output": true}, {"input": {"nums": [20, 20, 9, 3, 25, 24, 15, 16]}, "output": true}, {"input": {"nums": [1, 13, 8, 12, 12, 10, 7]}, "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": "883f8be1ae51d616"} |
vcr_2442f10859ed | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-30, -27, -17, 0, 6, 22, 23, 27, 28, 33, 34], "target": 55}, "output": [5, 9]}, {"input": {"nums": [-10, 18, 19, 28, 36], "target": 37}, "output": [1, 2]}, {"input": {"nums": [-19, -13, 9, 19, 20, 21, 23, 33, 36, 39], "target": 62}, "output": [6, 9]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "755571eb70f40e26"} |
vcr_5d8bdf877838 | 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": "bbabacbcaba"}, "output": 10}, {"input": {"s": "bbacaccbabcb"}, "output": 10}, {"input": {"s": "cbccb"}, "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": "0a22023ebe4d89c2"} |
vcr_4a711d7358be | 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, -13, 27]}, "output": [29, 16, 43]}, {"input": {"nums": [-10, 12, 3, -12, 1]}, "output": [-10, 2, 5, -7, -6]}, {"input": {"nums": [14, 14, -8, -9, 16, -9, 12, -3, 6, 12]}, "output": [14, 28, 20, 11, 27, 18, 30, 27, 33, 45]}] | 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": "84a57e1a0b5fac6c"} |
vcr_4a72d9914d94 | 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": "acacaddacaca"}, "output": true}, {"input": {"s": "bbbbaccca"}, "output": false}, {"input": {"s": "baabbaabab"}, "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": "776f49b4f527f0ab"} |
vcr_163769989c8b | 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": "bbdd ca dcd"}, "output": "dcd ca bbdd"}, {"input": {"s": "aaba ba bbbb aa a"}, "output": "a aa bbbb ba aaba"}, {"input": {"s": "bab aab aaab aa"}, "output": "aa aaab aab bab"}] | 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": "62737458be690b32"} |
vcr_ec3984be92b3 | 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": [17, 26, 38, 49], "target": 26}, "output": 1}, {"input": {"nums": [1, 3, 10, 11, 25, 28, 30, 32, 33, 34, 35, 37], "target": 10}, "output": 2}, {"input": {"nums": [1, 3, 4, 7, 11, 13, 15, 18, 19, 24, 29], "target": 12}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "01665d0dd58006b5"} |
vcr_314053d6f11c | 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": [27, 18, 50, 3, 32, 27]}, "output": true}, {"input": {"nums": [12, 20, 18, 12, 23, 13, 12, 7, 1, 19, 6]}, "output": true}, {"input": {"nums": [2, 13, 10, 11, 23, 4, 23, 15, 7, 0, 3]}, "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": "015b6ca6d7ca1cff"} |
vcr_c0fc9f981985 | 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": "ddabdadaad"}, "output": false}, {"input": {"s": "bbc"}, "output": false}, {"input": {"s": "cacbcb"}, "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": "d97fdd4e32b2359c"} |
vcr_7d9eaeb8206a | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [20, 20, 8, 27, 33, 60, 33, 27, 3, 8, 3]}, "output": 60}, {"input": {"nums": [12, 4, 23, 46, 12, 5, 18, 18, 4, 5, 23]}, "output": 46}, {"input": {"nums": [15, 51, 15, 9, 9]}, "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": "c12f5ebbbe5282c0"} |
vcr_10788bfe3731 | 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": [-25, 31, 23, 9, -10, -22, -21, -4, -5, 3, 28, 12, -10]}, "output": [-25, 6, 29, 38, 28, 6, -15, -19, -24, -21, 7, 19, 9]}, {"input": {"nums": [-5, 6]}, "output": [-5, 1]}, {"input": {"nums": [14, 7]}, "output": [14, 21]}] | 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": "17ca0eafc66352f6"} |
vcr_fda4bf2dbf22 | 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": 50341}, "output": 7}, {"input": {"n": 16027}, "output": 10}, {"input": {"n": 5493}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "b3c5c7dc5c54dc57"} |
vcr_bd9372c718ac | 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": [25, -18, -22, 28, -6]}, "output": [25, 7, -15, 13, 7]}, {"input": {"nums": [16, 4, 5, 16, -3, -12, 18, 14, -9, -5]}, "output": [16, 20, 25, 41, 38, 26, 44, 58, 49, 44]}, {"input": {"nums": [-12, -6, 8, 11, 17, -11, -11, -10, 2, -10]}, "output": [-12, -18, -10, 1, 18, 7, -4, -14, -12, -22]}] | 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": "b3655814ccdee097"} |
vcr_4e51a30596e3 | 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": [-26, 19, 25, 53], "target": 27}, "output": [0, 3]}, {"input": {"nums": [-28, -16, -17, 18, -30, -15, -6, -9], "target": 12}, "output": [3, 6]}, {"input": {"nums": [10, 23, -13, -7], "target": -20}, "output": [2, 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": "85dc680a6a233ee7"} |
vcr_2750c2876497 | 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": "bbcdacadcbb"}, "output": true}, {"input": {"s": "cabab"}, "output": false}, {"input": {"s": "cba"}, "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": "971f147ef5edec78"} |
vcr_15b61d1f15f4 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [19, 32, 22, 1, 57]}, "output": 56}, {"input": {"prices": [21, 32, 14, 4, 19, 23, 20, 1, 15, 34]}, "output": 33}, {"input": {"prices": [4, 16, 27]}, "output": 23}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "fdf0f4c83af57cc6"} |
vcr_a4445ad6fddb | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [15, 10, 24, 30, 18, 17, 30, 7, 2]}, "output": 89}, {"input": {"nums": [13, 17, 2, 11, 4, 20, 10]}, "output": 48}, {"input": {"nums": [7, 7, 12, 19, 7]}, "output": 26}] | 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": "dd7b7d55d75a401a"} |
vcr_236405c88f6b | 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, -18, 4, 24, 39, 43], "target": 47}, "output": [2, 5]}, {"input": {"nums": [14, 18, 29, 35], "target": 43}, "output": [0, 2]}, {"input": {"nums": [-12, -4, -3, 6, 7, 20, 24, 32, 34, 35], "target": -6}, "output": [0, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "282506a0bb7f6104"} |
vcr_7ba129e524fc | 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": "bbaaacaacbacb"}, "output": 9}, {"input": {"s": "aaabaabbb"}, "output": 4}, {"input": {"s": "bccaaa"}, "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": "aede260558b928f7"} |
vcr_1a80cbfe6724 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [11, 8, 19, 8, 12, 27, 14, 6, 5, 29, 29, 3]}, "output": 95}, {"input": {"nums": [2, 17, 10, 11, 12, 11]}, "output": 39}, {"input": {"nums": [19, 1, 16, 6, 1, 6, 16, 10, 15]}, "output": 67}] | 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": "58b05a781188be3e"} |
vcr_6cc1fc03dec3 | 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, 7, -12, -18, 17], "k": 0}, "output": [12, 7, -12, -18, 17]}, {"input": {"nums": [-4, 6, 0, -4, -3, -3, 4, -8, -10, -10], "k": 9}, "output": [6, 0, -4, -3, -3, 4, -8, -10, -10, -4]}, {"input": {"nums": [10, 2, 8, -9, -7], "k": 13}, "output": [8, -9, -7, 10, 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": "53a4d83f08408102"} |
vcr_3f1631a7ce83 | 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, 28, 14, 16, 21, 13, 16, 28, 14, 21, 65]}, "output": 65}, {"input": {"nums": [15, 21, 23, 15, 48, 21, 14, 23, 14]}, "output": 48}, {"input": {"nums": [7, 14, 7, 8, 16, 8, 3, 16, 54, 3, 14]}, "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": "23c641a1fe258bd5"} |
vcr_9281afea706d | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [1, 2, 5, 5, 9, 10, 11, 11]}, "output": [1, 2, 5, 9, 10, 11]}, {"input": {"nums": [5, 7, 11, 11, 11, 11, 12]}, "output": [5, 7, 11, 12]}, {"input": {"nums": [1, 1, 2, 4, 5, 5, 5, 6, 9, 12, 12]}, "output": [1, 2, 4, 5, 6, 9, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "3d0e7cdafafdf3ce"} |
vcr_9b69ce344d2e | 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, -17, 32, 13, 9, 9, 13, 19, 25]}, "output": [23, 6, 38, 51, 60, 69, 82, 101, 126]}, {"input": {"nums": [-2, 9, 6, 10, 0, 13, -5, 10, -9, 2]}, "output": [-2, 7, 13, 23, 23, 36, 31, 41, 32, 34]}, {"input": {"nums": [-7, 8, -1]}, "output": [-7, 1, 0]}] | 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": "1b84190d1b64353e"} |
vcr_56ad47f94ba3 | 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": [20, 1, -10, 15, 10, -5]}, "output": 36}, {"input": {"nums": [-2, -1, 0, -12, -12, 4]}, "output": 4}, {"input": {"nums": [-11, -5, 7, -12, 7, -5, 8]}, "output": 10}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "830ca476fb6adf52"} |
vcr_022a82e0d193 | 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": [12, -5, -16, -16, 11, -11, 1, 4, 14, 22, 10, 17, 0, -12]}, "output": 68}, {"input": {"nums": [11, -5, 2]}, "output": 11}, {"input": {"nums": [12, -12, -8, 4, 0, 2, 13, 6]}, "output": 25}] | 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": "3d3d7eca779fe21e"} |
vcr_37b582f87b50 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [66, 30, 17, 15, 19, 9, 13, 17, 19, 30, 13, 15, 9]}, "output": 66}, {"input": {"nums": [13, 22, 21, 21, 19, 26, 5, 26, 13, 19, 5, 22, 59]}, "output": 59}, {"input": {"nums": [8, 3, 12, 22, 23, 3, 23, 8, 12, 15, 58, 22, 15]}, "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": "8566e1306622055e"} |
vcr_8acf9748f34d | 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": "cacbbabbbbba"}, "output": 7}, {"input": {"s": "babcabbbaaca"}, "output": 9}, {"input": {"s": "cacbab"}, "output": 6}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "e592ca6174332e11"} |
vcr_a72915a2f0ef | 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": [91, 191, 10, 50, 15, 176, 6, 125, 127, 37, 8, 53, 42, 77, 21, 178, 166]}, "output": false}, {"input": {"nums": [20, 20, 7, 18, 20, 21, 16, 12, 22, 3, 13]}, "output": true}, {"input": {"nums": [12, 1, 24]}, "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": "5a0417e9e7886045"} |
vcr_b0153cfdf334 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [5, 7, 14, 17, 19, 26, 28, 34, 36, 49], "target": 49}, "output": 9}, {"input": {"nums": [15, 16, 24, 26, 27, 28], "target": 33}, "output": 6}, {"input": {"nums": [5, 9, 18, 29, 30, 33, 39], "target": 33}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "0c67d963bca86d63"} |
vcr_17a567fc52a2 | 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": [22, 40, 45, 19, 22]}, "output": true}, {"input": {"nums": [15, 25, 5, 10, 11, 11, 20, 14, 14, 3]}, "output": true}, {"input": {"nums": [25, 6, 1, 10, 19, 13, 22, 4, 17, 15]}, "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": "d675e9b1556b2301"} |
vcr_7def098b2df1 | 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": [-15, 8, 16, 22, 4, -25, 29, 27, -25, -3, 4]}, "output": [-15, -7, 9, 31, 35, 10, 39, 66, 41, 38, 42]}, {"input": {"nums": [5, 18, -5, 18, -4, 9, -6]}, "output": [5, 23, 18, 36, 32, 41, 35]}, {"input": {"nums": [7, 3, 1, 7, -2, 8, -11, 16, 8]}, "output": [7, 10, 11, 18, 16, 24, 13, 29, 37]}] | 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": "30ab14ad00c421e9"} |
vcr_fdeb33283c51 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [0, -17, 5, -16, 11, 5, 19, -16, 7, -6, 19, 0], "k": 15}, "output": [-6, 19, 0, 0, -17, 5, -16, 11, 5, 19, -16, 7]}, {"input": {"nums": [5, -2, 10, -4, 9, -7, -8], "k": 7}, "output": [5, -2, 10, -4, 9, -7, -8]}, {"input": {"nums": [4, 10, 6, 5, -3, -2, -2, 0, -7], "k": 4}, "output": [-2, -2, 0, -7, ... | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "07bffb95ca8a4439"} |
vcr_f1d08690aea4 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [1, 4, 10, 12, 16, 19, 29, 33, 37, 38, 40, 45], "target": 27}, "output": 6}, {"input": {"nums": [6, 7, 11, 13, 14, 16, 31, 32, 36, 37, 39], "target": 9}, "output": 2}, {"input": {"nums": [4, 27, 37], "target": 10}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "31feed5fc00176e8"} |
vcr_1a81cab08808 | 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": [-26, -10, 34, 20, -22, 65, 30, 77], "target": 20}, "output": [1, 6]}, {"input": {"nums": [24, 1, 36, 29], "target": 60}, "output": [0, 2]}, {"input": {"nums": [42, -30, 35, 23, 32, -20, -15, 34, -1], "target": 27}, "output": [0, 6]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "c148173824763909"} |
vcr_211451f2ea34 | 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": [51, 34, 30, 36, 38, 46, 48, 46, 21]}, "output": 18}, {"input": {"prices": [32, 30, 36]}, "output": 6}, {"input": {"prices": [18, 1, 16, 13]}, "output": 15}] | 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": "5242ca484f8f50d7"} |
vcr_9988370e999a | 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": 59001}, "output": 10}, {"input": {"n": 14572}, "output": 8}, {"input": {"n": 7106}, "output": 7}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "c375c3faa185437a"} |
vcr_95ec4430f4ed | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["aa", "aaa", "aa", "cbbc", "bba", "ba", "cc", "aba", "cab"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["baa", "aab", "abb", "abb", "bab"]}, "output": [2, 3]}, {"input": {"strs": ["aaa", "aaa", "bba", "aaa", "aaa"]}, "output": [1, 4]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "770a7fea6d493ac3"} |
vcr_92d7470b3b85 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [30, 4, 22, 15, 16, 6]}, "output": 68}, {"input": {"nums": [12, 20, 6, 0]}, "output": 20}, {"input": {"nums": [1, 15, 0, 9, 16, 4, 18, 16, 8]}, "output": 57}] | 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": "354a824021ae5e33"} |
vcr_04fc9f9b4db8 | 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, 26, 26, 17, 4, 29, 25, 21, 22, 27]}, "output": 121}, {"input": {"nums": [3, 18, 13, 14, 20, 19, 14]}, "output": 52}, {"input": {"nums": [11, 9]}, "output": 11}] | 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": "bfb3d132eb9f93e0"} |
vcr_0aabbfc48e08 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [31, 21, 20, -16, 2, 26, 26, 14, 30, 31, 8, 25, -7, 12]}, "output": [31, 52, 72, 56, 58, 84, 110, 124, 154, 185, 193, 218, 211, 223]}, {"input": {"nums": [-2, 8, -11, -3, -11, 1, 2, 11, 10, 10]}, "output": [-2, 6, -5, -8, -19, -18, -16, -5, 5, 15]}, {"input": {"nums": [-2, 3, 3, 13, 7, 11, -2, -6, 4... | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "3b35eb73e9c70840"} |
vcr_1a52f793940f | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 0, 4, 1, 2, 2, 0]}, "output": false}, {"input": {"nums": [0, 0, 0, 1, 0, 1, 1, 2, 2]}, "output": false}, {"input": {"nums": [1, 2, 0, 3, 2, 1, 0, 2, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "ad7747a57aa86657"} |
vcr_fd2bc58e42a8 | 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, -26, -20, -2, 6, 9, 10, 20, 39], "target": -46}, "output": [1, 2]}, {"input": {"nums": [-20, -16, 7, 18, 39], "target": -2}, "output": [0, 3]}, {"input": {"nums": [-18, -10, -4, 13, 39], "target": 9}, "output": [2, 3]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "95ad6cbb13be93a3"} |
vcr_4ac6fc45d3d8 | 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": [36, 36, 28, 56, 55, 45, 43, 41]}, "output": 28}, {"input": {"prices": [33, 32, 34, 33, 4, 13, 17, 18, 22, 24]}, "output": 20}, {"input": {"prices": [26, 13, 6, 27, 24, 9, 14, 5, 11, 20, 13, 15]}, "output": 21}] | 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": "0bbdac22be8ba682"} |
vcr_e7edab736d19 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [0, 13, 18, 22, 26, 34, 36], "target": 18}, "output": 2}, {"input": {"nums": [3, 10, 14, 18, 23, 33], "target": 19}, "output": 4}, {"input": {"nums": [5, 14, 15, 20, 23, 27, 34, 35, 36], "target": 21}, "output": 4}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "a9cfb237ff270ae2"} |
vcr_0bc9a887d90d | 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, 14, 18, 24, 28, 32, 33, 34, 36, 41, 49], "target": 23}, "output": 3}, {"input": {"nums": [8, 10, 12, 15, 19, 37], "target": 21}, "output": 5}, {"input": {"nums": [3, 5, 11, 17, 19, 25, 28, 35], "target": 35}, "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": "66b9ae4d95a4894f"} |
vcr_3a981719a43d | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [49, 24, 31, 52, 30, 56, 46, 14, 16, 45, 1, 39, 10, 19, 15]}, "output": 38}, {"input": {"prices": [12, 17, 27, 40, 16, 9, 30, 24]}, "output": 28}, {"input": {"prices": [31, 3, 17, 24, 30, 6, 36, 2, 38]}, "output": 36}] | 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": "12ec6c79f5c4ba24"} |
vcr_904258d299d0 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 11, 22, 23, 24, 26, 29, 30, 39, 45, 46, 47], "target": 32}, "output": 8}, {"input": {"nums": [0, 4, 6, 14, 21, 22, 26, 31], "target": 32}, "output": 8}, {"input": {"nums": [4, 6, 9, 12, 15, 24, 35], "target": 36}, "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": "32d83e4f9df32c46"} |
vcr_ffa0cbbad6ad | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [2, 0, 0, 0, 1, 2, 4, 0]}, "output": false}, {"input": {"nums": [3, 2, 0, 3]}, "output": true}, {"input": {"nums": [1, 2, 3, 0, 1, 0, 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": "a5e4e23a945b0847"} |
vcr_acb3d054e693 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [5, -4, -7, -5, -15, 3, 15, -7, -12, 18, -7], "k": 28}, "output": [3, 15, -7, -12, 18, -7, 5, -4, -7, -5, -15]}, {"input": {"nums": [-8, 3, 7], "k": 16}, "output": [7, -8, 3]}, {"input": {"nums": [1, 9, -2], "k": 20}, "output": [9, -2, 1]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "5aa845892b0f594b"} |
vcr_e9045013a2af | 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, 26, 30, 32], "target": 32}, "output": 3}, {"input": {"nums": [13, 21, 27, 31, 36], "target": 15}, "output": 1}, {"input": {"nums": [3, 5, 8, 10, 15, 22, 32, 37, 38], "target": 17}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "28797ffb9e15944b"} |
vcr_a14acaf64f2c | 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": [13, 16, 20, 21, 33, 34, 37, 41, 44, 46, 47], "target": 28}, "output": 4}, {"input": {"nums": [2, 7, 18], "target": 39}, "output": 3}, {"input": {"nums": [9, 20, 24, 28], "target": 18}, "output": 1}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "c39002101736c61b"} |
vcr_5081427330e4 | 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": [-28, 33, 66, 25, 61, 8, -38, 13, -40, -26, 20], "target": 26}, "output": [2, 8]}, {"input": {"nums": [-23, 38, 4, 37, 16, -17], "target": -13}, "output": [2, 5]}, {"input": {"nums": [-9, 40, -15, 1, -20, -19, -17, 37], "target": -24}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "54712a8ac9cc9fd1"} |
vcr_5c2bc25cc854 | 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": "b bdbaae ccad bcaebc eaeb ee"}, "output": "ee eaeb bcaebc ccad bdbaae b"}, {"input": {"s": "b abbb aaa"}, "output": "aaa abbb b"}, {"input": {"s": "abb b a bb"}, "output": "bb a b abb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "828a04f9b0fb3b66"} |
vcr_965d956f14bf | 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": [8, 16, 38, 16, 57, 38, 21, 26, 26, 21, 8]}, "output": 57}, {"input": {"nums": [28, 29, 28, 51, 29]}, "output": 51}, {"input": {"nums": [4, 22, 25, 22, 19, 45, 1, 25, 1, 19, 4]}, "output": 45}] | 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": "9f08c9aef4220ce6"} |
vcr_e38f591f0f33 | 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": [122, 88, 70, 19, 172, 104, 185, 44, 112, 32, 127, 71, 9, 183, 56, 196]}, "output": false}, {"input": {"nums": [18, 13, 24, 12, 24, 16, 2]}, "output": true}, {"input": {"nums": [16, 23, 15]}, "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": "f1dd9a7b648ef2dc"} |
vcr_3358c24fda4c | 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, 29, 15, 30, 15, 13, 29, 19, 30, 79, 19]}, "output": 79}, {"input": {"nums": [2, 44, 20, 25, 7, 7, 20, 25, 6, 6, 16, 2, 16]}, "output": 44}, {"input": {"nums": [12, 4, 7, 4, 23, 26, 12, 7, 44, 23, 26]}, "output": 44}] | 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": "059da474967ecd90"} |
vcr_a653361614a7 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [36, 2, 36, 53, 11, 11, 2]}, "output": 53}, {"input": {"nums": [19, 59, 5, 5, 19]}, "output": 59}, {"input": {"nums": [24, 24, 8, 8, 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": "fe0f0a65a26644a7"} |
vcr_d255d89a33ee | 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": [17, 19, 13, 17, 24, 24, 77, 19, 13]}, "output": 77}, {"input": {"nums": [3, 3, 16, 17, 26, 55, 16, 26, 17]}, "output": 55}, {"input": {"nums": [8, 18, 1, 8, 26, 13, 26, 1, 18, 51, 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": "93a9d58c5d1a9292"} |
vcr_eb6d3257dd47 | 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, -3, 17, -22, 10, -5, 8, -19, -9, 15, 0, 6, 12, 0]}, "output": [32, 29, 46, 24, 34, 29, 37, 18, 9, 24, 24, 30, 42, 42]}, {"input": {"nums": [2, -11]}, "output": [2, -9]}, {"input": {"nums": [10, 9, -3, -5, -6, 7, 8, 8, -11]}, "output": [10, 19, 16, 11, 5, 12, 20, 28, 17]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "7bd53dcf995029a2"} |
vcr_f736cadc3488 | 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": "bcbbabacbbcbab"}, "output": 12}, {"input": {"s": "cccccaabc"}, "output": 4}, {"input": {"s": "aaa"}, "output": 1}] | 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": "f1f3300a8294f236"} |
vcr_e6a373aba8ad | 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": [49, 20, 34, 28, 49, 42, 22, 43, 39, 5, 45, 27, 34, 25]}, "output": true}, {"input": {"nums": [11, 9, 18, 21, 11, 21, 22, 13, 20, 24, 22, 12]}, "output": true}, {"input": {"nums": [2, 20, 4, 6, 24, 3, 24]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "99c463294849a706"} |
vcr_04e3635d1283 | 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": [-16, -10, -2, 20, 32], "target": 16}, "output": [0, 4]}, {"input": {"nums": [-18, -7, -3, 18], "target": 15}, "output": [2, 3]}, {"input": {"nums": [-13, -1, 3, 8, 14, 16, 24, 26, 28, 29, 32], "target": 35}, "output": [2, 10]}] | 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": "c4c1c1b494f846ce"} |
vcr_31c1027cac51 | 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": [70, 30, 22, 37, 22, 30, 37]}, "output": 70}, {"input": {"nums": [29, 22, 54, 1, 28, 12, 12, 28, 1, 29, 22]}, "output": 54}, {"input": {"nums": [44, 9, 28, 2, 27, 2, 27, 9, 28]}, "output": 44}] | 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": "f40540c987ab84bd"} |
vcr_567c8716b6c5 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [-14, -3, -17, -17, 20, 22, -18, -16, -9, -13, -2, -3, -10, -2]}, "output": 42}, {"input": {"nums": [-2, 0, 6, -12, 9, 6, 1, -5, 5]}, "output": 16}, {"input": {"nums": [10, -10, 8, -6, 13, -3, -6, 3, 3, -5]}, "output": 15}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "b2d6b8da2dfad072"} |
vcr_196ed081469b | 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": [39, 38, -34, -23, 65, -3, 5, 11, 60, 14, -14, -32, 54, -6], "target": 93}, "output": [0, 12]}, {"input": {"nums": [31, 19, 7, 27, 14, 5, -26, 28], "target": 19}, "output": [4, 5]}, {"input": {"nums": [-30, 3, 11, -25], "target": -19}, "output": [0, 2]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "c2fc651ec10a668a"} |
vcr_acf9af2aec92 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 29, 31, 40], "target": 32}, "output": 3}, {"input": {"nums": [3, 4, 7, 10, 17, 19, 23, 24, 27, 33, 36, 39], "target": 15}, "output": 4}, {"input": {"nums": [1, 3, 19, 30, 33], "target": 27}, "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": "1bcc709f7e310cd5"} |
vcr_330876b5cb7f | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [2, 12, 14, 19, 33, 35, 43, 49], "target": 13}, "output": 2}, {"input": {"nums": [0, 4, 5, 6, 11, 12, 17, 23, 30, 33, 36, 39], "target": 28}, "output": 8}, {"input": {"nums": [4, 7, 11, 14, 15, 17, 21, 33, 35, 38], "target": 28}, "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": "c8aef7a63e0d7d83"} |
vcr_4bb0ff36f164 | medium | hashmap | Group anagrams by sorted character signature. Return sorted group sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]: | Signature is sorted characters. Bucket words, then emit sorted sizes. | def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
for w in strs:
g[''.join(sorted(w))].append(w)
return sorted(len(v) for v in g.values())
| [{"input": {"strs": ["ab", "ccbc", "bca", "ca", "abcb", "ba", "cb"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "abb", "bbb", "aab", "aaa", "bbb", "aba"]}, "output": [1, 2, 2, 2]}, {"input": {"strs": ["aab", "aba", "baa"]}, "output": [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": "5c76127622568be8"} |
vcr_566f5f9eb0be | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [3, 14, 24, 44, 46, 48], "target": 9}, "output": 1}, {"input": {"nums": [3, 5, 7, 11, 13, 22, 25, 26, 31, 33], "target": 9}, "output": 3}, {"input": {"nums": [23, 31, 35, 37], "target": 15}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "48e8fad19d0dc322"} |
vcr_cc8568e64e00 | 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": "cbbda e b dbbdab acdcdc"}, "output": "acdcdc dbbdab b e cbbda"}, {"input": {"s": "b a"}, "output": "a b"}, {"input": {"s": "baba bb aa bbbb aaa"}, "output": "aaa bbbb aa bb 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": "e12bab461931afb8"} |
vcr_1c04ae2051e5 | 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": "dbccc dadb"}, "output": "dadb dbccc"}, {"input": {"s": "aaa abab a bab"}, "output": "bab a abab aaa"}, {"input": {"s": "ab a"}, "output": "a ab"}] | 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": "7c29de9f2f304432"} |
vcr_87993f5f985c | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-5, 18, 4, -7, -2, -8, -19, -17], "k": 9}, "output": [-17, -5, 18, 4, -7, -2, -8, -19]}, {"input": {"nums": [8, 4, 6, 4], "k": 2}, "output": [6, 4, 8, 4]}, {"input": {"nums": [8, 6, -1, 1, 6, 8, -8, 0], "k": 9}, "output": [0, 8, 6, -1, 1, 6, 8, -8]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "54e11a98626707d3"} |
vcr_9607b62e3b66 | 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": "da dbeaee dcad da"}, "output": "da dcad dbeaee da"}, {"input": {"s": "bb a baba"}, "output": "baba a bb"}, {"input": {"s": "aabb b a ab"}, "output": "ab a b aabb"}] | 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": "21a3268fc459dcd7"} |
vcr_27be57349cae | 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": 72, "b": 44}, "output": 4}, {"input": {"a": 158, "b": 72}, "output": 2}, {"input": {"a": 93, "b": 123}, "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": "3a9a1108059e08a7"} |
vcr_ebee66545096 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 0, 0, 1, 1, 3, 1, 0]}, "output": true}, {"input": {"nums": [1, 2, 1, 2]}, "output": true}, {"input": {"nums": [2, 3, 3, 3]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "053e6f36b4b7878d"} |
vcr_0cecfc7c6515 | 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": 95, "b": 145}, "output": 5}, {"input": {"a": 43, "b": 125}, "output": 1}, {"input": {"a": 100, "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": "35edee168438ed28"} |
vcr_6bd6316e3e01 | 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": 165, "b": 174}, "output": 3}, {"input": {"a": 42, "b": 114}, "output": 6}, {"input": {"a": 127, "b": 110}, "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": "9589a6c93a9e47e9"} |
vcr_0869736c5834 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [4, 25, -18, 26, -10]}, "output": [4, 29, 11, 37, 27]}, {"input": {"nums": [10, 2, 4, 12, 14, -9]}, "output": [10, 12, 16, 28, 42, 33]}, {"input": {"nums": [-1, -11, -10, -1, -1]}, "output": [-1, -12, -22, -23, -24]}] | 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": "7321747f5782aad7"} |
vcr_4f06c71d2ff2 | 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": "bbcdaabcccadbb"}, "output": false}, {"input": {"s": "abbabb"}, "output": false}, {"input": {"s": "caccbbaacc"}, "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": "851b76512f8ec802"} |
vcr_9d87e38ff5ea | 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": [99, 74, 143, 191, 186, 63, 35, 14, 76, 30, 25, 43]}, "output": false}, {"input": {"nums": [15, 23, 0, 15, 1, 19, 9, 6, 5, 25]}, "output": true}, {"input": {"nums": [9, 13, 11, 3, 0, 20, 23, 0, 7, 24, 24, 25]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "5192c4158d9a083c"} |
vcr_529d97c46896 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 7, 9, 10, 10, 10, 14, 15]}, "output": [0, 7, 9, 10, 14, 15]}, {"input": {"nums": [2, 3, 4, 4, 4, 8, 9, 10, 10, 11]}, "output": [2, 3, 4, 8, 9, 10, 11]}, {"input": {"nums": [2, 3, 4, 8, 8, 8, 9, 10, 12]}, "output": [2, 3, 4, 8, 9, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "6b3efcda585b9f84"} |
vcr_7c3c8154292c | easy | hashmap | Return True if any value appears at least twice in nums. | def contains_duplicate(nums: list[int]) -> bool: | Duplicates exist if and only if len(list) > len(set). O(n). | def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
| [{"input": {"nums": [24, 38, 2, 48, 26, 31, 27, 27, 42, 27, 13]}, "output": true}, {"input": {"nums": [17, 2, 19, 23, 19, 0, 12, 4, 12]}, "output": true}, {"input": {"nums": [18, 22, 5, 9, 13, 12, 15]}, "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": "ea5bf1f2a42dec06"} |
vcr_24b0deddcf9d | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [6, 10, 21, 22, 24, 25, 27, 30, 34, 39, 44, 49], "target": 38}, "output": 9}, {"input": {"nums": [3, 5, 8, 10, 19, 21, 23, 24, 25, 31, 33, 39], "target": 27}, "output": 9}, {"input": {"nums": [9, 28, 35], "target": 3}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "9f6821e733cca1ba"} |
vcr_4babb0340079 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 1, 2, 4, 1, 4, 2, 0]}, "output": false}, {"input": {"nums": [0, 3, 0, 2, 0]}, "output": false}, {"input": {"nums": [0, 1, 0, 0, 2, 0, 2, 1, 2]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "02d2f81de231c51a"} |
vcr_4bc3b69cd312 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [9, 12, 24, 25, 36, 40, 42], "target": 20}, "output": 2}, {"input": {"nums": [2, 9, 11, 18, 19, 20, 22, 38], "target": 11}, "output": 2}, {"input": {"nums": [0, 2, 9, 15, 16, 33, 37], "target": 17}, "output": 5}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "ee9a4e315b10c1ef"} |
vcr_c67da42a8cbf | 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": "acbdcdb"}, "output": false}, {"input": {"s": "ccca"}, "output": false}, {"input": {"s": "bbbb"}, "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": "938c0edf57416786"} |
vcr_4647dd7cf3e6 | 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": [-9, 12, -2, 0, 0, -20, 1, -11, -7, -13, -15, 10], "k": 17}, "output": [-11, -7, -13, -15, 10, -9, 12, -2, 0, 0, -20, 1]}, {"input": {"nums": [8, -10, -1, -9, 10], "k": 9}, "output": [-10, -1, -9, 10, 8]}, {"input": {"nums": [-3, 2, -3, -10, 1, 3], "k": 8}, "output": [1, 3, -3, 2, -3, -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": "e862138171c4b8ed"} |
vcr_7aabaf14f37d | 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": [21, 27, 39, 20, -23, -6, 59], "target": 21}, "output": [1, 5]}, {"input": {"nums": [-13, -1, 39, 6, -4, 45], "target": 45}, "output": [2, 3]}, {"input": {"nums": [3, 14, 10, 35, -24], "target": 45}, "output": [2, 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": "d9bb46ead0dd0a66"} |
vcr_698632a173f7 | 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": [35, 39, 9, 8, 16, 39, 35, 8, 17, 17, 9, 16, 59]}, "output": 59}, {"input": {"nums": [27, 28, 28, 40, 27]}, "output": 40}, {"input": {"nums": [57, 10, 10, 16, 7, 16, 7]}, "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": "69b4ae1361411bdb"} |
vcr_a40f3dd010bf | 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": [32, 16, 59, 53, 60, 51, 11, 22, 28, 18, 51]}, "output": 44}, {"input": {"prices": [40, 11, 29, 37, 17, 15, 32, 23, 31, 24, 17]}, "output": 26}, {"input": {"prices": [26, 7, 17, 1, 27, 30, 17, 30, 5, 1, 40]}, "output": 39}] | 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": "8a3c5abf9dbf70b6"} |
vcr_89f0d07db6d2 | 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": "cacbcaaabacc"}, "output": 9}, {"input": {"s": "cbcbababbbc"}, "output": 9}, {"input": {"s": "caaaacccbc"}, "output": 5}] | true | python | b9d6cf6fe0c38dc7 | ### Problem
Count contiguous groups of identical characters (example: aabbc -> 3).
### Reasoning
Walk the string and increment whenever the character changes.
### Solution
```python
def count_char_groups(s: str) -> int:
if not s:
return 0
groups = 1
for i in range(1, len(s)):
if s[i] != s[... | {"v": "ok", "content_hash": "c6ddc71b0f2375ad"} |
vcr_f40077e3183c | 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": 55841}, "output": 7}, {"input": {"n": 3661}, "output": 7}, {"input": {"n": 15057}, "output": 8}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "baa17cf11f1ca456"} |
vcr_e6292a065f74 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [4, 4, 0, 0, 0, 3, 0, 3, 2, 0]}, "output": true}, {"input": {"nums": [2, 1, 3]}, "output": true}, {"input": {"nums": [0, 2, 3, 1, 3, 2, 3, 3, 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": "dec12c1f5d6996e2"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.