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_e86e08af8971
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, 4, 2, 7, 3, 0, 6]}, "output": [0, 0, 0, 0, 0, 3024, 0]}, {"input": {"nums": [2, 5, 1]}, "output": [5, 2, 10]}, {"input": {"nums": [4, 3, 4, 4]}, "output": [48, 64, 48, 48]}]
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": "cc0c7ce1ca45f5ac"}
vcr_62a7ef0a2297
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": [-14, 62, 39, 42, -33], "target": 25}, "output": [0, 2]}, {"input": {"nums": [-7, 28, -16, -8, 29, -13, -5, -17], "target": 23}, "output": [1, 6]}, {"input": {"nums": [38, -29, 49, 9], "target": 20}, "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": "e4ec09ab1e72af5d"}
vcr_bfaf514398e1
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -25, -11, -2, 26, 28, 29, 34, 37, 41], "target": 65}, "output": [5, 8]}, {"input": {"nums": [-18, -17, 4, 7, 10, 15, 22, 27, 29, 30], "target": 26}, "output": [2, 6]}, {"input": {"nums": [-14, -9, 2, 9, 10, 13, 15, 17, 20], "target": 8}, "output": [1, 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": "ac30e07314d57e84"}
vcr_98063dd2c6d0
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [13, 35, 10, 2, 13, 58, 37, 10, 21, 21, 36, 37, 32, 2, 32, 36, 35]}, "output": 58}, {"input": {"nums": [22, 26, 22, 26, 48]}, "output": 48}, {"input": {"nums": [26, 29, 22, 13, 52, 29, 13, 10, 26, 10, 22]}, "output": 52}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "6f8765ba46aec07f"}
vcr_49e0ffcbbdc8
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": 65283}, "output": 10}, {"input": {"n": 6325}, "output": 7}, {"input": {"n": 8805}, "output": 6}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "27998aa22d1f4398"}
vcr_f7227ae87951
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "cdbbbad"}, "output": false}, {"input": {"s": "ab"}, "output": false}, {"input": {"s": "bcaccacba"}, "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": "e9b8cc47657abaed"}
vcr_6a8384c91513
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": [13, -6, -11]}, "output": [13, 7, -4]}, {"input": {"nums": [18, -12, 18, 18, -7]}, "output": [18, 6, 24, 42, 35]}, {"input": {"nums": [16, -7, -12, 18, -8, -12, -10, 14, -1]}, "output": [16, 9, -3, 15, 7, -5, -15, -1, -2]}]
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": "45edb38af33b9c91"}
vcr_fc1d2be6dd78
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": [34, 19, 19, -20, 21, -3, 24, -11, 6]}, "output": [34, 53, 72, 52, 73, 70, 94, 83, 89]}, {"input": {"nums": [-12, -7, 8, 5, 16, -11, 13, 6, -9, -11]}, "output": [-12, -19, -11, -6, 10, -1, 12, 18, 9, -2]}, {"input": {"nums": [-11, 1, 6, 15, 18, -2, 4, 14, 12, 4]}, "output": [-11, -10, -4, 11, 29, 27...
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": "e5ce375a3d0f65f1"}
vcr_dc943d911381
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, 2, 3, 13, 28, 27, 17, 25, 11, 4]}, "output": 91}, {"input": {"nums": [1, 7, 1, 9, 17, 9, 17]}, "output": 41}, {"input": {"nums": [13, 14, 2, 5, 15, 14, 18]}, "output": 48}]
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": "a261c44a1199c6a6"}
vcr_66e2aa5885fe
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 1, 2, 3]}, "output": [6, 30, 15, 10]}, {"input": {"nums": [2, 2, 2, 5, 3]}, "output": [60, 60, 60, 24, 40]}, {"input": {"nums": [4, 5, 2, 5, 4]}, "output": [200, 160, 400, 160, 200]}]
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": "54a502e7f3d703c3"}
vcr_a5fce16481f4
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [4, 27, 47, 12, 27, 54, 52, 22, 22]}, "output": 50}, {"input": {"prices": [33, 20, 29, 31]}, "output": 11}, {"input": {"prices": [32, 2, 21, 6, 34, 6, 5]}, "output": 32}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "c8952a6da65dd54f"}
vcr_8bb423bbad5a
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 3, 4, 5, 5, 5, 6, 9, 14]}, "output": [0, 3, 4, 5, 6, 9, 14]}, {"input": {"nums": [4, 5, 5, 5, 9, 11, 12]}, "output": [4, 5, 9, 11, 12]}, {"input": {"nums": [1, 4, 9, 10, 11]}, "output": [1, 4, 9, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "2c723eed7e15897a"}
vcr_cbdf76044823
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [47, 23, 32, 4, 44, 45, 9, 7, 32, 33, 58, 18, 60, 18, 24]}, "output": 56}, {"input": {"prices": [37, 13, 24, 33]}, "output": 20}, {"input": {"prices": [19, 32, 31, 17, 29, 12, 14, 8, 27, 16]}, "output": 19}]
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": "52a045f1cf24edc8"}
vcr_96dacdfc5fbd
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": [29, 20, 6, 6, 38, 3, 22, 29, 38, 20, 26, 3, 26, 22, 65]}, "output": 65}, {"input": {"nums": [25, 25, 27, 27, 46]}, "output": 46}, {"input": {"nums": [28, 12, 16, 12, 16, 25, 25, 28, 53]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "ad3804f6e528e862"}
vcr_42ef491c6f3a
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [8, 9, 13, 18, 23, 26, 28, 29, 35, 38, 39, 47, 49], "target": 9}, "output": 1}, {"input": {"nums": [9, 10, 21, 32, 33, 34, 37], "target": 20}, "output": 2}, {"input": {"nums": [4, 5, 7, 10, 13, 16, 26, 32, 36, 37, 39], "target": 19}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "0b2c178ebb29af5f"}
vcr_9405cc3a6ad8
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, 48, 36, 13, 44, 10, 6, 1, 10, 5]}, "output": true}, {"input": {"nums": [24, 5, 15, 16, 10, 6, 14, 5, 1]}, "output": true}, {"input": {"nums": [6, 0, 25, 7, 1, 2, 24, 1]}, "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": "af9bc936a0a198ec"}
vcr_f1fd22057b57
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": "bcbb bbadb e"}, "output": "e bbadb bcbb"}, {"input": {"s": "a aa"}, "output": "aa a"}, {"input": {"s": "ba baa baba ab aba"}, "output": "aba ab baba baa ba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "8d7c604c0880ecda"}
vcr_dfc1c136ae6f
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": "be ba e dee cbadc aae cadc"}, "output": "cadc aae cbadc dee e ba be"}, {"input": {"s": "abaa aa ba b aa"}, "output": "aa b ba aa abaa"}, {"input": {"s": "bbb bbaa"}, "output": "bbaa bbb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "2988b282d4f37acd"}
vcr_ec9d3ce408d3
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["cabb", "bac", "aaba", "cc", "caca", "aba", "caab"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "baa", "aba", "abb", "aba", "aaa"]}, "output": [1, 1, 4]}, {"input": {"strs": ["bbb", "bba", "abb", "aba", "baa", "bbb", "aaa"]}, "output": [1, 2, 2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "ac437bbf7e2a384d"}
vcr_c6e938da70a6
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, -20, 10, 17], "k": 2}, "output": [10, 17, -8, -20]}, {"input": {"nums": [2, 6, -1, 7, 0, 4, 10, 2], "k": 5}, "output": [7, 0, 4, 10, 2, 2, 6, -1]}, {"input": {"nums": [6, -2, -5], "k": 6}, "output": [6, -2, -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": "c50c46d446c8798a"}
vcr_73a0b6872111
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 12, "b": 161}, "output": 1}, {"input": {"a": 63, "b": 78}, "output": 3}, {"input": {"a": 13, "b": 97}, "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": "35e8e53d8bd0cd29"}
vcr_9dbe7c6d2b35
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": 229, "b": 176}, "output": 1}, {"input": {"a": 24, "b": 173}, "output": 1}, {"input": {"a": 179, "b": 157}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "ecdb927d19cd8a47"}
vcr_6631a1725430
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, 4, 20, 21, 27, 28, 32, 38, 39, 47], "target": 17}, "output": 2}, {"input": {"nums": [1, 4, 8, 14, 24, 33, 36, 38], "target": 36}, "output": 6}, {"input": {"nums": [19, 24, 33, 39], "target": 25}, "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": "bc90cfebccbae924"}
vcr_2ba437fcca26
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", "abab", "abab", "ca", "cbba", "bac"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["aaa", "abb", "bba", "abb", "bba", "aba"]}, "output": [1, 1, 4]}, {"input": {"strs": ["bab", "aab", "bab", "baa", "aaa", "aaa"]}, "output": [2, 2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "7468442076f81476"}
vcr_cdf26fedea54
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 3, 4, 4, 6, 7, 7, 7, 8, 9, 11, 13, 14]}, "output": [0, 3, 4, 6, 7, 8, 9, 11, 13, 14]}, {"input": {"nums": [2, 4, 10, 12]}, "output": [2, 4, 10, 12]}, {"input": {"nums": [3, 3, 9, 9]}, "output": [3, 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": "45b82691dab8537c"}
vcr_c8f544164699
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [27, 33, 73, 15, 35, 27, 15, 33, 35]}, "output": 73}, {"input": {"nums": [2, 11, 23, 19, 19, 11, 2, 47, 23]}, "output": 47}, {"input": {"nums": [28, 13, 51, 1, 28, 13, 1]}, "output": 51}]
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": "529063d69922f34b"}
vcr_65dbfe079db6
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 6, 6, 11, 13, 15, 15]}, "output": [0, 1, 6, 11, 13, 15]}, {"input": {"nums": [1, 3, 4, 4, 4, 4, 6, 9, 9, 11]}, "output": [1, 3, 4, 6, 9, 11]}, {"input": {"nums": [4, 6, 8, 10, 10, 11, 12, 12]}, "output": [4, 6, 8, 10, 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": "e70ee6b0f1971165"}
vcr_1c918153ba71
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "dcddddcd"}, "output": true}, {"input": {"s": "baccababa"}, "output": false}, {"input": {"s": "acbcbacc"}, "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": "7b733b5f2c9d3bf7"}
vcr_3e1be61c061c
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": [16, 26, 20, 9, -4, 29, -11, -3, 34, 27]}, "output": [16, 42, 62, 71, 67, 96, 85, 82, 116, 143]}, {"input": {"nums": [16, -3, 2, 2, -12, 15, 0]}, "output": [16, 13, 15, 17, 5, 20, 20]}, {"input": {"nums": [-2, -6, 15, 9]}, "output": [-2, -8, 7, 16]}]
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": "5d6e3dcff0952cf9"}
vcr_d8d6c9c0da26
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": [25, 59, 44, 16, 32, 51, 29, 54, 36, 23, 43, 16, 44, 11]}, "output": 38}, {"input": {"prices": [15, 13, 12, 17, 36, 23]}, "output": 24}, {"input": {"prices": [29, 16, 26, 27, 21, 4, 3]}, "output": 11}]
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": "c672be7f399996b0"}
vcr_b335b72786b4
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": 156, "b": 56}, "output": 4}, {"input": {"a": 143, "b": 101}, "output": 1}, {"input": {"a": 119, "b": 34}, "output": 17}]
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": "53e48ae8b81437e6"}
vcr_4425a5b273ec
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, 20, 7, 11, 3, 3]}, "output": 36}, {"input": {"nums": [8, 13, 1, 6, 9, 15, 18]}, "output": 40}, {"input": {"nums": [9, 3, 19, 7, 9, 4]}, "output": 37}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "bb0ac37843543a63"}
vcr_f82e4bd04b6a
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": [15, 11, 40, 39, 6, 32, 5, 37, 24, 15]}, "output": true}, {"input": {"nums": [4, 9, 25, 6, 7, 23]}, "output": false}, {"input": {"nums": [9, 4, 12, 10, 2, 17, 1, 0]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "d4e3f34154816326"}
vcr_3366bf704c71
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [1, 17, 24, 22, 29, 5, 17]}, "output": 71}, {"input": {"nums": [0, 20, 15, 11, 5, 17, 5, 12, 0, 9]}, "output": 69}, {"input": {"nums": [4, 0, 2, 9, 17, 2, 9, 8, 2]}, "output": 34}]
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": "3a45534d4b1172e2"}
vcr_0b94ca396f61
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, 17, 3, 27, 30, 15, 17, 24, 4]}, "output": 83}, {"input": {"nums": [14, 12]}, "output": 14}, {"input": {"nums": [5, 9]}, "output": 9}]
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": "ec77ee8ca5c95b74"}
vcr_d8e88b8698b8
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "aaadbcbaabcdaa"}, "output": false}, {"input": {"s": "bcaabaa"}, "output": false}, {"input": {"s": "aacaca"}, "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": "589c0d62d202b139"}
vcr_6378ab00fcba
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": ["cbc", "bc", "ab", "cba", "bc", "caca", "abca", "ba", "cb"]}, "output": [1, 1, 1, 1, 2, 3]}, {"input": {"strs": ["baa", "aba", "bba", "bbb", "bba", "aab"]}, "output": [1, 2, 3]}, {"input": {"strs": ["bba", "bbb", "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": "9e841cf57d4a563c"}
vcr_77afe73df404
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": [-15, -10, -8, -4, 7, 12, 21, 38], "target": 6}, "output": [0, 6]}, {"input": {"nums": [-20, -13, -9, -2, 9, 11, 13, 15, 17, 24], "target": 37}, "output": [6, 9]}, {"input": {"nums": [-5, -3, 1, 3, 7, 10, 34], "target": 2}, "output": [0, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "2e2d87e8432c113b"}
vcr_428d96cdacec
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-10, 4, 7, 10, 18, 7, -15, -5, 11, 8, -5, -4, 11, -6]}, "output": 47}, {"input": {"nums": [-5, 0, -9, 9, -12, -4, 1, 2, 8, -10]}, "output": 11}, {"input": {"nums": [0, 11, 7, 11, 11, -3, -10, 9, -10, -4, 5]}, "output": 40}]
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": "20db0bf4b00372ea"}
vcr_9d00a5b59012
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, 36, 54, 32, 24, 36, 24]}, "output": 54}, {"input": {"nums": [24, 11, 24, 11, 47, 10, 10]}, "output": 47}, {"input": {"nums": [20, 18, 18, 54, 20]}, "output": 54}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "59e35bd4f3182ed4"}
vcr_b2fb07210ee9
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": [-11, 5, 8, -10, 10, -8, -13]}, "output": 13}, {"input": {"nums": [-7, -10, 14, 2, -8, -1, 5]}, "output": 16}, {"input": {"nums": [0, 6, 13, 1, 1, -8]}, "output": 21}]
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": "60b7ed99449ec167"}
vcr_b90cab0ee256
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 3, 0, 2, 7, 1]}, "output": [0, 0, 84, 0, 0, 0]}, {"input": {"nums": [3, 1, 1, 1, 4]}, "output": [4, 12, 12, 12, 3]}, {"input": {"nums": [1, 2, 1, 5, 2, 1]}, "output": [20, 10, 20, 4, 10, 20]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "0ab01baac17bf07d"}
vcr_9a0e52d79c8a
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": [-16, -24, 29]}, "output": [-16, -40, -11]}, {"input": {"nums": [-4, 11, -7, -7, -10, -10, 2]}, "output": [-4, 7, 0, -7, -17, -27, -25]}, {"input": {"nums": [12, 0, 16, 0, 1]}, "output": [12, 12, 28, 28, 29]}]
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": "5fafd26e3e013a78"}
vcr_5eef0f474bf9
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 1, 0, 0, 4, 2, 0, 4, 1, 0]}, "output": false}, {"input": {"nums": [1, 0, 3, 3, 2]}, "output": false}, {"input": {"nums": [1, 3, 0, 1, 0, 3, 1, 1, 0]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "994b8ca04d925c20"}
vcr_ba7f76d96050
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": [-13, -7, -10, -16, -25, -14, 9, -7, 27, 15, 5]}, "output": [-13, -20, -30, -46, -71, -85, -76, -83, -56, -41, -36]}, {"input": {"nums": [-5, 17, -11, 7, 16]}, "output": [-5, 12, 1, 8, 24]}, {"input": {"nums": [4, 13, 16, -8]}, "output": [4, 17, 33, 25]}]
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": "0dc78287c5df9f9e"}
vcr_c6084b72faf0
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, 2, 3, 7, 2, 6, 23, 28]}, "output": 43}, {"input": {"nums": [2, 19, 6, 7, 13, 4]}, "output": 32}, {"input": {"nums": [2, 7, 18]}, "output": 20}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "ef9ccb451534d7a9"}
vcr_6503bafb6a0b
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [9, 20, 18]}, "output": 27}, {"input": {"nums": [12, 15, 18, 10, 19, 1, 1, 11, 19, 15]}, "output": 75}, {"input": {"nums": [10, 16, 11, 13, 5, 0, 6, 2, 9, 9]}, "output": 44}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "153d5d2bf158cbf0"}
vcr_77d25d0153e2
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": [24, 5, 36, 21, 12, 8, 36, 37, 21, 5, 24, 8, 30, 55, 30, 12, 37]}, "output": 55}, {"input": {"nums": [26, 23, 12, 12, 20, 15, 43, 2, 15, 23, 26, 2, 20]}, "output": 43}, {"input": {"nums": [44, 22, 29, 22, 29]}, "output": 44}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "44d24d0a0b7a80d4"}
vcr_6776e8057dd9
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": [24, 5, 28, 11, 1, 0, 15, 26, 15, 19, 0, 12]}, "output": 110}, {"input": {"nums": [1, 7, 1, 6, 7]}, "output": 14}, {"input": {"nums": [9, 20, 15, 7, 5, 5, 16]}, "output": 45}]
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": "c4f026e5554211c4"}
vcr_055d01c76b98
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, 4, -12, -14, 15, -20, 12, -20, 13, 12, 9, 0], "k": 6}, "output": [12, -20, 13, 12, 9, 0, -9, 4, -12, -14, 15, -20]}, {"input": {"nums": [-5, -10, 5, 1, -8, 1, -1, -3], "k": 19}, "output": [1, -1, -3, -5, -10, 5, 1, -8]}, {"input": {"nums": [10, -8, -9], "k": 14}, "output": [-8, -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": "b15012c09fb6b666"}
vcr_c2d8ab9511fc
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": ["ac", "cb", "ac", "cab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bab", "bab", "bba", "aba", "aab", "bab", "abb"]}, "output": [2, 5]}, {"input": {"strs": ["bba", "baa", "bbb", "aba"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "6c97355b0a476afc"}
vcr_8e329d961ecf
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, 3, 2, 3, 2]}, "output": [36, 84, 126, 84, 126]}, {"input": {"nums": [3, 1, 3, 1]}, "output": [3, 9, 3, 9]}, {"input": {"nums": [3, 1, 4]}, "output": [4, 12, 3]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "c5946a19e95d87ca"}
vcr_4037f8630add
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", "bc", "cac", "abbb", "ba", "abc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bba", "baa", "bbb", "baa"]}, "output": [1, 2, 2]}, {"input": {"strs": ["aaa", "bbb", "baa", "abb"]}, "output": [1, 1, 1, 1]}]
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": "120e2e5ee11ffc6b"}
vcr_433970fe3eb3
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, -18, 16, 32, 35], "target": -2}, "output": [1, 2]}, {"input": {"nums": [-16, -15, -1, 1, 8, 10], "target": 9}, "output": [2, 5]}, {"input": {"nums": [-11, -1, 6, 32], "target": 38}, "output": [2, 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": "0856c0db063fc395"}
vcr_cfb1b4e88af6
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": [16, 63, -14, -32, 48, 61, -4, 58, 57, -31], "target": -46}, "output": [2, 3]}, {"input": {"nums": [-7, 39, -6, 41, 35, 25, 28, -1], "target": 38}, "output": [1, 7]}, {"input": {"nums": [-27, 18, 32, 19], "target": 50}, "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": "5020b6f7b1bd5aa6"}
vcr_8f1ec197b914
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": [-17, 4, -1, -19, 26, 35]}, "output": [-17, -13, -14, -33, -7, 28]}, {"input": {"nums": [-6, -6, 6, 5]}, "output": [-6, -12, -6, -1]}, {"input": {"nums": [6, 16, 15]}, "output": [6, 22, 37]}]
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": "9e7a59f161150691"}
vcr_0b497083692c
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": [123, 42, 192, 81, 112, 198, 34, 132, 71, 119, 97, 51, 168, 92, 16, 55]}, "output": false}, {"input": {"nums": [4, 13, 3, 19, 18]}, "output": false}, {"input": {"nums": [19, 16, 7, 5]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "167a900a4920488b"}
vcr_45c1e1872ce4
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": 65427}, "output": 12}, {"input": {"n": 7292}, "output": 8}, {"input": {"n": 9959}, "output": 9}]
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": "1a19681aa436cf8f"}
vcr_42c9fdc9d1d4
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, 1, 2, 0, 2, 3, 3, 3, 0]}, "output": true}, {"input": {"nums": [1, 1, 1, 1, 2]}, "output": true}, {"input": {"nums": [1, 1, 2, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "fd3bd471f1bc4535"}
vcr_c953deada5ce
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, -16, 9, -8, -14, 7, -2, 19, -4], "k": 11}, "output": [19, -4, -8, -16, 9, -8, -14, 7, -2]}, {"input": {"nums": [-1, -7, -6, -2, -4, 4, -8, -8, -1, 4], "k": 12}, "output": [-1, 4, -1, -7, -6, -2, -4, 4, -8, -8]}, {"input": {"nums": [-3, 0, 5, -6, -3, -4, 3, 5], "k": 10}, "output": [3, 5, -3, 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": "dc13491b8d746e71"}
vcr_fa71b6b40843
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": 9321}, "output": 6}, {"input": {"n": 6019}, "output": 7}, {"input": {"n": 8324}, "output": 3}]
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": "07c0f2ddabcb3d9a"}
vcr_0b0a3b9758c6
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-14, -8, 21, 23, 43], "target": 9}, "output": [0, 3]}, {"input": {"nums": [-6, -2, 3, 10, 11, 18, 22], "target": 29}, "output": [4, 5]}, {"input": {"nums": [-19, -18, -17, -8, 5, 16, 18, 26, 27, 31, 35], "target": -36}, "output": [0, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "38563d3a9d8981fc"}
vcr_29d1cee38f5d
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 1, 4, 4, 4, 0, 0]}, "output": true}, {"input": {"nums": [3, 1, 3, 3, 1]}, "output": true}, {"input": {"nums": [3, 2, 2, 1, 3, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "14f432f2ae83ce16"}
vcr_7bb9d68996af
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": [22, 15, 2, 36, 75, 2, 36, 15, 22]}, "output": 75}, {"input": {"nums": [21, 25, 22, 44, 21, 4, 25, 22, 3, 4, 3, 1, 1]}, "output": 44}, {"input": {"nums": [41, 16, 28, 5, 5, 28, 8, 10, 8, 16, 10]}, "output": 41}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "1bff3f3ec8c6bada"}
vcr_a42e149020f0
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "c cdce ead ccaec"}, "output": "ccaec ead cdce c"}, {"input": {"s": "a bb ab baa abba"}, "output": "abba baa ab bb a"}, {"input": {"s": "aba b baa"}, "output": "baa b aba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "36d0bb55ac40a0c2"}
vcr_4dd4d6386977
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": [75, 103, 16, 90, 140, 49, 20, 21, 124, 30, 183, 123, 155, 141, 195, 137, 194]}, "output": false}, {"input": {"nums": [13, 22, 19, 10, 25, 2, 1]}, "output": false}, {"input": {"nums": [2, 16, 0, 22, 8, 16, 12, 17, 25]}, "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": "4cd2a4457b48761c"}
vcr_c66c40435ada
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [10, -18, 18, 0, -6, 12, -14, 0, 9, 17, 10, -8], "k": 1}, "output": [-8, 10, -18, 18, 0, -6, 12, -14, 0, 9, 17, 10]}, {"input": {"nums": [-2, -10, 2, -10, 5, 3, 5, 2, 10, 2], "k": 8}, "output": [2, -10, 5, 3, 5, 2, 10, 2, -2, -10]}, {"input": {"nums": [-8, -7, -1, 10, -10, 9, -1, -10, 3, 6], "k": 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": "942f93bcfe4a8f88"}
vcr_b86de680e55d
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [10, -4, -4, 30, 11, -4, -19, -11, 14, -6, -5, 23, 31, 27]}, "output": [10, 6, 2, 32, 43, 39, 20, 9, 23, 17, 12, 35, 66, 93]}, {"input": {"nums": [-5, 16, -7, 11, 6, 7]}, "output": [-5, 11, 4, 15, 21, 28]}, {"input": {"nums": [14, -5]}, "output": [14, 9]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "342f0914beb1f460"}
vcr_fd30d2a0897b
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-14, 19, -17, -7, 30, 33, 28, -13, 2, -10, 21, -14]}, "output": [-14, 5, -12, -19, 11, 44, 72, 59, 61, 51, 72, 58]}, {"input": {"nums": [-5, -8, 5, 14, -12]}, "output": [-5, -13, -8, 6, -6]}, {"input": {"nums": [7, 3, 15, 2, 11, -2, 13, 5, 15]}, "output": [7, 10, 25, 27, 38, 36, 49, 54, 69]}]
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": "65c212a8880ce3a8"}
vcr_de12f14967c1
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": 230, "b": 109}, "output": 1}, {"input": {"a": 143, "b": 7}, "output": 1}, {"input": {"a": 6, "b": 178}, "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": "492a5bf7f887d565"}
vcr_15c52480938b
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": "cced aaad aaeea ece"}, "output": "ece aaeea aaad cced"}, {"input": {"s": "ab baaa b aab abb"}, "output": "abb aab b baaa ab"}, {"input": {"s": "abab bbb b a"}, "output": "a b bbb abab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "fad846590edab7b3"}
vcr_ca34a257e87f
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": [19, -7, -6, -4, 16, 16, 20, -19, -6, -18], "k": 3}, "output": [-19, -6, -18, 19, -7, -6, -4, 16, 16, 20]}, {"input": {"nums": [-3, 8, -5, -3, 7, 1, -4, 8, 7, 1], "k": 3}, "output": [8, 7, 1, -3, 8, -5, -3, 7, 1, -4]}, {"input": {"nums": [-1, 5, 2, 3, 9], "k": 20}, "output": [-1, 5, 2, 3, 9]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "a0bd0f8ad6fe7276"}
vcr_60908d1e243a
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": [7, 17, 11, 17, 28, 9, 20, 5, 7, 30]}, "output": 96}, {"input": {"nums": [1, 15, 1, 18, 3, 1, 6, 3, 6, 16]}, "output": 55}, {"input": {"nums": [2, 14, 11, 8]}, "output": 22}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "9ed91500df0f9e5e"}
vcr_8c52a3d50b86
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": [19, 32, 54, 57, 77, -28, -26, 35, 73, 18, 27, 79], "target": 45}, "output": [5, 8]}, {"input": {"nums": [44, 2, 42, 14, 16, 24], "target": 66}, "output": [2, 5]}, {"input": {"nums": [39, 28, -5, 1, -26, 25], "target": 67}, "output": [0, 1]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "0bf3be7f1948171e"}
vcr_fe97fc9cf941
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-3, -12, -7, -7, 19, 15, -7, -12, -11, -13, -10, 10, 13, 10]}, "output": 34}, {"input": {"nums": [-4, 3, 7, -12, -8]}, "output": 10}, {"input": {"nums": [13, 9, -11, -10, 9, 0]}, "output": 22}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "d70e2a56b6d7093f"}
vcr_01c9f6086bcb
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": [12, 28, 23, 9]}, "output": 37}, {"input": {"nums": [15, 5, 7]}, "output": 22}, {"input": {"nums": [10, 0, 2, 7, 4, 2, 4, 13, 11, 6]}, "output": 38}]
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": "6fc89301137982e3"}
vcr_4a353d01a4c4
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": [14, 17, 20, 23, 34, 35, 36, 42, 45], "target": 34}, "output": 4}, {"input": {"nums": [1, 9, 12, 13, 18, 19, 24, 25, 34, 38, 39], "target": 34}, "output": 8}, {"input": {"nums": [3, 8, 14, 22, 23, 25, 26, 28, 29, 34], "target": 8}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "0f037b7bdd0af547"}
vcr_76085547a8c8
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": "ac ccba cad bdaece aed cebbe cdce"}, "output": "cdce cebbe aed bdaece cad ccba ac"}, {"input": {"s": "abb aaab aa"}, "output": "aa aaab abb"}, {"input": {"s": "aa aab a a"}, "output": "a a aab aa"}]
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": "61fac103d33c7b92"}
vcr_665d4d621bed
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [18, 0, -4, -10, 10, 2, 8, -17, -6, 18, -16, 10, -10]}, "output": 24}, {"input": {"nums": [1, 12, -6, 2]}, "output": 13}, {"input": {"nums": [-9, -11, -10, 8, 2, 9, 2]}, "output": 21}]
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": "613cdabb0630d436"}
vcr_2fe4c385f3c8
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": [3, 51, 14, 40, 17, 42, 37, 38, 59, 43, 27, 58, 46]}, "output": 56}, {"input": {"prices": [3, 21, 34, 5, 2]}, "output": 31}, {"input": {"prices": [26, 22, 20, 30, 17, 33, 33, 7, 25, 23, 1, 37]}, "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": "7a9d2ce89de9a2f4"}
vcr_52c78d965436
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 6, 7, 6, 2, 1, 5, 7]}, "output": [17640, 14700, 12600, 14700, 44100, 88200, 17640, 12600]}, {"input": {"nums": [3, 2, 1, 1, 2, 1]}, "output": [4, 6, 12, 12, 6, 12]}, {"input": {"nums": [2, 2, 2, 1, 4, 1, 1]}, "output": [16, 16, 16, 32, 8, 32, 32]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "8e2874a4eaf2393a"}
vcr_97473f940095
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-18, 4, 18, 17, 5, -9, -13, 25, -2]}, "output": [-18, -14, 4, 21, 26, 17, 4, 29, 27]}, {"input": {"nums": [-7, 14, -2, 10, 5, -2, 4, -6]}, "output": [-7, 7, 5, 15, 20, 18, 22, 16]}, {"input": {"nums": [0, 13, 0, -1, 5, 5, 18, 9, 10, 14]}, "output": [0, 13, 13, 12, 17, 22, 40, 49, 59, 73]}]
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": "0ebce99b53f2f64b"}
vcr_14c6ffc54f0f
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": [0, 45, 45, 44, 31, 50, 38, 2]}, "output": true}, {"input": {"nums": [6, 16, 25, 11]}, "output": false}, {"input": {"nums": [4, 1, 5, 16, 24, 7, 22, 5, 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": "813414f24dd61942"}
vcr_8f1111d79b53
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": "bbbaba ce bce ccceb aab bdba"}, "output": "bdba aab ccceb bce ce bbbaba"}, {"input": {"s": "bab ab abab aa"}, "output": "aa abab ab bab"}, {"input": {"s": "aa b"}, "output": "b aa"}]
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": "3779f5663b3d28de"}
vcr_a5e75f3712a3
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 1, 2, 2, 3, 3, 4, 6, 7, 11, 13, 14, 15]}, "output": [1, 2, 3, 4, 6, 7, 11, 13, 14, 15]}, {"input": {"nums": [3, 5, 7, 8, 9, 11, 11, 12]}, "output": [3, 5, 7, 8, 9, 11, 12]}, {"input": {"nums": [0, 3, 6, 8]}, "output": [0, 3, 6, 8]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "4bbede052cf9d007"}
vcr_bf0759f10187
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": [16, 25, 22, 22, 9, 24, 11, 10, 1, 27, 30, 8]}, "output": 116}, {"input": {"nums": [3, 7, 9, 10, 13]}, "output": 25}, {"input": {"nums": [16, 1, 9, 16, 18, 11, 5, 5]}, "output": 48}]
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": "88dc920944ff2ca2"}
vcr_4a368855fd0e
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": [139, 25, 144, 20, 11, 82, 6, 194, 67, 109, 186, 165]}, "output": false}, {"input": {"nums": [18, 17, 13, 7, 17, 8, 2, 6]}, "output": true}, {"input": {"nums": [10, 19, 9, 6, 5, 11, 18, 22, 12, 2]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "6e79d6874913f7a6"}
vcr_4451a90222a7
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": "ecbe eeaa eaaca e d"}, "output": "d e eaaca eeaa ecbe"}, {"input": {"s": "aa a bb"}, "output": "bb a aa"}, {"input": {"s": "abba ab bbb aab"}, "output": "aab bbb ab abba"}]
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": "502c3a136028f00b"}
vcr_9ce75467138d
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [25, 11, 5, 25, 20, 2, 12, 6, 0, 18]}, "output": 80}, {"input": {"nums": [17, 15, 4, 2, 8]}, "output": 29}, {"input": {"nums": [11, 3, 12, 12, 0, 7]}, "output": 30}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "9c49e1a2add65398"}
vcr_2a81baabad34
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, 16, 1, 8, 1, 18], "k": 0}, "output": [7, 16, 1, 8, 1, 18]}, {"input": {"nums": [7, 4, 2, 4, 1, -6, 5, 0, 8], "k": 13}, "output": [-6, 5, 0, 8, 7, 4, 2, 4, 1]}, {"input": {"nums": [0, 6, 0, 0, -4, -2, -10, 6, -8, 9], "k": 14}, "output": [-10, 6, -8, 9, 0, 6, 0, 0, -4, -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": "c9c99da5f02df098"}
vcr_ff95d3ab154f
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, -2, 10, 11, 12, 18, 23, 33, 35, 40, 43, 47], "target": 45}, "output": [1, 11]}, {"input": {"nums": [-13, -1, 4, 6, 11, 15, 18, 27, 35, 37], "target": 14}, "output": [0, 7]}, {"input": {"nums": [-15, -10, 4, 14, 16, 19, 24, 27, 30, 39], "target": -6}, "output": [1, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "18f956693960e439"}
vcr_68f159559784
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, 2, 7, 13, 49, 16, 19]}, "output": 47}, {"input": {"prices": [16, 2, 28, 13, 34, 40, 5]}, "output": 38}, {"input": {"prices": [36, 31, 30, 3, 9, 32, 24, 2, 20]}, "output": 29}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "31d610ca53abeac4"}
vcr_036b3b81c2fd
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": [21, 37, 21, 18, 37, 54, 18]}, "output": 54}, {"input": {"nums": [14, 4, 16, 48, 12, 14, 4, 29, 12, 24, 16, 24, 29]}, "output": 48}, {"input": {"nums": [41, 2, 2, 9, 9]}, "output": 41}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "f7e1e88c89e5f5ff"}
vcr_f26648a74fd7
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-7, 0, -4, 7, -6, -1, 22, 7, -8, -8, -7, 4]}, "output": 29}, {"input": {"nums": [-6, -10, 2, 14, 0, -10, 12]}, "output": 18}, {"input": {"nums": [2, -9, 10]}, "output": 10}]
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": "e3d2b2a4ddeaf345"}
vcr_7ccdb1b4e164
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, -6, -5, 5, 22, 42, 47], "target": -11}, "output": [1, 2]}, {"input": {"nums": [-18, -13, -11, -9, -5, -2, 9, 23, 34, 35], "target": -4}, "output": [1, 6]}, {"input": {"nums": [-19, -16, -10, -3, -1, 8, 14, 23, 24, 28], "target": 37}, "output": [6, 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": "54df92e95317c974"}
vcr_5a7a0c748776
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": 216, "b": 7}, "output": 1}, {"input": {"a": 39, "b": 14}, "output": 1}, {"input": {"a": 113, "b": 158}, "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": "04944a627d0c4ec1"}
vcr_8c3e31d10d79
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, 8, 16, 12]}, "output": 20}, {"input": {"nums": [2, 10, 20, 7, 18, 13, 4, 12]}, "output": 52}, {"input": {"nums": [5, 8, 15]}, "output": 20}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "5dbac79989aa79cc"}
vcr_e039863dbbaa
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [13, 15, 37, 13, 7, 15, 22, 22, 67, 9, 7, 37, 9]}, "output": 67}, {"input": {"nums": [26, 1, 7, 18, 43, 1, 7, 18, 26]}, "output": 43}, {"input": {"nums": [28, 28, 23, 23, 21, 46, 21]}, "output": 46}]
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": "066dcb5987faac28"}
vcr_e1b5d2ffb7bc
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [35, 35, 33, 37, 31, 32, 62, 9, 31, 37, 32, 9, 33]}, "output": 62}, {"input": {"nums": [16, 1, 19, 12, 19, 53, 16, 12, 1]}, "output": 53}, {"input": {"nums": [18, 15, 20, 17, 20, 15, 14, 17, 14, 23, 23, 43, 18]}, "output": 43}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "bcd9bf25792fbef2"}
vcr_344fab25e717
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, 11, 6, -16, 8, -9, -17, -14, 13], "k": 10}, "output": [13, -18, 11, 6, -16, 8, -9, -17, -14]}, {"input": {"nums": [6, 1, 9, 10, 5, -7], "k": 1}, "output": [-7, 6, 1, 9, 10, 5]}, {"input": {"nums": [2, -4, -2, -10, 10, 7, 10, 10], "k": 6}, "output": [-2, -10, 10, 7, 10, 10, 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": "51e4d15850526b7b"}