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_57b4b99033f5
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": [29, 29, 15]}, "output": 44}, {"input": {"nums": [20, 12, 11]}, "output": 31}, {"input": {"nums": [3, 9, 4, 4, 10]}, "output": 19}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "22150f4995e1ebc3"}
vcr_3fbd7689ff5a
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, 9, 10, 14, 19, 22, 30, 32, 47, 48, 49], "target": 50}, "output": 11}, {"input": {"nums": [0, 1, 9, 10, 11, 24, 31, 34, 37, 38, 39], "target": 16}, "output": 5}, {"input": {"nums": [2, 4, 6, 11, 14, 16, 19, 20, 25, 27, 32, 35], "target": 23}, "output": 8}]
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": "6d71b4b85ed7b583"}
vcr_8af3c56eeca1
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["bba", "abc", "cab", "aac", "cb", "cc", "cb", "bacb"]}, "output": [1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["aab", "bbb", "aaa", "aaa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["baa", "aab", "bbb", "bba", "aba", "bba"]}, "output": [1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "06b783cc0519b36a"}
vcr_2a842f5de64c
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [2, 2, 3, 3, 3, 6, 8, 9, 11, 13, 14]}, "output": [2, 3, 6, 8, 9, 11, 13, 14]}, {"input": {"nums": [1, 5, 7, 8, 10, 11, 11, 12, 12, 12]}, "output": [1, 5, 7, 8, 10, 11, 12]}, {"input": {"nums": [1, 2, 2, 5, 7, 8, 8, 9]}, "output": [1, 2, 5, 7, 8, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "c5fdb024d2ccc2de"}
vcr_9ce8c77ffc78
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-24, -25, -6, 30, 34]}, "output": [-24, -49, -55, -25, 9]}, {"input": {"nums": [2, 6, -4, 0, 7, 4]}, "output": [2, 8, 4, 4, 11, 15]}, {"input": {"nums": [-6, 8, -3]}, "output": [-6, 2, -1]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "12702348a3b2107d"}
vcr_e65f16bfea7b
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": [19, 6, 18, 1, 25, 18, 16]}, "output": 78}, {"input": {"nums": [12, 15, 5, 4, 14]}, "output": 31}, {"input": {"nums": [10, 1, 0, 10, 11, 3, 12, 0, 5, 14]}, "output": 47}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "87415c465793d9c4"}
vcr_a38642c9c22d
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [23, 11, 44, 9, 6, 43]}, "output": 37}, {"input": {"prices": [13, 19, 8, 30, 22, 32]}, "output": 24}, {"input": {"prices": [26, 2, 3, 36, 2]}, "output": 34}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "4a7f2ef01e23ee89"}
vcr_4ffdbbcf014d
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": [17, -15, -12, 4, 11, -4, 8, -13, -8]}, "output": 19}, {"input": {"nums": [-3, -8, -10, 5, 3, 0, 10, 1, 1, 10]}, "output": 30}, {"input": {"nums": [2, -11, 0, 10, 2]}, "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": "d4c1e82f23fe68d9"}
vcr_21087eace515
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": ["cba", "aacb", "ccb", "aa", "acaa", "aaa", "bc", "bc"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "abb", "bba"]}, "output": [1, 2]}, {"input": {"strs": ["aab", "abb", "aab"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "fa3ba6fe55b55cbf"}
vcr_269c9404b81d
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": [-22, 25, 43, 10, 46, -27, -15], "target": 35}, "output": [1, 3]}, {"input": {"nums": [22, 3, 37, 0, -6, 42, -29, 39], "target": 45}, "output": [1, 5]}, {"input": {"nums": [-17, -22, -18, 48, 13, 46, 31], "target": 94}, "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": "69b85ef3f82a830e"}
vcr_04f9f851b94b
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [21, 0, 2, 25, 22, 23, 21, 13]}, "output": 82}, {"input": {"nums": [0, 6, 19]}, "output": 19}, {"input": {"nums": [7, 11, 8, 7, 20, 13, 12]}, "output": 47}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "da00ec6bbc45659b"}
vcr_bd2b23f6ede6
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": ["ccca", "cb", "bcb", "bc", "cc"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aab", "bab", "aaa", "bba", "bab"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aab", "bab", "aaa", "baa", "abb"]}, "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": "d185bfdfda1cb4aa"}
vcr_e9df84d467b4
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 3, 12, 16, 18, 20, 21, 23, 28, 36, 39, 44], "target": 37}, "output": 10}, {"input": {"nums": [2, 16, 39], "target": 2}, "output": 0}, {"input": {"nums": [0, 1, 2, 5, 10, 14, 25, 32, 38], "target": 8}, "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": "314dd8e9f66fe9c6"}
vcr_6042ef470ff4
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": 48, "b": 218}, "output": 2}, {"input": {"a": 10, "b": 42}, "output": 2}, {"input": {"a": 47, "b": 70}, "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": "21d6c3e21f19b247"}
vcr_1700e05f3252
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-9, 6, -10, 3, 17, 3, -13, 18, -6, 3, -5]}, "output": 28}, {"input": {"nums": [0, 13, 0, 7, 10, 10]}, "output": 40}, {"input": {"nums": [3, -9, -6, 9, 9, 10, -7]}, "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": "4a408afb5995a35c"}
vcr_e54403c42b7f
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, 30, 36, 38, 43], "target": 73}, "output": [1, 4]}, {"input": {"nums": [-13, -7, -2, 8, 9, 17, 20, 32, 37, 39], "target": 26}, "output": [0, 9]}, {"input": {"nums": [-15, 10, 14, 16, 17, 32, 33, 34], "target": 66}, "output": [5, 7]}]
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": "41a5c8cb161b7145"}
vcr_a4168e9e7a29
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": ["bcac", "bbc", "cabb", "ab", "cb", "bcbb", "abcc", "bb", "cab"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bbb", "aaa", "abb"]}, "output": [1, 1, 1]}, {"input": {"strs": ["bba", "bba", "bba", "aab"]}, "output": [1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "fca8e6abd71810a7"}
vcr_5b579bd066b2
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, 0, -9, 20, 17], "k": 5}, "output": [2, 0, -9, 20, 17]}, {"input": {"nums": [1, -1, 5, 5, 0], "k": 0}, "output": [1, -1, 5, 5, 0]}, {"input": {"nums": [-1, -8, 0, -2, 2, -4, -7], "k": 8}, "output": [-7, -1, -8, 0, -2, 2, -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": "121c1e1f27326b57"}
vcr_2c619b6a2b44
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": [26, 20, 1, 8, 16, 5, 51, 20, 26, 5, 8, 16, 1]}, "output": 51}, {"input": {"nums": [24, 1, 22, 22, 1, 52, 10, 10, 24]}, "output": 52}, {"input": {"nums": [19, 16, 19, 25, 25, 6, 6, 8, 42, 8, 16]}, "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": "be8ae4c65f2503da"}
vcr_7b0eb1883835
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 5, 5, 1, 0, 4, 4]}, "output": [0, 0, 0, 0, 1200, 0, 0]}, {"input": {"nums": [2, 1, 5, 4, 3]}, "output": [60, 120, 24, 30, 40]}, {"input": {"nums": [4, 2, 5, 5, 4, 4, 1]}, "output": [800, 1600, 640, 640, 800, 800, 3200]}]
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": "5063552d83453d72"}
vcr_47cfe6a4f726
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [5, 6, 8, 12, 16, 18, 24, 27, 46], "target": 36}, "output": 8}, {"input": {"nums": [1, 11, 12, 13, 14, 18, 19, 22, 24, 25, 31, 35], "target": 30}, "output": 10}, {"input": {"nums": [11, 18, 26], "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": "180ed14c36f0c30f"}
vcr_5be4d53367bb
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, -18, 18, -15, 10, -18, -3, -2, 9, -3, -17], "k": 11}, "output": [2, -18, 18, -15, 10, -18, -3, -2, 9, -3, -17]}, {"input": {"nums": [-8, 5, 8, -3], "k": 13}, "output": [-3, -8, 5, 8]}, {"input": {"nums": [-5, 9, 7, -5, 4, -2], "k": 2}, "output": [4, -2, -5, 9, 7, -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": "d0628f933d7f3603"}
vcr_dc821d10d486
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": [53, -13, 48, 71, 30, 0], "target": 119}, "output": [2, 3]}, {"input": {"nums": [1, 8, 6, -9, -27], "target": -19}, "output": [1, 4]}, {"input": {"nums": [2, 46, 49, -30, -17, -20, 3, 44], "target": 47}, "output": [6, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "f21d4369ae186693"}
vcr_1c47065532c6
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": "e caacba ae aebcd d ebddce cecadc"}, "output": "cecadc ebddce d aebcd ae caacba e"}, {"input": {"s": "a abb aa a aaa"}, "output": "aaa a aa abb a"}, {"input": {"s": "bbaa ab aba b"}, "output": "b aba ab 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": "56bae81ecb614ebe"}
vcr_82b62a04f639
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, -22, -4, 5, 12, 19, 30, 47], "target": 52}, "output": [3, 7]}, {"input": {"nums": [-20, -5, 11, 14, 19, 20, 25], "target": -25}, "output": [0, 1]}, {"input": {"nums": [-14, -9, -4, 13, 19, 21, 26], "target": 47}, "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": "c7c69c88c9b08fd6"}
vcr_4cf74789a8e3
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": "dc caa e dedcd d cbb bbdbcd"}, "output": "bbdbcd cbb d dedcd e caa dc"}, {"input": {"s": "aaab abab abab"}, "output": "abab abab aaab"}, {"input": {"s": "bbab b bbb a"}, "output": "a bbb b bbab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "413fb66eaed539d8"}
vcr_4e73f4a5a12f
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": 7424}, "output": 4}, {"input": {"n": 7019}, "output": 9}, {"input": {"n": 10859}, "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": "25afc43006281423"}
vcr_ba6663b41f6f
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, -14, 18, 9], "k": 0}, "output": [15, -14, 18, 9]}, {"input": {"nums": [0, -6, -7, 7, 2, -4, -1], "k": 18}, "output": [7, 2, -4, -1, 0, -6, -7]}, {"input": {"nums": [2, 9, -3, 4, -9, 3, 7, -3, -2], "k": 9}, "output": [2, 9, -3, 4, -9, 3, 7, -3, -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": "018ece587f55895a"}
vcr_86832818511e
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 0, 2, 6, 6, 8, 8, 9, 10, 10, 12, 14, 14]}, "output": [0, 2, 6, 8, 9, 10, 12, 14]}, {"input": {"nums": [1, 3, 4, 6, 12]}, "output": [1, 3, 4, 6, 12]}, {"input": {"nums": [1, 2, 3, 4, 4, 11]}, "output": [1, 2, 3, 4, 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": "ed37b2e1ab9022f1"}
vcr_2b6c57a5f2ff
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, -14, -6, 17, 23, 38], "target": 55}, "output": [3, 5]}, {"input": {"nums": [-19, -10, -5, 12, 22, 23, 24, 26, 38, 39], "target": 46}, "output": [4, 6]}, {"input": {"nums": [-20, -19, -17, 16], "target": -4}, "output": [0, 3]}]
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": "2d85db0753e28b8c"}
vcr_0adfa597016c
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": [22, 16, 21, 7]}, "output": 43}, {"input": {"nums": [8, 13, 2, 19, 8, 18, 8, 16]}, "output": 66}, {"input": {"nums": [5, 13, 6, 19]}, "output": 32}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "9993646370da2a88"}
vcr_81d6674afc8c
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": ["bcbc", "aca", "caaa", "ac", "bb", "cc", "ba", "aac"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bba", "baa"]}, "output": [1, 2]}, {"input": {"strs": ["aba", "aab", "abb"]}, "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": "d9032a5230254191"}
vcr_e9ba0f76dc6d
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": 48727}, "output": 11}, {"input": {"n": 1705}, "output": 6}, {"input": {"n": 16203}, "output": 10}]
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": "046c4200a957f4ac"}
vcr_1ab8faa79a68
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, 10, 27, 37], "target": 49}, "output": 4}, {"input": {"nums": [1, 6, 11, 14, 17, 31, 34, 38, 39], "target": 9}, "output": 2}, {"input": {"nums": [8, 14, 28, 30, 31, 34], "target": 22}, "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": "d36a7ce40e7974bb"}
vcr_e54b939ec23f
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [2, 3, 4, 6, 6]}, "output": [2, 3, 4, 6]}, {"input": {"nums": [0, 6, 7, 8, 9, 9, 11, 12]}, "output": [0, 6, 7, 8, 9, 11, 12]}, {"input": {"nums": [4, 9, 10]}, "output": [4, 9, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "820398bd5d37d8c2"}
vcr_4dee065495b6
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": "d edcec bece cdeadb ceeaa eccda aeeeee"}, "output": "aeeeee eccda ceeaa cdeadb bece edcec d"}, {"input": {"s": "baba aa babb"}, "output": "babb aa baba"}, {"input": {"s": "aab aabb"}, "output": "aabb aab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "979cf25e9a84e244"}
vcr_826205175e47
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [0, 4, 9, 27, 18]}, "output": 31}, {"input": {"nums": [13, 11, 11, 10, 2, 14, 0, 0, 5, 1]}, "output": 43}, {"input": {"nums": [19, 12, 9, 10, 20, 3, 6, 10, 1]}, "output": 58}]
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": "3f3b075791fefa9b"}
vcr_0490910e2e29
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": [-3, -18, -10, -16, -18], "k": 4}, "output": [-18, -10, -16, -18, -3]}, {"input": {"nums": [-7, -2, -1, -10, 1, 4, -9, -6], "k": 12}, "output": [1, 4, -9, -6, -7, -2, -1, -10]}, {"input": {"nums": [9, 10, 0, 9, -4, 6], "k": 18}, "output": [9, 10, 0, 9, -4, 6]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "7ef78fc354c4ad08"}
vcr_599cb6621789
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": [44, 47, 42, 57]}, "output": 15}, {"input": {"prices": [38, 9, 30, 29, 9]}, "output": 21}, {"input": {"prices": [17, 15, 27, 38, 29, 15, 20, 24, 5, 39, 6, 23]}, "output": 34}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "aa1391bc6231a9ec"}
vcr_e78d8d48fe82
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": [23, 42, 31, 38, 5, 11, 40, 43, 44, 23]}, "output": true}, {"input": {"nums": [2, 1, 4, 18]}, "output": false}, {"input": {"nums": [15, 2, 10, 24, 7, 22, 5, 13, 16, 13, 18, 12]}, "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": "6d1947fc9dcdf217"}
vcr_fdc7ae02b4a9
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": "dbccbaddaccbbdbc"}, "output": false}, {"input": {"s": "cab"}, "output": false}, {"input": {"s": "cbaabacbcc"}, "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": "a5fc9e56ffec0497"}
vcr_b9a2633f922b
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 bbabaa"}, "output": "bbabaa a"}, {"input": {"s": "a bbba"}, "output": "bbba a"}, {"input": {"s": "bbab bbb"}, "output": "bbb bbab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "d53bfc511539ec62"}
vcr_2408a874603e
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 12, 18, 21, 25, 29, 30, 33, 36, 37, 42, 44, 47], "target": 16}, "output": 2}, {"input": {"nums": [10, 21, 23, 25, 29, 37], "target": 26}, "output": 4}, {"input": {"nums": [4, 11, 22, 25, 27, 29, 33, 37, 39], "target": 22}, "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": "84bf331337d280b6"}
vcr_970c038c1102
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [41, 34, 34, 39, 55, 24, 56, 39, 48]}, "output": 32}, {"input": {"prices": [27, 13, 5, 22, 36]}, "output": 31}, {"input": {"prices": [22, 40, 15, 21, 35, 24, 17, 34, 6, 31, 31]}, "output": 25}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "e44adf3d4cd43197"}
vcr_d55fcae1c4db
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": [30, 55, 4, 30, 38, 33, 5, 5, 38, 4, 33]}, "output": 55}, {"input": {"nums": [53, 27, 19, 22, 15, 9, 15, 27, 9, 22, 19]}, "output": 53}, {"input": {"nums": [48, 9, 9, 15, 15, 21, 21]}, "output": 48}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "ecb0d182c89b1ac7"}
vcr_bb6a12b6956f
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": [0, 1, 6, 2, 5, 7, 4, 6]}, "output": [10080, 0, 0, 0, 0, 0, 0, 0]}, {"input": {"nums": [3, 1, 1]}, "output": [1, 3, 3]}, {"input": {"nums": [2, 1, 2]}, "output": [2, 4, 2]}]
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": "8b2be5e53550ecfe"}
vcr_8aef8e6961b2
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, 5, 8, 9, 9, 10, 11, 13, 14, 15]}, "output": [0, 5, 8, 9, 10, 11, 13, 14, 15]}, {"input": {"nums": [2, 5, 5, 5, 11, 11]}, "output": [2, 5, 11]}, {"input": {"nums": [0, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9, 10]}, "output": [0, 4, 5, 6, 7, 8, 9, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "4860ad076cc4cc04"}
vcr_96202a0fd92d
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, 7, 6, 2]}, "output": [84, 588, 84, 98, 294]}, {"input": {"nums": [5, 3, 3, 5, 1, 2, 5]}, "output": [450, 750, 750, 450, 2250, 1125, 450]}, {"input": {"nums": [2, 3, 3, 1, 3, 2]}, "output": [54, 36, 36, 108, 36, 54]}]
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": "8159f357b27ee575"}
vcr_55da0c4d773c
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": [144, 113, 143, 10, 145, 197, 54, 35, 109, 4, 95, 97, 39, 74, 101, 180]}, "output": false}, {"input": {"nums": [1, 22, 15, 22, 13, 22, 22, 5]}, "output": true}, {"input": {"nums": [10, 13, 12, 9, 1, 8, 10, 0, 6]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "55335771ded3be16"}
vcr_d1e07b77a1df
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["aa", "acac", "bbc", "cac"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bab", "bab", "abb", "aaa", "aab", "aaa"]}, "output": [1, 2, 3]}, {"input": {"strs": ["abb", "bba", "aab", "bba", "bbb", "aaa", "bbb"]}, "output": [1, 1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "7d08d7ad9e215ae5"}
vcr_310e9be3642e
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, 5, 5, 3, 3, 5, 1, 2]}, "output": [2250, 3150, 3150, 5250, 5250, 3150, 15750, 7875]}, {"input": {"nums": [4, 5, 5, 1]}, "output": [25, 20, 20, 100]}, {"input": {"nums": [1, 2, 1]}, "output": [2, 1, 2]}]
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": "2503209285edbdb2"}
vcr_120d99980d5c
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": [55, 25, 10, 43, 34, 58, 56, 56, 26, 46]}, "output": 48}, {"input": {"prices": [23, 14, 9, 28, 33, 5, 14, 15]}, "output": 24}, {"input": {"prices": [18, 15, 23, 2, 38, 25, 29, 25]}, "output": 36}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "2674d29f689dfe02"}
vcr_aba79b382521
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": "babbacabccb"}, "output": 9}, {"input": {"s": "ccacccabccc"}, "output": 6}, {"input": {"s": "aacacaaccba"}, "output": 8}]
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": "89cc033d78c1bffc"}
vcr_3b26be92fd30
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": ["cacc", "bb", "ab", "aab", "caa", "bcb", "aca", "aa"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "aba", "bbb", "aab", "bba", "aba", "abb"]}, "output": [1, 3, 3]}, {"input": {"strs": ["bba", "baa", "bbb", "aaa", "bba", "bbb"]}, "output": [1, 1, 2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "d682f2bd68e9d4c3"}
vcr_0ae7a8723ab5
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": [28, 5, 39, 28, 11, 43, 23, 42]}, "output": 38}, {"input": {"prices": [15, 33, 8, 10, 21, 40, 29]}, "output": 32}, {"input": {"prices": [25, 17, 39, 33]}, "output": 22}]
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": "8e16646426a79dc0"}
vcr_e3267be2149a
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": "d aedd ececcb ba"}, "output": "ba ececcb aedd d"}, {"input": {"s": "aba a a"}, "output": "a a aba"}, {"input": {"s": "a b babb"}, "output": "babb b 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": "ef7c756814e0a79e"}
vcr_9395e3f18e45
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-9, -5, -8, -9, -18, -9], "k": 9}, "output": [-9, -18, -9, -9, -5, -8]}, {"input": {"nums": [-3, -9, -7, -9, 4, 1, -2], "k": 14}, "output": [-3, -9, -7, -9, 4, 1, -2]}, {"input": {"nums": [6, 3, 8, 5, -2, -9, -7, -4], "k": 9}, "output": [-4, 6, 3, 8, 5, -2, -9, -7]}]
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": "879cc95702714085"}
vcr_b6b1198868b4
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, 18, 4, 14]}, "output": 32}, {"input": {"nums": [18, 7, 8]}, "output": 26}, {"input": {"nums": [12, 9, 14, 16, 5, 15, 12, 11, 18, 14]}, "output": 68}]
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": "8e37bc2d458d05d7"}
vcr_3ac17db208b7
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, 5, 6, 14, 15, 20, 22, 26, 32, 39, 43, 48], "target": 3}, "output": 1}, {"input": {"nums": [4, 8, 12, 21, 29, 30, 33, 34, 39], "target": 31}, "output": 6}, {"input": {"nums": [3, 16, 19, 20, 21, 28, 29, 31, 34, 36], "target": 3}, "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": "cb9c36fad6877717"}
vcr_1102ba471f38
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, 12, 18, 29, 32, 41, 44, 45, 47], "target": 4}, "output": 0}, {"input": {"nums": [3, 17, 36, 37, 38], "target": 9}, "output": 1}, {"input": {"nums": [19, 21, 22, 24], "target": 20}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "cb65980089e0104f"}
vcr_1957808ff21b
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 6, 12, 13, 21, 23, 24, 25, 27, 29, 38, 48], "target": 34}, "output": 10}, {"input": {"nums": [0, 1, 4, 5, 7, 8, 10, 12, 28, 30, 31], "target": 27}, "output": 8}, {"input": {"nums": [16, 21, 22], "target": 23}, "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": "b9e3b05cf668cfe7"}
vcr_bc17a6928728
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-7, 9, -19, 5, -6], "k": 8}, "output": [-19, 5, -6, -7, 9]}, {"input": {"nums": [-8, -1, 2, 3, -8, -9], "k": 8}, "output": [-8, -9, -8, -1, 2, 3]}, {"input": {"nums": [-1, 3, 7, 7, 7, -9, 4, 2, -3, -7], "k": 3}, "output": [2, -3, -7, -1, 3, 7, 7, 7, -9, 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": "1d5894e79105224f"}
vcr_a13d65159856
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": "babbaccbaaaaa"}, "output": 7}, {"input": {"s": "aacba"}, "output": 4}, {"input": {"s": "ccabaaccccac"}, "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": "db4ae78d66f0146d"}
vcr_4fc51a735aa9
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-12, 13, 5, -7, 9, 6, 22, 13, -8, 9, 15, -5]}, "output": 77}, {"input": {"nums": [-11, -8, 9, -10, 13, 5, 14, -8]}, "output": 32}, {"input": {"nums": [-2, 8, -11]}, "output": 8}]
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": "3948966bd8ed3777"}
vcr_18d3893000c1
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 7, 20, 27, 43, 44], "target": 35}, "output": 4}, {"input": {"nums": [1, 4, 29, 31, 38], "target": 17}, "output": 2}, {"input": {"nums": [4, 13, 37, 39], "target": 16}, "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": "c8c441c354cc0075"}
vcr_53f33c9bdd71
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [32, 5, 11, 5, 11, 19, 19, 37, 37, 12, 12, 32, 75]}, "output": 75}, {"input": {"nums": [25, 24, 25, 24, 27, 27, 6, 58, 6]}, "output": 58}, {"input": {"nums": [22, 55, 10, 3, 13, 3, 13, 20, 20, 22, 10]}, "output": 55}]
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": "3ed16867eccd4efc"}
vcr_a605adc6ecb0
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [27, 30, 34, 17]}, "output": 7}, {"input": {"prices": [16, 10, 10, 23]}, "output": 13}, {"input": {"prices": [8, 37, 39, 13, 33, 17, 9, 16, 25, 15]}, "output": 31}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "63349a0fb6defbbd"}
vcr_83f61564eeae
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": [36, 134, 124, 174, 139, 131, 118, 72, 150, 96, 12, 74]}, "output": false}, {"input": {"nums": [19, 3, 8, 20, 5]}, "output": false}, {"input": {"nums": [10, 13, 14, 14, 1, 10, 21]}, "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": "0b0e5bebbcd78ca7"}
vcr_b1a8f465dc5e
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": "cbbbaabbbbabb"}, "output": 6}, {"input": {"s": "ccca"}, "output": 2}, {"input": {"s": "abbcccabaabb"}, "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": "bcc28d41ce45b9a8"}
vcr_7ca4c6756ceb
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["ca", "aba", "bb", "aa", "acc", "caa", "cac", "cc"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "abb", "aab", "bbb", "bba"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aab", "aba", "bab", "aab", "bbb"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "8be4fa7c27bfcfea"}
vcr_c0e1101d97f4
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [13, 7, 24, 3, 24, 3, 13, 0, 21, 2]}, "output": 95}, {"input": {"nums": [12, 3, 0, 9, 0, 18, 20, 5]}, "output": 44}, {"input": {"nums": [17, 12]}, "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": "f7fb04486d1e4e5a"}
vcr_aae6e41af0b1
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-17, -15, -12, 6, 19, 31, 38, 44], "target": 50}, "output": [3, 7]}, {"input": {"nums": [-7, -4, -3, -2, 1, 22, 34, 39], "target": 23}, "output": [4, 5]}, {"input": {"nums": [-11, -9, -7, -5, 5, 10, 21, 27, 30, 35, 38], "target": 0}, "output": [3, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "e15f335299331dd4"}
vcr_5b3c47db747c
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-8, -7, 2, 1, -11, 2, 16, -7, -14, -14, 8], "k": 5}, "output": [16, -7, -14, -14, 8, -8, -7, 2, 1, -11, 2]}, {"input": {"nums": [9, 6, -1, 1, 6, 9], "k": 0}, "output": [9, 6, -1, 1, 6, 9]}, {"input": {"nums": [-6, 9, 10, -10], "k": 9}, "output": [-10, -6, 9, 10]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "a09add95b69b0a1e"}
vcr_05c7718ad752
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": "daaaea ad aea cdbebe c"}, "output": "c cdbebe aea ad daaaea"}, {"input": {"s": "ba bb baa"}, "output": "baa bb ba"}, {"input": {"s": "abbb b a baab aabb"}, "output": "aabb baab a b abbb"}]
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": "c92e6d1f4e3b670b"}
vcr_8710a4e30d68
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": [23, 10, 54, -12, 17, -31, 73, 22, -19, 15], "target": 61}, "output": [3, 6]}, {"input": {"nums": [8, 9, 3, 21, 29, -12, 6, -23, 22], "target": 25}, "output": [2, 8]}, {"input": {"nums": [-9, -17, 42, 48], "target": 25}, "output": [1, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "78e1f2a841c0be65"}
vcr_b26558bbab0e
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["caba", "aaca", "cb", "aaaa", "ca", "ca", "aca", "cbb"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["baa", "baa", "aab", "aaa"]}, "output": [1, 3]}, {"input": {"strs": ["bab", "aab", "aba"]}, "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": "b748c83219d79ea9"}
vcr_8b060d240b20
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 4, 4, 2, 4, 4, 2, 0]}, "output": true}, {"input": {"nums": [1, 3, 1, 1, 3, 3, 2, 1, 2]}, "output": true}, {"input": {"nums": [3, 2, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "8cdc92473c766e2d"}
vcr_39e39a621e42
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": "bbdcaaaaacdbb"}, "output": true}, {"input": {"s": "bacaab"}, "output": false}, {"input": {"s": "cacaccbca"}, "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": "60360f79aca7fa00"}
vcr_1ec9ebff8e93
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": [44, 9, 9, 15, 9, 10]}, "output": 6}, {"input": {"prices": [1, 2, 20, 29, 12, 7, 6, 18, 32, 23]}, "output": 31}, {"input": {"prices": [11, 21, 30, 15, 39, 5, 15, 25]}, "output": 28}]
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": "924d97f69e4b9987"}
vcr_392993314c30
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-25, -24, -16, -12, 16, 17, 21, 26, 34, 38], "target": 22}, "output": [2, 9]}, {"input": {"nums": [-12, 0, 4, 6, 7, 19, 22, 26, 27, 29, 38], "target": 23}, "output": [2, 5]}, {"input": {"nums": [-18, -13, -10, -7, -2, -1, 11, 13, 15, 16, 33], "target": 3}, "output": [1, 9]}]
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": "93677d32c7b40ac3"}
vcr_9233a425c306
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [18, 1, 10, 2, 2, -4, 3, -7, 15, -17, 7], "k": 17}, "output": [-4, 3, -7, 15, -17, 7, 18, 1, 10, 2, 2]}, {"input": {"nums": [-3, -10, -10, 5], "k": 1}, "output": [5, -3, -10, -10]}, {"input": {"nums": [-1, -2, -4, -5, 6], "k": 18}, "output": [-4, -5, 6, -1, -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": "1115a43ff9abbbcb"}
vcr_686d3258e0c3
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, 9, 11, 14, 20, 22, 31, 32, 34, 35, 37, 41], "target": 17}, "output": 4}, {"input": {"nums": [24, 27, 33, 36, 37], "target": 14}, "output": 0}, {"input": {"nums": [5, 11, 28, 29, 33, 38], "target": 3}, "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": "d39a65cd0c36ef43"}
vcr_e2a0763745f1
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, 6, 42, 11, 3, 46, 29, 49]}, "output": true}, {"input": {"nums": [1, 17, 13, 1, 1, 14, 6, 7, 16, 16]}, "output": true}, {"input": {"nums": [4, 4, 18, 24, 7, 8, 22, 21, 19, 17, 4, 14]}, "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": "930c2b3116073209"}
vcr_c6510b2cb8c8
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": [-34, 15, -40, -12, 68, -10], "target": -46}, "output": [0, 3]}, {"input": {"nums": [34, 24, 44, 29, -15, -8, 43, -5, -22], "target": 29}, "output": [2, 4]}, {"input": {"nums": [47, -6, 8, -18, 40, 14, -10, 31], "target": -28}, "output": [3, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "dd1c6dd8ddc0da03"}
vcr_6a3fb286a43f
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [38, 22, 72, 22, 4, 38, 4]}, "output": 72}, {"input": {"nums": [9, 2, 14, 47, 8, 9, 14, 8, 2]}, "output": 47}, {"input": {"nums": [48, 17, 14, 9, 14, 5, 7, 9, 8, 7, 17, 8, 5]}, "output": 48}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "ae492469ea7b849f"}
vcr_d96ab7f3f375
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": [68, 77, -20, -1, 23, -25], "target": 76}, "output": [1, 3]}, {"input": {"nums": [4, -15, 5, 48, -8, -21, -28, -24, 44, 36], "target": -49}, "output": [5, 6]}, {"input": {"nums": [-12, 38, -17, 16, 2, 12, 35, 4, 15], "target": 3}, "output": [0, 8]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "75127dc0bea36c21"}
vcr_104eafc7b95f
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": 168, "b": 139}, "output": 1}, {"input": {"a": 144, "b": 170}, "output": 2}, {"input": {"a": 28, "b": 63}, "output": 7}]
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": "ea13d542b6a72690"}
vcr_e823e36a9185
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, 2, 1, 3, 0, 4, 0]}, "output": true}, {"input": {"nums": [2, 0, 0, 1]}, "output": false}, {"input": {"nums": [1, 2, 2, 1, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "f3b5b0a742c9da3b"}
vcr_99fe0c996328
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": "abbbabdbbdacb"}, "output": false}, {"input": {"s": "baaacc"}, "output": false}, {"input": {"s": "baaaabcca"}, "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": "e05f5770d1c9d015"}
vcr_e042e2e2edfa
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["caba", "bab", "aac", "cccc", "accc", "acbc", "cc", "cb", "abba"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "aaa", "aba", "bab", "baa"]}, "output": [1, 2, 2]}, {"input": {"strs": ["bba", "aba", "bab", "baa", "aaa"]}, "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": "d710f54dc7591187"}
vcr_13c6ede11458
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [13, 24, 15, 18, 15, 22, 6, 24, 6, 4, 11, 17]}, "output": 109}, {"input": {"nums": [4, 7, 5, 10, 18, 14, 2, 1, 4]}, "output": 35}, {"input": {"nums": [20, 6, 11, 1, 17, 12, 4, 7, 6, 10]}, "output": 65}]
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": "3a418aa6eae5c174"}
vcr_eb0bdde82b85
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": [179, 27, 38, 58, 102, 191, 121, 79]}, "output": false}, {"input": {"nums": [8, 14, 20, 25, 5, 7]}, "output": false}, {"input": {"nums": [18, 23, 1, 8, 22, 8, 9, 7, 3, 0, 3]}, "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": "6a490eb03f0dc043"}
vcr_e14aa6439779
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-18, -8, -20, 8, -11, 19, 4, -7, 17, 4, -20, -11], "k": 7}, "output": [19, 4, -7, 17, 4, -20, -11, -18, -8, -20, 8, -11]}, {"input": {"nums": [6, 8, 6], "k": 10}, "output": [6, 6, 8]}, {"input": {"nums": [7, 10, 9, -10, 8, -10, -5, 0, 1, -5], "k": 12}, "output": [1, -5, 7, 10, 9, -10, 8, -10, -5, 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": "4a53e4b34471509b"}
vcr_47a861e7906c
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [33, -18, -18, -11, 18, -18, -13, 0]}, "output": [33, 15, -3, -14, 4, -14, -27, -27]}, {"input": {"nums": [16, -12, 16, 3]}, "output": [16, 4, 20, 23]}, {"input": {"nums": [5, 18, 4, 15, 17, -4, 12]}, "output": [5, 23, 27, 42, 59, 55, 67]}]
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": "44259c9b1f37f18b"}
vcr_91eca7065362
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": [53, -39, 20, 46, 12, -29, -27], "target": 66}, "output": [2, 3]}, {"input": {"nums": [-10, 44, 17, 0, 27, 6, 3, 19], "target": 3}, "output": [3, 6]}, {"input": {"nums": [35, -21, 3, 48, -18, 37, 25, 28, 45, -3, -2], "target": 16}, "output": [1, 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": "af04d4d518f8b4bf"}
vcr_57421825df52
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": 23, "b": 147}, "output": 1}, {"input": {"a": 143, "b": 1}, "output": 1}, {"input": {"a": 134, "b": 140}, "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": "922963c35c33dbda"}
vcr_522b072d3f16
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["caba", "abb", "bcaa", "aac", "aab", "bcaa"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["aaa", "bba", "aba", "aaa", "bba", "bba", "bbb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["bab", "abb", "bbb"]}, "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": "692eaf65fc5e915b"}
vcr_e12185dd83ef
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": "bcbbbcabbacb"}, "output": 9}, {"input": {"s": "bccaabcbccb"}, "output": 8}, {"input": {"s": "aabacba"}, "output": 6}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "08199bf67a001449"}
vcr_7758943a2e4b
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 7, 9, 9, 10, 12]}, "output": [0, 1, 7, 9, 10, 12]}, {"input": {"nums": [0, 2, 7, 7, 9, 11]}, "output": [0, 2, 7, 9, 11]}, {"input": {"nums": [0, 1, 1, 2, 5, 8, 9, 9, 11, 11, 12]}, "output": [0, 1, 2, 5, 8, 9, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "754604c155730e03"}
vcr_14e5e25a08e0
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": "baacaaaaab"}, "output": 5}, {"input": {"s": "bbba"}, "output": 2}, {"input": {"s": "bcabccbcbb"}, "output": 8}]
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": "febbf9d14425908f"}