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_85af7ad4472c
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [6, 6, 7, 4, 4]}, "output": [672, 672, 576, 1008, 1008]}, {"input": {"nums": [1, 1, 4, 2, 2, 1]}, "output": [16, 16, 4, 8, 8, 16]}, {"input": {"nums": [4, 5, 4, 2, 4]}, "output": [160, 128, 160, 320, 160]}]
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": "f8b05979442e5130"}
vcr_f095e5da4e65
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": "dbacdcbcdcabd"}, "output": true}, {"input": {"s": "cccabcbbac"}, "output": false}, {"input": {"s": "abb"}, "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": "f98b8fde2607b075"}
vcr_a008c0ec913e
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 63, "b": 154}, "output": 7}, {"input": {"a": 81, "b": 155}, "output": 1}, {"input": {"a": 117, "b": 160}, "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": "497f1a7a4433de35"}
vcr_b15e43b2b8f6
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [8, 8, 59, 35, 17, 38, 38, 35, 17]}, "output": 59}, {"input": {"nums": [2, 17, 2, 1, 1, 55, 17]}, "output": 55}, {"input": {"nums": [5, 9, 1, 44, 5, 21, 29, 19, 19, 29, 9, 1, 21]}, "output": 44}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "c1ef2dba2e5a7dd1"}
vcr_c875fd5e75ce
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-11, -11, -20, -3, 12, -6, 3, -7, 16, 20, 1]}, "output": [-11, -22, -42, -45, -33, -39, -36, -43, -27, -7, -6]}, {"input": {"nums": [10, 8, 13, 12, 4]}, "output": [10, 18, 31, 43, 47]}, {"input": {"nums": [3, -2, -3, 11, 18, 3, 3, 9]}, "output": [3, 1, -2, 9, 27, 30, 33, 42]}]
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": "f9fd35d280a29142"}
vcr_90f21b561fa0
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": [19, -5, 4, 22, -8]}, "output": 40}, {"input": {"nums": [1, -12, -12, 11, -1, -8, 10, -4, -11, 10, 10]}, "output": 20}, {"input": {"nums": [8, 2, 2, 2, -6, -8, -9, 3, 14, 11]}, "output": 28}]
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": "d2a6f3ed9f5621a0"}
vcr_94ad4e2a2319
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": [49, -24, 66, -20, 26, 67, -39, -7], "target": -59}, "output": [3, 6]}, {"input": {"nums": [42, -14, -19, 23], "target": 23}, "output": [0, 2]}, {"input": {"nums": [23, 36, 22, -24, 7, 2, 37, 38], "target": 60}, "output": [0, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "07eb27538e270e21"}
vcr_6ca620c3415d
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-13, 13, 14, -13, 20]}, "output": 34}, {"input": {"nums": [-1, 7, 11, 4, -3, -7]}, "output": 22}, {"input": {"nums": [4, 9, 4, -11, 3, -9, -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": "aa8ed72d35e4aca9"}
vcr_afae27eb9c48
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-25, -16, -5, 35, 32, -4, 15, 28, -13, -3, -10, 27]}, "output": [-25, -41, -46, -11, 21, 17, 32, 60, 47, 44, 34, 61]}, {"input": {"nums": [1, 8, -4, 0, 16, 14, -10, -9, 6]}, "output": [1, 9, 5, 5, 21, 35, 25, 16, 22]}, {"input": {"nums": [-5, 10, -4, 4, 11, 5, 8, 8]}, "output": [-5, 5, 1, 5, 16, 21...
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "e4f34842e825a44a"}
vcr_74458be3cca4
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 4, 0, 3, 2, 1, 0, 3, 0]}, "output": false}, {"input": {"nums": [3, 0, 2]}, "output": true}, {"input": {"nums": [0, 2, 1, 3, 0, 0, 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": "f24270040e82dae6"}
vcr_48f8733a077d
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": [75, -24, 26, 14], "target": 101}, "output": [0, 2]}, {"input": {"nums": [17, 9, 48, 29, 13, 22, -28, 8, 2], "target": -15}, "output": [4, 6]}, {"input": {"nums": [37, 18, 33, 31, -30, 26, 25, 27, -22, -12], "target": 58}, "output": [2, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "391bbbaba834b707"}
vcr_ab3821534619
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": [-13, -8, -4, 13, 18, -2, -11, 12, -11, 9], "k": 1}, "output": [9, -13, -8, -4, 13, 18, -2, -11, 12, -11]}, {"input": {"nums": [6, 3, -2, 10, 10, 1, 10, -2, -6], "k": 12}, "output": [10, -2, -6, 6, 3, -2, 10, 10, 1]}, {"input": {"nums": [-3, -6, -8, 4, 3, 6, -5, 3], "k": 8}, "output": [-3, -6, -8, 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": "785459e4aa382d4b"}
vcr_b4220bea83e5
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [15, 3, -4, -6, -13, 0, 5, -11, 6], "k": 1}, "output": [6, 15, 3, -4, -6, -13, 0, 5, -11]}, {"input": {"nums": [-2, 9, 1, -2], "k": 0}, "output": [-2, 9, 1, -2]}, {"input": {"nums": [-6, -8, 3, 3, -8, -4, -4, 10, -7, 1], "k": 19}, "output": [-8, 3, 3, -8, -4, -4, 10, -7, 1, -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": "5fbb7d4546e9cd94"}
vcr_bb0ce5821680
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": [21, 23, 32, 40, 42, 45, 47], "target": 7}, "output": 0}, {"input": {"nums": [9, 19, 24, 32], "target": 29}, "output": 3}, {"input": {"nums": [9, 20, 26, 29, 31, 34, 39], "target": 9}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "833f6dd4efab4877"}
vcr_7f5d414ae5b9
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": [5, 6, 6, 8, 8, 8, 14, 14]}, "output": [5, 6, 8, 14]}, {"input": {"nums": [2, 2, 5, 9, 10, 12, 12, 12, 12]}, "output": [2, 5, 9, 10, 12]}, {"input": {"nums": [0, 2, 2, 5, 5, 6, 7, 7]}, "output": [0, 2, 5, 6, 7]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "69312a3495f33b68"}
vcr_5d394ce0148b
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": 26383}, "output": 9}, {"input": {"n": 1161}, "output": 4}, {"input": {"n": 8410}, "output": 6}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "926bad82e2db7ff6"}
vcr_aa4adbb7f854
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": "caabbcccb"}, "output": 5}, {"input": {"s": "bcaaabcb"}, "output": 6}, {"input": {"s": "caa"}, "output": 2}]
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": "f510c843ae377cd4"}
vcr_533287e57292
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": 52299}, "output": 8}, {"input": {"n": 7033}, "output": 9}, {"input": {"n": 4377}, "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": "81b55e7b268fde62"}
vcr_96ac585a76af
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": "cae db aec ba cecd cac eaadde"}, "output": "eaadde cac cecd ba aec db cae"}, {"input": {"s": "abb aa ba abb"}, "output": "abb ba aa abb"}, {"input": {"s": "a aa bbaa a ba"}, "output": "ba a bbaa aa a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "c5b20965386c28b7"}
vcr_801733f0d625
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": [-6, 34, -32, -17, -10, 59, -31, 51, -40, 18, -15, 30, 21], "target": -14}, "output": [2, 9]}, {"input": {"nums": [-3, 11, -26, 25, 42], "target": 16}, "output": [2, 4]}, {"input": {"nums": [-22, 41, 24, -23, -19, 16, 39, -18, -2, 33, 26], "target": 17}, "output": [0, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "44d164dea38ec6a8"}
vcr_9c6c74ab0806
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [14, 6, 20]}, "output": 34}, {"input": {"nums": [4, 13, 17, 7, 14, 18, 9, 14]}, "output": 53}, {"input": {"nums": [12, 1, 1]}, "output": 13}]
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": "8b547db718c2ada2"}
vcr_abcd4bce63d3
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, -20, -18, -16, -3, 29, 33], "target": -50}, "output": [0, 1]}, {"input": {"nums": [-18, -14, -13, 20, 26, 28, 29], "target": 2}, "output": [0, 3]}, {"input": {"nums": [-15, -11, -5, 9, 11, 14, 15, 19, 24, 32], "target": -16}, "output": [1, 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": "f55fd9e506bd62c9"}
vcr_52aa826bb980
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": [38, 47, 37, 22, 29, 35, 42, 9, 8, 5]}, "output": 20}, {"input": {"prices": [33, 37, 20, 11, 8, 39, 11, 15, 5]}, "output": 31}, {"input": {"prices": [26, 21, 11, 25, 35, 15, 26]}, "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": "eb1763e519419e08"}
vcr_039ede9e9ce4
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": [-16, 40, -28, -32, -33, 21, -8, -27, 68, 32], "target": -59}, "output": [3, 7]}, {"input": {"nums": [-3, -20, 28, 9, 8, 3, 47, -2, 39, 6], "target": -17}, "output": [1, 5]}, {"input": {"nums": [23, 1, -9, -15, 21, 24, 27], "target": 24}, "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": "c86aa5e17ca88388"}
vcr_b817c8c32b94
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": [-4, 0, -18, -14, -9, -5, 2, 12, -12, -15, 5, 14, 5]}, "output": 24}, {"input": {"nums": [14, 2, -6, 3, -12]}, "output": 16}, {"input": {"nums": [-11, 3, -3, 12, -8]}, "output": 12}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "c6aa223d1b92dbd8"}
vcr_c787a1baccad
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, 3, 3, 5, 6, 6, 6, 7, 11, 11, 14, 14]}, "output": [0, 3, 5, 6, 7, 11, 14]}, {"input": {"nums": [3, 9, 11, 12]}, "output": [3, 9, 11, 12]}, {"input": {"nums": [4, 7, 7, 7, 8, 8, 8, 10, 10, 11, 11]}, "output": [4, 7, 8, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "473bb0c27c644155"}
vcr_b8ab5a547fe3
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": "ccccbcadacbcccc"}, "output": true}, {"input": {"s": "aababcb"}, "output": false}, {"input": {"s": "aaaac"}, "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": "17b9da3709614c9c"}
vcr_dd4fa23a57ee
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [20, -8, -8, 4, -13, 2, 3, -10, 11, -6, 15, 14, 19, -7]}, "output": 53}, {"input": {"nums": [2, 0, -1, 2, -4]}, "output": 3}, {"input": {"nums": [2, 8, -8, -6, 12, 3, -5]}, "output": 15}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "2ecd94d3db8e10f7"}
vcr_1577e0c0c6ba
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": [11, 22, 34, 48, 20, 56, 59, 23, 35, 29, 44, 38, 1, 48]}, "output": 48}, {"input": {"prices": [24, 35, 20]}, "output": 11}, {"input": {"prices": [22, 22, 40, 5, 11, 39, 6, 25, 40, 32]}, "output": 35}]
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": "b5060cb8677a3f04"}
vcr_c2db5f288b70
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [13, 22, 29, 42, 43, 45, 48], "target": 31}, "output": 3}, {"input": {"nums": [1, 6, 8, 9, 11, 14, 16, 29, 30, 34, 37], "target": 31}, "output": 9}, {"input": {"nums": [2, 3, 4, 7, 11, 13, 16, 17, 20, 21, 30, 36], "target": 11}, "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": "2c0de160d4250e58"}
vcr_9bdc57f7ee50
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": "aaabbcbbaa"}, "output": 5}, {"input": {"s": "babcbcbca"}, "output": 9}, {"input": {"s": "bab"}, "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": "9f8e06955ae5079e"}
vcr_4fb511acb7ae
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [49, 7, 49, 3, 6, 16, 45, 15, 0, 17, 31]}, "output": true}, {"input": {"nums": [18, 15, 1, 17]}, "output": false}, {"input": {"nums": [7, 24, 9, 0, 8, 3, 12, 25, 7, 23, 4]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "fd07ac47a59a09a4"}
vcr_f9d9e3c1102f
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [6, 7, 27, 9, 1]}, "output": 34}, {"input": {"nums": [3, 12, 18]}, "output": 21}, {"input": {"nums": [0, 13, 11, 16]}, "output": 29}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "340cd04c5b97f1b3"}
vcr_6d8e8f3057d6
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, 1, 7, 9, 22, 0, 20, 17, 16, 25, 20, 10]}, "output": 112}, {"input": {"nums": [4, 19, 5]}, "output": 19}, {"input": {"nums": [14, 0, 4, 4, 8, 9, 1, 15]}, "output": 42}]
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": "60729cd6d3dfd5dc"}
vcr_1b418994f739
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": "ccceea db aeba dcdaab cd bae eaa"}, "output": "eaa bae cd dcdaab aeba db ccceea"}, {"input": {"s": "aab aaa b"}, "output": "b aaa aab"}, {"input": {"s": "aaa b baa"}, "output": "baa b aaa"}]
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": "d399224575fc3c7f"}
vcr_894203332c7f
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": [56, 39, 31, 11, 7, 16, 38, 9, 10, 34]}, "output": 31}, {"input": {"prices": [25, 16, 1, 26, 8, 19]}, "output": 25}, {"input": {"prices": [8, 13, 5, 26, 40, 19, 7, 21, 1, 21, 22, 3]}, "output": 35}]
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": "25b41f3327fa4837"}
vcr_1b791a5ae6da
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [12, 7, 0, -14, -19, -18, -18, 14, 2, 2, -2], "k": 17}, "output": [-18, -18, 14, 2, 2, -2, 12, 7, 0, -14, -19]}, {"input": {"nums": [-3, -5, 8, -4, -5, -6, 1, 9, 3, -10], "k": 13}, "output": [9, 3, -10, -3, -5, 8, -4, -5, -6, 1]}, {"input": {"nums": [-3, 5, -9, 3, 6, -4, -8], "k": 0}, "output": [-3,...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "0d3406840be40529"}
vcr_4a9733331c5e
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-12, -5, -14, 14, 13, -8, -17, -3], "k": 8}, "output": [-12, -5, -14, 14, 13, -8, -17, -3]}, {"input": {"nums": [8, -4, 1, 0, -7, -4, -5], "k": 13}, "output": [-4, 1, 0, -7, -4, -5, 8]}, {"input": {"nums": [0, 0, 5, 9, 8, 5, 4, 10, -7], "k": 17}, "output": [0, 5, 9, 8, 5, 4, 10, -7, 0]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "ad9006e5047c3510"}
vcr_7e99c3a647b3
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [18, -11, 1, 17, 12, 32, 22, -15, -10, -5, 17, 24, 19]}, "output": [18, 7, 8, 25, 37, 69, 91, 76, 66, 61, 78, 102, 121]}, {"input": {"nums": [-8, 18, -1, 7, 17, 13, 15, 6, 16]}, "output": [-8, 10, 9, 16, 33, 46, 61, 67, 83]}, {"input": {"nums": [6, 9]}, "output": [6, 15]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "f5aac2b20911a1fd"}
vcr_894efa45f140
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": "cbabababbccba"}, "output": 11}, {"input": {"s": "bbaaaacaa"}, "output": 4}, {"input": {"s": "bcabacaabcc"}, "output": 9}]
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": "93ab375d0501b7e0"}
vcr_1eb08a450b2a
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, 8, 7, -16], "k": 8}, "output": [-2, 8, 7, -16]}, {"input": {"nums": [-4, 5, 4, -7, -3, 2, 9, 3, 8], "k": 20}, "output": [3, 8, -4, 5, 4, -7, -3, 2, 9]}, {"input": {"nums": [4, -8, 1, -6, -2, 8, -2, 9, 9], "k": 12}, "output": [-2, 9, 9, 4, -8, 1, -6, -2, 8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "de80eccf81974a0e"}
vcr_461937e4538c
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [4, -14, -2, 1, -19, -1], "k": 4}, "output": [-2, 1, -19, -1, 4, -14]}, {"input": {"nums": [4, 5, 10, 1, -4, -5, 5, -5], "k": 11}, "output": [-5, 5, -5, 4, 5, 10, 1, -4]}, {"input": {"nums": [0, 0, 6, 1, 2, 4, 4], "k": 14}, "output": [0, 0, 6, 1, 2, 4, 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": "5c2e6b0d53d6d20a"}
vcr_b041148d38b2
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": [173, 0, 169, 124, 177, 125, 176, 154, 179, 115, 61, 157, 36, 197, 140]}, "output": false}, {"input": {"nums": [24, 13, 1, 21, 14, 7, 6, 9]}, "output": false}, {"input": {"nums": [18, 24, 16, 15, 8]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "2b046e8b750b53d7"}
vcr_46091afac7df
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": 17235}, "output": 7}, {"input": {"n": 8427}, "output": 7}, {"input": {"n": 7404}, "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": "6e6a9c5ac59cec45"}
vcr_28805a565a7f
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": ["acbc", "ccbc", "aacc", "abb", "cc", "bbbb"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "aaa", "bab", "bba", "aab"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aaa", "bab", "baa", "baa"]}, "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": "4b366f3308602df1"}
vcr_7d15c76a8eeb
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, 1, 3, 1, 0, 4, 0]}, "output": false}, {"input": {"nums": [3, 3, 2]}, "output": true}, {"input": {"nums": [0, 2, 1, 3, 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": "cebbf1061313c923"}
vcr_cf1bf3f333d6
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": [2, 35, -30, 34, 64, -19, 69, -11, -20, 46, 52, 40, 71, 70], "target": -9}, "output": [0, 7]}, {"input": {"nums": [-13, 36, 12, 39, -10], "target": -23}, "output": [0, 4]}, {"input": {"nums": [-3, 13, -30, 7, 32, 19, 36, -4, -2, 2, -7], "target": 26}, "output": [3, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "fa6c1965b368a181"}
vcr_bd222f61c293
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [12, 10, -15, 2, 16, -17, -17, 2, 17, -6, -13], "k": 6}, "output": [-17, -17, 2, 17, -6, -13, 12, 10, -15, 2, 16]}, {"input": {"nums": [3, 10, -3, -10, 10], "k": 2}, "output": [-10, 10, 3, 10, -3]}, {"input": {"nums": [2, 9, -6], "k": 9}, "output": [2, 9, -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": "042b8db5dd78f74e"}
vcr_e7c273fef28c
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": 16676}, "output": 4}, {"input": {"n": 15357}, "output": 12}, {"input": {"n": 2048}, "output": 1}]
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": "1f37aee821d22cc1"}
vcr_0ba72cff49e8
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": 51541}, "output": 8}, {"input": {"n": 9593}, "output": 8}, {"input": {"n": 11674}, "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": "d69cc17baa77f60f"}
vcr_cf3bd7c0cc7e
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": "bbbbacbabaabc"}, "output": 9}, {"input": {"s": "cbbbbcc"}, "output": 3}, {"input": {"s": "bbbaacabaabb"}, "output": 7}]
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": "ef2e375c04cc8c2d"}
vcr_5da817d669d8
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": [-9, 9, 19, 3, -9, 27, 17, 12, 34, 2, 6, -5, 33]}, "output": [-9, 0, 19, 22, 13, 40, 57, 69, 103, 105, 111, 106, 139]}, {"input": {"nums": [-11, 5, 0]}, "output": [-11, -6, -6]}, {"input": {"nums": [13, -9, 10, 11, -4, 15, -7, -6, 2, 1]}, "output": [13, 4, 14, 25, 21, 36, 29, 23, 25, 26]}]
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": "ff3f8dfb5081267a"}
vcr_49b737be292d
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, 34, 7, 73, 22, 31, -29, -15, 27, 6], "target": 80}, "output": [2, 3]}, {"input": {"nums": [28, 17, 36, -13, 21, 49, 29], "target": 66}, "output": [1, 5]}, {"input": {"nums": [39, 16, 28, 14, -16, -24, -2, 21], "target": 23}, "output": [0, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "201429e8301cdf93"}
vcr_b39f37583d66
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [31, 3, -9, 17, -23, 15, 9, 34, 25, 20, 20, 20, 20, 29]}, "output": [31, 34, 25, 42, 19, 34, 43, 77, 102, 122, 142, 162, 182, 211]}, {"input": {"nums": [-5, 18, -1, 2, -12, -10, -2, -11]}, "output": [-5, 13, 12, 14, 2, -8, -10, -21]}, {"input": {"nums": [-2, 6, -4, 14, 14]}, "output": [-2, 4, 0, 14,...
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": "5d13031e958c22c6"}
vcr_9628682c421e
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": [12, 3, 3, 55, 36, 12, 36]}, "output": 55}, {"input": {"nums": [21, 14, 2, 2, 21, 1, 1, 3, 17, 14, 43, 17, 3]}, "output": 43}, {"input": {"nums": [22, 12, 42, 12, 20, 22, 20]}, "output": 42}]
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": "411f55035589c257"}
vcr_6ad4da88c0a5
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": "a dbaa dcccd cabeb a beaca"}, "output": "beaca a cabeb dcccd dbaa a"}, {"input": {"s": "aaab bbba ba a"}, "output": "a ba bbba aaab"}, {"input": {"s": "b aaa"}, "output": "aaa b"}]
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": "8e5bf535d4495085"}
vcr_8591802b5d5e
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": "bdcbee ebae eccbce bceeac aeb eaac bbecec"}, "output": "bbecec eaac aeb bceeac eccbce ebae bdcbee"}, {"input": {"s": "aba aaa aa abbb b"}, "output": "b abbb aa aaa aba"}, {"input": {"s": "bbaa a aaaa"}, "output": "aaaa a bbaa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "19fb568597607c20"}
vcr_4005ff4fdff1
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, 5, 11, 12, 20, 28, 29, 34, 39, 41, 44, 49], "target": 23}, "output": 5}, {"input": {"nums": [14, 19, 39], "target": 7}, "output": 0}, {"input": {"nums": [0, 5, 14, 16, 18, 23], "target": 11}, "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": "f68c3513f9e916ac"}
vcr_38bcdb0e1621
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, -3, 0, 1, 5, 12, 18, 23, 28, 29, 32, 38], "target": 18}, "output": [0, 11]}, {"input": {"nums": [-10, 14, 17, 34], "target": 24}, "output": [0, 3]}, {"input": {"nums": [-18, -13, -2, 11, 27, 31, 35], "target": 17}, "output": [0, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "1b898d119875000c"}
vcr_f675f80dcede
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [2, 24, 21, 10, 2]}, "output": 34}, {"input": {"nums": [3, 10, 9, 8, 7, 16, 4]}, "output": 34}, {"input": {"nums": [11, 5, 6, 0, 16, 16, 10]}, "output": 43}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "7e4a7c66074bc8c5"}
vcr_4765c5127f7d
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, -8, 9, -5, 0, 6, -3, 1, -5, -11, -6, -10], "k": 1}, "output": [-10, 14, -8, 9, -5, 0, 6, -3, 1, -5, -11, -6]}, {"input": {"nums": [8, 10, -8, -2, -4, -3, -6, -10, 5, 7], "k": 2}, "output": [5, 7, 8, 10, -8, -2, -4, -3, -6, -10]}, {"input": {"nums": [-10, -1, -2, 10, -7, -7, 7], "k": 16}, "outpu...
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": "a0cec4800203b877"}
vcr_a10a26478dbf
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": [79, 3, 19, -31, 59, 40, 22, 4, -39, 50, -14, -21, 47], "target": 59}, "output": [2, 5]}, {"input": {"nums": [-14, -13, 9, 4, 12], "target": 21}, "output": [2, 4]}, {"input": {"nums": [13, 44, -18, -15, -24, 36, 3], "target": 47}, "output": [1, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "2a9f4496bbd4e767"}
vcr_dabbaf2147de
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": "adbbba"}, "output": false}, {"input": {"s": "bcbbabb"}, "output": false}, {"input": {"s": "bbc"}, "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": "8776901c9775d021"}
vcr_b3cd07819aca
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, -11, 13, 17, 1, 16, 6], "k": 19}, "output": [13, 17, 1, 16, 6, 10, -11]}, {"input": {"nums": [1, -1, 5, 0, -9, -2, 0, -6, 10], "k": 15}, "output": [0, -9, -2, 0, -6, 10, 1, -1, 5]}, {"input": {"nums": [-3, -7, 5, 9, 5, -5, -10, 2, 8, -5], "k": 5}, "output": [-5, -10, 2, 8, -5, -3, -7, 5, 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": "a4102932ad33a53d"}
vcr_4d466391cdbd
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": "dacebb bccd accbd b bb"}, "output": "bb b accbd bccd dacebb"}, {"input": {"s": "b abaa b"}, "output": "b abaa b"}, {"input": {"s": "b a"}, "output": "a b"}]
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": "9aaa787d4b671a74"}
vcr_991ef8b83427
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, 6, 13, 14, 20, 28, 35, 36, 37, 43, 47], "target": 29}, "output": 6}, {"input": {"nums": [5, 11, 12, 13, 14, 28, 33, 34, 35, 36, 37, 38], "target": 13}, "output": 3}, {"input": {"nums": [5, 9, 10, 12, 14, 16, 23, 32, 36, 39], "target": 17}, "output": 6}]
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": "0c124b5b6ef8aec2"}
vcr_478abeb923cb
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": ["acc", "abb", "cac", "cac", "bc", "bac"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["abb", "aaa", "bba"]}, "output": [1, 2]}, {"input": {"strs": ["bab", "aab", "bbb", "abb", "aaa", "bab"]}, "output": [1, 1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "6630aea0aa2bafc0"}
vcr_431f4334cdef
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, 3, 8, 13, 16, 23, 33, 34, 36, 37, 39, 41, 48], "target": 41}, "output": 11}, {"input": {"nums": [6, 23, 24, 26, 28, 29, 35, 39], "target": 37}, "output": 7}, {"input": {"nums": [2, 4, 6, 9, 11, 21, 22], "target": 25}, "output": 7}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "ec17e0f0a065beaf"}
vcr_db22678b7bdf
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, 2, 70, 7, 31, 12, 20, 20, 34, 12, 31, 7, 35, 18, 2, 18, 34]}, "output": 70}, {"input": {"nums": [20, 2, 28, 28, 29, 2, 29, 46, 20]}, "output": 46}, {"input": {"nums": [52, 5, 5, 25, 25]}, "output": 52}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "e16ad3223c914fd4"}
vcr_e83d5268614f
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, 2, 5, 8, 23, 26, 28, 29, 33, 38, 43, 44, 48], "target": 14}, "output": 4}, {"input": {"nums": [4, 7, 8, 12, 21, 29, 30, 31, 32, 33, 34, 39], "target": 18}, "output": 4}, {"input": {"nums": [19, 23, 24, 27], "target": 7}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "f9bd19bc9a51e0df"}
vcr_4b70d4d13580
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [43, 26, 22, 45, 20, 38, 15, 43]}, "output": true}, {"input": {"nums": [23, 18, 9, 2, 11, 2, 4]}, "output": true}, {"input": {"nums": [20, 21, 19]}, "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": "8ddbb756217507c7"}
vcr_17a94033f649
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, 8, 13, 30, 34, 37, 42], "target": 5}, "output": 1}, {"input": {"nums": [0, 10, 26, 32], "target": 10}, "output": 1}, {"input": {"nums": [12, 24, 27], "target": 28}, "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": "a5e635feb2d6c246"}
vcr_30f5d2343929
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-21, -16, 2, 4, 20, 21], "target": 5}, "output": [1, 5]}, {"input": {"nums": [-14, -10, -4, 6, 16, 22, 26, 33], "target": -8}, "output": [0, 3]}, {"input": {"nums": [-20, -12, 3, 7, 25, 29, 32], "target": 61}, "output": [5, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "89ef08aad744ae81"}
vcr_ddc9cb34e41e
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": [2, -2, 14, -13, 12, -25, 11, -13]}, "output": [2, 0, 14, 1, 13, -12, -1, -14]}, {"input": {"nums": [-5, 7, -7, -9, 12, 15, 17]}, "output": [-5, 2, -5, -14, -2, 13, 30]}, {"input": {"nums": [17, -3]}, "output": [17, 14]}]
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": "e3b41cc96900aa77"}
vcr_c5f0f0c42080
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [22, 22, 10, 48, 3, 17, 2, 21, 9, 40, 49, 48, 12]}, "output": true}, {"input": {"nums": [8, 6, 19, 12, 20, 14, 24, 25]}, "output": false}, {"input": {"nums": [7, 20, 15, 12, 11, 1, 18, 21, 0, 16, 21, 17]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "72ba989d8b9c6b69"}
vcr_2a4ca765d6bb
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, 1, -15, -14, -12, 17, -4], "k": 8}, "output": [-4, -20, 1, -15, -14, -12, 17]}, {"input": {"nums": [8, -2, 6, -4, 9, -5], "k": 15}, "output": [-4, 9, -5, 8, -2, 6]}, {"input": {"nums": [9, 9, 6], "k": 8}, "output": [9, 6, 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": "a45e3c20bfa62ba2"}
vcr_0962b8a44c54
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-21, -16, -9, 27, 13, 35, -14, 35, -13, -21, 35]}, "output": [-21, -37, -46, -19, -6, 29, 15, 50, 37, 16, 51]}, {"input": {"nums": [-4, 12, -8, 8, 3, 6, 7, 14, 10, 7]}, "output": [-4, 8, 0, 8, 11, 17, 24, 38, 48, 55]}, {"input": {"nums": [5, 9, 18, 12]}, "output": [5, 14, 32, 44]}]
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": "cb0657bbc1329e3e"}
vcr_e50e1a859957
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": "dde bdeae"}, "output": "bdeae dde"}, {"input": {"s": "bb b"}, "output": "b bb"}, {"input": {"s": "baaa bbbb"}, "output": "bbbb baaa"}]
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": "8b70707a5d75d596"}
vcr_c8b103ba0ae1
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, 17, 7, 9, 4, 30, 25, 4, 18]}, "output": 74}, {"input": {"nums": [9, 16]}, "output": 16}, {"input": {"nums": [14, 10, 3]}, "output": 17}]
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": "9a84385de50266f8"}
vcr_ec3a15f274f5
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", "ba", "cb", "ac", "bbbc", "cb", "abcb", "ccba", "acb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "bba", "bbb", "abb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "aaa", "bba", "bab", "bbb", "bba", "abb"]}, "output": [1, 1, 1, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "da09e13473061fe4"}
vcr_d04d877338e1
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": [53, 82, 94, 104, 134, 72, 49, 149, 66]}, "output": false}, {"input": {"nums": [20, 10, 9, 24]}, "output": false}, {"input": {"nums": [11, 3, 15, 8, 20]}, "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": "5cc31bf9a1fac596"}
vcr_ed3316f61073
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [7, 10, 21, 24, 39, 40, 42, 46], "target": 34}, "output": 4}, {"input": {"nums": [2, 6, 11, 13, 24, 26, 33], "target": 5}, "output": 1}, {"input": {"nums": [4, 7, 12, 16, 17, 25, 28], "target": 27}, "output": 6}]
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": "4c724fa098b2cb9e"}
vcr_34cc2c803b5c
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": "badbddbbddbdab"}, "output": true}, {"input": {"s": "bbcabcba"}, "output": false}, {"input": {"s": "acbaacabca"}, "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": "fd71f834ba9024db"}
vcr_57f1ebe78c3e
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-2, 3, -12, 3, -2, -9, -15, 12, -4, 8, -3, -16, 21, 6, 0]}, "output": 27}, {"input": {"nums": [11, -9, 3, -9]}, "output": 11}, {"input": {"nums": [-5, 1, -12]}, "output": 1}]
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": "c02ecbdb221aaa7a"}
vcr_5a3df0af1d7f
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": [30, 2, -13, -19, -9, -10, 1]}, "output": [30, 32, 19, 0, -9, -19, -18]}, {"input": {"nums": [-2, -11, -12, 11, 11, -5, 4, 5]}, "output": [-2, -13, -25, -14, -3, -8, -4, 1]}, {"input": {"nums": [-3, -11, 18, 15, 18, -5, 10, 7]}, "output": [-3, -14, 4, 19, 37, 32, 42, 49]}]
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": "db449511913624f6"}
vcr_839ad1c6c62c
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": 148, "b": 214}, "output": 2}, {"input": {"a": 64, "b": 140}, "output": 4}, {"input": {"a": 88, "b": 38}, "output": 2}]
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": "ab488d4294a03364"}
vcr_ca96b7e1d06f
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [2, 6, 15, 7, 3, 3, 21, 17, 29]}, "output": 70}, {"input": {"nums": [2, 6, 15, 17, 7, 6, 2]}, "output": 29}, {"input": {"nums": [11, 20, 7, 13, 13, 11]}, "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": "d411c9d689e86917"}
vcr_fef4b698246b
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": [-6, -13, -5, 20, -4]}, "output": 20}, {"input": {"nums": [11, 13, 14, -1, -6, -12, 14]}, "output": 38}, {"input": {"nums": [13, 3, 14, -2, -8, -3]}, "output": 30}]
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": "1239dd2f39347c85"}
vcr_c262bfb3718f
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [5, 23, 24, 30, 32, 35, 47], "target": 6}, "output": 1}, {"input": {"nums": [5, 13, 15, 19, 30, 32], "target": 39}, "output": 6}, {"input": {"nums": [4, 15, 24], "target": 32}, "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": "0a5334895fb28351"}
vcr_c5ddfa597a5f
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": [25, 26, 34, 38, -32, 35, 61, 74, -10], "target": 25}, "output": [5, 8]}, {"input": {"nums": [26, -28, 45, -15, -20, 2, 19], "target": 28}, "output": [0, 5]}, {"input": {"nums": [0, 49, 43, -17, -29, 26, 30, 31], "target": 26}, "output": [2, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "48068ae3cb6dc2f6"}
vcr_c1ce51346c7f
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", "ccb", "bba", "ca"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aaa", "aba", "bbb", "baa", "abb", "aba", "baa"]}, "output": [1, 1, 1, 4]}, {"input": {"strs": ["baa", "abb", "bba"]}, "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": "8b4d1736fe2a5165"}
vcr_000d26998740
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [7, 1, 1, 1, 2]}, "output": [2, 14, 14, 14, 7]}, {"input": {"nums": [5, 4, 3, 3, 5, 2]}, "output": [360, 450, 600, 600, 360, 900]}, {"input": {"nums": [1, 1, 1, 3, 4]}, "output": [12, 12, 12, 4, 3]}]
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": "5f35650a9dd1a6c1"}
vcr_f5b9bfa14d89
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [17, 20, 19, -20, 7, 12, -19, -10], "k": 21}, "output": [-20, 7, 12, -19, -10, 17, 20, 19]}, {"input": {"nums": [7, -10, 4, 0, 4, 8, 3, 3], "k": 4}, "output": [4, 8, 3, 3, 7, -10, 4, 0]}, {"input": {"nums": [7, -9, -6, -6, 2, -7], "k": 13}, "output": [-7, 7, -9, -6, -6, 2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "46f05bbc6d0cd0f5"}
vcr_b197e28b9ceb
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-12, 16, -13, -6, 11, 5, 16, 2, -7], "k": 20}, "output": [2, -7, -12, 16, -13, -6, 11, 5, 16]}, {"input": {"nums": [3, -3, -5, 5, 3, -10, 8, -9], "k": 10}, "output": [8, -9, 3, -3, -5, 5, 3, -10]}, {"input": {"nums": [-7, 6, -3, -9, 0, 3], "k": 15}, "output": [-9, 0, 3, -7, 6, -3]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "fa29bd873517cf5f"}
vcr_e6da28ae5640
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": 55422}, "output": 10}, {"input": {"n": 4687}, "output": 7}, {"input": {"n": 5644}, "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": "d6a16aa48b2a6002"}
vcr_78608113517f
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": [4, 7, 7, 9, 11, 11, 14, 15]}, "output": [4, 7, 9, 11, 14, 15]}, {"input": {"nums": [4, 5, 6, 7, 10, 10, 11, 12]}, "output": [4, 5, 6, 7, 10, 11, 12]}, {"input": {"nums": [0, 1, 4, 4, 7, 8, 9, 10, 12]}, "output": [0, 1, 4, 7, 8, 9, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "b147c4d43ff76084"}
vcr_14e00084c4c9
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": [-10, 71, -38, -13, 22, 68, -34, 62, -14], "target": 9}, "output": [3, 4]}, {"input": {"nums": [-9, -2, 28, 42], "target": 26}, "output": [1, 2]}, {"input": {"nums": [-18, 35, -6, 2, -1, -16, 13, -9, 22, 28, 41], "target": 41}, "output": [6, 9]}]
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": "df4a25c6ef71c633"}
vcr_7413aa819ef8
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": "caabbccbbaab"}, "output": 7}, {"input": {"s": "bccbacb"}, "output": 6}, {"input": {"s": "cbcac"}, "output": 5}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "59854ecf8185884e"}
vcr_58e1d7f77571
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, 3, 0, 2, 0]}, "output": true}, {"input": {"nums": [1, 0, 0, 3, 3, 2]}, "output": false}, {"input": {"nums": [3, 0, 1, 0, 3, 2, 2, 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": "c4a5f544132421bb"}
vcr_2872250d8600
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": ["bac", "bbbc", "aa", "bcc", "cac"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["baa", "aba", "aba", "aba", "baa", "aab", "bbb"]}, "output": [1, 6]}, {"input": {"strs": ["bab", "aaa", "abb", "aaa", "bbb"]}, "output": [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": "1f798466428a0e1b"}