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_595733560391
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "c cadac cdd addca cccc eaeac"}, "output": "eaeac cccc addca cdd cadac c"}, {"input": {"s": "aaaa bbaa"}, "output": "bbaa aaaa"}, {"input": {"s": "aab ab abab a"}, "output": "a abab ab 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": "52fe2f760e44443b"}
vcr_bbce51155929
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, 2, 3, 0, 4, 4, 0]}, "output": false}, {"input": {"nums": [1, 2, 3, 3, 2]}, "output": true}, {"input": {"nums": [1, 3, 0, 1, 3, 1, 0, 2, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "8a4a30b76e64d6cb"}
vcr_c5753aa4366d
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": [47, 25, 34, 2, 34, 27, 53, 43, 43, 40, 10, 28]}, "output": 51}, {"input": {"prices": [35, 18, 33]}, "output": 15}, {"input": {"prices": [28, 10, 38, 7, 2, 35, 21]}, "output": 33}]
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": "c5c583caea93defd"}
vcr_a248ecefb450
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", "cbc", "ca", "cc"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bbb", "aaa", "bab", "bab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["baa", "aab", "aaa", "bbb"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "9b5ec7c5944c371f"}
vcr_a00382565753
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [2, 17, -1, -14, 3], "k": 3}, "output": [-1, -14, 3, 2, 17]}, {"input": {"nums": [6, -7, 9, -6, 2, 9, 8, 5], "k": 12}, "output": [2, 9, 8, 5, 6, -7, 9, -6]}, {"input": {"nums": [-6, -5, -5, -9, 7, 6, 2, 2], "k": 2}, "output": [2, 2, -6, -5, -5, -9, 7, 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": "4c4f6880ae00141a"}
vcr_3db4aadaae47
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": [24, 47, 17, 35, 30]}, "output": 23}, {"input": {"prices": [5, 34, 27, 18, 33, 32, 3]}, "output": 29}, {"input": {"prices": [28, 15, 37, 1, 23, 15, 30, 17, 10]}, "output": 29}]
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": "5de4e57d82d7f7b8"}
vcr_e509c4afcbd2
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": [15, 17, 49, 19, 20, 43, 39]}, "output": 34}, {"input": {"prices": [34, 35, 19, 1, 38, 4, 13, 21, 5]}, "output": 37}, {"input": {"prices": [29, 4, 36, 2]}, "output": 32}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "655998b59dfcbbd1"}
vcr_057c6f712945
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [35, 14, 35, 21, 21, 71, 14]}, "output": 71}, {"input": {"nums": [9, 43, 9, 28, 21, 28, 21]}, "output": 43}, {"input": {"nums": [9, 12, 10, 6, 18, 10, 18, 53, 28, 6, 12, 28, 9]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "d73715a84eff12ab"}
vcr_5c13282d9c95
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": [44, 23, 66, 53, -28, 42, 47, 76], "target": 110}, "output": [0, 2]}, {"input": {"nums": [8, -4, -22, 25, 1, -16, 12, 7, 35], "target": 36}, "output": [4, 8]}, {"input": {"nums": [21, 23, 13, 47, -7], "target": 70}, "output": [1, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "dae3abe7a01c9367"}
vcr_1d486fdc6e09
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, -24, -19, -14, -10, 32, 33, 34, 38], "target": 11}, "output": [0, 8]}, {"input": {"nums": [-17, -2, 4, 12, 14, 18, 27, 34], "target": 10}, "output": [0, 6]}, {"input": {"nums": [-18, -8, -7, 13, 16, 31], "target": 44}, "output": [3, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "7209825b62edfb95"}
vcr_e22fb77eb751
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, 6, 6, 7, 5, 6]}, "output": [15120, 37800, 12600, 12600, 10800, 15120, 12600]}, {"input": {"nums": [4, 5, 1, 3, 5, 2, 5]}, "output": [750, 600, 3000, 1000, 600, 1500, 600]}, {"input": {"nums": [2, 5, 2, 3, 4, 5, 4]}, "output": [2400, 960, 2400, 1600, 1200, 960, 1200]}]
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": "9e7f6067cf8e841f"}
vcr_c508c51caf10
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [20, 30, 10, 9, 30, 4, 25, 2]}, "output": 85}, {"input": {"nums": [4, 7, 15, 7, 7, 10, 11]}, "output": 37}, {"input": {"nums": [3, 2, 18]}, "output": 21}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "50cdf2699aa7a18c"}
vcr_9ded5424c1d8
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": [-2, 20, 19, 15, -5, -1, 3, 11, 22, 20]}, "output": 104}, {"input": {"nums": [9, -9, 0]}, "output": 9}, {"input": {"nums": [-6, 6, -4]}, "output": 6}]
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": "b9f2e58771a3cde3"}
vcr_d6402a99e545
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 7, 8, 12, 15, 16, 32, 35, 42, 45, 47], "target": 23}, "output": 6}, {"input": {"nums": [8, 32, 39], "target": 31}, "output": 1}, {"input": {"nums": [2, 6, 10, 19, 22, 24, 30, 32, 33, 34], "target": 15}, "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": "420b841ed0c257a4"}
vcr_e6d935602c1d
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-25, -20, -14, -10, 3, 15, 25, 43], "target": 18}, "output": [0, 7]}, {"input": {"nums": [-20, -6, 8, 18, 23], "target": 3}, "output": [0, 4]}, {"input": {"nums": [-18, -7, -1, 32, 33, 34, 35, 38], "target": 27}, "output": [1, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "7389ffe9b8358d1a"}
vcr_33ea39670bdd
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": [16, 8, 67, 8, 24, 16, 24]}, "output": 67}, {"input": {"nums": [14, 4, 14, 23, 23, 10, 40, 10, 4]}, "output": 40}, {"input": {"nums": [16, 21, 16, 45, 21]}, "output": 45}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "f582acccd8aad63f"}
vcr_1f48fc910bc9
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-13, 21, -12, 4, 13, -11, 22, 8, 8, 17, -11, 6, -11]}, "output": 70}, {"input": {"nums": [-12, 8, 12, -10, 9, -1, -3, -4, -9]}, "output": 20}, {"input": {"nums": [-12, 14, 12, 3, -7, -11, -5, 6, -8, 7, 8]}, "output": 29}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "88a45e270f92b8ef"}
vcr_e12c58cb9549
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": 27982}, "output": 9}, {"input": {"n": 302}, "output": 5}, {"input": {"n": 10949}, "output": 7}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "80f6436ef2259ba0"}
vcr_59ed438dcacb
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": "dccbb aed bdcea cdda bccd ed ebba"}, "output": "ebba ed bccd cdda bdcea aed dccbb"}, {"input": {"s": "aaba aa ab a bb"}, "output": "bb a ab aa aaba"}, {"input": {"s": "abaa bbb ab b aa"}, "output": "aa b ab bbb abaa"}]
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": "beaba47ae3e99ef8"}
vcr_d4f67e1e5de5
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": 78, "b": 247}, "output": 13}, {"input": {"a": 99, "b": 173}, "output": 1}, {"input": {"a": 89, "b": 163}, "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": "9b8d985810685657"}
vcr_66c712c62ca0
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": [-23, 3, 9, 12, 18, 24, 44], "target": 47}, "output": [1, 6]}, {"input": {"nums": [3, 20, 29, 30, 37], "target": 40}, "output": [0, 4]}, {"input": {"nums": [-13, -6, -3, 5, 17, 28, 34, 37], "target": 11}, "output": [1, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "54a2ea08df854f78"}
vcr_98722e243333
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, 2, 2, 3, 3, 4, 6, 7, 9, 10, 13, 14, 15]}, "output": [2, 3, 4, 6, 7, 9, 10, 13, 14, 15]}, {"input": {"nums": [0, 0, 2, 3, 4, 9, 10, 10, 11]}, "output": [0, 2, 3, 4, 9, 10, 11]}, {"input": {"nums": [3, 4, 6, 7, 9, 11, 11, 12]}, "output": [3, 4, 6, 7, 9, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "43d73fc1a5b6793c"}
vcr_a1cf4619e92a
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": [57, 25, 21, 20, 34, 25, 34, 8, 20, 21, 8]}, "output": 57}, {"input": {"nums": [49, 1, 24, 1, 24]}, "output": 49}, {"input": {"nums": [15, 9, 43, 25, 25, 9, 3, 15, 3]}, "output": 43}]
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": "06d4f8dba18f22a6"}
vcr_2bdf381ad59d
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": [-18, 7, 18, -9], "k": 12}, "output": [-18, 7, 18, -9]}, {"input": {"nums": [6, -5, 0], "k": 2}, "output": [-5, 0, 6]}, {"input": {"nums": [2, -6, 0], "k": 6}, "output": [2, -6, 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": "4c3c58c89308ec7e"}
vcr_b5e282cec560
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": [48, 15, 36, 40, 3, 38, 39, 47, 48]}, "output": true}, {"input": {"nums": [10, 17, 4, 5, 0, 20, 5, 22]}, "output": true}, {"input": {"nums": [18, 24, 25, 10, 19, 6, 15, 14]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "c6198b611c75cce8"}
vcr_230e298295b9
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": [1, 0, 2, 4, 1, 2, 0]}, "output": false}, {"input": {"nums": [0, 1, 2, 0, 2, 2, 0, 2]}, "output": false}, {"input": {"nums": [2, 3, 2, 1, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "661fd002a70a98eb"}
vcr_e58443a7f643
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": [-6, 72, 30, 77, -28, 78, 65, -21, 32], "target": 155}, "output": [3, 5]}, {"input": {"nums": [-24, 5, 31, 45], "target": 7}, "output": [0, 2]}, {"input": {"nums": [44, 15, -23, 4], "target": -8}, "output": [1, 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": "966ee6a314eeb8d6"}
vcr_3359be9bd4cb
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 3, 8, 10, 10, 13, 15, 15]}, "output": [1, 2, 3, 8, 10, 13, 15]}, {"input": {"nums": [1, 1, 1, 4, 4, 6, 7, 8, 9, 11, 11]}, "output": [1, 4, 6, 7, 8, 9, 11]}, {"input": {"nums": [0, 2, 2, 3, 3, 5, 5, 7, 7, 9, 9]}, "output": [0, 2, 3, 5, 7, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "9eb0479d9839bc1b"}
vcr_382b4351b65d
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "dadbadc"}, "output": false}, {"input": {"s": "ccbcbccac"}, "output": false}, {"input": {"s": "ba"}, "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": "3ccc6618db7f0c1f"}
vcr_a0e2a8e460f4
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [14, -9, -11, -2, 14, -10, -13, -6, 0]}, "output": 14}, {"input": {"nums": [13, 0, 10, -5]}, "output": 23}, {"input": {"nums": [-2, -3, -7, -6, 11, 7, 13, 11, -10]}, "output": 42}]
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": "a2a5fa0e9d38d748"}
vcr_b8c81261291a
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": "eaddd cae abadc"}, "output": "abadc cae eaddd"}, {"input": {"s": "aab ab a a ab"}, "output": "ab a a ab aab"}, {"input": {"s": "ba b baaa bab"}, "output": "bab baaa b ba"}]
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": "98ffcc042cd58ec8"}
vcr_770c34e5760c
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": [-22, -6, 26, 33, 45, 49], "target": 39}, "output": [1, 4]}, {"input": {"nums": [-9, 0, 2, 4, 7, 8, 9, 14, 33], "target": 5}, "output": [0, 7]}, {"input": {"nums": [-19, -5, -3, -2, 0, 3, 6, 9, 10, 38], "target": -19}, "output": [0, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "4dc2c39adb6b7ca0"}
vcr_6c3f9b7b7985
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["ca", "abc", "caaa", "caaa", "abb", "ac"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["bbb", "aab", "bbb"]}, "output": [1, 2]}, {"input": {"strs": ["abb", "bba", "bab", "baa", "bba", "bba", "aba"]}, "output": [2, 5]}]
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": "3300d2c4ce2b4e5d"}
vcr_e06d29144006
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": 60362}, "output": 10}, {"input": {"n": 12530}, "output": 7}, {"input": {"n": 1524}, "output": 7}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "64e98bd7e793d299"}
vcr_9f1b76a9f5ea
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-10, 8, -20, -5, -13, 26, 19, 4, 21, -14, 1, -23]}, "output": [-10, -2, -22, -27, -40, -14, 5, 9, 30, 16, 17, -6]}, {"input": {"nums": [-12, 2, 1, 15, -1, 18, 6]}, "output": [-12, -10, -9, 6, 5, 23, 29]}, {"input": {"nums": [11, 15, -7, 9, 5]}, "output": [11, 26, 19, 28, 33]}]
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": "617c1cbe1458701a"}
vcr_5c7961ea9f93
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", "cbcb", "bb", "aab", "bb", "aa", "aba"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["bab", "aab", "aab", "abb", "bab"]}, "output": [2, 3]}, {"input": {"strs": ["aba", "bba", "aab", "abb", "abb", "baa"]}, "output": [3, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "b65d672fa1fc6f40"}
vcr_5b2a074f4926
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, 4, 11, -20, -11, -4, -3, 0, 2, -19, 18], "k": 16}, "output": [-3, 0, 2, -19, 18, -6, 4, 11, -20, -11, -4]}, {"input": {"nums": [2, -10, 3, 6, 9], "k": 9}, "output": [-10, 3, 6, 9, 2]}, {"input": {"nums": [-6, -6, 9], "k": 13}, "output": [9, -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": "baa23d03acccc32e"}
vcr_9369a51f0bfb
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [17, 9, 30, 13, 27, 4]}, "output": 74}, {"input": {"nums": [20, 1, 19, 9, 5, 7, 12, 17, 1]}, "output": 63}, {"input": {"nums": [12, 17, 8, 1]}, "output": 20}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "777d7caefd09611e"}
vcr_c88d2408ccd9
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 4, 1, 4, 1, 0, 0, 2, 2, 0]}, "output": true}, {"input": {"nums": [1, 0, 3, 2, 3, 3, 2, 2]}, "output": false}, {"input": {"nums": [3, 2, 3, 0, 0, 2, 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": "d62907c324774057"}
vcr_f8310fdd6f7b
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [14, 30, 1, 13, 13, 5, 25, 13, 1, 24, 27]}, "output": 96}, {"input": {"nums": [11, 18, 16, 0, 13, 14, 7]}, "output": 47}, {"input": {"nums": [12, 20, 0, 11, 6, 11, 18]}, "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": "96987620576c0ca5"}
vcr_2c2bbf3e1eb3
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": [103, 197, 125, 186, 8, 167, 16, 176, 86, 82, 187, 164, 18, 17, 193]}, "output": false}, {"input": {"nums": [1, 24, 17, 0, 0, 23, 6]}, "output": true}, {"input": {"nums": [3, 18, 7, 1, 10, 21, 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": "0fcf79d3abcf80d6"}
vcr_401c6ca4b28a
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [76, 75, -38, 34, 20, 30], "target": 110}, "output": [0, 3]}, {"input": {"nums": [10, -3, -21, 13, -28, -13], "target": -41}, "output": [4, 5]}, {"input": {"nums": [-2, -17, 11, -18, -30], "target": 9}, "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": "2f16fe6e7a165b8b"}
vcr_e1450e2b5629
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": [14, 32, 14, 24, 32, 66, 24]}, "output": 66}, {"input": {"nums": [56, 18, 24, 23, 12, 16, 24, 8, 12, 23, 16, 18, 8]}, "output": 56}, {"input": {"nums": [10, 27, 27, 10, 47]}, "output": 47}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "77f92136101f4683"}
vcr_1e6d611df11e
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": [1, 2, 1, 0, 3, 0, 1, 3, 3, 0]}, "output": false}, {"input": {"nums": [0, 3, 0, 1, 0, 3, 3, 1, 3]}, "output": false}, {"input": {"nums": [3, 0, 0, 0, 2, 3]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "292c585b0d585589"}
vcr_41f6e2ef8644
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": ["baba", "caa", "bac", "aa", "ca", "ca", "abc", "aac", "ca"]}, "output": [1, 1, 2, 2, 3]}, {"input": {"strs": ["aaa", "aab", "aaa"]}, "output": [1, 2]}, {"input": {"strs": ["bbb", "bba", "bba", "aaa", "bab"]}, "output": [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": "9f1b51bdfe62b003"}
vcr_29cdc6488e2c
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-25, -21, -19, -16, -15, -4, 20, 22, 33, 38], "target": 29}, "output": [5, 8]}, {"input": {"nums": [-16, -11, 1, 2, 7, 11, 15, 18], "target": 33}, "output": [6, 7]}, {"input": {"nums": [-8, 8, 13, 17, 24], "target": 41}, "output": [3, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "9bad8f51a486a96f"}
vcr_8f120c825153
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [7, 4, 7, 5, 6, 3, 7]}, "output": [17640, 30870, 17640, 24696, 20580, 41160, 17640]}, {"input": {"nums": [4, 3, 1]}, "output": [3, 4, 12]}, {"input": {"nums": [4, 1, 5, 2, 1]}, "output": [10, 40, 8, 20, 40]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "72cd0dbeff75c1ee"}
vcr_6114d77c2737
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-24, -21, -11, -9, -3, 4, 23, 26, 29, 32, 40], "target": 11}, "output": [1, 9]}, {"input": {"nums": [-14, -11, 22, 26], "target": 15}, "output": [1, 3]}, {"input": {"nums": [-7, 11, 19, 29], "target": 4}, "output": [0, 1]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "a1bfbed6f51164eb"}
vcr_07aca00829a1
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, 2, 3, 3, 4, 4, 7, 7, 9, 11, 12]}, "output": [2, 3, 4, 7, 9, 11, 12]}, {"input": {"nums": [4, 4, 4, 8, 9, 9, 10, 11, 11]}, "output": [4, 8, 9, 10, 11]}, {"input": {"nums": [0, 4, 6, 9, 11, 11, 11, 12]}, "output": [0, 4, 6, 9, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "8b9fd93454efcd15"}
vcr_d1a556cd16f9
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, 5, -15, -17, 5, -22, 28, 18, 16]}, "output": [-15, -10, -25, -42, -37, -59, -31, -13, 3]}, {"input": {"nums": [2, -12]}, "output": [2, -10]}, {"input": {"nums": [17, -3, -2, 17, 6, -9, 15, -7, -10]}, "output": [17, 14, 12, 29, 35, 26, 41, 34, 24]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "6dcd50e3dd92e505"}
vcr_1019a31ea822
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": ["aaca", "cc", "baac", "cca", "bb", "bc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "baa", "bab"]}, "output": [1, 1, 1]}, {"input": {"strs": ["bba", "aab", "aab"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "de644ae765a9168a"}
vcr_1302165caf94
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": "aabaabaaacbaab"}, "output": 9}, {"input": {"s": "aac"}, "output": 2}, {"input": {"s": "bccabb"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "b390841c828dbdd3"}
vcr_dd71dab3d212
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": [1, 3, 7, 2]}, "output": [42, 14, 6, 21]}, {"input": {"nums": [1, 5, 4, 5, 2, 3, 5]}, "output": [3000, 600, 750, 600, 1500, 1000, 600]}, {"input": {"nums": [1, 1, 2, 4, 1, 5]}, "output": [40, 40, 20, 10, 40, 8]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "c4692c73461e1ad3"}
vcr_f33a3b9e95e8
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, 4, 6, 6, 7, 13, 13, 14, 15, 15]}, "output": [3, 4, 6, 7, 13, 14, 15]}, {"input": {"nums": [0, 4, 5, 7, 7, 8, 12]}, "output": [0, 4, 5, 7, 8, 12]}, {"input": {"nums": [0, 0, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8]}, "output": [0, 1, 2, 3, 4, 5, 7, 8]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "7ef813bfecf10bba"}
vcr_ead69149f1a9
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": [1, 2, 5, 4, 7, 6, 1]}, "output": [1680, 840, 336, 420, 240, 280, 1680]}, {"input": {"nums": [3, 4, 5]}, "output": [20, 15, 12]}, {"input": {"nums": [5, 1, 5]}, "output": [5, 25, 5]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "0dca733610208a30"}
vcr_48999791c79c
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": [1, 39, 23, 3, 31, 39, 59, 43, 20, 38, 29, 49, 12]}, "output": 58}, {"input": {"prices": [16, 36, 25, 31, 37, 26, 27, 34, 23, 3]}, "output": 21}, {"input": {"prices": [1, 40, 34, 12, 20, 34, 34, 1, 3, 21, 40]}, "output": 39}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "e52dd193158249eb"}
vcr_122590eaf3a6
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [5, 24, 25, 32, 38, 49], "target": 25}, "output": 2}, {"input": {"nums": [2, 4, 7, 10, 17, 18, 25, 29, 36], "target": 35}, "output": 8}, {"input": {"nums": [2, 4, 18, 26, 30, 39], "target": 9}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "ec1d6a4c604d3291"}
vcr_8938d4dd3641
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [15, 1, 13, 14]}, "output": 29}, {"input": {"nums": [2, 10]}, "output": 10}, {"input": {"nums": [0, 6, 6, 11, 11, 0, 9]}, "output": 26}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "2b2bc19cf2080019"}
vcr_e82a1228b908
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 3, 4, 4, 7, 5, 6, 6]}, "output": [60480, 40320, 30240, 30240, 17280, 24192, 20160, 20160]}, {"input": {"nums": [4, 4, 4, 4, 2]}, "output": [128, 128, 128, 128, 256]}, {"input": {"nums": [5, 5, 5, 4, 1, 3, 2]}, "output": [600, 600, 600, 750, 3000, 1000, 1500]}]
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": "0abf0c62f0350e09"}
vcr_0e4e986e44f6
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, 2, 5, 5, 7, 9, 11, 12, 14]}, "output": [0, 2, 5, 7, 9, 11, 12, 14]}, {"input": {"nums": [1, 3, 4, 7, 7, 9, 9, 9, 10, 11, 11]}, "output": [1, 3, 4, 7, 9, 10, 11]}, {"input": {"nums": [2, 3, 4, 8, 9, 9]}, "output": [2, 3, 4, 8, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "c3f2edcbbc38ffdc"}
vcr_fde8b8ff961c
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [10, 24, 16, 26, 2]}, "output": 50}, {"input": {"nums": [15, 10, 19, 19, 4, 5, 4, 19, 16, 5]}, "output": 63}, {"input": {"nums": [16, 16, 8, 20, 0, 15, 7]}, "output": 51}]
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": "dd3162dab483cc16"}
vcr_be4da25b2baf
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": [27, 38, 31, 35, 33, 35, 2, 22, 38, 69, 33, 29, 27, 2, 29, 22, 31]}, "output": 69}, {"input": {"nums": [57, 11, 29, 26, 2, 2, 10, 6, 26, 6, 11, 10, 29]}, "output": 57}, {"input": {"nums": [12, 3, 21, 18, 29, 18, 21, 3, 29, 45, 12]}, "output": 45}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "c7efe14733099125"}
vcr_6a44f614f10e
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": [6, 28, 9, 22, 0, 21, 17, 2, 21]}, "output": 92}, {"input": {"nums": [6, 8, 19, 11, 15, 7, 17, 1, 1, 17]}, "output": 74}, {"input": {"nums": [15, 20, 9, 16, 20, 18, 9]}, "output": 54}]
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": "1f35af9321179a0c"}
vcr_a7da0d17148e
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": ["bba", "bc", "abb", "cbb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bbb", "aab", "aab", "aaa", "aab", "bbb", "bab"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["abb", "bbb", "baa", "bbb", "abb", "aba"]}, "output": [2, 2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "fce212ebff5e01cb"}
vcr_8a440a0ae7df
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": 42279}, "output": 8}, {"input": {"n": 10596}, "output": 6}, {"input": {"n": 16357}, "output": 11}]
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": "893140d1ce568366"}
vcr_fa48576707e1
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [14, 11, -17, 1, 11, -14, -9, -5, 25, 33, 27, -17]}, "output": [14, 25, 8, 9, 20, 6, -3, -8, 17, 50, 77, 60]}, {"input": {"nums": [17, -10, 7]}, "output": [17, 7, 14]}, {"input": {"nums": [3, 1, 8, -4, -12]}, "output": [3, 4, 12, 8, -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": "a4fc0a079220f98a"}
vcr_6c0709e5dc74
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, -4, 11, 16, 18, 22, 32], "target": 50}, "output": [4, 6]}, {"input": {"nums": [-20, 0, 3, 5, 7, 28, 32], "target": -15}, "output": [0, 3]}, {"input": {"nums": [-18, 3, 4, 6, 15, 20, 31], "target": 18}, "output": [1, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "74bc02e8299795cd"}
vcr_8d048e7c3f8a
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, 7, 7, 2, 4, 2, 3]}, "output": [2352, 2352, 2352, 8232, 4116, 8232, 5488]}, {"input": {"nums": [4, 5, 4, 2, 2]}, "output": [80, 64, 80, 160, 160]}, {"input": {"nums": [2, 4, 1, 2]}, "output": [8, 4, 16, 8]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "ec61e7d7e73f368c"}
vcr_59ea545666d2
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 6, 11, 21, 34, 36, 47], "target": 39}, "output": 6}, {"input": {"nums": [12, 28, 35, 39], "target": 34}, "output": 2}, {"input": {"nums": [4, 7, 11, 13, 14, 19, 20, 28, 36], "target": 6}, "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": "dbeed8cd2f5ae2d7"}
vcr_f4e3b5bec053
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [51, 53, 31, 57, 1, 14, 16, 15, 46, 19, 47, 37, 13, 60, 37]}, "output": 59}, {"input": {"prices": [28, 14, 25, 26, 31]}, "output": 17}, {"input": {"prices": [4, 36, 14, 10, 17, 22, 1, 35, 38, 25, 22, 11]}, "output": 37}]
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": "ea53cd6100e88865"}
vcr_6a4177659da4
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [15, 15, 15, 13, 3]}, "output": 33}, {"input": {"nums": [8, 1, 7, 4, 17, 3, 14, 3, 7, 6]}, "output": 53}, {"input": {"nums": [17, 7, 15, 12]}, "output": 32}]
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": "06a5596673b48dd6"}
vcr_2cac20d549a2
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": [1, 4, 0, 0, 4, 4, 2, 3, 1, 0]}, "output": true}, {"input": {"nums": [1, 1, 2, 0, 0, 0, 1, 0]}, "output": false}, {"input": {"nums": [1, 2, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "a0edf330b6adddeb"}
vcr_64a68f366343
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": "ad ce ca eeadd"}, "output": "eeadd ca ce ad"}, {"input": {"s": "aa baab a"}, "output": "a baab aa"}, {"input": {"s": "aba aab a abb"}, "output": "abb a aab aba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "6a6da8c74677c071"}
vcr_b5a7108e9f0a
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": ["caba", "aaaa", "ba", "cca", "cc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "aaa", "bbb", "bbb", "bbb", "bba", "bba"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["bab", "baa", "aba", "bba", "aba", "bab"]}, "output": [3, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "6296d25eb01c2e98"}
vcr_c66c3ecf5512
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [8, 14, 21, 14, 7, 6, 15, 25]}, "output": 61}, {"input": {"nums": [3, 8, 2, 0, 1, 11, 11]}, "output": 20}, {"input": {"nums": [0, 1]}, "output": 1}]
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": "de277e13316fb6ed"}
vcr_331cfdddfad0
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": "abdda cdcb cbed ce aeeed"}, "output": "aeeed ce cbed cdcb abdda"}, {"input": {"s": "baaa bb bbab"}, "output": "bbab bb baaa"}, {"input": {"s": "bb aba aaba ba aaa"}, "output": "aaa ba aaba aba bb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "b829fcbbe1d1b3df"}
vcr_55270b8430f0
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": [6, 29, 15, 30, 4, 16, 12, 20, 14, 14, 26, 19]}, "output": 128}, {"input": {"nums": [17, 14, 6, 0, 6, 10, 10, 19]}, "output": 52}, {"input": {"nums": [16, 20, 1, 10, 6, 4, 19, 18, 20]}, "output": 69}]
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": "135ac67176d200b3"}
vcr_4038f54ba3d9
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": [-21, 5, 15, 7, 32, -9, 4, -16, 33, 4]}, "output": [-21, -16, -1, 6, 38, 29, 33, 17, 50, 54]}, {"input": {"nums": [-1, 6, 12, -10, -11, 8, 14, 1, 7, -4]}, "output": [-1, 5, 17, 7, -4, 4, 18, 19, 26, 22]}, {"input": {"nums": [12, 15, 3]}, "output": [12, 27, 30]}]
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": "bdfa688130045a3a"}
vcr_3bb7010ead2e
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": ["cbcb", "aaab", "aa", "bb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["baa", "bab", "bba", "aaa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["abb", "bba", "bba"]}, "output": [3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "7fb54549c7911f84"}
vcr_650a873b7373
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, 37, 28, 44, 53, 58, 22, 33, 30, 48, 46, 27]}, "output": 30}, {"input": {"prices": [22, 4, 32, 7, 7, 6, 23, 4, 35, 7, 33]}, "output": 31}, {"input": {"prices": [11, 34, 23, 6, 37, 7, 29, 3, 16, 14]}, "output": 31}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "7e3a7a8f01414dff"}
vcr_5fa46eb7da54
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, 12, 13, 15, 17, 24, 38, 46, 48], "target": 41}, "output": 7}, {"input": {"nums": [0, 33, 35], "target": 33}, "output": 1}, {"input": {"nums": [6, 9, 12, 13, 14, 20, 31, 34, 35], "target": 8}, "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": "7999ed8053a33788"}
vcr_49b4dbed9e07
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [34, 45, 45, 0, 32, 20, 23, 48, 26, 22, 26, 48, 44, 38, 32, 25]}, "output": true}, {"input": {"nums": [23, 6, 9, 16, 10, 2, 17, 12, 14, 14]}, "output": true}, {"input": {"nums": [18, 24, 20, 6, 1, 8, 7, 23, 9, 3, 25, 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": "b0b1c68c7a81ff96"}
vcr_a7e44e09db96
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", "bc", "baab", "aa", "ba", "cbcc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "bba", "bab", "aab", "bab", "aab", "aba"]}, "output": [3, 4]}, {"input": {"strs": ["abb", "aba", "bba", "abb", "bab", "abb"]}, "output": [1, 5]}]
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": "cba09c92b68d2a5d"}
vcr_069dff14a694
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-1, -8, 13, -9, -19, -8, 16, 11, 11, 12, -15, -4], "k": 10}, "output": [13, -9, -19, -8, 16, 11, 11, 12, -15, -4, -1, -8]}, {"input": {"nums": [-8, -1, -3, 10, -3, 7, 10], "k": 11}, "output": [10, -3, 7, 10, -8, -1, -3]}, {"input": {"nums": [-1, -2, -10, 4, 3, -1, -5, 5], "k": 15}, "output": [-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": "4356088495d9d350"}
vcr_ca0b0ea2c711
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": ["abcb", "baac", "bab", "cbca", "cabc", "cb"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "bba", "aab", "abb", "aaa", "aab", "aaa"]}, "output": [2, 2, 3]}, {"input": {"strs": ["abb", "bba", "bbb", "bbb", "bba"]}, "output": [2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "07cf5a89a53984c7"}
vcr_df84f2fbcedd
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": [-17, 9, -17, 11, 17, -20, -19], "k": 5}, "output": [-17, 11, 17, -20, -19, -17, 9]}, {"input": {"nums": [-5, -4, 5, -5, 9], "k": 6}, "output": [9, -5, -4, 5, -5]}, {"input": {"nums": [2, -2, -6, -9, 3, -1, -5], "k": 2}, "output": [-1, -5, 2, -2, -6, -9, 3]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "3058117391f38679"}
vcr_16322eca7fe1
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": [16, 34, 37, 33, 18, 21, 16]}, "output": true}, {"input": {"nums": [10, 23, 7, 24]}, "output": false}, {"input": {"nums": [8, 16, 23, 21]}, "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": "dc38f34392475a61"}
vcr_dea30b9ea917
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": [0, 21, 15, 16, 15, 11, 1, 3, 21, 26]}, "output": 77}, {"input": {"nums": [6, 17, 12, 7, 10, 17, 15, 1, 18]}, "output": 61}, {"input": {"nums": [6, 20, 18, 9, 13, 17, 14]}, "output": 51}]
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": "fdc1142e2b7c0de7"}
vcr_d908a823e9b1
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": 164, "b": 5}, "output": 1}, {"input": {"a": 15, "b": 54}, "output": 3}, {"input": {"a": 161, "b": 89}, "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": "f2bf228b3466f2e9"}
vcr_b23a0d36bbec
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [6, 3, 5, 6, 0, 1, 2]}, "output": [0, 0, 0, 0, 1080, 0, 0]}, {"input": {"nums": [3, 3, 4, 3, 2, 1, 4]}, "output": [288, 288, 216, 288, 432, 864, 216]}, {"input": {"nums": [4, 5, 2]}, "output": [10, 8, 20]}]
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": "4606b8ad7ef48e7e"}
vcr_43bbea79ab03
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, 2, 2, 9, 13, 14]}, "output": [0, 1, 2, 9, 13, 14]}, {"input": {"nums": [2, 2, 4, 4, 4, 5, 7, 8, 9]}, "output": [2, 4, 5, 7, 8, 9]}, {"input": {"nums": [0, 0, 1, 3, 4, 4, 5, 5, 8, 9, 12]}, "output": [0, 1, 3, 4, 5, 8, 9, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "8199259f54657f3d"}
vcr_e5c9956e6acd
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 3, 3, 10, 11, 11]}, "output": [3, 10, 11]}, {"input": {"nums": [3, 4, 4, 7, 12]}, "output": [3, 4, 7, 12]}, {"input": {"nums": [1, 1, 2, 4, 7, 8, 8, 8, 9]}, "output": [1, 2, 4, 7, 8, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "e974bf197a2ad7b7"}
vcr_14e192af7184
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, -19, 26, 21, 16, 15, 21]}, "output": [4, -15, 11, 32, 48, 63, 84]}, {"input": {"nums": [5, -2, 5]}, "output": [5, 3, 8]}, {"input": {"nums": [0, 2, 5, 13, 9, 2, 7, -7, 5, 17]}, "output": [0, 2, 7, 20, 29, 31, 38, 31, 36, 53]}]
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": "51e1c26d174e922f"}
vcr_0c68c021cd3a
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": [15, 18, 32, 33, 34, 48, 49], "target": 40}, "output": 5}, {"input": {"nums": [2, 4, 7, 12, 18, 19, 23, 26, 28, 30, 38], "target": 6}, "output": 2}, {"input": {"nums": [0, 2, 4, 6, 7, 10, 32, 35], "target": 8}, "output": 5}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "e59ab2c63c46aed5"}
vcr_65682e2e9cec
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, 79, 197, 166, 100, 98, 147, 55, 176, 192, 180, 4]}, "output": false}, {"input": {"nums": [6, 24, 9, 23, 25, 11, 21, 19, 15, 10]}, "output": false}, {"input": {"nums": [16, 4, 8, 10, 24, 0, 19, 9]}, "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": "0bebe943ead08bb6"}
vcr_432f0be65aa0
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, 0, 30, 28, 26, 7, 18, 15]}, "output": 87}, {"input": {"nums": [17, 2, 4, 8, 16]}, "output": 37}, {"input": {"nums": [14, 19, 10, 20, 12, 5, 20, 12]}, "output": 59}]
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": "aa02fc3b59cb3ecd"}
vcr_5a2852309e72
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": 21836}, "output": 7}, {"input": {"n": 8206}, "output": 4}, {"input": {"n": 6298}, "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": "7558aae9e8dca831"}
vcr_22a7a808b7c3
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, -12, 10, 17, -10, 12, 13]}, "output": 42}, {"input": {"nums": [-8, -7, 9, -6]}, "output": 9}, {"input": {"nums": [1, 7, -1, -2, 4, -11, 4, -1, -10, -7, 11]}, "output": 11}]
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": "ab88905a8f55e05c"}
vcr_06c96aad5f14
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, 4, 2, 3, 2, 4, 2, 0]}, "output": true}, {"input": {"nums": [0, 1, 3, 1, 2]}, "output": false}, {"input": {"nums": [3, 0, 0, 0, 1, 3, 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": "8b45d5f0c322648a"}
vcr_0b898c3d507d
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [23, 18, 29, 3, 27, 26, 27, 7, 18, 8, 18, 4]}, "output": 142}, {"input": {"nums": [5, 6, 5, 18, 12, 15]}, "output": 39}, {"input": {"nums": [7, 2, 15]}, "output": 22}]
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": "c513ff040661d712"}