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_43c4ab88402a
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 1, 1, 2, 0, 3, 3, 1, 0]}, "output": false}, {"input": {"nums": [0, 3, 0, 3]}, "output": false}, {"input": {"nums": [2, 1, 0, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "f405718dab73a397"}
vcr_11ff35633dd0
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, 0, 7, 7, 2, 1]}, "output": [0, 686, 0, 0, 0, 0]}, {"input": {"nums": [3, 1, 3]}, "output": [3, 9, 3]}, {"input": {"nums": [3, 2, 5, 4]}, "output": [40, 60, 24, 30]}]
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": "bfe39111fb51dddf"}
vcr_e461de2be1cb
medium
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "dadddbcbc"}, "output": false}, {"input": {"s": "baacaba"}, "output": false}, {"input": {"s": "bcc"}, "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": "dd64bb5a62faa2f0"}
vcr_73c0af2daf70
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, 6, 6, 8, 9, 15]}, "output": [0, 6, 8, 9, 15]}, {"input": {"nums": [0, 0, 1, 11, 12]}, "output": [0, 1, 11, 12]}, {"input": {"nums": [0, 0, 1, 3, 3, 6, 6, 8, 10, 10]}, "output": [0, 1, 3, 6, 8, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "2b85319d480d161a"}
vcr_fb8b0c4da0c6
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 4, 1, 4, 2, 4, 0]}, "output": true}, {"input": {"nums": [2, 2, 2, 3]}, "output": true}, {"input": {"nums": [2, 2, 1, 1, 1, 3, 0, 1, 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": "cbeb1ee6a4db19de"}
vcr_d3226c4dfaa6
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": [27, 22, 53, 10, 35, 9, 13, 54, 11, 49, 42, 40, 55]}, "output": 46}, {"input": {"prices": [8, 5, 28, 30, 21, 12]}, "output": 25}, {"input": {"prices": [39, 34, 23, 25, 12, 7, 1]}, "output": 2}]
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": "c9a11ed25cf00f8a"}
vcr_c4926e69b202
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": [41, -38, 46, 3, 2, -9, 48, -17], "target": 51}, "output": [3, 6]}, {"input": {"nums": [-1, -8, 4, 38, 19], "target": 3}, "output": [0, 2]}, {"input": {"nums": [35, 43, 48, 20, 8, 34, 25, 15, 7, -7], "target": 22}, "output": [7, 8]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "85424b962dfc2870"}
vcr_d6136c9c4e1d
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, 26, 1, 22, 19, 17, 2]}, "output": 65}, {"input": {"nums": [0, 1, 3, 16]}, "output": 17}, {"input": {"nums": [3, 2, 19, 0, 15, 1]}, "output": 37}]
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": "a97d9e676f0ebcbf"}
vcr_9d0c0b0fc7d0
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": [32, 33, 24, 20, 32]}, "output": true}, {"input": {"nums": [22, 20, 22, 20]}, "output": true}, {"input": {"nums": [4, 14, 22, 1, 24, 9, 7, 7, 9]}, "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": "2ef0ab68b36c5295"}
vcr_88d7ac98a2d2
medium
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "adbdbdcca"}, "output": false}, {"input": {"s": "cc"}, "output": true}, {"input": {"s": "caca"}, "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": "ea696df2d818cf84"}
vcr_62bb63f645e7
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 4, 4, 2, 4, 0, 1, 3, 0]}, "output": true}, {"input": {"nums": [0, 1, 1, 0, 3, 3, 1]}, "output": false}, {"input": {"nums": [2, 0, 3, 0, 2, 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": "084bfd5a344bdcb8"}
vcr_d0b17df7d3e8
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": [127, 141, 164, 173, 59, 54, 109, 31, 168, 5, 129]}, "output": false}, {"input": {"nums": [20, 12, 3, 22, 8, 8]}, "output": true}, {"input": {"nums": [15, 16, 25, 12, 21, 4, 0, 11, 14, 23, 1]}, "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": "533ffe81e43356a2"}
vcr_235a72cac781
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": 243, "b": 128}, "output": 1}, {"input": {"a": 96, "b": 174}, "output": 6}, {"input": {"a": 48, "b": 7}, "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": "3d771af91453ce5b"}
vcr_1f38eebd808b
medium
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "aacbbddbababa"}, "output": false}, {"input": {"s": "caacabab"}, "output": false}, {"input": {"s": "aab"}, "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": "801f8565f926e652"}
vcr_7f6645f4be94
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, 5, 5, 7, 8, 8, 11, 12, 14, 14, 15]}, "output": [3, 4, 5, 7, 8, 11, 12, 14, 15]}, {"input": {"nums": [2, 5, 6, 6, 7, 7, 7, 10, 10]}, "output": [2, 5, 6, 7, 10]}, {"input": {"nums": [0, 2, 3, 3, 4, 5, 8, 12]}, "output": [0, 2, 3, 4, 5, 8, 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": "c013f1cfbc2871f7"}
vcr_df9c85b2b4b6
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": 12, "b": 229}, "output": 1}, {"input": {"a": 131, "b": 92}, "output": 1}, {"input": {"a": 47, "b": 161}, "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": "d1da22709d24b526"}
vcr_3fecc4c2d277
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [4, 17, 12, 28, 28, 42, 44, 44, 3]}, "output": 40}, {"input": {"prices": [21, 11, 14, 26, 15, 27, 6, 2]}, "output": 16}, {"input": {"prices": [37, 15, 32, 14, 3, 3, 15, 16, 40, 17, 15, 7]}, "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": "2685243117a90eb7"}
vcr_cc0eb4b72405
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": [30, 50, 30, 49, 32, 18, 3]}, "output": true}, {"input": {"nums": [9, 5, 18, 7, 23, 25, 1, 16, 15, 7, 8]}, "output": true}, {"input": {"nums": [9, 3, 1, 18, 2, 10, 25, 18, 14, 12, 22]}, "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": "145cbee06f95204f"}
vcr_fced3adb7574
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [18, -11, -17, 17, 15, 7, 12, 6, -9, -6, 22, 7, 2, -12, -16]}, "output": 73}, {"input": {"nums": [11, 0, -6, 2, 6]}, "output": 13}, {"input": {"nums": [2, -10, 3, -6, 12, -1, 3, 14, 3, -6, -8]}, "output": 31}]
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": "3000282f01acd8af"}
vcr_a721669626ed
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": ["aaa", "cc", "ab", "bbcc"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["abb", "bba", "bbb", "aba", "abb"]}, "output": [1, 1, 3]}, {"input": {"strs": ["bba", "abb", "bbb", "bba", "aaa", "bbb", "baa"]}, "output": [1, 1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "8d4ea8406432179f"}
vcr_d9d610d59f0d
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": 115, "b": 216}, "output": 1}, {"input": {"a": 47, "b": 173}, "output": 1}, {"input": {"a": 55, "b": 21}, "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": "1bc40628991093d5"}
vcr_8daa9bfdee25
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": 8162}, "output": 9}, {"input": {"n": 10946}, "output": 6}, {"input": {"n": 1808}, "output": 4}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "c304dc95ccefacd8"}
vcr_88b7a3134068
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": [8, 12, 14, 17, 20, 34, 35, 37, 40, 46, 47], "target": 39}, "output": 8}, {"input": {"nums": [6, 18, 25, 27, 29, 30], "target": 0}, "output": 0}, {"input": {"nums": [0, 4, 6, 7, 17, 31, 32, 36], "target": 21}, "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": "35a3d827918c087c"}
vcr_a734251a6eb5
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": "cdbeb bbeea daa bedb adec"}, "output": "adec bedb daa bbeea cdbeb"}, {"input": {"s": "aab bbab b b aab"}, "output": "aab b b bbab aab"}, {"input": {"s": "abb a aa"}, "output": "aa a abb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "824b8adbfde0495a"}
vcr_45e601145453
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": [2, 26, 6, 2]}, "output": 28}, {"input": {"nums": [17, 3, 3, 16, 8]}, "output": 33}, {"input": {"nums": [14, 20, 14, 17, 15]}, "output": 43}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "24709a90831cdcbf"}
vcr_635c32e3cfda
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, -2, 9, -17, -24, -7, 35, -17, 16, 28, -14, -1, -19, -13]}, "output": [15, 13, 22, 5, -19, -26, 9, -8, 8, 36, 22, 21, 2, -11]}, {"input": {"nums": [-10, -12, -9, 0, 6, 4]}, "output": [-10, -22, -31, -31, -25, -21]}, {"input": {"nums": [-12, -9, -1, 0, 5, -6]}, "output": [-12, -21, -22, -22, -17,...
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "8ad6d31a853c3563"}
vcr_36483a9031dc
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, 56, 31, 3, 49, 20]}, "output": 46}, {"input": {"prices": [6, 8, 2, 37, 27]}, "output": 35}, {"input": {"prices": [26, 9, 24, 21, 15, 36, 29, 2, 33, 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": "19a135d5f7d6d156"}
vcr_e7fe3f46d4dd
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, 5, 15, 17, 42], "target": 35}, "output": 4}, {"input": {"nums": [0, 3, 5, 7, 12, 16, 19, 30, 34, 36, 39], "target": 31}, "output": 8}, {"input": {"nums": [2, 25, 28], "target": 36}, "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": "66bc99262208e7b7"}
vcr_6e6b692c6c34
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": "aeedd cadd cadda ccda bde dcb"}, "output": "dcb bde ccda cadda cadd aeedd"}, {"input": {"s": "aabb ab bbbb"}, "output": "bbbb ab aabb"}, {"input": {"s": "bbaa bbb a b"}, "output": "b a bbb bbaa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "3756c01cb07523e3"}
vcr_4e1c078cb2c7
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, 47, 45, 40, 19, 12, 41]}, "output": true}, {"input": {"nums": [19, 2, 23, 11]}, "output": false}, {"input": {"nums": [25, 16, 8, 0, 10, 4, 13, 3, 16, 0]}, "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": "35d56f4f53839358"}
vcr_9d59ecb2db89
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": [9, 20, 1, 9, 28, 32, 32, 38, 38, 51, 20, 1, 28]}, "output": 51}, {"input": {"nums": [16, 13, 16, 3, 11, 13, 3, 11, 7, 52, 7]}, "output": 52}, {"input": {"nums": [24, 17, 56, 11, 17, 29, 29, 27, 24, 11, 27]}, "output": 56}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "ea3df1b30188beed"}
vcr_cbb0a051a51c
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 184, "b": 236}, "output": 4}, {"input": {"a": 28, "b": 149}, "output": 1}, {"input": {"a": 89, "b": 27}, "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": "04cade448200a1fd"}
vcr_c8f2c826eeda
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [57, 60, 19, 64, 71], "target": 79}, "output": [1, 2]}, {"input": {"nums": [-24, -28, -25, -3, 27, 37, -15, 38, 35], "target": 14}, "output": [0, 7]}, {"input": {"nums": [44, 39, -21, 13, 30], "target": 57}, "output": [0, 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": "40301f949026cef6"}
vcr_92f348f6d177
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, -12, 20]}, "output": [-14, -26, -6]}, {"input": {"nums": [-1, -4]}, "output": [-1, -5]}, {"input": {"nums": [-4, -7, -12]}, "output": [-4, -11, -23]}]
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": "3274f888e82630b4"}
vcr_d56f437c0fb5
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 5, 7, 9, 13, 19, 20, 21, 23, 27, 31, 39, 45, 47], "target": 24}, "output": 9}, {"input": {"nums": [6, 22, 26], "target": 37}, "output": 3}, {"input": {"nums": [2, 7, 10, 12, 15, 25], "target": 1}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "c68f7746f01be587"}
vcr_683a6517e299
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, -18, -3, 8, 15, 35, 39, 41], "target": 13}, "output": [0, 5]}, {"input": {"nums": [-20, -16, -2, 5, 7, 15, 16, 18, 26, 28, 35], "target": 5}, "output": [2, 4]}, {"input": {"nums": [-7, -5, 0, 11, 17, 37], "target": 54}, "output": [4, 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": "960bbad692ab175a"}
vcr_16e60aebbe8d
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, 6, 3, 1, 4]}, "output": [216, 72, 36, 72, 216, 54]}, {"input": {"nums": [4, 4, 2, 5, 2]}, "output": [80, 80, 160, 64, 160]}, {"input": {"nums": [5, 5, 2, 1, 2, 5]}, "output": [100, 100, 250, 500, 250, 100]}]
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": "56b389a86778ef67"}
vcr_689784d44e3f
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [18, 5, -12, -12, 11, 1, -16, -7, -18, -15, 9, -11, 4, 21, -1]}, "output": 25}, {"input": {"nums": [-4, 9, -11, 0, -8, -1, 6, 12, -5, 12]}, "output": 25}, {"input": {"nums": [3, 14, -7, 5]}, "output": 17}]
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": "94501174d3ddf4a4"}
vcr_cccb845fd91a
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": 47, "b": 164}, "output": 1}, {"input": {"a": 95, "b": 119}, "output": 1}, {"input": {"a": 161, "b": 18}, "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": "009ccffb0c10b554"}
vcr_70d7d6f77358
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": 246, "b": 105}, "output": 3}, {"input": {"a": 158, "b": 33}, "output": 1}, {"input": {"a": 81, "b": 62}, "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": "3c2044e6da60aee9"}
vcr_b424648a9189
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [21, 33, 9, 66, -3, 39, -16, -8, 49, 1], "target": -24}, "output": [6, 7]}, {"input": {"nums": [41, 18, -30, 37], "target": 55}, "output": [1, 3]}, {"input": {"nums": [10, -16, -3, -20, 22, 37], "target": -6}, "output": [0, 1]}]
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": "f1ab23c8ab38da94"}
vcr_60638f3edb52
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [28, 23, 6, 10, 18, 20, 14, 11]}, "output": 69}, {"input": {"nums": [18, 18, 7, 8, 11, 15, 10, 19, 12]}, "output": 60}, {"input": {"nums": [16, 5, 17, 10, 19, 19, 2]}, "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": "66ef8eee2cb1ab98"}
vcr_ecae82ba7483
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": "dcb b"}, "output": "b dcb"}, {"input": {"s": "bbb abaa"}, "output": "abaa bbb"}, {"input": {"s": "abab a abb"}, "output": "abb a abab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "c87aebc3a978eed9"}
vcr_71bce0b43288
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": [31, 34, 26, 15, 29, 7, 45, 23, 31]}, "output": true}, {"input": {"nums": [22, 21, 1]}, "output": false}, {"input": {"nums": [9, 7, 15, 5, 25, 10, 17, 11]}, "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": "db2bc67e2bd202f8"}
vcr_3d658e278bec
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": "eeb bd dbbaea e"}, "output": "e dbbaea bd eeb"}, {"input": {"s": "babb ba baa abbb"}, "output": "abbb baa ba babb"}, {"input": {"s": "a aaaa"}, "output": "aaaa a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "efa087591b0fb21d"}
vcr_e66736846551
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": "acc cd dbddbd be cadca eeeac"}, "output": "eeeac cadca be dbddbd cd acc"}, {"input": {"s": "baba ab"}, "output": "ab baba"}, {"input": {"s": "a ba ab b abb"}, "output": "abb b ab ba a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "306e825600e0503f"}
vcr_0eb7be627cf3
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": ["bab", "abab", "ab", "bac", "bcab"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bab", "aaa", "abb", "abb", "bab", "baa"]}, "output": [1, 1, 5]}, {"input": {"strs": ["baa", "bab", "abb", "baa"]}, "output": [2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "787e6483cfac3762"}
vcr_72c70fd012a7
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 5, 4, 5, 1]}, "output": [100, 60, 75, 60, 300]}, {"input": {"nums": [1, 3, 3, 4, 3, 1]}, "output": [108, 36, 36, 27, 36, 108]}, {"input": {"nums": [3, 2, 1, 4, 5, 4]}, "output": [160, 240, 480, 120, 96, 120]}]
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": "1216a36054cc3b62"}
vcr_58691cf2026e
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": [10, 12, 20, 25, 31, 32], "target": 33}, "output": 6}, {"input": {"nums": [1, 6, 16, 29, 30, 36], "target": 38}, "output": 6}, {"input": {"nums": [0, 8, 20, 23, 24, 26, 27, 32, 33, 34, 38], "target": 31}, "output": 7}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "c8fbf9a0e70dde4d"}
vcr_90dd7590468e
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": "eeeb bac abecc dbe ccab e bde"}, "output": "bde e ccab dbe abecc bac eeeb"}, {"input": {"s": "baab a"}, "output": "a baab"}, {"input": {"s": "ab aab a b"}, "output": "b a aab ab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "7d9de68af2fa16b8"}
vcr_d3a6f3cad06e
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": [-19, -18, -17, 3, 13, 28, 34, 45], "target": 28}, "output": [2, 7]}, {"input": {"nums": [-14, -10, 0, 4, 7], "target": -24}, "output": [0, 1]}, {"input": {"nums": [-20, -11, -7, -4, 2, 23, 26, 37], "target": 3}, "output": [0, 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": "eb1760a8eb46dedd"}
vcr_bc7b7cbbaf73
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, 27, 20, 3, 28, 22, 0, 27, 6, 16, 12, 11]}, "output": 119}, {"input": {"nums": [16, 3, 11, 13, 2, 18]}, "output": 47}, {"input": {"nums": [9, 7, 15, 11, 6]}, "output": 30}]
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": "b88325aa11df494d"}
vcr_380470ff6e75
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": 49678}, "output": 6}, {"input": {"n": 1689}, "output": 6}, {"input": {"n": 10311}, "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": "48cbc1c6a71a67d8"}
vcr_1a9dde7bc6d5
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 10, 26, 35, 37, 39, 48], "target": 11}, "output": 2}, {"input": {"nums": [1, 7, 13, 16, 17, 21, 30, 31], "target": 1}, "output": 0}, {"input": {"nums": [0, 2, 5, 8, 15, 16, 19, 27, 29, 30, 31, 33], "target": 39}, "output": 12}]
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": "5b947a7d2cd45506"}
vcr_e728eb660ce1
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-24, 17, 1, 16, 14, 20]}, "output": [-24, -7, -6, 10, 24, 44]}, {"input": {"nums": [14, -6, -9, 12, 4, 18, 0, 1]}, "output": [14, 8, -1, 11, 15, 33, 33, 34]}, {"input": {"nums": [12, 3, -9, 16]}, "output": [12, 15, 6, 22]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "d114428002b54f23"}
vcr_48675e0e45d0
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": 25108}, "output": 5}, {"input": {"n": 12405}, "output": 7}, {"input": {"n": 2172}, "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": "9404b781d10d6290"}
vcr_262726ae1e03
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, 1, 2, 6, 6, 9, 15]}, "output": [1, 2, 6, 9, 15]}, {"input": {"nums": [0, 2, 6, 7, 7, 9, 10]}, "output": [0, 2, 6, 7, 9, 10]}, {"input": {"nums": [0, 7, 10]}, "output": [0, 7, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "5b8f40348d3adbed"}
vcr_88319efb0819
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 3, 4, 6, 7, 8, 9, 10, 31, 38, 39, 42, 48], "target": 32}, "output": 9}, {"input": {"nums": [4, 12, 13, 15, 16, 22, 29, 30, 34], "target": 17}, "output": 5}, {"input": {"nums": [4, 14, 25, 29], "target": 27}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "5698162ba9925457"}
vcr_6b496758f8cb
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, 6, 8, 12, 14, 15, 25, 29, 39, 43], "target": 48}, "output": 10}, {"input": {"nums": [0, 1, 12, 16, 31], "target": 18}, "output": 4}, {"input": {"nums": [0, 8, 13, 24, 35, 36], "target": 5}, "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": "a78685a17e632a77"}
vcr_4a8ab1110b88
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 3, 3, 7, 12, 13, 13, 14]}, "output": [1, 3, 7, 12, 13, 14]}, {"input": {"nums": [1, 3, 4, 6, 7, 12]}, "output": [1, 3, 4, 6, 7, 12]}, {"input": {"nums": [3, 4, 6, 6, 6, 7, 8, 9, 10, 10, 12, 12]}, "output": [3, 4, 6, 7, 8, 9, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "d24985c9b1ac41bc"}
vcr_b812632e1beb
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [7, 0, 12, 15, 17, 12, 21, 11, 20]}, "output": 115}, {"input": {"nums": [-11, -5, 14]}, "output": 14}, {"input": {"nums": [12, -5, -7, 3, -2, -4]}, "output": 12}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "7977532116d94acb"}
vcr_86b545cdea67
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": [18, 46, 50, 50, 47, 35, 9, 27, 47, 19, 41, 22]}, "output": true}, {"input": {"nums": [24, 13, 4, 20, 8, 0, 12, 7, 14]}, "output": false}, {"input": {"nums": [7, 16, 21, 1, 3]}, "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": "53efd3163d6a0081"}
vcr_f0bb45364499
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": [2, 12, 18, 18, 2, 36, 15, 26, 16, 6, 4, 35, 50, 9, 11]}, "output": true}, {"input": {"nums": [2, 7, 2]}, "output": true}, {"input": {"nums": [20, 25, 5, 24, 1, 24, 12, 7, 19, 0, 19]}, "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": "110e7ace3b419740"}
vcr_92a989d84810
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": [4, 6, 7, 3, 7, 5, 7, 5]}, "output": [154350, 102900, 88200, 205800, 88200, 123480, 88200, 123480]}, {"input": {"nums": [2, 3, 1, 4]}, "output": [12, 8, 24, 6]}, {"input": {"nums": [2, 3, 3, 1, 3]}, "output": [27, 18, 18, 54, 18]}]
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": "9f0e060aa230af01"}
vcr_67ab04a10ddf
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": [-3, 10, 34, 31, 14, -11]}, "output": [-3, 7, 41, 72, 86, 75]}, {"input": {"nums": [15, 16, 2, -6]}, "output": [15, 31, 33, 27]}, {"input": {"nums": [7, 17, 7, 12]}, "output": [7, 24, 31, 43]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "1967af1a093d5182"}
vcr_268d2c2ce52d
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-9, -1, 38, 3, 21, 4, 61, -25, 67], "target": 99}, "output": [2, 6]}, {"input": {"nums": [17, -29, -3, 39, 31, 19, -25, 12], "target": -32}, "output": [1, 2]}, {"input": {"nums": [46, 17, -25, 29, -3, -18], "target": 4}, "output": [2, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "8bfcc9a6565cf862"}
vcr_b53597e09dc1
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [65, 61, 57, 11, 48, 14, 21], "target": 71}, "output": [2, 5]}, {"input": {"nums": [13, -30, 3, 40, 5, 2, 33, -16], "target": 36}, "output": [2, 6]}, {"input": {"nums": [21, 37, -19, 39, 29, 44, -9, 42, 8, -17, -13], "target": 18}, "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": "0ab5e259001ac58f"}
vcr_edf5f8efe4f3
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [10, 4, 22, 3, -23, -1, 27, -21, 17, 10, 29, 32, -15, 3]}, "output": [10, 14, 36, 39, 16, 15, 42, 21, 38, 48, 77, 109, 94, 97]}, {"input": {"nums": [9, -8, 18, 17, 16, -9, 16, 16, 11, -3]}, "output": [9, 1, 19, 36, 52, 43, 59, 75, 86, 83]}, {"input": {"nums": [-1, -2, 7, -6, 0, 6, -2, -5, 14, -9]}, ...
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": "ba266337d0d3343b"}
vcr_f6948d2cb54a
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, 4, 6, 10, 12, 14]}, "output": [0, 4, 6, 10, 12, 14]}, {"input": {"nums": [0, 2, 3, 6, 6, 8, 8, 10, 11]}, "output": [0, 2, 3, 6, 8, 10, 11]}, {"input": {"nums": [2, 4, 7, 7, 11, 11, 12, 12]}, "output": [2, 4, 7, 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": "31b4d37eee8b0e50"}
vcr_80274049b612
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": [27, 26, 6, -22, 30, 5, -16, 17, 21, 29, 8, 10]}, "output": [27, 53, 59, 37, 67, 72, 56, 73, 94, 123, 131, 141]}, {"input": {"nums": [0, 16, -8, 11, 3]}, "output": [0, 16, 8, 19, 22]}, {"input": {"nums": [4, -9]}, "output": [4, -5]}]
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": "8bb35f166754efc6"}
vcr_d1771aeb273e
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["aa", "bba", "cccb", "ca", "cbc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "aaa", "aaa", "abb"]}, "output": [2, 2]}, {"input": {"strs": ["aba", "abb", "abb", "bba", "aab"]}, "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": "29cff237e0e647f8"}
vcr_5b637e709c0d
medium
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "adcdcda"}, "output": true}, {"input": {"s": "cbb"}, "output": false}, {"input": {"s": "cabaaaab"}, "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": "ac7fea12723909d8"}
vcr_b0f0a79a7d89
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": [-33, 34, 17, -35, 76, 78, 70], "target": 93}, "output": [2, 4]}, {"input": {"nums": [-19, 6, 26, -21, 47, 35, 38, 2, 25, 33, -10], "target": 16}, "output": [0, 5]}, {"input": {"nums": [18, 49, 8, 29, -6, -28, 35, 44, -10, 0], "target": -20}, "output": [2, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "cea85c25cc7da41e"}
vcr_0ab50888d34b
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": [-18, -14, 8, 9, 22, 38, 43, 46], "target": 30}, "output": [2, 4]}, {"input": {"nums": [-16, -14, -6, 6, 17, 22, 39], "target": 25}, "output": [1, 6]}, {"input": {"nums": [-9, -6, -2, 2, 9, 14, 28, 37], "target": 30}, "output": [3, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "abc5a8cde791d82b"}
vcr_f9a3404fe015
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": [9, 33, 23, 30, 15, 22, 34, 22, 5, 65, 30, 33, 15, 34, 5, 23, 9]}, "output": 65}, {"input": {"nums": [27, 24, 28, 20, 10, 53, 28, 27, 24, 20, 10]}, "output": 53}, {"input": {"nums": [13, 43, 19, 21, 13, 21, 19]}, "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": "48dbc6958d281aba"}
vcr_5fc7c42b5d40
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, 2, 4, 1, 2, 0]}, "output": true}, {"input": {"nums": [2, 0, 3, 1, 1, 3]}, "output": true}, {"input": {"nums": [2, 0, 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": "3d691d32bb82e2e2"}
vcr_d07843a1de8c
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [38, 4, 36, 79, 11, 4, 36, 10, 38, 11, 10]}, "output": 79}, {"input": {"nums": [27, 9, 20, 12, 20, 27, 12, 18, 9, 18, 57]}, "output": 57}, {"input": {"nums": [22, 11, 8, 11, 8, 19, 9, 19, 22, 1, 1, 52, 9]}, "output": 52}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "cc61401a1fbcdab3"}
vcr_8b81815c292b
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, 5, 6, 8, 11, 11]}, "output": [2, 5, 6, 8, 11]}, {"input": {"nums": [1, 1, 3, 3, 4, 5, 7, 7, 9]}, "output": [1, 3, 4, 5, 7, 9]}, {"input": {"nums": [7, 7, 8]}, "output": [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": "b2444c25dcc40424"}
vcr_995130463d70
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [32, 29, 29, 53, 32, 25, 25]}, "output": 53}, {"input": {"nums": [19, 5, 15, 13, 15, 20, 28, 19, 28, 20, 13, 44, 5]}, "output": 44}, {"input": {"nums": [26, 17, 23, 17, 26, 10, 10, 41, 23]}, "output": 41}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "03efcfc13afe0f0b"}
vcr_2354c802e158
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": ["bccc", "bcbc", "cacc", "cacc", "ca", "cac", "ba", "ab"]}, "output": [1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["aab", "bab", "baa", "bba"]}, "output": [2, 2]}, {"input": {"strs": ["baa", "bba", "bab"]}, "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": "bb16b7bd57ae8e7f"}
vcr_1a2032436afe
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": [-15, 16, 7, -5, -7, 3, 20, 4, -13, -15, -16, -16], "k": 21}, "output": [-5, -7, 3, 20, 4, -13, -15, -16, -16, -15, 16, 7]}, {"input": {"nums": [-9, -4, 6, -2, 3, 4, -8, -5, -4, -2], "k": 10}, "output": [-9, -4, 6, -2, 3, 4, -8, -5, -4, -2]}, {"input": {"nums": [-10, 8, 2, 4, 10], "k": 11}, "output"...
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": "0594f436b308b48d"}
vcr_a49630598911
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, -3, 14, -20, 8, 19, 7, 10, 21, 17]}, "output": [10, 7, 21, 1, 9, 28, 35, 45, 66, 83]}, {"input": {"nums": [17, 4, -8, 7, 1, 14, -1, -5, -6]}, "output": [17, 21, 13, 20, 21, 35, 34, 29, 23]}, {"input": {"nums": [10, 7, 17, 13, 18, -12, 3, -8, 7, -8]}, "output": [10, 17, 34, 47, 65, 53, 56, 48, 5...
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": "dd18e6653904dd6f"}
vcr_619a8fc26eab
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 0, 4, 0, 2, 0, 1, 0]}, "output": true}, {"input": {"nums": [1, 1, 3, 1, 3]}, "output": true}, {"input": {"nums": [2, 1, 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": "70b6a3ac20bcc510"}
vcr_f290e9ee4280
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [22, -2, 20]}, "output": [22, 20, 40]}, {"input": {"nums": [10, -9, 16, -7, 13, 18]}, "output": [10, 1, 17, 10, 23, 41]}, {"input": {"nums": [7, -7, -8, -7]}, "output": [7, 0, -8, -15]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "bb64a3e3eaa3fc1d"}
vcr_313e6dc8c04c
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [40, 25, 28, -2], "target": 65}, "output": [0, 1]}, {"input": {"nums": [47, 44, -26, 13, 7, 16], "target": 91}, "output": [0, 1]}, {"input": {"nums": [49, 25, 28, 10, 11, 9, -27, 20], "target": 19}, "output": [3, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "67c8ddff87c1ecaf"}
vcr_465a8001d2dd
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, 6, 27, 30, 44], "target": 10}, "output": 2}, {"input": {"nums": [2, 4, 12, 15, 16, 18, 26], "target": 15}, "output": 3}, {"input": {"nums": [0, 4, 11, 16, 25, 29, 32, 36], "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": "7da8753201e55219"}
vcr_23cfbb257286
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [8, -18, 13, 0, -8, -2, -20, 12, -17], "k": 3}, "output": [-20, 12, -17, 8, -18, 13, 0, -8, -2]}, {"input": {"nums": [2, 3, 4, -6, -3, 0, -10, 4], "k": 20}, "output": [-3, 0, -10, 4, 2, 3, 4, -6]}, {"input": {"nums": [-3, 7, -10, 1, 1, 10, 5, 4], "k": 0}, "output": [-3, 7, -10, 1, 1, 10, 5, 4]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "2559450e428f9475"}
vcr_7ee4dcc880bb
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": 35797}, "output": 9}, {"input": {"n": 5446}, "output": 6}, {"input": {"n": 14901}, "output": 8}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "7e09a3d28433520b"}
vcr_3b479aca04cb
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": [9, 22, 18, -2, 16, 21, -16, -14, 14, 8, -7, 4, -3, 16, -8]}, "output": 86}, {"input": {"nums": [-7, -10, 7, 3, 0]}, "output": 10}, {"input": {"nums": [-3, 2, 4, -5, -12, 1, -7, 12, 14, -2]}, "output": 26}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "13f2449f5d738a8d"}
vcr_6b51e6465321
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 14, 15, 41], "target": 37}, "output": 3}, {"input": {"nums": [4, 10, 15, 17, 18, 26, 31, 34], "target": 11}, "output": 2}, {"input": {"nums": [7, 10, 15, 16, 18, 20, 24, 28], "target": 17}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "bad8958c231f9885"}
vcr_34fc96a3c6d4
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [22, 5, 19, 18]}, "output": [22, 27, 46, 64]}, {"input": {"nums": [15, 0, 1]}, "output": [15, 15, 16]}, {"input": {"nums": [4, -12, 13]}, "output": [4, -8, 5]}]
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": "c95eae031d99ba61"}
vcr_bb109efe6787
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": "ccbacac"}, "output": 6}, {"input": {"s": "cabcbbcc"}, "output": 6}, {"input": {"s": "bccabcbb"}, "output": 6}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "e93d01f319172f52"}
vcr_89ae3708a841
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 6, 7, 8, 10, 12, 12, 13, 13, 14, 15]}, "output": [0, 1, 6, 7, 8, 10, 12, 13, 14, 15]}, {"input": {"nums": [0, 0, 2, 3, 3, 4, 6, 6, 7, 7]}, "output": [0, 2, 3, 4, 6, 7]}, {"input": {"nums": [1, 3, 4, 7, 8, 8, 10, 12]}, "output": [1, 3, 4, 7, 8, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "efd175cfbd3bdabb"}
vcr_8b70c0afb505
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": "aadabdbdbadaa"}, "output": true}, {"input": {"s": "cccbbb"}, "output": false}, {"input": {"s": "cccccccbcc"}, "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": "a1358ef10f1e4fb6"}
vcr_6902165fae3c
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": [43, 30, 15, 2, 48, 26, 29, 31, 41, 39, 6, 43]}, "output": true}, {"input": {"nums": [0, 12, 2, 12, 15, 7, 14, 0, 1]}, "output": true}, {"input": {"nums": [18, 14, 3, 8]}, "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": "b82f3178ab53ddb1"}
vcr_8bde86f65cb6
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": 530}, "output": 3}, {"input": {"n": 12351}, "output": 8}, {"input": {"n": 2357}, "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": "7764b7a7554d2b5f"}
vcr_da4ef6c5e704
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": "bbabbcaaacc"}, "output": 6}, {"input": {"s": "cabcc"}, "output": 4}, {"input": {"s": "cac"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "9efff8392778c22b"}
vcr_b14680e2a91e
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": [20, 7, -16, -1, -3, 2, -2], "k": 3}, "output": [-3, 2, -2, 20, 7, -16, -1]}, {"input": {"nums": [-2, -10, -6], "k": 2}, "output": [-10, -6, -2]}, {"input": {"nums": [0, 5, -7, -10, -7, -6, 10, -6, -9, 8], "k": 13}, "output": [-6, -9, 8, 0, 5, -7, -10, -7, -6, 10]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "a659215440ee0524"}
vcr_5ca27af8de5c
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["ab", "bccc", "bcac", "ab", "bbb", "bbbc", "abc"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["abb", "aaa", "abb"]}, "output": [1, 2]}, {"input": {"strs": ["bab", "bbb", "baa", "abb", "bba", "aab", "bab"]}, "output": [1, 2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "7e6d91e8da63ea21"}
vcr_5b608eb1ecf5
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": [41, 29, 46, 8, 35, 2, 60, 44, 30, 42]}, "output": 58}, {"input": {"prices": [11, 36, 38, 34, 18, 24, 23, 13, 28]}, "output": 27}, {"input": {"prices": [10, 28, 33, 13]}, "output": 23}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "2fad4c94e1cd5568"}