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_1208def73b36
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": [16, 28, 20, 6, 43, 42, 8, 48, 4, 27, 39, 26, 18, 16]}, "output": true}, {"input": {"nums": [8, 7, 10, 18]}, "output": false}, {"input": {"nums": [7, 19, 16, 18, 16, 18, 18, 12]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "7ac1305976b23488"}
vcr_da1cd9eb607d
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-28, -27, -14, -8, -7, -3, 1, 2, 4, 11, 24, 37], "target": 34}, "output": [5, 11]}, {"input": {"nums": [-19, -13, -8, 8, 17], "target": -27}, "output": [0, 2]}, {"input": {"nums": [-20, -18, 0, 1, 3, 17, 18, 22, 27, 28], "target": 50}, "output": [7, 9]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "c3b5f26bdfac6b5e"}
vcr_3798999836e8
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": [10, 12, 27, 15, 22]}, "output": 59}, {"input": {"nums": [19, 7]}, "output": 19}, {"input": {"nums": [3, 1, 19, 8, 2, 10, 10, 11]}, "output": 43}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "e58e775bcebbf53c"}
vcr_8db5a992a3a1
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": "dbcbaaa"}, "output": false}, {"input": {"s": "bc"}, "output": false}, {"input": {"s": "ccacc"}, "output": true}]
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": "d1f2bcaaed36d905"}
vcr_74d5bc5013fc
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [21, 18, 18, 18, 17, 29, 0, 3]}, "output": 71}, {"input": {"nums": [2, 4, 15, 12, 3]}, "output": 20}, {"input": {"nums": [9, 13, 2]}, "output": 13}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "819d689fe1cfc10b"}
vcr_c139c803660d
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, 0, 0, 4, 4, 2, 2, 2, 1, 0]}, "output": false}, {"input": {"nums": [3, 0, 1, 3, 0, 2, 1, 1]}, "output": true}, {"input": {"nums": [1, 1, 0, 2, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "3c0a9b5a9517c844"}
vcr_dd252b44dcef
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": "ec aeec baedb d a d"}, "output": "d a d baedb aeec ec"}, {"input": {"s": "aa aa a b bb"}, "output": "bb b a aa aa"}, {"input": {"s": "baa a bab bbab abb"}, "output": "abb bbab bab a baa"}]
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": "bc16ee573d0aab60"}
vcr_20ae523643b2
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 0, 2, 3, 1, 2, 0]}, "output": true}, {"input": {"nums": [0, 1, 0, 2, 0, 2, 2, 2]}, "output": false}, {"input": {"nums": [3, 2, 3, 2, 2, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "e2cdc8d212b572ed"}
vcr_986e19ea8645
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": "aae beb"}, "output": "beb aae"}, {"input": {"s": "bab ba"}, "output": "ba bab"}, {"input": {"s": "bbb aaa aaaa bb bbbb"}, "output": "bbbb bb aaaa aaa 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": "1859aa5f233b1ae5"}
vcr_bd5b549b96c7
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [0, 7, 6, 3, 1, 7, 5]}, "output": [4410, 0, 0, 0, 0, 0, 0]}, {"input": {"nums": [5, 4, 1, 3, 1, 2]}, "output": [24, 30, 120, 40, 120, 60]}, {"input": {"nums": [3, 4, 1, 2, 5, 1, 1]}, "output": [40, 30, 120, 60, 24, 120, 120]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "b2c30e2a3e2f2d88"}
vcr_4a328142f8b2
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 8, 15, 28, 40, 44, 47, 48], "target": 2}, "output": 0}, {"input": {"nums": [1, 19, 26, 29], "target": 4}, "output": 1}, {"input": {"nums": [1, 21, 33, 37, 39], "target": 23}, "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": "f834a13a9750c9ec"}
vcr_b5c02db34e12
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [28, 17, 10, 27, 18, 10, 12, 14, 16, 6]}, "output": 85}, {"input": {"nums": [2, 1, 7, 14, 16, 11, 20, 10, 6, 2]}, "output": 51}, {"input": {"nums": [13, 3, 10, 1, 13, 5, 17]}, "output": 53}]
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": "c6afb94c3aa7b024"}
vcr_1c8dc74ef1f3
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": ["bb", "bc", "cab", "ba", "aa", "bbca", "aab", "baba", "bcbc"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bbb", "abb", "bba", "aab"]}, "output": [1, 1, 3]}, {"input": {"strs": ["baa", "aab", "bba", "aaa", "bab", "bba", "abb"]}, "output": [1, 2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "7bd1197b4cbb56ff"}
vcr_9b3e6273a4a6
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-12, 0, 5, -3, 1, 1, 20, -2]}, "output": 24}, {"input": {"nums": [3, -4, -1, -8, -10]}, "output": 3}, {"input": {"nums": [3, 2, 7, 5, -4, -9, 3, 6, 11, -9, -4]}, "output": 24}]
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": "5a1b53bd0d39816e"}
vcr_e17e5bca6fe9
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, 4, -6, 2, 5, 19, 22, 12, -7, 13, -17, -3, -10, -13, 7]}, "output": 66}, {"input": {"nums": [-5, -10, 3, 1, -2, 5, 2, 4, -10, 3, -9]}, "output": 13}, {"input": {"nums": [9, 13, -7, -11, -12]}, "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": "9fcc57ac2da04dea"}
vcr_c186e39773aa
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": "eae e ececdd abdbea bbcbad a"}, "output": "a bbcbad abdbea ececdd e eae"}, {"input": {"s": "ba b"}, "output": "b ba"}, {"input": {"s": "aaba b abab"}, "output": "abab b aaba"}]
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": "769158b44c4c64de"}
vcr_078f781c8d2d
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-22, 24, 29, 25, -24, -16, 2, 10, 28, 19, -6, 3]}, "output": [-22, 2, 31, 56, 32, 16, 18, 28, 56, 75, 69, 72]}, {"input": {"nums": [5, 17, 1, -1, 12, -10, -7, -11, -4, -8]}, "output": [5, 22, 23, 22, 34, 24, 17, 6, 2, -6]}, {"input": {"nums": [1, 3, -6, -10]}, "output": [1, 4, -2, -12]}]
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": "25b12b6581dec870"}
vcr_2b767e5dd0f8
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, -7, 3, -8, -4, 3, 11, 4, -17, 8, -16]}, "output": 18}, {"input": {"nums": [1, -8, -7, -7, 6, 9]}, "output": 15}, {"input": {"nums": [-7, 6, 7, 13, -8, -8, 6, 10, -11, 13, -10]}, "output": 28}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "04a99880c272407c"}
vcr_9cedfc686ace
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": [17, 15, 21, 20, 29]}, "output": 14}, {"input": {"prices": [37, 28, 14, 5, 7, 29, 36]}, "output": 31}, {"input": {"prices": [22, 13, 11, 34, 10, 16, 12]}, "output": 23}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "44ca61cc992b3d54"}
vcr_9d6f11c1e20f
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": "bb e bcbcb"}, "output": "bcbcb e bb"}, {"input": {"s": "b bb a"}, "output": "a bb b"}, {"input": {"s": "abab abb"}, "output": "abb 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": "1654b54dea0e431c"}
vcr_8baadaaa1c2a
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": "ccdce ada"}, "output": "ada ccdce"}, {"input": {"s": "ab bab"}, "output": "bab ab"}, {"input": {"s": "ab b ab b a"}, "output": "a b ab b ab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "4104d857fa9983e3"}
vcr_c4405a1f101e
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, 15, 3, 9, 45, 27, 53]}, "output": 50}, {"input": {"prices": [35, 10, 33, 7]}, "output": 23}, {"input": {"prices": [21, 17, 10]}, "output": 0}]
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": "1924ccedb609735d"}
vcr_e502d6d0d745
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [7, 16, 30, 9, 15, 15]}, "output": [7, 23, 53, 62, 77, 92]}, {"input": {"nums": [11, -4]}, "output": [11, 7]}, {"input": {"nums": [4, 0, -6, 5]}, "output": [4, 4, -2, 3]}]
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": "418f63cefca24176"}
vcr_187fa5702188
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": [-23, -6, -22, -11, 21, -20, 10, 8, 6, -5]}, "output": [-23, -29, -51, -62, -41, -61, -51, -43, -37, -42]}, {"input": {"nums": [16, 4, 12, -12, 8, 12, -5]}, "output": [16, 20, 32, 20, 28, 40, 35]}, {"input": {"nums": [-10, 4, 2, -4, -1, 18, -7]}, "output": [-10, -6, -4, -8, -9, 9, 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": "a36fc3ce15950773"}
vcr_767347c969e7
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [6, 3, 4, 6, 4, 5, 4]}, "output": [5760, 11520, 8640, 5760, 8640, 6912, 8640]}, {"input": {"nums": [4, 1, 5, 4, 1]}, "output": [20, 80, 16, 20, 80]}, {"input": {"nums": [3, 3, 1, 3, 5, 3, 3]}, "output": [405, 405, 1215, 405, 243, 405, 405]}]
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": "21a78c731a1abda7"}
vcr_1cb542d25ad0
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 6, 7, 8, 10, 12, 15, 17, 18, 21, 36, 38, 43], "target": 34}, "output": 10}, {"input": {"nums": [1, 25, 28], "target": 16}, "output": 1}, {"input": {"nums": [0, 12, 14, 19, 21, 26, 28, 29], "target": 14}, "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": "cb3349e6814abee3"}
vcr_a1799c269e9c
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": [50, -26, 17, -17, 48, 41, 66, 38, -11, -31, 52, 45, 44, -30], "target": 18}, "output": [1, 12]}, {"input": {"nums": [-11, -23, 4, -5, -7, -3, 11], "target": -1}, "output": [2, 3]}, {"input": {"nums": [-23, 35, -9, 47, 21, -8, -26, -30], "target": -34}, "output": [5, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "5f7c6f12c652f1b7"}
vcr_8e90919e3b38
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, 27, 17, 16, 16, 0, 22, 13]}, "output": 67}, {"input": {"nums": [10, 8, 9, 12, 9, 1, 2, 11, 4, 3]}, "output": 42}, {"input": {"nums": [20, 8, 12]}, "output": 32}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "83af1f6114e28dea"}
vcr_b3ad7fb851b3
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "caabbbb"}, "output": 3}, {"input": {"s": "ccc"}, "output": 1}, {"input": {"s": "cbaab"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "a4099aa1d5dcd4cf"}
vcr_7345f1e2aa5b
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, 2, 3, 4, 4, 4, 5, 6, 7, 9, 9, 11, 11, 14]}, "output": [1, 2, 3, 4, 5, 6, 7, 9, 11, 14]}, {"input": {"nums": [0, 0, 1, 1, 3, 7, 7, 9]}, "output": [0, 1, 3, 7, 9]}, {"input": {"nums": [0, 2, 9, 10, 11, 12]}, "output": [0, 2, 9, 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": "fafbc7b078abcd13"}
vcr_ab09b9002172
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": [52, -11, 55, 20, -21, -18, -37, 17, 8, 32], "target": 11}, "output": [4, 9]}, {"input": {"nums": [-13, -3, -1, 5, 31, -24, 46, 18, 33], "target": 64}, "output": [6, 7]}, {"input": {"nums": [17, -7, 15, 45, 26], "target": 38}, "output": [1, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "d1a35777e8f114e6"}
vcr_c264e58ede90
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": [-33, 51, -22, 20, 50, 9, 62, 37], "target": 82}, "output": [3, 6]}, {"input": {"nums": [38, -20, -3, 25, -28], "target": 18}, "output": [0, 1]}, {"input": {"nums": [19, -1, 2, 35, 17, -28], "target": 19}, "output": [2, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "066bd6297890e6be"}
vcr_6ceb7251633d
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, -4, 19, -4, 20, 13, -15, 4, 11, 4, -13, 8], "k": 26}, "output": [-13, 8, -8, -4, 19, -4, 20, 13, -15, 4, 11, 4]}, {"input": {"nums": [7, -3, 1], "k": 13}, "output": [1, 7, -3]}, {"input": {"nums": [3, 5, 2], "k": 17}, "output": [5, 2, 3]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "202057f18cd3517b"}
vcr_857ff0a46d79
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-10, 0, -4, 18, -9, -16, 1, 0]}, "output": 18}, {"input": {"nums": [11, -2, -11, 2, -8, -3, 0, 2]}, "output": 11}, {"input": {"nums": [6, -9, 6, -9, -9, 14, -8, 10, 9, 1]}, "output": 26}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "6c880df6a0a8e7fd"}
vcr_6b8a884bb745
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": [-1, 14, -1, 15, 9, -9, 0, -4], "k": 20}, "output": [9, -9, 0, -4, -1, 14, -1, 15]}, {"input": {"nums": [-4, -2, -7], "k": 5}, "output": [-2, -7, -4]}, {"input": {"nums": [3, -3, -3, 0, 9, -1, 4, 1, 5, -9], "k": 14}, "output": [4, 1, 5, -9, 3, -3, -3, 0, 9, -1]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "c3edfdec09d62ab3"}
vcr_b8ca6bf417ad
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [3, 28, -22, -5, -5, -17, 33]}, "output": [3, 31, 9, 4, -1, -18, 15]}, {"input": {"nums": [-12, -11, -1, 12, -11, -2, 4]}, "output": [-12, -23, -24, -12, -23, -25, -21]}, {"input": {"nums": [7, -4, 0, 8, 18, 12, -10, -11, -6, 14]}, "output": [7, 3, 3, 11, 29, 41, 31, 20, 14, 28]}]
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": "44f89841b2c37562"}
vcr_48aafcea1691
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, 7, 5, 3, 3, 1]}, "output": [315, 135, 189, 315, 315, 945]}, {"input": {"nums": [1, 4, 3, 4, 2]}, "output": [96, 24, 32, 24, 48]}, {"input": {"nums": [1, 2, 4, 4, 3]}, "output": [96, 48, 24, 24, 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": "77c110b4e0474bdd"}
vcr_1830d9cefe1e
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "bcbabbaabcbab"}, "output": 11}, {"input": {"s": "cbaabaacac"}, "output": 8}, {"input": {"s": "abbcbcccaaba"}, "output": 8}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "264bb427f2c34e3e"}
vcr_734d0984b9fc
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": "cbaddabccabbab"}, "output": false}, {"input": {"s": "accbcbca"}, "output": false}, {"input": {"s": "aabcabaa"}, "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": "4e993716392bd55b"}
vcr_6b66a48ab968
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "aabcacabbbba"}, "output": 8}, {"input": {"s": "bcbbccbcacb"}, "output": 9}, {"input": {"s": "cccc"}, "output": 1}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "94005830ef7fdeb1"}
vcr_bce80259fb31
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": 16079}, "output": 11}, {"input": {"n": 4398}, "output": 6}, {"input": {"n": 9581}, "output": 8}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "77170cacd465ba28"}
vcr_34bdeac5f89b
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [51, 38, 55, -40, 46, 39, 26, -19, 37, 10, -38, 0], "target": 89}, "output": [0, 1]}, {"input": {"nums": [-12, 22, -30, 29, 7, 13, 8, -9, 49], "target": -1}, "output": [2, 3]}, {"input": {"nums": [-22, -14, 1, 11, -25, -4, 35, 16, -11, 37], "target": 46}, "output": [3, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "abc96251b1924704"}
vcr_366fe66eb392
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": "dabbbbbbbad"}, "output": true}, {"input": {"s": "ccbbabaacc"}, "output": false}, {"input": {"s": "babcbbabca"}, "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": "0d3518fd7ab9c8c5"}
vcr_a66762709d40
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [13, 21, 22, 25, 27, 29, 34, 42, 45, 46], "target": 38}, "output": 7}, {"input": {"nums": [11, 18, 22, 35], "target": 15}, "output": 1}, {"input": {"nums": [0, 2, 3, 9, 12, 14, 18, 20, 24, 29, 33], "target": 22}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "dd3095e03636798e"}
vcr_6b1b3807f597
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, 8, 11, 11, 13, 15, 15, 15]}, "output": [0, 3, 4, 6, 8, 11, 13, 15]}, {"input": {"nums": [1, 2, 2, 5, 5, 7, 9, 10, 12, 12, 12]}, "output": [1, 2, 5, 7, 9, 10, 12]}, {"input": {"nums": [3, 4, 9, 9, 10, 11, 12]}, "output": [3, 4, 9, 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": "83aef0f606902869"}
vcr_68e789447bda
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, 1, 5, 18, 29, 31, 34, 39, 42, 45], "target": 3}, "output": 2}, {"input": {"nums": [3, 18, 23, 30, 34, 35, 36], "target": 9}, "output": 1}, {"input": {"nums": [2, 9, 12, 33], "target": 15}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "688f10bcd9d95c66"}
vcr_24871e81bee6
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": 34816}, "output": 2}, {"input": {"n": 9672}, "output": 6}, {"input": {"n": 9299}, "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": "11005dbb1d7e2d00"}
vcr_a4d2abc49bbe
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": [15, 72, 13, 15, 30, 13, 35, 35, 30, 32, 32]}, "output": 72}, {"input": {"nums": [16, 59, 22, 16, 22]}, "output": 59}, {"input": {"nums": [29, 29, 12, 12, 26, 26, 49]}, "output": 49}]
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": "cd39a9ac20ce31b4"}
vcr_69b41214649a
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": "ddaea d"}, "output": "d ddaea"}, {"input": {"s": "bab aaa b ab ab"}, "output": "ab ab b aaa bab"}, {"input": {"s": "b b"}, "output": "b b"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "616c95477c5588e1"}
vcr_735affa085ed
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": [-6, 5, 18, 24, -11, 6]}, "output": [-6, -1, 17, 41, 30, 36]}, {"input": {"nums": [-1, 3, 13]}, "output": [-1, 2, 15]}, {"input": {"nums": [-9, 6, 7, 0, 4, -9, 10, -4, 17, -8]}, "output": [-9, -3, 4, 4, 8, -1, 9, 5, 22, 14]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "8f43cc52e11ab24a"}
vcr_6a8eb6eea511
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, 19, 27, -1, -25, -8, -22, 24, 35, 7, -2]}, "output": [16, 35, 62, 61, 36, 28, 6, 30, 65, 72, 70]}, {"input": {"nums": [6, 18, -7, 6, -9, 9, -9, -5, 6]}, "output": [6, 24, 17, 23, 14, 23, 14, 9, 15]}, {"input": {"nums": [10, -3, 4, 0, -2, 11, 15]}, "output": [10, 7, 11, 11, 9, 20, 35]}]
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": "bdcd1d204145549d"}
vcr_af47bff24211
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": [-8, -10, -11, 17, 26, -18, 29, 16, -21, -1, 25, 7]}, "output": [-8, -18, -29, -12, 14, -4, 25, 41, 20, 19, 44, 51]}, {"input": {"nums": [-8, 6, 4, 14, 12, 0, 2, 13, 17]}, "output": [-8, -2, 2, 16, 28, 28, 30, 43, 60]}, {"input": {"nums": [-6, -1, -12, 10, 14, 0]}, "output": [-6, -7, -19, -9, 5, 5]}...
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "d56004143110cd42"}
vcr_bd65fb87081e
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, 7, 39, 34, 39, 35, 60, 28, 13, 25, 34, 25, 28, 7, 13]}, "output": 60}, {"input": {"nums": [23, 53, 8, 23, 17, 22, 8, 22, 9, 9, 17]}, "output": 53}, {"input": {"nums": [2, 4, 4, 2, 50]}, "output": 50}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "7bc679af9a3f449c"}
vcr_390b7d61e47f
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": [30, 22, 23, 3, 6, 18, 25, 19, 5]}, "output": 90}, {"input": {"nums": [3, 4, 16, 14, 20, 20, 20]}, "output": 59}, {"input": {"nums": [12, 3, 16, 1, 10, 19, 2]}, "output": 47}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "744647a25218ce12"}
vcr_68c7854ef053
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": [-18, -5, 70, -10, -14, 27, 64, 28, 65, 40, 75, 8, 78], "target": 106}, "output": [7, 12]}, {"input": {"nums": [9, 36, -4, 33], "target": 69}, "output": [1, 3]}, {"input": {"nums": [-6, -13, 30, 42, 14, -27, 13, -1], "target": 72}, "output": [2, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "11cd738e488d5576"}
vcr_7e29dd20280f
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-4, 20, -20, -5, 14, 9, -19], "k": 19}, "output": [-20, -5, 14, 9, -19, -4, 20]}, {"input": {"nums": [7, -9, 4], "k": 11}, "output": [-9, 4, 7]}, {"input": {"nums": [-10, -2, -6, -1, -5], "k": 2}, "output": [-1, -5, -10, -2, -6]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "c648ba872a7a3377"}
vcr_99d0b9f1d847
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [26, 39, 35, 26, 22, 38, 22, 39, 24, 35, 20, 50, 38, 20, 24]}, "output": 50}, {"input": {"nums": [17, 21, 17, 48, 21]}, "output": 48}, {"input": {"nums": [51, 26, 3, 17, 17, 8, 14, 26, 14, 8, 3]}, "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": "fdd5802aadf4c643"}
vcr_0b58b8efbebe
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": 96, "b": 226}, "output": 2}, {"input": {"a": 23, "b": 170}, "output": 1}, {"input": {"a": 61, "b": 22}, "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": "2d7622952445c15f"}
vcr_4ab380afb89e
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": [48, 2, 2, 34, 23, 32, 48, 32, 36]}, "output": true}, {"input": {"nums": [0, 10, 15, 6, 12, 7, 11, 9, 4]}, "output": false}, {"input": {"nums": [15, 21, 10, 25, 14, 17, 14, 10, 6, 3, 11]}, "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": "55f32f3f71e04801"}
vcr_7dceea494911
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": [28, 10, 31, 6, 19, 30, 34, 35, 34, 28]}, "output": true}, {"input": {"nums": [0, 8, 22, 1, 12, 10, 21, 7, 6, 14]}, "output": false}, {"input": {"nums": [0, 2, 1, 13, 23]}, "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": "9c7bf05a4be95ba2"}
vcr_aae8a73bcf6a
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [4, 6, 1, 4, 6, 1, 2, 5]}, "output": [1440, 960, 5760, 1440, 960, 5760, 2880, 1152]}, {"input": {"nums": [4, 4, 4, 3, 2, 5, 4]}, "output": [1920, 1920, 1920, 2560, 3840, 1536, 1920]}, {"input": {"nums": [3, 1, 2]}, "output": [2, 6, 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": "e924b1ed6cfdfe7c"}
vcr_86e10b2751f6
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, 22, 15, -15, 12, 19, -16, 17, 22, 15, 12, 6, -17]}, "output": 119}, {"input": {"nums": [-2, 1, 8, -7, 8, -9, -6, 12, 11, 7, 14]}, "output": 44}, {"input": {"nums": [10, 3, -4, 7, -7, -11, 11, -8, 12, 3, 2]}, "output": 20}]
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": "15d916655749ebd3"}
vcr_fc701a4df57f
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": [-40, -5, 33, -38, -2], "target": -40}, "output": [3, 4]}, {"input": {"nums": [27, 20, 33, 19, -11, -29], "target": -2}, "output": [0, 5]}, {"input": {"nums": [47, 41, 48, 20, 45, 25], "target": 45}, "output": [3, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "b914a2fad2841f4b"}
vcr_cef31caff9c2
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, 14, -8, -4, 18, 9], "k": 13}, "output": [9, 7, 14, -8, -4, 18]}, {"input": {"nums": [0, 4, 10, 5, 3], "k": 3}, "output": [10, 5, 3, 0, 4]}, {"input": {"nums": [-9, 7, -1, 5, -7, -1, 2, -1], "k": 12}, "output": [-7, -1, 2, -1, -9, 7, -1, 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": "4b39dfc58104e10e"}
vcr_583737a6454d
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 4, 5, 10, 13, 13, 14, 14]}, "output": [3, 4, 5, 10, 13, 14]}, {"input": {"nums": [2, 3, 9]}, "output": [2, 3, 9]}, {"input": {"nums": [0, 2, 3, 3, 3, 8, 9, 11]}, "output": [0, 2, 3, 8, 9, 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": "5df42e0a7cc01493"}
vcr_bc50ce42239e
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, 12, 7, 29, 1, 21, 13, 22, 27]}, "output": 102}, {"input": {"nums": [14, 8, 14, 6]}, "output": 28}, {"input": {"nums": [4, 2, 3, 8]}, "output": 12}]
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": "eb21ce0bb51dceb7"}
vcr_ef8c31a708bb
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": [46, 39, 2, 21, 41, 6, 33, 31, 26, 25, 45, 6, 27, 12, 13]}, "output": true}, {"input": {"nums": [20, 23, 16, 22, 2, 4, 3]}, "output": false}, {"input": {"nums": [7, 5, 5, 8]}, "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": "eb544cef1a41138f"}
vcr_e45d92617abd
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": "dbdbce b cbac cdabb ccdac eeeeea"}, "output": "eeeeea ccdac cdabb cbac b dbdbce"}, {"input": {"s": "aaab abb bbaa aba baa"}, "output": "baa aba bbaa abb aaab"}, {"input": {"s": "abbb baba"}, "output": "baba abbb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "601b267ea88a1d60"}
vcr_ccc5d7771f98
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": ["aba", "cba", "aaac", "abc", "cac", "cba"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["baa", "aba", "abb", "aaa", "aba", "bba", "baa"]}, "output": [1, 2, 4]}, {"input": {"strs": ["aba", "aaa", "aba", "bbb"]}, "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": "bab3df4ea031fe3b"}
vcr_1152b573d169
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [22, 3, 6, 6, -14, -18, -2, -14, 9, 22, 7, -18, 22, 13, -6]}, "output": 55}, {"input": {"nums": [11, -1, -5, -12, 14, -10, 2, -12, 5]}, "output": 14}, {"input": {"nums": [-10, -11, 5, 4]}, "output": 9}]
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": "bae18c848b3f7ab6"}
vcr_a470052d46cb
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": [79, 95, 178, 100, 109, 32, 136, 175, 30, 28, 14, 111]}, "output": false}, {"input": {"nums": [16, 2, 0, 23, 4, 6, 9, 19, 6, 21, 17]}, "output": true}, {"input": {"nums": [15, 15, 22]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "a180355ed14eaa9e"}
vcr_e2b221312ec5
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-22, 3, 4, 8, 13, 14, 19, 24, 42, 49], "target": 32}, "output": [3, 7]}, {"input": {"nums": [-17, -15, 1, 6, 9, 13, 22, 24, 31, 33], "target": 9}, "output": [1, 7]}, {"input": {"nums": [7, 20, 25, 31], "target": 45}, "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": "576f71fecc1223c0"}
vcr_5dd92833a9b7
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -27, -9, 3, 6, 9], "target": -36}, "output": [1, 2]}, {"input": {"nums": [-17, -15, 5, 14, 16, 24, 39], "target": 53}, "output": [3, 6]}, {"input": {"nums": [-9, -7, 2, 24], "target": -5}, "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": "f1f7dc8f76bd8eb4"}
vcr_c7a5ddd3115d
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": 35082}, "output": 5}, {"input": {"n": 6260}, "output": 6}, {"input": {"n": 3381}, "output": 7}]
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": "cb641744c453cde1"}
vcr_0687c32f2dad
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": [11, 7, -20, -13, -20, -14, 10, 1, -5, 19], "k": 5}, "output": [-14, 10, 1, -5, 19, 11, 7, -20, -13, -20]}, {"input": {"nums": [-8, 1, -5], "k": 20}, "output": [1, -5, -8]}, {"input": {"nums": [0, 1, -4, -2, -9, -3, 7, -10], "k": 11}, "output": [-3, 7, -10, 0, 1, -4, -2, -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": "2cd4dc0025b41b69"}
vcr_c7b5fdb2bc78
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": [17, 18, 19, 21, 46, 48], "target": 23}, "output": 4}, {"input": {"nums": [0, 2, 17, 18, 23, 24, 26, 32], "target": 16}, "output": 2}, {"input": {"nums": [0, 1, 3, 8, 13, 17, 31], "target": 0}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "27e7d61718bd6322"}
vcr_4e5bd2a555e3
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-22, -18, 10, 16, 24, 36], "target": 34}, "output": [2, 4]}, {"input": {"nums": [-4, 17, 18, 23], "target": 35}, "output": [1, 2]}, {"input": {"nums": [-14, 5, 8, 9, 20, 21, 28, 39], "target": 13}, "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": "0c71aa54124cb172"}
vcr_7e832026ebee
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, 5, 8, 8, 9, 9, 10, 11, 14]}, "output": [0, 3, 5, 8, 9, 10, 11, 14]}, {"input": {"nums": [3, 4, 5, 7, 9, 10, 10, 10, 12]}, "output": [3, 4, 5, 7, 9, 10, 12]}, {"input": {"nums": [5, 5, 6]}, "output": [5, 6]}]
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": "0ba81986b0ffd9ad"}
vcr_56235111593f
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [27, 15, 15]}, "output": 42}, {"input": {"nums": [12, 20, 4, 4, 6, 14, 9, 14]}, "output": 52}, {"input": {"nums": [16, 3, 4, 12, 9, 20, 11]}, "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": "7afa15cb83fe5bb6"}
vcr_3e3cf64d65b8
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 2, 6, 6, 6, 9, 11, 15]}, "output": [0, 2, 6, 9, 11, 15]}, {"input": {"nums": [0, 0, 2, 3, 3, 5, 5, 7, 12]}, "output": [0, 2, 3, 5, 7, 12]}, {"input": {"nums": [1, 1, 4, 6, 6, 7, 7, 7, 10, 11]}, "output": [1, 4, 6, 7, 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": "ff02da82160fe66f"}
vcr_3d6a1d23e782
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [22, -22, 15, 14, 17, 5]}, "output": [22, 0, 15, 29, 46, 51]}, {"input": {"nums": [11, 12]}, "output": [11, 23]}, {"input": {"nums": [-6, -7, -5, 18, 15, 16, 0, 3]}, "output": [-6, -13, -18, 0, 15, 31, 31, 34]}]
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": "f7ebb148a4b32446"}
vcr_b434f65096c3
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": [84, 145, 72, 90, 106, 172]}, "output": false}, {"input": {"nums": [4, 0, 0, 9, 18]}, "output": true}, {"input": {"nums": [22, 20, 3, 15, 5, 9, 5, 9, 16]}, "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": "7eb7afc11ce7d4b2"}
vcr_ef6561822518
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [23, 11, 6]}, "output": 29}, {"input": {"nums": [3, 18, 11, 8, 13, 18, 17, 7, 18]}, "output": 66}, {"input": {"nums": [0, 16, 12, 12]}, "output": 28}]
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": "f09075ec50707c54"}
vcr_f021538bd240
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, -27, -21, 1, 17, 35, 46], "target": -29}, "output": [0, 3]}, {"input": {"nums": [-17, -15, -4, 2, 20, 29, 38], "target": 16}, "output": [2, 4]}, {"input": {"nums": [-19, -15, -13, 1, 7, 9, 13, 24, 28, 34, 37], "target": 43}, "output": [5, 9]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "e8cfc67d56c9776d"}
vcr_b3b8f35a1311
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 4, 2, 2, 4, 1, 3, 0, 1, 0]}, "output": true}, {"input": {"nums": [0, 1, 2]}, "output": false}, {"input": {"nums": [2, 2, 2, 2, 2, 1, 3, 0, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "0e0a3eea4c5b4173"}
vcr_ac26e10bf8cc
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": [17, 17, 28, 1, 60, 49, 60, 44, 49, 20, 27, 1, 52, 14, 56, 36]}, "output": 59}, {"input": {"prices": [8, 10, 1, 7, 29, 37]}, "output": 36}, {"input": {"prices": [1, 15, 7, 13, 31, 13, 22, 32, 27]}, "output": 31}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "388ae1b733083273"}
vcr_81ba19c62ef6
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": [-6, 2, 12, 20, 32, 36, 38, 45], "target": 52}, "output": [3, 4]}, {"input": {"nums": [-14, -4, 2, 10, 20], "target": 22}, "output": [2, 4]}, {"input": {"nums": [-17, -10, -3, 14, 16, 19, 29, 34, 35], "target": -20}, "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": "850e36b19786debb"}
vcr_3f01c7742c28
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [17, 7, 4, 23, 10]}, "output": 40}, {"input": {"nums": [2, 20, 7, 5, 0, 13]}, "output": 38}, {"input": {"nums": [9, 1, 15, 5]}, "output": 24}]
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": "360c7d6cf3798de8"}
vcr_c9c938160dc7
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, 32, 14, 23]}, "output": [16, 48, 62, 85]}, {"input": {"nums": [-4, 16, 16, -12, 12, -3, -3, 5, 9]}, "output": [-4, 12, 28, 16, 28, 25, 22, 27, 36]}, {"input": {"nums": [17, 4, -12, 10, -1]}, "output": [17, 21, 9, 19, 18]}]
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": "9159293f782a1ba5"}
vcr_6434946dbe76
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, 2, 5, 7, 9, 15, 22, 28, 30, 32, 42, 44, 48], "target": 12}, "output": 5}, {"input": {"nums": [1, 6, 10, 17, 35, 39], "target": 21}, "output": 4}, {"input": {"nums": [5, 18, 24, 26, 29, 34, 35], "target": 31}, "output": 5}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "efa4d64349467960"}
vcr_70c9a60a1940
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, 5, 4, 7, 4, 2, 3]}, "output": [3360, 3360, 4200, 2400, 4200, 8400, 5600]}, {"input": {"nums": [1, 4, 2, 4, 5]}, "output": [160, 40, 80, 40, 32]}, {"input": {"nums": [2, 3, 1, 2]}, "output": [6, 4, 12, 6]}]
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": "c8af2fe7086a3454"}
vcr_2bfa5894aa95
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [22, 18, -9, 8, -18, -7, 17, 14]}, "output": 45}, {"input": {"nums": [4, -12, -12, 14, -12, 0]}, "output": 14}, {"input": {"nums": [-6, 3, -7, 4, 5, 7, -12, -11, -11, 7]}, "output": 16}]
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": "81a5db2218c8d6d6"}
vcr_643b69e99a0f
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, -27, -26, -24, -21, -9, -8, 24, 25, 26, 30], "target": -4}, "output": [0, 9]}, {"input": {"nums": [-18, -7, 2, 18, 21], "target": -16}, "output": [0, 2]}, {"input": {"nums": [-14, 3, 4, 9, 10, 28, 34], "target": 32}, "output": [2, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "ea370bffa06a6b58"}
vcr_18109775c1fe
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 3, 2, 3, 4, 1, 1, 0]}, "output": true}, {"input": {"nums": [2, 2, 0, 3]}, "output": true}, {"input": {"nums": [1, 0, 3, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "dbb2fe526daa58a6"}
vcr_503ec5d879ad
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [57, -3, 37, 9, 6, 24, 12, -27, -40, 64, 42], "target": 70}, "output": [4, 9]}, {"input": {"nums": [8, 23, 43, -11], "target": 51}, "output": [0, 2]}, {"input": {"nums": [45, -24, 10, 31, -22], "target": 76}, "output": [0, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "201685481a40e310"}
vcr_b2f6d31f66d4
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [2, -6, -14, 18, -7, 3]}, "output": 18}, {"input": {"nums": [-4, -1, 13, -6, 6, -11, 12, -8]}, "output": 14}, {"input": {"nums": [0, 3, -6, 10, 4, 10, -7, -9, 1, 14]}, "output": 24}]
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": "f9251461f9ac6272"}
vcr_88f47441023c
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [41, 19, 16, 18, 17, 51, 13, 18, 8, 51]}, "output": 43}, {"input": {"prices": [22, 15, 19, 9, 15, 13, 35, 9]}, "output": 26}, {"input": {"prices": [17, 30, 7, 14, 38, 17]}, "output": 31}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "813312e9bd4a746f"}
vcr_7b26fc808214
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 3, 5, 6, 7, 7, 9, 10, 11, 11, 11]}, "output": [0, 2, 3, 5, 6, 7, 9, 10, 11]}, {"input": {"nums": [2, 3, 4, 8, 11, 12]}, "output": [2, 3, 4, 8, 11, 12]}, {"input": {"nums": [0, 1, 1, 2, 3, 7, 7, 8, 9, 10, 11]}, "output": [0, 1, 2, 3, 7, 8, 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": "33ae12a7dfe4145f"}
vcr_350f30bee4a6
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [10, 16, 17, 21, 29, 32, 35, 39, 43], "target": 26}, "output": 4}, {"input": {"nums": [2, 4, 7, 14, 19, 21, 28, 30, 33], "target": 2}, "output": 0}, {"input": {"nums": [9, 11, 18], "target": 33}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "43ec78118a0cbdab"}
vcr_57c50f38d666
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": [-11, -16, 16, -9, -11, -18, -19, 11, -19, -15], "k": 24}, "output": [-19, 11, -19, -15, -11, -16, 16, -9, -11, -18]}, {"input": {"nums": [1, -9, -6, 3], "k": 4}, "output": [1, -9, -6, 3]}, {"input": {"nums": [-6, 9, 4, 3], "k": 8}, "output": [-6, 9, 4, 3]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "350ac2dfa06f604b"}