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_69c4bd9c50a5 | 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": [199, 74, 168, 11, 111, 44, 110, 113, 121, 24, 48, 32, 195]}, "output": false}, {"input": {"nums": [19, 19, 23, 0, 22]}, "output": true}, {"input": {"nums": [0, 14, 17, 11, 15, 2, 4, 14, 11, 23, 5, 16]}, "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": "75261a8e5038968d"} |
vcr_5ea33fec0d32 | 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": [-3, 20, 1, -16, -2, 3, -18, -1, 12, 17, -12]}, "output": 29}, {"input": {"nums": [11, 5, 8, -12, -2, 5, -2, 1, -9, 5]}, "output": 24}, {"input": {"nums": [4, 6, 13, 3, 1, 0, 7, 12, 4]}, "output": 50}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "05843bd2f40eb6e7"} |
vcr_1b5fb2e453c6 | 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": [45, 24, 58, 40, 46, 25, 60, 24, 46]}, "output": 36}, {"input": {"prices": [16, 28, 17, 30, 25, 14, 22, 14, 35]}, "output": 21}, {"input": {"prices": [33, 40, 28, 9, 19, 11]}, "output": 10}] | 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": "d235f8e6b5386830"} |
vcr_7bfa30270ba7 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 1, 1, 4, 7, 8, 8, 8, 10, 13, 15, 15, 15]}, "output": [0, 1, 4, 7, 8, 10, 13, 15]}, {"input": {"nums": [3, 7, 10]}, "output": [3, 7, 10]}, {"input": {"nums": [1, 1, 1, 9, 9, 9, 9, 10, 10]}, "output": [1, 9, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "0e6828e919343d2d"} |
vcr_73561efa3e07 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [3, 3, 4, 10, 12, 13, 15]}, "output": [3, 4, 10, 12, 13, 15]}, {"input": {"nums": [0, 1, 1, 3, 6, 6, 8, 8, 9, 9, 11]}, "output": [0, 1, 3, 6, 8, 9, 11]}, {"input": {"nums": [2, 2, 8, 10, 11]}, "output": [2, 8, 10, 11]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "5a66e40589e79d53"} |
vcr_7c1085231d24 | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 4, 3, 3, 3, 2, 3, 3, 2, 0]}, "output": false}, {"input": {"nums": [2, 0, 2, 3, 1, 2, 2]}, "output": true}, {"input": {"nums": [0, 2, 3, 0, 0, 1, 1, 3, 1]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "901e754a80f1f1d1"} |
vcr_dd64380903ea | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [2, 3, 7, 12, 13, 13, 14, 15]}, "output": [2, 3, 7, 12, 13, 14, 15]}, {"input": {"nums": [0, 5, 8]}, "output": [0, 5, 8]}, {"input": {"nums": [0, 1, 4, 5, 7, 7, 7, 10, 10]}, "output": [0, 1, 4, 5, 7, 10]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "6e61871371921141"} |
vcr_2c003792d996 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 2, 5, 6, 6, 8, 9, 10, 11, 13, 13, 13]}, "output": [0, 2, 5, 6, 8, 9, 10, 11, 13]}, {"input": {"nums": [6, 7, 9, 12]}, "output": [6, 7, 9, 12]}, {"input": {"nums": [1, 2, 4, 7, 10, 10, 12, 12]}, "output": [1, 2, 4, 7, 10, 12]}] | true | python | 9a3cff9b9f3d1889 | ### Problem
Sorted nums may contain duplicates. Return unique values in order as a new list.
### Reasoning
Scan left to right. Append only when the value differs from the last kept value.
### Solution
```python
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [n... | {"v": "ok", "content_hash": "95d446d6fdfe8241"} |
vcr_8f4bba21b5f5 | 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": "deab ecba bc aeeace e a"}, "output": "a e aeeace bc ecba deab"}, {"input": {"s": "a ab abb"}, "output": "abb ab a"}, {"input": {"s": "abab aaaa aa baab a"}, "output": "a baab aa aaaa abab"}] | 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": "fedee1c0e84a7d3f"} |
vcr_c4423a16ae5f | 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": [41, 41, 2, 28, 29, 14, 48, 18, 48, 0, 28, 34, 48, 46, 7, 8, 25]}, "output": true}, {"input": {"nums": [7, 13, 20, 2]}, "output": false}, {"input": {"nums": [25, 23, 12]}, "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": "ef6f9cd28432f700"} |
vcr_173dca2aafba | 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, 2, 13, 31, 32, 37, 43, 46, 49], "target": 13}, "output": 2}, {"input": {"nums": [2, 3, 5, 7, 8, 11, 15, 18, 22, 33, 34, 35], "target": 35}, "output": 11}, {"input": {"nums": [1, 6, 8, 23, 27, 28], "target": 0}, "output": 0}] | true | python | 97339c7a2a724cba | ### Problem
Sorted distinct nums. Return index of target, or the insertion index if missing.
### Reasoning
Binary search for the lower bound of target.
### Solution
```python
def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nu... | {"v": "ok", "content_hash": "c79cf9382977ded3"} |
vcr_e06e16e2ef47 | easy | bit | Every element appears twice except one. Find the single element using XOR. | def single_number(nums: list[int]) -> int: | XOR cancels pairs. The remaining value is the single number. | def single_number(nums: list[int]) -> int:
x = 0
for v in nums:
x ^= v
return x
| [{"input": {"nums": [6, 29, 1, 18, 29, 6, 1, 52, 18]}, "output": 52}, {"input": {"nums": [2, 17, 9, 6, 17, 57, 19, 9, 6, 19, 2]}, "output": 57}, {"input": {"nums": [48, 29, 17, 17, 29]}, "output": 48}] | 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": "9dedbf8958c0b97d"} |
vcr_fafcdaf600c2 | 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, 3, 5, 8, 14, 24, 40], "target": 8}, "output": 3}, {"input": {"nums": [13, 15, 22, 29, 31, 33, 36, 39], "target": 39}, "output": 7}, {"input": {"nums": [3, 6, 16], "target": 33}, "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": "77426b0083c8a1ec"} |
vcr_bf25a5dcdd8e | 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": 63, "b": 213}, "output": 3}, {"input": {"a": 80, "b": 103}, "output": 1}, {"input": {"a": 57, "b": 34}, "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": "e0f208b455913d52"} |
vcr_332882262978 | 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": 38633}, "output": 9}, {"input": {"n": 12830}, "output": 7}, {"input": {"n": 11954}, "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": "0b8bf19d85f6761a"} |
vcr_5d2716e2ab82 | 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": ["aabb", "aaab", "bcca", "bc", "ac", "cac", "bcaa", "bbb", "aa"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "abb", "aab", "bbb", "aaa", "aab", "bab"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["bbb", "bbb", "bab", "aab", "aba", "aab"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "cde8015bf30ac7d6"} |
vcr_dc2b80a0144a | 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": "cbacc"}, "output": 4}, {"input": {"s": "cacbbabbc"}, "output": 7}, {"input": {"s": "ccbcc"}, "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": "6124ab277cd304a1"} |
vcr_a43c5a7a70ef | 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": 81, "b": 81}, "output": 81}, {"input": {"a": 72, "b": 35}, "output": 1}, {"input": {"a": 125, "b": 167}, "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": "a799e11dc73642f8"} |
vcr_5052c64b74de | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [7, -18, -15, -16, 2, -17, -16, -9]}, "output": 7}, {"input": {"nums": [-8, -6, 9, 10]}, "output": 19}, {"input": {"nums": [-8, 6, -7, 0, -11, 9, 8, 9, -1, -4]}, "output": 26}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "ade839b0153c4ee9"} |
vcr_76322757d22e | 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": "accee eacdcd bed"}, "output": "bed eacdcd accee"}, {"input": {"s": "bbba abab aa"}, "output": "aa abab bbba"}, {"input": {"s": "bbab baa bab bba a"}, "output": "a bba bab baa bbab"}] | 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": "7396ac755d113ff3"} |
vcr_385a824fd6c1 | 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": [24, 1, -4, 16, 1, 1, 33, -13, -3, -20, 35, -16, -1]}, "output": [24, 25, 21, 37, 38, 39, 72, 59, 56, 36, 71, 55, 54]}, {"input": {"nums": [-4, 2, -9, 5]}, "output": [-4, -2, -11, -6]}, {"input": {"nums": [15, 6]}, "output": [15, 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": "f9c5c8082db4f5d4"} |
vcr_21a4713d08fd | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [7, 6, 1, 7, 5]}, "output": [210, 245, 1470, 210, 294]}, {"input": {"nums": [1, 2, 1, 1, 2, 5, 3]}, "output": [60, 30, 60, 60, 30, 12, 20]}, {"input": {"nums": [4, 2, 2, 3]}, "output": [12, 24, 24, 16]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "f1c74729a0c6274a"} |
vcr_89e9d12d0e91 | 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": 12549}, "output": 5}, {"input": {"n": 14239}, "output": 11}, {"input": {"n": 2307}, "output": 4}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "595ac95bfe3edd6c"} |
vcr_581641da510d | 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, 10, 21, 23, 31, 33], "target": 18}, "output": 2}, {"input": {"nums": [4, 22, 25, 30], "target": 17}, "output": 1}, {"input": {"nums": [8, 10, 12, 17, 18, 23, 25, 30], "target": 14}, "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": "fa05a08477572ce7"} |
vcr_8dd10e8f57e4 | 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, 16, 24, 25, 29, 32, 33, 37, 46, 48, 49], "target": 45}, "output": 8}, {"input": {"nums": [6, 8, 17, 19, 22, 23, 28, 32, 33, 38], "target": 18}, "output": 3}, {"input": {"nums": [2, 4, 8, 9, 15, 16, 19, 30, 33, 35, 38], "target": 39}, "output": 11}] | 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": "3a4290ee9e7888d5"} |
vcr_5287075786f3 | 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": [116, 13, 103, 122, 161, 196, 28]}, "output": false}, {"input": {"nums": [13, 19, 13, 25, 1, 11, 24, 12, 8, 0, 15, 6]}, "output": true}, {"input": {"nums": [18, 18, 5, 5, 14, 6]}, "output": true}] | true | python | f35d656879667375 | ### Problem
Return True if any value appears at least twice in nums.
### Reasoning
Duplicates exist if and only if len(list) > len(set). O(n).
### Solution
```python
def contains_duplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
``` | {"v": "ok", "content_hash": "63e82f924fca4ab7"} |
vcr_c28437ebabce | 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": [55, 60, 71, 65, 67, 15, 1, 72, 30, 4, 39, -40, 54, -19], "target": 87}, "output": [5, 7]}, {"input": {"nums": [-22, -26, 9, 2, -5, 14, -13, -7], "target": 11}, "output": [2, 3]}, {"input": {"nums": [-1, -3, -10, -27, 20, 34, 18, 10, -21], "target": -9}, "output": [3, 6]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "3540d06eb96dda2f"} |
vcr_cb3c4392f061 | 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, -19, -2, -7, 14, 12, -12]}, "output": [15, -4, -6, -13, 1, 13, 1]}, {"input": {"nums": [-12, 11, 2]}, "output": [-12, -1, 1]}, {"input": {"nums": [-1, -9, 18, -4, 15]}, "output": [-1, -10, 8, 4, 19]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "d2a385cecfdcab3f"} |
vcr_7fd4be9e40c9 | 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, -24, 20, 3, 1]}, "output": [-4, -28, -8, -5, -4]}, {"input": {"nums": [13, -12, -10, -7, 0, 7, -10]}, "output": [13, 1, -9, -16, -16, -9, -19]}, {"input": {"nums": [-5, -10]}, "output": [-5, -15]}] | 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": "f660d23e3d61888e"} |
vcr_23255211c8b8 | medium | array | out[i] is product of all elements except nums[i]. O(n) without division. | def product_except_self(nums: list[int]) -> list[int]: | Build prefix products, then multiply by suffix products in a reverse pass. | def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in range(n):
out[i] = left
left *= nums[i]
right = 1
for i in range(n - 1, -1, -1):
out[i] *= right
right *= nums[i]
return out
| [{"input": {"nums": [5, 2, 4, 4, 2, 4, 7, 7]}, "output": [12544, 31360, 15680, 15680, 31360, 15680, 8960, 8960]}, {"input": {"nums": [3, 4, 2]}, "output": [8, 6, 12]}, {"input": {"nums": [4, 4, 2, 1, 2, 3, 1]}, "output": [48, 48, 96, 192, 96, 64, 192]}] | true | python | e6962ae39100e8b1 | ### Problem
out[i] is product of all elements except nums[i]. O(n) without division.
### Reasoning
Build prefix products, then multiply by suffix products in a reverse pass.
### Solution
```python
def product_except_self(nums: list[int]) -> list[int]:
n = len(nums)
out = [1] * n
left = 1
for i in rang... | {"v": "ok", "content_hash": "e93630d952b299ac"} |
vcr_780ae96000a0 | 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": [16, 19, 21, 26, 35, 42], "target": 44}, "output": 6}, {"input": {"nums": [3, 5, 6, 10, 16, 18, 21, 26, 27, 30, 31, 35], "target": 8}, "output": 3}, {"input": {"nums": [21, 29, 31, 34, 36], "target": 23}, "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": "19e667e6ef4fd5d4"} |
vcr_5361218e1e9d | 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, 1, 2, 4, 4, 0]}, "output": true}, {"input": {"nums": [1, 3, 2, 0, 1, 1, 3, 2, 2]}, "output": true}, {"input": {"nums": [0, 0, 1, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "5eb99698d99552f3"} |
vcr_7c5017b44d29 | easy | greedy | Daily stock prices. Max profit from one buy and one later sell (0 if impossible). | def max_profit(prices: list[int]) -> int: | Track the minimum price so far. Maximize price minus that minimum. | def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, x - lo)
lo = min(lo, x)
return best
| [{"input": {"prices": [48, 41, 46, 59]}, "output": 18}, {"input": {"prices": [9, 5, 10, 4]}, "output": 5}, {"input": {"prices": [13, 30, 38, 6, 9, 29, 3, 39]}, "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": "9a4ca7eedc50e9c3"} |
vcr_3d97ee36d8ef | 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", "ccab", "ca", "bab", "bac"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "aaa", "baa", "abb", "aaa", "bbb", "bab"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["aaa", "aab", "bab", "aab"]}, "output": [1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "6a0346e4a775a985"} |
vcr_9a69fda175bb | 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": [-23, 43, 62, 18, -9, 53, 15, 54, 42, 77], "target": 130}, "output": [5, 9]}, {"input": {"nums": [-22, 42, -4, 43, 0, 10, 20, -7], "target": 16}, "output": [2, 6]}, {"input": {"nums": [24, 18, 43, 37], "target": 67}, "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": "5dd1dcfc83947051"} |
vcr_2197252ffab6 | 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": [5, 29, 34, 42, 44, 43, 13, 48, 23, 5]}, "output": true}, {"input": {"nums": [11, 17, 3, 8, 11, 21, 15]}, "output": true}, {"input": {"nums": [6, 2, 25, 13]}, "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": "8e4d4dcaeff8f7c5"} |
vcr_694f6dde44f2 | 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": [33, -17, -23, 21, 25]}, "output": [33, 16, -7, 14, 39]}, {"input": {"nums": [13, 7, -8, -4, -1, 2, -9]}, "output": [13, 20, 12, 8, 7, 9, 0]}, {"input": {"nums": [-1, 1, 5, 1, 6, 1]}, "output": [-1, 0, 5, 6, 12, 13]}] | 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": "2c0852cd13b83a65"} |
vcr_78c2f425070f | 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": ["aba", "ccac", "cc", "cbb", "aabc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bab", "aaa", "bbb", "abb"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aaa", "bab", "bbb", "bba", "aba"]}, "output": [1, 1, 1, 2]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "7a750792cf97b98e"} |
vcr_d02d24f414b2 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [5, 11, 12, 1, 6, 28]}, "output": 45}, {"input": {"nums": [13, 15, 8, 10, 9, 17, 15, 18]}, "output": 60}, {"input": {"nums": [13, 4, 17, 14, 9, 2, 16, 5, 5, 1]}, "output": 60}] | 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": "a4ae3f9d0fd0d441"} |
vcr_8625eb579a9c | medium | two_pointers | Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. | def two_sum_sorted(nums: list[int], target: int) -> list[int]: | Start at both ends. Move the pointer that brings the sum closer to target. | def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return [i, j]
if s < target:
i += 1
else:
j -= 1
return []
| [{"input": {"nums": [-21, -19, 9, 33, 36, 48], "target": 12}, "output": [0, 3]}, {"input": {"nums": [-15, -12, -7, 15, 26], "target": 0}, "output": [0, 3]}, {"input": {"nums": [-18, -14, 13, 14, 26, 30, 35, 36], "target": 21}, "output": [1, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "62aed9b5bd9f36c7"} |
vcr_3f1176de5b6a | 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": ["ba", "abcb", "ac", "ba", "bc", "ccaa", "cacb", "ab", "cca"]}, "output": [1, 1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bbb", "bbb", "baa", "aba", "aaa", "aaa", "bba"]}, "output": [1, 2, 2, 2]}, {"input": {"strs": ["bba", "aab", "aab", "aab"]}, "output": [1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "39c675b36004dc81"} |
vcr_b52045cd5c19 | 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": [-3, 41, 57, -37], "target": -40}, "output": [0, 3]}, {"input": {"nums": [-25, 44, 34, 11, 0, -11, 24, 7, 23, -5, -8], "target": 12}, "output": [5, 8]}, {"input": {"nums": [-1, 49, -11, 40, -24, 33], "target": 29}, "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": "7362eea2e282afc1"} |
vcr_e13cde2e2868 | 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, 3, 4, 2, 0, 4, 1, 1, 0]}, "output": true}, {"input": {"nums": [1, 0, 0, 2, 2]}, "output": false}, {"input": {"nums": [1, 0, 3, 2, 3, 3]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "f7bbd385936b65e0"} |
vcr_7d505327b95b | 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": [34, 34, 10, 14, 59, 26, 6]}, "output": 49}, {"input": {"prices": [14, 27, 8, 22, 28, 4, 10, 6, 37, 4]}, "output": 33}, {"input": {"prices": [31, 20, 28, 21, 16, 19, 29]}, "output": 13}] | 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": "90aa301c62dad8f4"} |
vcr_0e9f6f7481d8 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [28, 3, 1]}, "output": 29}, {"input": {"nums": [7, 17, 14, 14]}, "output": 31}, {"input": {"nums": [11, 11, 14, 20]}, "output": 31}] | 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": "f35895d0511fb0dc"} |
vcr_8b607dbc57e9 | 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": ["abca", "acac", "bc", "bab", "aabb", "aa", "ccaa", "acaa", "bc"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["aaa", "bbb", "aab", "aba", "bba", "aaa", "baa"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["aba", "baa", "bba", "baa", "bba", "bab", "aab"]}, "output": [3, 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": "46e3195eee66fd84"} |
vcr_60e7214c1966 | 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": ["cab", "aa", "aab", "bc", "baa", "acbb", "abcc", "aca"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "aab", "bab"]}, "output": [1, 2]}, {"input": {"strs": ["bbb", "aba", "aba", "aaa", "abb", "baa"]}, "output": [1, 1, 1, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "145891d7d4d18ab3"} |
vcr_d0df885d6f57 | hard | two_pointers | Return True if lowercase string s is a palindrome. | def is_palindrome(s: str) -> bool: | Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. | def is_palindrome(s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
| [{"input": {"s": "adabbbdbbdbbbada"}, "output": true}, {"input": {"s": "babaacb"}, "output": false}, {"input": {"s": "abababcaac"}, "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": "40eed7346f396b96"} |
vcr_c9b5fdd36f6b | 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, 14, 9, 23, 16, 7, 16, 4, 23, 17, 27, 8]}, "output": 104}, {"input": {"nums": [7, 14, 10, 0, 1, 12, 2, 9]}, "output": 38}, {"input": {"nums": [3, 13, 20, 13, 9, 2, 16, 6, 1]}, "output": 49}] | 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": "58f7efa8bd22a0bf"} |
vcr_f46c5ae1501e | 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": [-12, -5, 6, 14, 24, 30, 42, 46], "target": 48}, "output": [2, 6]}, {"input": {"nums": [-15, -10, -7, 10, 20, 34], "target": 24}, "output": [1, 5]}, {"input": {"nums": [-17, 3, 8, 27, 34, 39], "target": -9}, "output": [0, 2]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "84237475568dbe0e"} |
vcr_23fcb3215373 | 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": "cbc ba"}, "output": "ba cbc"}, {"input": {"s": "abba bab bba abba"}, "output": "abba bba bab abba"}, {"input": {"s": "a aab a bb a"}, "output": "a bb a aab 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": "7dca762a37f67880"} |
vcr_b371a6581164 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [-11, -2, -4, 25, 14, 34, 4, -22, 20, 35, 34, -10, -14, 12]}, "output": [-11, -13, -17, 8, 22, 56, 60, 38, 58, 93, 127, 117, 103, 115]}, {"input": {"nums": [10, 10, 1, 11, 15, 1, 3, -6, -3]}, "output": [10, 20, 21, 32, 47, 48, 51, 45, 42]}, {"input": {"nums": [9, 2, -2, -4, -9, 17]}, "output": [9, 1... | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "4c5927e6d5d8571e"} |
vcr_5bca3b30badf | 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": "adb cbc dcb eecb aaeea bcceab baa"}, "output": "baa bcceab aaeea eecb dcb cbc adb"}, {"input": {"s": "bb bbab"}, "output": "bbab bb"}, {"input": {"s": "a aaba abb"}, "output": "abb aaba 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": "f1542fb9e0c39798"} |
vcr_f5235130faed | 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": 53758}, "output": 11}, {"input": {"n": 5864}, "output": 7}, {"input": {"n": 9002}, "output": 6}] | true | python | 24a3439f03888452 | ### Problem
Return the number of set bits in non-negative integer n (Hamming weight).
### Reasoning
Count the least bit with n & 1, then shift right until n is 0.
### Solution
```python
def hamming_weight(n: int) -> int:
c = 0
while n:
c += n & 1
n >>= 1
return c
``` | {"v": "ok", "content_hash": "80acaf260b0f6bc9"} |
vcr_fbbbfd41f99d | 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, -3, 3, 14, 21, -1, 0, 11, 22]}, "output": 79}, {"input": {"nums": [9, 5, -8, 14, 10]}, "output": 30}, {"input": {"nums": [-2, -3, 4, -4, 12]}, "output": 12}] | true | python | ec0ac96e944ed958 | ### Problem
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
### Reasoning
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
### Solution
```python
def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
... | {"v": "ok", "content_hash": "198d3b9cf7a4ca65"} |
vcr_eab4c82987ca | 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": [28, -19, 73, -37, 32, 5, -23, -25, 49, -2], "target": 60}, "output": [0, 4]}, {"input": {"nums": [16, 1, 17, 44], "target": 60}, "output": [0, 3]}, {"input": {"nums": [28, 45, -22, -25, 24, 8, 12, 20, 15], "target": 52}, "output": [0, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "28dabfc785a1a33e"} |
vcr_a76c3c3e6a85 | 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, 18, -16, 1, 53, 5, -13, 78, 46, 30, 6, -1, 44, -22], "target": -16}, "output": [0, 5]}, {"input": {"nums": [48, -21, -5, 34, 49], "target": 27}, "output": [0, 1]}, {"input": {"nums": [24, 13, -5, 16, 45], "target": 69}, "output": [0, 4]}] | true | python | 98882f4a63a35075 | ### Problem
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
### Reasoning
1) Find i, j with nums[i] + nums[j] = target.
2) Store value to index in a hash map while scanning.
3) For each x, look up target - x; if present, return sorted indices.
4) Time O(n), space... | {"v": "ok", "content_hash": "573d20bebc68bde4"} |
vcr_161dd60e3de1 | 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": ["babc", "acb", "ac", "accc", "ccac", "bb"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "baa", "baa", "bbb", "baa", "aaa", "bba"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["baa", "aaa", "aab", "aba", "bba", "aaa"]}, "output": [1, 2, 3]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "3b25c3b08ae1ccce"} |
vcr_290f9d117195 | easy | array | Return running sum where out[i] = nums[0] + ... + nums[i]. | def running_sum(nums: list[int]) -> list[int]: | Accumulate once left to right. O(n). | def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
| [{"input": {"nums": [18, -18, 28, 7, 14, -23, 25, 6, -21, 28, -4]}, "output": [18, 0, 28, 35, 49, 26, 51, 57, 36, 64, 60]}, {"input": {"nums": [-11, -5, 7, 7]}, "output": [-11, -16, -9, -2]}, {"input": {"nums": [7, -11, 15, 17, -7, 7, 0]}, "output": [7, -4, 11, 28, 21, 28, 28]}] | true | python | 9222adfde6ffe86d | ### Problem
Return running sum where out[i] = nums[0] + ... + nums[i].
### Reasoning
Accumulate once left to right. O(n).
### Solution
```python
def running_sum(nums: list[int]) -> list[int]:
acc, out = 0, []
for x in nums:
acc += x
out.append(acc)
return out
``` | {"v": "ok", "content_hash": "9e72299b84223d8f"} |
vcr_8fd885d59937 | 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": [11, 11, 2, 22, 37, 6, 15, 2, 73, 19, 22, 15, 6, 39, 39, 37, 19]}, "output": 73}, {"input": {"nums": [3, 2, 2, 59, 14, 10, 17, 3, 17, 13, 14, 10, 13]}, "output": 59}, {"input": {"nums": [26, 12, 58, 12, 26, 29, 29]}, "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": "29d70c7cc2d73bcb"} |
vcr_f7c07a8940fb | 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": [1, 30, -25, -5, 14, 17, -11, -6, 6, -10, -7, 27]}, "output": [1, 31, 6, 1, 15, 32, 21, 15, 21, 11, 4, 31]}, {"input": {"nums": [0, -9, 9, -12, 1, -10, 10, 7]}, "output": [0, -9, 0, -12, -11, -21, -11, -4]}, {"input": {"nums": [15, 7, -6, -12, -8]}, "output": [15, 22, 16, 4, -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": "c5f6f7b959d0b6e7"} |
vcr_44d773ef48d9 | 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": [21, 16, 5, 14, 22, 5, 25, 7, 14, 3, 3]}, "output": 90}, {"input": {"nums": [16, 3, 12, 10, 16, 3]}, "output": 44}, {"input": {"nums": [6, 11, 5, 18, 8]}, "output": 29}] | 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": "d71653bb3b0227ea"} |
vcr_b8a0ad55d9b1 | 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": "bbccacabaa"}, "output": 7}, {"input": {"s": "babb"}, "output": 3}, {"input": {"s": "aacbbaabbb"}, "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": "2242ec84a2aac1d3"} |
vcr_662efae22780 | 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": ["aac", "ca", "ab", "cbcb", "ccba", "baac", "aaaa", "acb", "baa"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "baa", "aab", "bba", "aba", "aaa", "aaa"]}, "output": [1, 2, 4]}, {"input": {"strs": ["aba", "abb", "aab", "abb", "aab", "bab", "bab"]}, "output": [3, 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": "dc309c2c94dc7488"} |
vcr_a1314b341be6 | 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": "abbbbaababbaac"}, "output": 8}, {"input": {"s": "abcaaacbc"}, "output": 7}, {"input": {"s": "bcccaacaa"}, "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": "b445d112bf5bec69"} |
vcr_893633788c1a | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-6, 8, 14, -17, -4, -14, 16, 17, -4, 10, 9, -3], "k": 1}, "output": [-3, -6, 8, 14, -17, -4, -14, 16, 17, -4, 10, 9]}, {"input": {"nums": [-7, -7, 9, 0, -4, 8], "k": 11}, "output": [-7, 9, 0, -4, 8, -7]}, {"input": {"nums": [-5, 9, 1, 0, 1], "k": 1}, "output": [1, -5, 9, 1, 0]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "046787a10e417bef"} |
vcr_ab79c672448b | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [5, 8, 16, 17, 5, 2, 21, 9, 28, 7]}, "output": 75}, {"input": {"nums": [14, 11]}, "output": 14}, {"input": {"nums": [14, 8, 8, 0, 3]}, "output": 25}] | 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": "cc1ae2d7a71c3cb3"} |
vcr_c0623c516b61 | 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": [-4, -20, -7, -15, -2, -6, -1, -15, -2], "k": 10}, "output": [-2, -4, -20, -7, -15, -2, -6, -1, -15]}, {"input": {"nums": [-3, 0, -10, -4, -2, 3, 9, 10], "k": 13}, "output": [-4, -2, 3, 9, 10, -3, 0, -10]}, {"input": {"nums": [-7, 6, 2, 6, 5], "k": 16}, "output": [5, -7, 6, 2, 6]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "a6c417b3c01eaff5"} |
vcr_4cc0c0b178f9 | 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, 1, 1, 4, 0]}, "output": false}, {"input": {"nums": [1, 3, 1, 0, 2, 3]}, "output": true}, {"input": {"nums": [3, 3, 3, 1]}, "output": true}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "28d9366dccaa6bca"} |
vcr_e5fcfa12135c | 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": "cad acdbb eec ecea ccddbc"}, "output": "ccddbc ecea eec acdbb cad"}, {"input": {"s": "bbb baa b ab ab"}, "output": "ab ab b baa bbb"}, {"input": {"s": "ab abbb"}, "output": "abbb 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": "f4272653fab5dd54"} |
vcr_5c7066d6153f | 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": ["bcc", "bbb", "aabb", "cca", "cbbc", "caa"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bab", "bba", "aba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "abb", "bbb"]}, "output": [1, 1, 1]}] | true | python | 98cc167af301acd0 | ### Problem
Group anagrams by sorted character signature. Return sorted group sizes.
### Reasoning
Signature is sorted characters. Bucket words, then emit sorted sizes.
### Solution
```python
def anagram_group_sizes(strs: list[str]) -> list[int]:
from collections import defaultdict
g = defaultdict(list)
f... | {"v": "ok", "content_hash": "5d6776ffc748d71e"} |
vcr_2a977255b66d | 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": [-8, -28, -36, 38, 46, -5, 65, 13, 43, -35, 76, 0], "target": 18}, "output": [1, 4]}, {"input": {"nums": [7, 4, 42, -8, 21, 23], "target": 25}, "output": [1, 4]}, {"input": {"nums": [40, 27, 47, -25], "target": 22}, "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": "fcd16b79db00be92"} |
vcr_21c652d7ea27 | 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, 3, 3, 2, 1, 0]}, "output": true}, {"input": {"nums": [0, 2, 2, 2]}, "output": false}, {"input": {"nums": [3, 1, 0, 0, 1, 1, 3, 1]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "00472081ed9d2553"} |
vcr_aa22ab77710d | 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": "dbdc a"}, "output": "a dbdc"}, {"input": {"s": "aaa ab a bbaa"}, "output": "bbaa a ab aaa"}, {"input": {"s": "aab bb bbbb"}, "output": "bbbb bb aab"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "bd3dd62a03c67877"} |
vcr_04c35374b8c6 | 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": [31, 51, 14, 21, 18, 14, 18, 21, 31]}, "output": 51}, {"input": {"nums": [11, 11, 12, 55, 12]}, "output": 55}, {"input": {"nums": [15, 28, 1, 24, 28, 24, 15, 46, 1]}, "output": 46}] | 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": "025f5f1be776ebcc"} |
vcr_91fac745c4be | 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": "bbdcdbdbdcdbb"}, "output": true}, {"input": {"s": "aaabbcaba"}, "output": false}, {"input": {"s": "cc"}, "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": "80f5309b9c8fef5c"} |
vcr_6bd352cc7c03 | 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, 21, 6, 10, 34, 1, 25, 5, 9, 37, 41, 11, 26, 27, 40]}, "output": 40}, {"input": {"prices": [18, 1, 22, 39, 22, 39, 25, 25]}, "output": 38}, {"input": {"prices": [13, 29, 4, 38, 4]}, "output": 34}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "d48bba4f818af01c"} |
vcr_33f73acbca79 | 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": "bcb bb de baceeb ce"}, "output": "ce baceeb de bb bcb"}, {"input": {"s": "a babb a"}, "output": "a babb a"}, {"input": {"s": "bab a baab b"}, "output": "b baab a 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": "8b5705f29d063d35"} |
vcr_23b9bb92a87d | 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": "beace dee aaedbe cce ca"}, "output": "ca cce aaedbe dee beace"}, {"input": {"s": "aaa bbba abaa b baba"}, "output": "baba b abaa bbba aaa"}, {"input": {"s": "aa aba b a a"}, "output": "a a b aba aa"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "745f647560fd3c55"} |
vcr_4c2df114eea6 | easy | two_pointers | Sorted nums may contain duplicates. Return unique values in order as a new list. | def remove_duplicates_sorted(nums: list[int]) -> list[int]: | Scan left to right. Append only when the value differs from the last kept value. | def remove_duplicates_sorted(nums: list[int]) -> list[int]:
if not nums:
return []
out = [nums[0]]
for x in nums[1:]:
if x != out[-1]:
out.append(x)
return out
| [{"input": {"nums": [0, 0, 1, 3, 7, 8, 8, 9, 11, 11, 11, 15]}, "output": [0, 1, 3, 7, 8, 9, 11, 15]}, {"input": {"nums": [1, 7, 10, 12]}, "output": [1, 7, 10, 12]}, {"input": {"nums": [0, 0, 1, 2, 4, 7, 7]}, "output": [0, 1, 2, 4, 7]}] | 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": "18db79fcbaf4377e"} |
vcr_18496b277c06 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [1, -8, -16, -17, 9, -1, -17, -7, -12, -13, -4]}, "output": 9}, {"input": {"nums": [-2, 0, -3, 3, 4, 13, 6, 5, -9, 6]}, "output": 31}, {"input": {"nums": [13, -4, -1, 0, 0, -5, 3, 9, 9, -11, 3]}, "output": 24}] | 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": "dd7b563f9ff43589"} |
vcr_a1b994a6ac31 | medium | array | Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). | def max_subarray(nums: list[int]) -> int: | At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. | def max_subarray(nums: list[int]) -> int:
best = cur = nums[0]
for x in nums[1:]:
cur = max(x, cur + x)
best = max(best, cur)
return best
| [{"input": {"nums": [8, 13, 4, 14, -3, -3, 14, 4, 14, 2, 13, 13, -14]}, "output": 93}, {"input": {"nums": [13, 9, -2]}, "output": 22}, {"input": {"nums": [-4, 13, 6, 9, -1, 13, -2]}, "output": 40}] | 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": "c5ad22608f06a901"} |
vcr_2494399db0d5 | medium | dp | Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. | def rob(nums: list[int]) -> int: | DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. | def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
return prev1
| [{"input": {"nums": [4, 30, 15, 20, 8, 4, 27, 19, 6, 24, 11]}, "output": 101}, {"input": {"nums": [13, 18, 8, 2, 16]}, "output": 37}, {"input": {"nums": [0, 1, 17, 16, 11]}, "output": 28}] | true | python | adc43e90c6cebcb1 | ### Problem
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
### Reasoning
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
### Solution
```python
def rob(nums: list[int]) -> int:
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, p... | {"v": "ok", "content_hash": "08ba7def2a2d5da6"} |
vcr_eef9fd10b24b | 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": "dace bedacc edc"}, "output": "edc bedacc dace"}, {"input": {"s": "aab ab baaa b"}, "output": "b baaa ab aab"}, {"input": {"s": "bba abaa ba"}, "output": "ba abaa bba"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "29ffd56685fb5590"} |
vcr_8c28a79c4a85 | 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": [4, -14, 18, 3], "k": 10}, "output": [18, 3, 4, -14]}, {"input": {"nums": [2, -7, -7, -5, 4, -5], "k": 13}, "output": [-5, 2, -7, -7, -5, 4]}, {"input": {"nums": [-2, -4, -3, 5, 4, -3, -2, -2], "k": 19}, "output": [-3, -2, -2, -2, -4, -3, 5, 4]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "a7fae4a539f5a389"} |
vcr_84903fb4fed1 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-16, -13, -4, -16, -18, 3, -6, -5, 1], "k": 3}, "output": [-6, -5, 1, -16, -13, -4, -16, -18, 3]}, {"input": {"nums": [9, 10, -4, 4, -5, -5, -1, -7], "k": 12}, "output": [-5, -5, -1, -7, 9, 10, -4, 4]}, {"input": {"nums": [-7, 6, -4, -2], "k": 8}, "output": [-7, 6, -4, -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": "0dc135157ad6bf18"} |
vcr_a1ecd3364f01 | easy | array | Rotate nums to the right by k steps (k may be larger than n). Return a new list. | def rotate(nums: list[int], k: int) -> list[int]: | Reduce k modulo n. Last k elements move to the front. | def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return nums[-k:] + nums[:-k]
| [{"input": {"nums": [-14, 2, -3, -8, 19, -7, 19, 1, 3, 8, -2], "k": 12}, "output": [-2, -14, 2, -3, -8, 19, -7, 19, 1, 3, 8]}, {"input": {"nums": [-9, 3, -2, -8, 5, 2], "k": 20}, "output": [5, 2, -9, 3, -2, -8]}, {"input": {"nums": [-2, 9, 2, -6, 6], "k": 5}, "output": [-2, 9, 2, -6, 6]}] | true | python | 132c4800a39464fd | ### Problem
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
### Reasoning
Reduce k modulo n. Last k elements move to the front.
### Solution
```python
def rotate(nums: list[int], k: int) -> list[int]:
n = len(nums)
k %= n
if k == 0:
return list(nums)
return num... | {"v": "ok", "content_hash": "cf78f426817b45f8"} |
vcr_23e851b8f2e9 | 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": [10, 3, 38, 47, 18, 45, 56, 60, 33, 36, 27, 50, 16, 50, 1, 45]}, "output": 57}, {"input": {"prices": [10, 17, 7, 35, 8]}, "output": 28}, {"input": {"prices": [31, 20, 31, 25, 5, 32, 13]}, "output": 27}] | 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": "861b58a4902d7797"} |
vcr_e6e1816664f9 | 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": "caccac"}, "output": true}, {"input": {"s": "bb"}, "output": true}, {"input": {"s": "bcabbbac"}, "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": "fe243a2aa0db3e26"} |
vcr_8caee715bad2 | 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": [38, 38, 36, 35, 23, 46, 7, 44, 36, 23]}, "output": true}, {"input": {"nums": [16, 13, 17]}, "output": false}, {"input": {"nums": [21, 20, 23, 16, 1, 25, 1, 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": "16ace14cd1df4125"} |
vcr_cf077c4e734a | medium | greedy | From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. | def can_jump(nums: list[int]) -> bool: | Track the farthest reachable index. If the current index exceeds it, fail. | def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > reach:
return False
reach = max(reach, i + x)
if reach >= len(nums) - 1:
return True
return reach >= len(nums) - 1
| [{"input": {"nums": [0, 3, 1, 0, 3, 0, 4, 0, 0]}, "output": false}, {"input": {"nums": [0, 2, 2, 2, 0]}, "output": false}, {"input": {"nums": [1, 3, 0, 1, 0, 0, 0, 1]}, "output": false}] | true | python | b6a822bf8a6dd0fa | ### Problem
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
### Reasoning
Track the farthest reachable index. If the current index exceeds it, fail.
### Solution
```python
def can_jump(nums: list[int]) -> bool:
reach = 0
for i, x in enumerate(nums):
if i > ... | {"v": "ok", "content_hash": "1b43e7e1e2880c03"} |
vcr_8f8c269cb1dd | easy | string | Reverse the order of words in s (words separated by single spaces). | def reverse_words(s: str) -> str: | Split on whitespace, reverse the list, join with spaces. | def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
| [{"input": {"s": "db ea bcbe"}, "output": "bcbe ea db"}, {"input": {"s": "abba aaab"}, "output": "aaab abba"}, {"input": {"s": "bbaa bba"}, "output": "bba bbaa"}] | 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": "eabe4291f7213746"} |
vcr_95587cea47d9 | 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": [23, 4, 37, 32, 29, 6, 54, 50, 28, 37, 7]}, "output": 50}, {"input": {"prices": [6, 1, 39, 11, 32, 29, 5]}, "output": 38}, {"input": {"prices": [3, 28, 17, 28, 9, 6]}, "output": 25}] | true | python | b40e7d74850552a7 | ### Problem
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
### Reasoning
Track the minimum price so far. Maximize price minus that minimum.
### Solution
```python
def max_profit(prices: list[int]) -> int:
best, lo = 0, prices[0]
for x in prices[1:]:
best = max(best, ... | {"v": "ok", "content_hash": "f7da63c67215ff11"} |
vcr_b74d1a12b9f8 | medium | binary_search | Sorted distinct nums. Return index of target, or the insertion index if missing. | def search_insert(nums: list[int], target: int) -> int: | Binary search for the lower bound of target. | def search_insert(nums: list[int], target: int) -> int:
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
| [{"input": {"nums": [4, 6, 7, 13, 16, 18, 22, 24, 32, 37, 39, 47, 48], "target": 22}, "output": 6}, {"input": {"nums": [0, 7, 23, 24, 30], "target": 20}, "output": 2}, {"input": {"nums": [1, 8, 9, 15, 17, 20, 21, 27, 30, 31, 34, 39], "target": 38}, "output": 11}] | 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": "ad6bdf268bd01a6b"} |
vcr_1bb60aef3520 | 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, 7, 14, 17, 18, 20, 21, 26, 27, 28, 31, 40, 41, 45], "target": 38}, "output": 11}, {"input": {"nums": [2, 5, 10, 15, 25, 29, 32, 34], "target": 12}, "output": 3}, {"input": {"nums": [3, 6, 12, 18, 23, 27, 30], "target": 19}, "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": "5ec699cf3cf72573"} |
vcr_887360abbd72 | 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": [11, 16, 17, 22, 23, 27, 31, 39, 40, 43, 46], "target": 38}, "output": 7}, {"input": {"nums": [0, 1, 6, 9, 17, 31, 32, 33, 34, 35, 39], "target": 1}, "output": 1}, {"input": {"nums": [9, 20, 36], "target": 39}, "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": "9b42c264d2c3935e"} |
vcr_400a70929225 | 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, -16, -8, -3, 3, 9, 10, 12, 20, 35, 46, 48], "target": 9}, "output": [3, 7]}, {"input": {"nums": [-12, 12, 18, 25, 27, 30, 37], "target": 37}, "output": [1, 3]}, {"input": {"nums": [-15, -13, -9, -7, -5, 6, 9, 20, 34], "target": -6}, "output": [0, 6]}] | true | python | 0fdf37b1be7420d0 | ### Problem
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
### Reasoning
Start at both ends. Move the pointer that brings the sum closer to target.
### Solution
```python
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
i, j = 0, len(nums) - 1
while i < ... | {"v": "ok", "content_hash": "06201bfc5c8b1af1"} |
vcr_a22b44f18d66 | 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, 3, 1, 0, 2, 2, 4, 0, 0]}, "output": true}, {"input": {"nums": [2, 3, 2, 2, 3, 0, 3, 0]}, "output": true}, {"input": {"nums": [0, 0, 1, 2, 2, 2, 2, 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": "41e1dc4d65151462"} |
vcr_68d19c331a95 | 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": "becccb aaaecb ab"}, "output": "ab aaaecb becccb"}, {"input": {"s": "b aab baa aaba aaa"}, "output": "aaa aaba baa aab b"}, {"input": {"s": "bbb bbaa"}, "output": "bbaa bbb"}] | true | python | e27e5a0fb61b865a | ### Problem
Reverse the order of words in s (words separated by single spaces).
### Reasoning
Split on whitespace, reverse the list, join with spaces.
### Solution
```python
def reverse_words(s: str) -> str:
return ' '.join(reversed(s.split()))
``` | {"v": "ok", "content_hash": "c53ceaeac6a8b54a"} |
vcr_76fd4018faff | 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": [130, 107, 74, 8, 76, 145, 99, 43, 104, 181, 98, 180, 118, 113, 192, 9]}, "output": false}, {"input": {"nums": [3, 25, 25, 17, 5, 0, 1, 5, 23]}, "output": true}, {"input": {"nums": [23, 25, 2]}, "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": "141f6c79554f5b33"} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.