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_b9f35e8713b7
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-16, -7, 22, 12, 14, 13, -13, -4, 12, -12]}, "output": 61}, {"input": {"nums": [-8, 11, 4, 4, -7, -11, 3, -5, -1, -12]}, "output": 19}, {"input": {"nums": [-1, -11, -12, -6, -7, 8, -11, 12, 10, -10]}, "output": 22}]
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": "d460286da07f6647"}
vcr_bdd147e6187e
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [4, 7, 15, 10, 27]}, "output": 46}, {"input": {"nums": [0, 18, 19]}, "output": 19}, {"input": {"nums": [8, 11, 12, 10, 2]}, "output": 22}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "a99ecda2754222e8"}
vcr_cda64985a77d
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": [22, 4, 11, 8, 20, 12, 4, 22, -6, 8, -6]}, "output": 105}, {"input": {"nums": [-9, -3, 14, 8, 2, 8, -6, -8]}, "output": 32}, {"input": {"nums": [0, 6, -7, -4, 14, 0, -3]}, "output": 14}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "8db192456fba5068"}
vcr_cfe06ed22150
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": [-8, 14, 19, 24, 29, 44, 45, 49], "target": 21}, "output": [0, 4]}, {"input": {"nums": [-2, 3, 6, 10, 11, 14, 20, 28, 29, 32, 36], "target": 35}, "output": [1, 9]}, {"input": {"nums": [29, 30, 36, 37], "target": 65}, "output": [0, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "3c68bd14c67ca902"}
vcr_739d7712d563
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-9, -2, -5, -7, 3, -8], "k": 11}, "output": [-2, -5, -7, 3, -8, -9]}, {"input": {"nums": [-9, -7, 5, 10, -9, 4, 5, 2], "k": 1}, "output": [2, -9, -7, 5, 10, -9, 4, 5]}, {"input": {"nums": [2, 6, -1], "k": 7}, "output": [-1, 2, 6]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "cd5fa7c1908f104b"}
vcr_6e72ec4eee12
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": [73, -35, 9, -33, 74, -19], "target": -52}, "output": [3, 5]}, {"input": {"nums": [30, 2, 16, 26, 4, 13, -11, 43, 23, 47], "target": 2}, "output": [5, 6]}, {"input": {"nums": [-19, -24, 5, -14, -3, -6, 26, 21, 46, -1, 44], "target": 47}, "output": [6, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "95450710f561e4fc"}
vcr_786ac1922701
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-23, -11, 2, 4, 21, 34, 48], "target": 25}, "output": [0, 6]}, {"input": {"nums": [-11, -8, -3, -2, 9, 15, 19, 23, 25, 27, 31], "target": 12}, "output": [0, 7]}, {"input": {"nums": [-15, -11, 9, 12, 15, 16, 23, 27, 30], "target": 5}, "output": [1, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "6bc97d06600e141e"}
vcr_038df771ac14
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -20, 10, 21, 24, 35], "target": -2}, "output": [0, 4]}, {"input": {"nums": [3, 11, 20, 37], "target": 48}, "output": [1, 3]}, {"input": {"nums": [-18, -1, 13, 18, 27, 31, 37], "target": 13}, "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": "76a96968ba5534fb"}
vcr_c17d2e67ccc8
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, 3, -15, 10, -18, 1, 7, -17, -12, -13, 1], "k": 4}, "output": [-17, -12, -13, 1, 20, 3, -15, 10, -18, 1, 7]}, {"input": {"nums": [5, -2, 0, -9, -10, 1, 4, -7, 0, 4], "k": 1}, "output": [4, 5, -2, 0, -9, -10, 1, 4, -7, 0]}, {"input": {"nums": [2, 2, -7, 4, 4, 8, -1, -4, 5, -10], "k": 14}, "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": "4fdfc29554166f68"}
vcr_8e79d0f03bad
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, 25, 19, 23, 5, 12, 30, 6, 19]}, "output": 97}, {"input": {"nums": [6, 14]}, "output": 14}, {"input": {"nums": [5, 16, 17, 20, 20, 18, 13, 14, 19, 7]}, "output": 75}]
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": "18a717a027980eae"}
vcr_0a656ef4897a
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-24, -23, -4, 1, 30, 33, 34, 42], "target": 19}, "output": [1, 7]}, {"input": {"nums": [-11, -6, -4, 9, 21, 24, 34], "target": 33}, "output": [3, 5]}, {"input": {"nums": [-16, -13, -12, -8, -5, 8, 15, 16, 37], "target": 24}, "output": [1, 8]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "bb90e1d10b7d22df"}
vcr_d9df760be3ef
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": [7, 9, 18, 7, -9, -15, 31, 25, 18, 11]}, "output": [7, 16, 34, 41, 32, 17, 48, 73, 91, 102]}, {"input": {"nums": [14, 18, 14, 15, 18]}, "output": [14, 32, 46, 61, 79]}, {"input": {"nums": [-1, -6, 6, -7]}, "output": [-1, -7, -1, -8]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "ce8941874dcce460"}
vcr_7fb9c37baf52
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [7, -6, -9, 5, -15, -8, -15, -8, 2], "k": 21}, "output": [-15, -8, 2, 7, -6, -9, 5, -15, -8]}, {"input": {"nums": [6, -8, -2, 9, 8, 2], "k": 5}, "output": [-8, -2, 9, 8, 2, 6]}, {"input": {"nums": [0, -10, -7, 3, -2, -4, 10, 5, 0, -5], "k": 12}, "output": [0, -5, 0, -10, -7, 3, -2, -4, 10, 5]}]
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": "9b9a46e5c6c3be91"}
vcr_9931efa33e5b
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, 26, 13, 24, 24, 17, 6, 13, 8, 22, 29]}, "output": 111}, {"input": {"nums": [13, 3, 17, 2, 13, 9, 5, 0, 0]}, "output": 48}, {"input": {"nums": [2, 8, 11, 15, 3, 12, 0, 12, 9]}, "output": 47}]
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": "db2f41a4d552f40b"}
vcr_655ef3be0285
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": 18, "b": 204}, "output": 6}, {"input": {"a": 100, "b": 121}, "output": 1}, {"input": {"a": 12, "b": 157}, "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": "6dca93053d34e2db"}
vcr_2a416767bc9a
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": [17, -22, -20, 63, -14, 73], "target": 53}, "output": [2, 5]}, {"input": {"nums": [10, 21, 38, 43, 26, -23, 35], "target": 47}, "output": [1, 4]}, {"input": {"nums": [35, -20, 34, -18, -25, -26], "target": 17}, "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": "b42bbcdd06b2082e"}
vcr_38286a37dd92
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": "baaacaaabbbab"}, "output": 7}, {"input": {"s": "acbbc"}, "output": 4}, {"input": {"s": "aba"}, "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": "bd6cf5cb9f1e2a78"}
vcr_a651aa1fc43e
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": [39, 46, 25, 55, 42, 30, 60, 25, 39, 50, 28, 19]}, "output": 35}, {"input": {"prices": [15, 5, 34, 33, 28, 9, 32, 23, 35]}, "output": 30}, {"input": {"prices": [35, 10, 8, 11, 13, 13, 16]}, "output": 8}]
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": "dfa9fa554e07ef5d"}
vcr_9abeec036e9f
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, 2, 3, 2, 6, 3, 5]}, "output": [3240, 1080, 1620, 1080, 1620, 540, 1080, 648]}, {"input": {"nums": [1, 5, 5, 5, 2]}, "output": [250, 50, 50, 50, 125]}, {"input": {"nums": [2, 3, 3, 1]}, "output": [9, 6, 6, 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": "2a612b980707bf59"}
vcr_e7c2459ce212
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", "ccb", "aab", "cb", "cccb", "bca", "abab", "aa", "acab"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "baa", "bba", "bab", "bbb"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["bab", "bab", "bbb", "bbb", "abb", "aaa"]}, "output": [1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "e8243669571ea17e"}
vcr_a3d89e219899
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "ab babad cdad b eacca"}, "output": "eacca b cdad babad ab"}, {"input": {"s": "bab aaba b bba aa"}, "output": "aa bba b aaba bab"}, {"input": {"s": "abb aaaa"}, "output": "aaaa 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": "3400aaa5ff97ee0b"}
vcr_c2065b74e824
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": 39290}, "output": 9}, {"input": {"n": 8592}, "output": 4}, {"input": {"n": 15204}, "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": "47429173b06235ef"}
vcr_0a23f7de83da
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": "caaaabccb"}, "output": 5}, {"input": {"s": "acabaaabbac"}, "output": 8}, {"input": {"s": "acaccc"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "6df9c7fc1dcfcfde"}
vcr_cded913543a7
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": "cb bcdcd"}, "output": "bcdcd cb"}, {"input": {"s": "bbb bbbb bab"}, "output": "bab bbbb bbb"}, {"input": {"s": "bbb ab"}, "output": "ab bbb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "16c3597f890cb8fc"}
vcr_b84941cc655d
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": ["aca", "ab", "bc", "cbb", "aa"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bab", "bba"]}, "output": [1, 2]}, {"input": {"strs": ["abb", "aba", "bba", "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": "5111c27fc9175381"}
vcr_10228e23bc32
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": [-17, -11, -8, -7, 16, 26, 34], "target": -28}, "output": [0, 1]}, {"input": {"nums": [-15, -4, -3, 23, 24], "target": -7}, "output": [1, 2]}, {"input": {"nums": [2, 6, 15, 26, 33, 37], "target": 48}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "42c45ecb226df28f"}
vcr_aed2fd6c67f0
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-16, -19, 18, -9, 7, -17, 0, 2, 10, -18], "k": 29}, "output": [-19, 18, -9, 7, -17, 0, 2, 10, -18, -16]}, {"input": {"nums": [-6, -5, 9, 3, 4, -8, 8, -1, -4], "k": 2}, "output": [-1, -4, -6, -5, 9, 3, 4, -8, 8]}, {"input": {"nums": [-3, 2, -5, 7, -1, -10, 10], "k": 19}, "output": [-5, 7, -1, -10, 1...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "5da8312d0b7f1542"}
vcr_1a0cc5b78a64
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": "bd caedcc bc a"}, "output": "a bc caedcc bd"}, {"input": {"s": "aa abb bba a"}, "output": "a bba abb aa"}, {"input": {"s": "aab aba b bbaa"}, "output": "bbaa b aba aab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "ca2cd2035d3588e0"}
vcr_0283d5c8b1f8
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": "cdcdcab"}, "output": false}, {"input": {"s": "bccacabbc"}, "output": false}, {"input": {"s": "cab"}, "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": "3885437407c5a6fa"}
vcr_34117029e276
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, -10, -6, -3, -1, 22, 41, 43], "target": -7}, "output": [2, 4]}, {"input": {"nums": [-14, -12, -1, 12, 25, 26, 32, 34, 37], "target": 12}, "output": [0, 5]}, {"input": {"nums": [-9, 18, 33, 36, 38], "target": 9}, "output": [0, 1]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "9410e5c1c1043af9"}
vcr_c17feb2fadb4
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": [25, 8, 19, 2, 14, 22, 20, 21, 10]}, "output": 88}, {"input": {"nums": [12, 10, 15, 6, 0]}, "output": 27}, {"input": {"nums": [19, 6, 8, 10, 6, 15]}, "output": 44}]
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": "90eb17b1a86e1dbd"}
vcr_1293de4367a6
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-19, 11, 20, -13, 18, -6, -18, 18, -9]}, "output": [-19, -8, 12, -1, 17, 11, -7, 11, 2]}, {"input": {"nums": [-7, 14]}, "output": [-7, 7]}, {"input": {"nums": [16, 18, 13, -11, 10, -10, -4, 5, 12, 14]}, "output": [16, 34, 47, 36, 46, 36, 32, 37, 49, 63]}]
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": "9c966012efa4eb85"}
vcr_f105f19bcffe
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": 55567}, "output": 9}, {"input": {"n": 12810}, "output": 5}, {"input": {"n": 3680}, "output": 5}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "8ed3fb18069f57e9"}
vcr_3409cebbe125
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [10, -17, -3, 0, 2, -13, 20, 10, 5, -4, 14], "k": 2}, "output": [-4, 14, 10, -17, -3, 0, 2, -13, 20, 10, 5]}, {"input": {"nums": [-5, 2, 9, -8], "k": 20}, "output": [-5, 2, 9, -8]}, {"input": {"nums": [5, -3, -3, 3, -3, 2, -8, -9], "k": 15}, "output": [-3, -3, 3, -3, 2, -8, -9, 5]}]
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": "fa37412ac0e8b4dd"}
vcr_d7ac0a027ef4
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [36, 27, 36, 20, 65, 2, 20, 19, 2, 19, 27]}, "output": 65}, {"input": {"nums": [5, 52, 23, 5, 23]}, "output": 52}, {"input": {"nums": [10, 50, 10, 7, 20, 29, 29, 20, 7]}, "output": 50}]
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": "660555a6d57826ed"}
vcr_4249819d218e
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, 4, 8, 9, 12, 16, 18, 35, 37, 39, 40, 49], "target": 36}, "output": 8}, {"input": {"nums": [4, 7, 9, 14, 16, 26], "target": 29}, "output": 6}, {"input": {"nums": [6, 8, 10, 11, 15, 18], "target": 12}, "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": "c7e9dd3cb45ae949"}
vcr_b365888990d2
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 6, 13, 25, 29, 32, 42, 46, 47], "target": 7}, "output": 2}, {"input": {"nums": [5, 6, 8, 18, 20, 23, 26, 27, 29, 33, 38], "target": 2}, "output": 0}, {"input": {"nums": [4, 13, 18, 38], "target": 7}, "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": "690778fa080732c2"}
vcr_fbe65af25886
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": [1, 2, 30, 22, 2, 26]}, "output": 57}, {"input": {"nums": [14, 6, 0, 3, 6, 12]}, "output": 29}, {"input": {"nums": [1, 5, 12, 13, 15, 9, 7, 12]}, "output": 40}]
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": "f0f8d66d951438cf"}
vcr_ff9896d5e43f
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": "abc bdadae ceda baebc"}, "output": "baebc ceda bdadae abc"}, {"input": {"s": "b a"}, "output": "a b"}, {"input": {"s": "aab ab baba abbb"}, "output": "abbb baba ab aab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "af5bf7ca3a4fdde9"}
vcr_b0041609c8fd
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": "acdbbdbbcbdcaa"}, "output": false}, {"input": {"s": "abcbcccb"}, "output": false}, {"input": {"s": "cba"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "30298a19eccca93d"}
vcr_e486ce824c14
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": [59, -14, 52, 9, -3, 18, 24, 27], "target": 49}, "output": [2, 4]}, {"input": {"nums": [-11, -15, -18, -9], "target": -26}, "output": [0, 1]}, {"input": {"nums": [-3, 49, 5, -26, 36, 44, 14, 25], "target": 41}, "output": [2, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "ccd13bd0b1a7fcc1"}
vcr_cd11464f312e
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": "decbab eadc e cbbd"}, "output": "cbbd e eadc decbab"}, {"input": {"s": "aba bbab b a"}, "output": "a b bbab aba"}, {"input": {"s": "aba aabb aaaa bbbb"}, "output": "bbbb aaaa aabb aba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "327420d2106612f4"}
vcr_ea73d331d340
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, 3, 7, 11, 13]}, "output": [0, 3, 7, 11, 13]}, {"input": {"nums": [5, 7, 8, 11, 12, 12]}, "output": [5, 7, 8, 11, 12]}, {"input": {"nums": [1, 2, 4, 4, 6, 12]}, "output": [1, 2, 4, 6, 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": "88bcf3be26168629"}
vcr_472bd82d162f
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": [18, 31, 4, 61, 4, 6, 18, 6, 31]}, "output": 61}, {"input": {"nums": [23, 12, 40, 28, 22, 22, 18, 23, 18, 28, 12]}, "output": 40}, {"input": {"nums": [4, 40, 4, 12, 12]}, "output": 40}]
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": "9eba9363763259a2"}
vcr_a1b3bcf19143
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-4, -10, 9, 21, -22, -9, -24, -25]}, "output": [-4, -14, -5, 16, -6, -15, -39, -64]}, {"input": {"nums": [-8, -4, -8, 8, -5, 15, -1, 14, 9]}, "output": [-8, -12, -20, -12, -17, -2, -3, 11, 20]}, {"input": {"nums": [-7, 5, 7, -7, -12, -12, 13]}, "output": [-7, -2, 5, -2, -14, -26, -13]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "afefe2767bcbc121"}
vcr_24a354ea7f02
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [14, 0, -19, -14, 11, -3, 19, -14, -12], "k": 8}, "output": [0, -19, -14, 11, -3, 19, -14, -12, 14]}, {"input": {"nums": [-10, -10, 9, -9, 2, -6], "k": 9}, "output": [-9, 2, -6, -10, -10, 9]}, {"input": {"nums": [3, 5, -4, 8, -6, 0, 6], "k": 11}, "output": [8, -6, 0, 6, 3, 5, -4]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "7915ce2ee475a1d0"}
vcr_a3990cdcf3b3
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 4, 7, 2, 4, 7]}, "output": [1568, 1960, 1120, 3920, 1960, 1120]}, {"input": {"nums": [4, 3, 5, 4, 1]}, "output": [60, 80, 48, 60, 240]}, {"input": {"nums": [4, 2, 5]}, "output": [10, 20, 8]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "4a89c6717df37600"}
vcr_0c03f91d97ea
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [3, 54, 79, 77, -23, -14, -17, 15, 46, -29], "target": 23}, "output": [4, 8]}, {"input": {"nums": [46, 45, 33, 1, 0], "target": 45}, "output": [1, 4]}, {"input": {"nums": [47, -2, -30, 38, 14, -4, -5], "target": 52}, "output": [3, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "027de166ff9eaae6"}
vcr_a630916328ff
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, 10, 19, 23, 26, 27, 28, 29, 31, 37, 44, 45, 47], "target": 9}, "output": 1}, {"input": {"nums": [1, 12, 23, 26, 27, 38], "target": 20}, "output": 2}, {"input": {"nums": [1, 5, 12, 19, 22, 24, 25, 27, 28, 29, 32, 36], "target": 29}, "output": 9}]
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": "e8d73febb194cf00"}
vcr_0db473734796
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, 4, 2, 3, 4, 4, 2, 1, 0]}, "output": false}, {"input": {"nums": [3, 1, 2, 3, 1]}, "output": true}, {"input": {"nums": [2, 2, 3, 0, 3, 0, 3, 3, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "1aa545c5e0604840"}
vcr_c9469ce79440
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": [-28, -27, -2, 2, 10, 11, 18, 22, 23], "target": 20}, "output": [2, 7]}, {"input": {"nums": [-16, -4, 1, 2, 9, 19, 20, 22], "target": 41}, "output": [5, 7]}, {"input": {"nums": [-19, -18, 7, 8, 10, 34, 37], "target": -8}, "output": [1, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "98638483db5ab3f7"}
vcr_c4c0ec172fcb
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": [9, 3, 4, 5, 11, 20, 5, 23, 19, 6]}, "output": 63}, {"input": {"nums": [14, 4, 3, 18, 1, 20, 13, 16, 11, 16]}, "output": 84}, {"input": {"nums": [19, 14]}, "output": 19}]
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": "248fa82b277b5ee1"}
vcr_53d1b58c5873
hard
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "aacaacacbaadc"}, "output": false}, {"input": {"s": "aa"}, "output": true}, {"input": {"s": "abcbccacb"}, "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": "8b4628d29e3ddaef"}
vcr_7ffac1d17e56
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, 1, 3, 2, 4, 1, 0, 4, 0]}, "output": true}, {"input": {"nums": [2, 2, 0, 0, 0]}, "output": false}, {"input": {"nums": [2, 1, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "3bfa47d9edd6f8c5"}
vcr_92900e366701
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": "acb cbe eceda eadb"}, "output": "eadb eceda cbe acb"}, {"input": {"s": "baa abba"}, "output": "abba baa"}, {"input": {"s": "bba aaba aba b"}, "output": "b aba aaba bba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "459822bbeca30962"}
vcr_d28cbf99900c
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": ["cccc", "ac", "cbc", "cc"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aba", "aab", "aaa"]}, "output": [1, 2]}, {"input": {"strs": ["baa", "aaa", "bab", "abb", "abb", "bab", "bba"]}, "output": [1, 1, 5]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "00b128a43fb02280"}
vcr_aad7b6172580
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-14, -9, 0, 6, 24, 25, 35, 42], "target": 41}, "output": [3, 6]}, {"input": {"nums": [-19, -12, -10, -9, -3, 6, 8, 20, 21, 22, 35], "target": -6}, "output": [1, 5]}, {"input": {"nums": [-15, -9, -4, -1, 0, 2, 3, 9, 30, 37], "target": 15}, "output": [0, 8]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "e12c6d0f0af1f8f2"}
vcr_dfc503d7fe96
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": 3287}, "output": 8}, {"input": {"n": 5586}, "output": 7}, {"input": {"n": 5075}, "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": "459c164ee0c15724"}
vcr_7591cb9d9e82
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-7, 9, 5, -11, 17, 4, 9, 3], "k": 2}, "output": [9, 3, -7, 9, 5, -11, 17, 4]}, {"input": {"nums": [3, 4, -10, -6, -6, 8, 5, 0, -3], "k": 7}, "output": [-10, -6, -6, 8, 5, 0, -3, 3, 4]}, {"input": {"nums": [1, 5, 9, -2], "k": 7}, "output": [5, 9, -2, 1]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "f9d4ab0cc0183356"}
vcr_43a611578eed
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": ["ccb", "cb", "cabb", "aa", "bba"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "abb", "aaa", "aaa", "aab", "baa", "bbb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["abb", "bab", "aaa", "abb", "bab", "bab"]}, "output": [1, 5]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "2a065a8311bef2b7"}
vcr_303f3d2a4e56
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["ba", "baba", "ab", "acc", "cb", "cabc"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "baa", "bbb", "abb", "bab"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aaa", "baa", "baa"]}, "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": "5c2a67a89a2e3c20"}
vcr_c72cb072120c
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 2, 0, 1, 4, 4, 4]}, "output": [0, 0, 256, 0, 0, 0, 0]}, {"input": {"nums": [2, 3, 1, 1, 1, 4, 5]}, "output": [60, 40, 120, 120, 120, 30, 24]}, {"input": {"nums": [5, 1, 3, 4, 4]}, "output": [48, 240, 80, 60, 60]}]
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": "95112c1db279356e"}
vcr_cb856effc302
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": [-20, -14, -3, -1, 38], "target": -34}, "output": [0, 1]}, {"input": {"nums": [-18, -16, -15, 33, 36], "target": -33}, "output": [0, 2]}, {"input": {"nums": [-20, -17, -16, -13, -10, -1, 9, 15, 21, 22, 26], "target": 6}, "output": [0, 10]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "32fad9d16e00066f"}
vcr_e42f68a1e7cf
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-3, 7, -12, -4, 20, 22, 16, 12, -13, 8, 15, -18, -11, 18]}, "output": 80}, {"input": {"nums": [-10, -8, 1, 14, 11, 11, -10, 7, -11, 4]}, "output": 37}, {"input": {"nums": [8, 5, -1, 13, 1, -4, 7, -2, 8, 8]}, "output": 43}]
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": "a0add0d64e0fdc88"}
vcr_fb6a22a64132
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -21, -16, 5, 22, 23, 39], "target": -50}, "output": [0, 1]}, {"input": {"nums": [-7, -4, -2, 5, 13, 14, 16, 23, 27, 28, 34], "target": 21}, "output": [0, 9]}, {"input": {"nums": [-18, 1, 12, 16, 22, 24, 28, 30, 31, 32, 35], "target": 4}, "output": [0, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "e2ca73f7cba93d3d"}
vcr_b3854f88b4be
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": [64, 47, 30, 10, -9, 7, -22, 25, 54, -3, -20, -27], "target": 22}, "output": [7, 9]}, {"input": {"nums": [-9, 1, 31, 2], "target": 32}, "output": [1, 2]}, {"input": {"nums": [6, -18, 33, 30, -19, 20], "target": -12}, "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": "09c06cb14e7b9586"}
vcr_98489b4d8f0c
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": "ababbbabbccb"}, "output": 8}, {"input": {"s": "bcabbacbac"}, "output": 9}, {"input": {"s": "bccbc"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "301615700792463e"}
vcr_905cd0955ad9
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-16, -14, 10, -1, 19, 21, -14, 13, -18, -6, -11]}, "output": 49}, {"input": {"nums": [11, 7, -6, -5]}, "output": 18}, {"input": {"nums": [9, 11, 9, 8]}, "output": 37}]
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": "9fffdf0ef91547d7"}
vcr_66f73233e3f0
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, 77, 8, 8, 37, 37, 32]}, "output": 77}, {"input": {"nums": [23, 14, 23, 27, 44, 15, 27, 15, 14]}, "output": 44}, {"input": {"nums": [15, 13, 58, 15, 20, 16, 7, 5, 20, 13, 5, 7, 16]}, "output": 58}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "0a0ff4c19b4a6a70"}
vcr_f3f4f06ece68
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": [153, 18, 30, 139, 54]}, "output": false}, {"input": {"nums": [19, 17, 20, 19, 3, 20]}, "output": true}, {"input": {"nums": [14, 20, 4, 7]}, "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": "63da217d4577130e"}
vcr_a63dbbf852cb
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": [27, 4, 27, 5, 4, 30, 0, 26, 16]}, "output": 110}, {"input": {"nums": [10, 4, 4, 6, 17, 8, 2, 3, 17, 17]}, "output": 51}, {"input": {"nums": [16, 19, 11]}, "output": 27}]
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": "1cb8db3d2af53466"}
vcr_3a3755a32d10
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 3, 7, 0, 6, 4, 1]}, "output": [0, 0, 0, 1008, 0, 0, 0]}, {"input": {"nums": [2, 5, 4, 5, 3, 4]}, "output": [1200, 480, 600, 480, 800, 600]}, {"input": {"nums": [1, 2, 4, 4, 4]}, "output": [128, 64, 32, 32, 32]}]
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": "6e4b42d0e795ccdb"}
vcr_e22218524ab7
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [2, 3, 2, -20, -16, -16, 10, 7, 0, -20, 7], "k": 22}, "output": [2, 3, 2, -20, -16, -16, 10, 7, 0, -20, 7]}, {"input": {"nums": [-4, -3, -7, -9, 9, 1, -6, 5, 10, -1], "k": 9}, "output": [-3, -7, -9, 9, 1, -6, 5, 10, -1, -4]}, {"input": {"nums": [-10, -2, -6, -8, -9, 9, 3, 10], "k": 11}, "output": [9...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "15c78783242be9f1"}
vcr_172ee0503092
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, 10, 12, 14, 29, 36, 45, 46], "target": 47}, "output": 8}, {"input": {"nums": [0, 2, 16, 21, 23, 24, 25, 27, 33, 38], "target": 27}, "output": 7}, {"input": {"nums": [10, 21, 23, 24, 31, 33, 34], "target": 19}, "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": "fd2424bd9e776fa3"}
vcr_fa0ba38e64b2
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [9, -2, -12, 16, -13, -7, -5, -7, 3, -1], "k": 13}, "output": [-7, 3, -1, 9, -2, -12, 16, -13, -7, -5]}, {"input": {"nums": [-9, 4, -9, -10, 1, -2], "k": 15}, "output": [-10, 1, -2, -9, 4, -9]}, {"input": {"nums": [8, 7, 7, -7, -6, 0, -6, 7, 7], "k": 20}, "output": [7, 7, 8, 7, 7, -7, -6, 0, -6]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "a2fc21da3df0d118"}
vcr_ceb874bd9595
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, 0, 1, 0, 0]}, "output": true}, {"input": {"nums": [2, 1, 1, 2, 0, 1, 2]}, "output": true}, {"input": {"nums": [0, 3, 2, 2, 3, 2, 3]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "566bc3beb880ecea"}
vcr_3f01c28e950e
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -14, -4, 4, 7, 17, 19, 21, 39, 45], "target": -5}, "output": [0, 7]}, {"input": {"nums": [2, 5, 22, 35], "target": 57}, "output": [2, 3]}, {"input": {"nums": [-19, -13, -8, 0, 5, 8, 9, 14, 20, 27, 35], "target": 7}, "output": [1, 8]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "848853722c243b5a"}
vcr_90b8071c5e1b
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, -27, -6, -34, 46], "target": 13}, "output": [0, 1]}, {"input": {"nums": [17, -11, 7, 15, 10], "target": 32}, "output": [0, 3]}, {"input": {"nums": [29, 41, 47, -15, -11, -9, -16, 25, -26], "target": 70}, "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": "4407805883c28587"}
vcr_f6f522a0a21c
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": ["aacb", "cc", "cba", "bb", "acca"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "bbb", "bba", "aba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aaa", "bba", "bbb", "abb"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "74f8980f97cc6bea"}
vcr_e870f75b0f48
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 4, 3, 0, 1, 1, 4, 4, 0]}, "output": false}, {"input": {"nums": [2, 2, 0, 3, 3, 3, 1, 3]}, "output": true}, {"input": {"nums": [1, 3, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "062a64b3cd9b0818"}
vcr_84c2961bc64e
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, 8, 9, 11, 16, 23, 30, 31, 33, 37, 41], "target": 41}, "output": 11}, {"input": {"nums": [3, 7, 10, 15, 16, 18, 23, 26, 29, 30, 35, 36], "target": 5}, "output": 1}, {"input": {"nums": [5, 8, 15, 22, 30], "target": 23}, "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": "56d1fc2614c83fae"}
vcr_a456b319e599
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": [-18, 71, -25, -17, 62, -36, -34, 34], "target": 0}, "output": [6, 7]}, {"input": {"nums": [6, -7, 48, -20, -28, -2], "target": 54}, "output": [0, 2]}, {"input": {"nums": [24, -15, -16, 12, 47, 5], "target": 9}, "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": "da6764dbd659cf0d"}
vcr_769e1fc5a714
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": ["cabb", "aa", "bc", "ccac", "aaba", "aa", "cb", "bcab"]}, "output": [1, 1, 2, 2, 2]}, {"input": {"strs": ["bba", "bab", "abb", "bab", "abb", "baa", "bbb"]}, "output": [1, 1, 5]}, {"input": {"strs": ["abb", "aba", "aaa", "bab"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "6181e82d1270533f"}
vcr_11e684fe5a7d
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [35, 18, 5, 18, 5, 76, 35, 22, 22]}, "output": 76}, {"input": {"nums": [13, 44, 18, 18, 13, 12, 12]}, "output": 44}, {"input": {"nums": [13, 13, 14, 25, 17, 14, 45, 16, 17, 16, 25]}, "output": 45}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "278173dc4b8c7ec3"}
vcr_22c3322bf900
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": [0, 20, -12, 13, 13, -6, 5, 4, -11, -9, -14, 4, -18]}, "output": 37}, {"input": {"nums": [-6, 9, 4]}, "output": 13}, {"input": {"nums": [4, 12, 5, 14, 2, 7, 3, 14]}, "output": 61}]
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": "9bd4194519e75744"}
vcr_cead6d62b0d9
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": [-12, 27, 9, 61, 50, -28, 4, -3, 55, -38, 78, 69, 68, 79], "target": 52}, "output": [7, 8]}, {"input": {"nums": [40, -30, 29, 6], "target": 10}, "output": [0, 1]}, {"input": {"nums": [18, 16, 33, 5], "target": 49}, "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": "a97e8e7cb9c40ae4"}
vcr_13f95fa5b21d
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 2, 5, 4, 2, 2, 3, 5]}, "output": [2400, 3600, 1440, 1800, 3600, 3600, 2400, 1440]}, {"input": {"nums": [4, 5, 5, 5, 3]}, "output": [375, 300, 300, 300, 500]}, {"input": {"nums": [5, 2, 4, 1, 2]}, "output": [16, 40, 20, 80, 40]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "0b847e641a5c649c"}
vcr_9401198f2e56
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["cbbc", "ccac", "acbc", "ccaa", "bc", "ba", "bbb", "ab"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "aba", "aab"]}, "output": [1, 2]}, {"input": {"strs": ["abb", "bba", "bba", "baa"]}, "output": [1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "3637415119e31977"}
vcr_b4e09e9c022d
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": [7, 68, 38, 7, 5, 27, 5, 38, 27]}, "output": 68}, {"input": {"nums": [3, 3, 9, 27, 4, 27, 2, 4, 2, 41, 9]}, "output": 41}, {"input": {"nums": [15, 13, 26, 54, 13, 15, 26]}, "output": 54}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "052415ac15d17c18"}
vcr_5feb83f8f0ad
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": [12, 29, 34, 37, 42, 45], "target": 20}, "output": 1}, {"input": {"nums": [0, 3, 18, 24, 34, 38, 39], "target": 5}, "output": 2}, {"input": {"nums": [1, 2, 18, 26], "target": 5}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "59f8dba814db932a"}
vcr_0eebbbf5932a
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 6, 17, 26, 27, 28, 32, 33, 35, 38, 44], "target": 8}, "output": 2}, {"input": {"nums": [1, 7, 18, 30, 38], "target": 39}, "output": 5}, {"input": {"nums": [6, 10, 13, 16, 20, 35], "target": 16}, "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": "6ca74d49b038ea9d"}
vcr_b8deec2b8b51
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": [9, 48, 29, 21]}, "output": 39}, {"input": {"prices": [20, 36, 11, 17, 2, 24]}, "output": 22}, {"input": {"prices": [35, 35, 13, 13, 20, 16, 37]}, "output": 24}]
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": "29b036a6adddfe4e"}
vcr_44f8d2353fb1
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, 1, 4, 2, 0]}, "output": true}, {"input": {"nums": [1, 1, 2]}, "output": true}, {"input": {"nums": [1, 1, 1, 0, 0, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "32f60ab63692eefe"}
vcr_66a0f2b80acc
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, -14, -9, 15, 6, -16, -4, 6, -16, -10, -18, 10], "k": 14}, "output": [-18, 10, -20, -14, -9, 15, 6, -16, -4, 6, -16, -10]}, {"input": {"nums": [-7, -4, -2, 10, 4, 1, 4, 1, 2], "k": 9}, "output": [-7, -4, -2, 10, 4, 1, 4, 1, 2]}, {"input": {"nums": [6, 6, -4], "k": 17}, "output": [6, -4, 6]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "97ef38384143f89f"}
vcr_90d66d73f7c8
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, 0, 4, 4, 0, 1, 0]}, "output": true}, {"input": {"nums": [3, 1, 3, 3, 1]}, "output": true}, {"input": {"nums": [3, 1, 2, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "37f4f3c7ce93127a"}
vcr_e2d96a547268
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 5, 6, 7, 9, 11, 12, 14, 14]}, "output": [0, 2, 5, 6, 7, 9, 11, 12, 14]}, {"input": {"nums": [1, 1, 1, 2, 4, 6, 8, 9, 10, 11, 12, 12]}, "output": [1, 2, 4, 6, 8, 9, 10, 11, 12]}, {"input": {"nums": [2, 3, 4, 4, 5, 5, 5, 7, 8, 9, 9]}, "output": [2, 3, 4, 5, 7, 8, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "31b9b839d30c5ee6"}
vcr_6f97aa90372a
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": "dcacddbbddcacd"}, "output": true}, {"input": {"s": "abb"}, "output": false}, {"input": {"s": "accacccab"}, "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": "19caf66f48a787cb"}
vcr_b78f9a723911
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": ["abb", "aabb", "ca", "bbbb", "ab"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "bbb", "aaa"]}, "output": [1, 1, 1]}, {"input": {"strs": ["aaa", "aab", "baa", "bba", "abb", "bbb"]}, "output": [1, 1, 2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "68c774e3c20c704b"}
vcr_a27a535a4502
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [13, 9, 34, 18, 30, 30, 77, 11, 2, 11, 13, 2, 18, 34, 9]}, "output": 77}, {"input": {"nums": [5, 9, 9, 5, 54]}, "output": 54}, {"input": {"nums": [1, 26, 40, 26, 1]}, "output": 40}]
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": "294c7a1765e7d6e6"}
vcr_af4f5af92a5f
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": [-10, 0, 4, 7, 18, -4, -17, -5, 4]}, "output": 29}, {"input": {"nums": [-7, 6, 13, 0, -12, 0, 13, -8, 11, -1, 10]}, "output": 32}, {"input": {"nums": [8, -10, 1, -4, 5, -10, -8, 13, -6, 10]}, "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": "8dad0c816cf07dac"}