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_d622cf1e5247
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, 11, -16, 9, 8, 4, -5, 8, -9, -3, -4, -1, 21, 12]}, "output": 40}, {"input": {"nums": [-1, 0, -1, 12, 9]}, "output": 21}, {"input": {"nums": [-10, -5, 0, 3, 5, -1]}, "output": 8}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "4b9e418dc95f1148"}
vcr_e79368e8d407
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, 8, -18, -4, -7], "k": 15}, "output": [1, 8, -18, -4, -7]}, {"input": {"nums": [-3, 9, -8, -4, 9, -1], "k": 11}, "output": [9, -8, -4, 9, -1, -3]}, {"input": {"nums": [3, -6, -2, -5, -10, -5, -3], "k": 10}, "output": [-10, -5, -3, 3, -6, -2, -5]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "9df0c890d4e94d47"}
vcr_4edac8d1f9ff
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, 0, 0, 3, 3, 0, 0]}, "output": false}, {"input": {"nums": [2, 2, 1, 2, 3, 3, 3, 3]}, "output": true}, {"input": {"nums": [2, 0, 0, 0, 1, 0, 0, 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": "8c056c5eb00dd506"}
vcr_07be805f616f
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["acc", "ccbc", "caca", "abbb", "acbc", "ca", "aaca"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bab", "bbb", "bba", "aba", "baa"]}, "output": [1, 2, 3]}, {"input": {"strs": ["baa", "abb", "baa"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "eada3618bd9d5a2f"}
vcr_590df9ba279d
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": [50, 94, 171, 64, 1, 80, 89, 72, 81, 82, 128, 133, 119, 83, 51, 93]}, "output": false}, {"input": {"nums": [16, 5, 25, 5, 6, 13, 7, 25, 7, 10, 6, 19]}, "output": true}, {"input": {"nums": [25, 24, 3, 3, 1, 22, 20, 9, 17, 16, 10, 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": "6ea3f180441d28e4"}
vcr_d8e363d57945
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [9, 17, -2, 14, 7, 11, -4, 10, -12, -8]}, "output": 62}, {"input": {"nums": [13, 4, 12, 3, 3, 0, 6, 3, -1]}, "output": 44}, {"input": {"nums": [6, 3, -2, 4, -6, 3]}, "output": 11}]
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": "a181d1968c0630d9"}
vcr_04cb5deafca6
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 3, 5, 6, 13, 14, 23, 24, 26, 37, 47], "target": 47}, "output": 10}, {"input": {"nums": [1, 3, 4, 9, 11, 16, 35, 36], "target": 28}, "output": 6}, {"input": {"nums": [4, 7, 8, 11, 13, 18, 20, 24, 29, 32, 34, 35], "target": 7}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "368ddd77609582df"}
vcr_33bbd8142cdf
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "e dceb dba"}, "output": "dba dceb e"}, {"input": {"s": "a abaa aba"}, "output": "aba abaa a"}, {"input": {"s": "aa abb a bb baa"}, "output": "baa bb a abb aa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "8c66883358b3ad9a"}
vcr_c55d3db68f00
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, -12, -12, 19, 13, 8, -17, 6, -18, 2, 13, 12, 1]}, "output": 40}, {"input": {"nums": [5, -4, 12]}, "output": 13}, {"input": {"nums": [12, 8, -12]}, "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": "2da722cdb3c0d4eb"}
vcr_65d9c41ff3b2
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, 2, -14, 4, -17, -6, -13, 7, 21, -8, -8, -8, 16]}, "output": 28}, {"input": {"nums": [5, 1, -4, -9, -11, -7, 8, 1, -3, -4, -5]}, "output": 9}, {"input": {"nums": [-8, -1, -10, -3, 7, 7, 8, -6, 8, 3]}, "output": 27}]
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": "c11e3c0aecff51d0"}
vcr_2ff433f71fae
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": "abbaaabc"}, "output": 5}, {"input": {"s": "abccbcaccb"}, "output": 8}, {"input": {"s": "aacac"}, "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": "f34b6c2e6256ee87"}
vcr_556351252769
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 7, 13, 16, 18, 31, 32, 36, 39, 45], "target": 34}, "output": 7}, {"input": {"nums": [3, 5, 38], "target": 6}, "output": 2}, {"input": {"nums": [11, 19, 27, 35, 37], "target": 13}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "d0baecded01571bc"}
vcr_c23d11a256c0
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": 65213}, "output": 13}, {"input": {"n": 553}, "output": 4}, {"input": {"n": 7827}, "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": "56e4b915e2ddf63a"}
vcr_827554146f5d
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, 3, 3, 0, 0, 4, 1, 4, 4, 0]}, "output": true}, {"input": {"nums": [3, 1, 1, 2]}, "output": true}, {"input": {"nums": [0, 1, 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": "a650b6bebb40ad58"}
vcr_7ec336fefe82
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": [31, 73, 31, 5, 12, 5, 12, 35, 35]}, "output": 73}, {"input": {"nums": [22, 1, 47, 22, 24, 24, 1]}, "output": 47}, {"input": {"nums": [1, 7, 40, 1, 7, 15, 8, 8, 16, 15, 16]}, "output": 40}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "b5d3e81b1649ab24"}
vcr_f9d79e72c7ce
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, 17, 5, 26, 15]}, "output": 53}, {"input": {"nums": [8, 11, 4, 19]}, "output": 30}, {"input": {"nums": [14, 10]}, "output": 14}]
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": "73300c0edd614bb1"}
vcr_05e589130619
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, 3, 3, 0, 1, 1, 0, 0]}, "output": false}, {"input": {"nums": [2, 3, 2]}, "output": true}, {"input": {"nums": [3, 1, 3, 1, 1, 0, 0, 0]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "99dbf694aa93bdcf"}
vcr_ae728b3810a9
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": "abbbbbabbba"}, "output": 5}, {"input": {"s": "ccbba"}, "output": 3}, {"input": {"s": "babba"}, "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": "c2948388e0d03a13"}
vcr_f25e1164ecc9
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 6, 9, 20, 22, 28, 30, 31, 35, 36, 39, 44, 45], "target": 34}, "output": 8}, {"input": {"nums": [3, 4, 7, 22, 23, 29, 31, 34, 37], "target": 37}, "output": 8}, {"input": {"nums": [4, 5, 7, 9, 11, 16, 18, 20, 25, 27, 35, 38], "target": 28}, "output": 10}]
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": "ad3a8068883b37d7"}
vcr_2d7e3b4a381e
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-20, -4, 5, 2, -3, 14, -3, 16, -11, 15], "k": 16}, "output": [-3, 14, -3, 16, -11, 15, -20, -4, 5, 2]}, {"input": {"nums": [-7, -4, 3, -6, -2], "k": 6}, "output": [-2, -7, -4, 3, -6]}, {"input": {"nums": [5, -4, -8], "k": 0}, "output": [5, -4, -8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "4ce830f6aeff509c"}
vcr_62bc943f0dde
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, 2, 2, 5, 7, 8, 12, 15, 15]}, "output": [1, 2, 5, 7, 8, 12, 15]}, {"input": {"nums": [0, 1, 1, 3, 3, 3, 5, 7, 11, 11, 11]}, "output": [0, 1, 3, 5, 7, 11]}, {"input": {"nums": [0, 2, 4, 5, 7, 9, 11, 12]}, "output": [0, 2, 4, 5, 7, 9, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "9f07c083ec2b8b21"}
vcr_02ca9431f26a
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": [15, 26, 31, -22, -1, -24, -24, -25, 34]}, "output": [15, 41, 72, 50, 49, 25, 1, -24, 10]}, {"input": {"nums": [13, 17, -2, -10, 9, 1, 10, -10, 9, 6]}, "output": [13, 30, 28, 18, 27, 28, 38, 28, 37, 43]}, {"input": {"nums": [1, -6, 14, -1, 16, -5, 12]}, "output": [1, -5, 9, 8, 24, 19, 31]}]
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": "b916af62b9fc1a0c"}
vcr_d37f264eaf4f
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "be ebbeed dadad ebec beebae cde abbd"}, "output": "abbd cde beebae ebec dadad ebbeed be"}, {"input": {"s": "aba babb bbb ba"}, "output": "ba bbb babb aba"}, {"input": {"s": "baab aaab ba abab b"}, "output": "b abab ba aaab baab"}]
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": "7b43a944ec3cbf6c"}
vcr_300dc801e876
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": [6, 12, 15, 19, 27, 29, 31, 33, 40], "target": 32}, "output": 7}, {"input": {"nums": [0, 2, 3, 15, 19, 22, 23, 24, 26, 32], "target": 12}, "output": 3}, {"input": {"nums": [3, 13, 14, 22, 26], "target": 10}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "0540a28a0f225264"}
vcr_f1f04efbb334
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": [14, 46, 20, 21, 22, 4, 11, 60, 60, 19, 20]}, "output": 56}, {"input": {"prices": [12, 34, 35, 1, 34, 17, 8, 29, 31, 23, 3, 1]}, "output": 33}, {"input": {"prices": [35, 16, 2, 24, 2, 3, 21, 19, 5]}, "output": 22}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "02c72c5acd09ed5a"}
vcr_f1c197d210c5
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-7, -14, 16, 12, 12, 10, 3, -14, 17, 9]}, "output": 65}, {"input": {"nums": [5, 10, 12, -6, 12, 4, 6]}, "output": 43}, {"input": {"nums": [-5, -7, -6, -5, 7, 3]}, "output": 10}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "b4a2cf6da9896b31"}
vcr_1c4849599cc8
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-15, 2, -1, -14, -9, -20, -17, -11, -6, -1], "k": 20}, "output": [-15, 2, -1, -14, -9, -20, -17, -11, -6, -1]}, {"input": {"nums": [-8, -4, -1, 5, -6], "k": 6}, "output": [-6, -8, -4, -1, 5]}, {"input": {"nums": [-3, 9, -1, 7, 9, 3, 6, 9, 0], "k": 13}, "output": [3, 6, 9, 0, -3, 9, -1, 7, 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": "0e5898639a65c65d"}
vcr_6eee06d7b31e
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": [6, -13, -16, 17, 11], "k": 0}, "output": [6, -13, -16, 17, 11]}, {"input": {"nums": [-7, -6, -2, 9], "k": 9}, "output": [9, -7, -6, -2]}, {"input": {"nums": [-7, 5, -10, -6, -5, -2, -3, 7], "k": 6}, "output": [-10, -6, -5, -2, -3, 7, -7, 5]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "ed93f6aad248ff20"}
vcr_92153d04824f
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, 1, 7, -11, -16, -8], "k": 5}, "output": [1, 7, -11, -16, -8, 7]}, {"input": {"nums": [-3, -1, 4, -7, 9, -1, -5, -7, -7], "k": 13}, "output": [-1, -5, -7, -7, -3, -1, 4, -7, 9]}, {"input": {"nums": [-10, -1, 0], "k": 18}, "output": [-10, -1, 0]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "bfe370d593a60c71"}
vcr_0a12371241b3
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": [8, 13, 21, 7, 8, 14, 28, 27]}, "output": 70}, {"input": {"nums": [11, 16, 10, 1, 8, 2]}, "output": 29}, {"input": {"nums": [11, 17, 5, 1, 11]}, "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": "deaae178006fb4be"}
vcr_7377e421ba00
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": 118, "b": 70}, "output": 2}, {"input": {"a": 155, "b": 142}, "output": 1}, {"input": {"a": 78, "b": 93}, "output": 3}]
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": "70f48380e8d28e35"}
vcr_5b982b80e452
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, 4, 8, 9, 10, 10, 10, 13, 15, 15]}, "output": [0, 3, 4, 8, 9, 10, 13, 15]}, {"input": {"nums": [3, 6, 10, 12, 12]}, "output": [3, 6, 10, 12]}, {"input": {"nums": [0, 1, 1, 1, 2, 4, 4, 4, 9, 9, 9, 11]}, "output": [0, 1, 2, 4, 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": "7a610abe32905cee"}
vcr_2f712d48d239
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": [11, 15, 7, 25, 7, 17, 3, 12]}, "output": 69}, {"input": {"nums": [4, 8, 20, 8, 8, 18, 5, 17, 9, 16]}, "output": 75}, {"input": {"nums": [18, 16]}, "output": 18}]
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": "69b458ee5df74b1e"}
vcr_8c7bb28f9a87
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": [33, 69, -27, 8, 28, -1, 73], "target": 72}, "output": [5, 6]}, {"input": {"nums": [4, 17, 8, 2], "target": 12}, "output": [0, 2]}, {"input": {"nums": [-30, -25, -15, 29, -21, 46, -7, 24, 41, -9], "target": 17}, "output": [6, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "75ab5fea8cab6250"}
vcr_b0b8c918fe38
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, 33, 32, -2, 7, 33, 29]}, "output": [16, 49, 81, 79, 86, 119, 148]}, {"input": {"nums": [17, 4]}, "output": [17, 21]}, {"input": {"nums": [-9, 18, -4, -5, -5, -3, 11, 13, 11]}, "output": [-9, 9, 5, 0, -5, -8, 3, 16, 27]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "bb0698876a10901a"}
vcr_ba15abb10e57
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [29, 9, 20, 20, 22, 6, 56, 29, 2, 6, 13, 2, 13, 24, 9, 22, 24]}, "output": 56}, {"input": {"nums": [8, 46, 25, 25, 8]}, "output": 46}, {"input": {"nums": [7, 7, 2, 20, 6, 18, 53, 2, 18, 6, 20]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "973ed39bec078544"}
vcr_c7fe459d21dc
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": "dcd bcbd aeebd dc ebddae e"}, "output": "e ebddae dc aeebd bcbd dcd"}, {"input": {"s": "bbaa baa"}, "output": "baa bbaa"}, {"input": {"s": "a bbab baab aba aaba"}, "output": "aaba aba baab bbab a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "204e318924ce8771"}
vcr_1241c1684caf
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": "dbe b"}, "output": "b dbe"}, {"input": {"s": "bbb b"}, "output": "b bbb"}, {"input": {"s": "bb a ab ab"}, "output": "ab ab a bb"}]
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": "8d4c0c7066597cb9"}
vcr_8616547496d7
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": [7, 7, 31, 3, 5, 22, 40, 8, 13, 24, 11, 30]}, "output": 37}, {"input": {"prices": [24, 7, 15, 33, 34]}, "output": 27}, {"input": {"prices": [14, 9, 26, 20, 10, 3, 33, 31, 18]}, "output": 30}]
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": "0138426b75b447d4"}
vcr_17ccf26a893d
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": "ddcdae abbde cedeee"}, "output": "cedeee abbde ddcdae"}, {"input": {"s": "bbbb bb"}, "output": "bb bbbb"}, {"input": {"s": "b ba b b"}, "output": "b b ba 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": "bcd00ca092c1a1d0"}
vcr_a4c873cd4863
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": "baecbc eeb ebabb ad ee"}, "output": "ee ad ebabb eeb baecbc"}, {"input": {"s": "bbaa aaa abab abab"}, "output": "abab abab aaa bbaa"}, {"input": {"s": "bb bbb aa bb"}, "output": "bb aa bbb bb"}]
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": "3a061c992f1d1c34"}
vcr_6cc9e2f27194
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": [9, 12, 13, 32, 38, 40, 43, 45], "target": 20}, "output": 3}, {"input": {"nums": [4, 16, 24, 39], "target": 36}, "output": 3}, {"input": {"nums": [5, 8, 9, 15, 19, 31], "target": 37}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "e4d1c36b336ce7f6"}
vcr_2d30bbdaec57
hard
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "adccccaccccda"}, "output": true}, {"input": {"s": "abcabaacab"}, "output": false}, {"input": {"s": "aba"}, "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": "1dc44b7b244b31f3"}
vcr_3ac93726e657
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": ["bbba", "aa", "ba", "aaca", "acba", "aaa", "baac", "ca"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bbb", "abb", "abb", "bba", "bba", "bab", "baa"]}, "output": [1, 1, 5]}, {"input": {"strs": ["aab", "baa", "bab"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "a0a14197f11b94be"}
vcr_b327b93bc3e0
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 5, 7, 17, 18, 19, 25, 28, 36, 46, 49], "target": 31}, "output": 8}, {"input": {"nums": [2, 8, 13, 17, 19, 20, 21, 24, 28, 31, 36], "target": 5}, "output": 1}, {"input": {"nums": [1, 5, 20, 22, 27, 28, 30], "target": 32}, "output": 7}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "ec2e6688e595d1c1"}
vcr_402f6a291b7b
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, 5, 0, 21, 14, 14, 6]}, "output": 56}, {"input": {"nums": [8, 20, 7, 18, 11, 6]}, "output": 44}, {"input": {"nums": [8, 14, 8, 19, 12, 12]}, "output": 45}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "2460a74a204084a6"}
vcr_a2f32a9b1e04
hard
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "dacadabaabadacad"}, "output": true}, {"input": {"s": "aaabcab"}, "output": false}, {"input": {"s": "bbcacba"}, "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": "43bede44f3a9151a"}
vcr_023c0f91d697
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [24, -13, 2, 25, 33, -4, 31, 14, -10]}, "output": [24, 11, 13, 38, 71, 67, 98, 112, 102]}, {"input": {"nums": [9, 5, 8, 8, -3, 9]}, "output": [9, 14, 22, 30, 27, 36]}, {"input": {"nums": [10, -4, 11]}, "output": [10, 6, 17]}]
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": "326d15ad38d3220d"}
vcr_4230de17670c
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": "bbbbe eaed ddeed aeabe"}, "output": "aeabe ddeed eaed bbbbe"}, {"input": {"s": "b bab a aab"}, "output": "aab a bab b"}, {"input": {"s": "abb b bba"}, "output": "bba b abb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "5a6ea4c39206a8ce"}
vcr_b872b7cc9d01
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, 30, 16, -24, -25, 21, 14, -19, 31, 31, -12]}, "output": [7, 37, 53, 29, 4, 25, 39, 20, 51, 82, 70]}, {"input": {"nums": [16, -8, 3, 17, 1, 16]}, "output": [16, 8, 11, 28, 29, 45]}, {"input": {"nums": [-5, 12, 12, 4, -2, 18, -1, -12, 6]}, "output": [-5, 7, 19, 23, 21, 39, 38, 26, 32]}]
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": "3280f3b1b751f067"}
vcr_9976c25a9578
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [2, 2, 3, 5, 13, 14]}, "output": [2, 3, 5, 13, 14]}, {"input": {"nums": [5, 6, 7, 7, 7, 8, 9, 9, 10, 10]}, "output": [5, 6, 7, 8, 9, 10]}, {"input": {"nums": [0, 1, 3, 5, 5, 10, 12]}, "output": [0, 1, 3, 5, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "bee8c76fbdb11271"}
vcr_74f771ca9e45
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": "cbeb eaacb adac ca dddabc baeb"}, "output": "baeb dddabc ca adac eaacb cbeb"}, {"input": {"s": "ba b bb ab bb"}, "output": "bb ab bb b ba"}, {"input": {"s": "baa a"}, "output": "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": "a721d6301c08ccfe"}
vcr_b743edaa1ca1
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 5, 7, 1, 2, 4, 1, 4]}, "output": [1120, 448, 320, 2240, 1120, 560, 2240, 560]}, {"input": {"nums": [5, 4, 1, 4, 3, 1, 2]}, "output": [96, 120, 480, 120, 160, 480, 240]}, {"input": {"nums": [5, 4, 3, 4, 4, 5, 1]}, "output": [960, 1200, 1600, 1200, 1200, 960, 4800]}]
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": "42364baef33a64d8"}
vcr_6d8e06b6d6de
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, 1, 6, 7, 7, 1, 1, 7]}, "output": [2058, 12348, 2058, 1764, 1764, 12348, 12348, 1764]}, {"input": {"nums": [1, 4, 3]}, "output": [12, 3, 4]}, {"input": {"nums": [3, 2, 3, 3, 2]}, "output": [36, 54, 36, 36, 54]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "b55285f9e657ab54"}
vcr_65cc54c3010c
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": [20, -6, -22, 5, 22, 34, 30, -9]}, "output": [20, 14, -8, -3, 19, 53, 83, 74]}, {"input": {"nums": [-2, 4, -8, 12, 5, 6, 16]}, "output": [-2, 2, -6, 6, 11, 17, 33]}, {"input": {"nums": [-3, 14]}, "output": [-3, 11]}]
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": "9c253cf0be9f030f"}
vcr_316e5cd56289
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [0, -10, -17, 4, 0, -17, -3, 19, 18, -4, -7, -6, -16, 4, 1]}, "output": 37}, {"input": {"nums": [7, -2, 6, 4, 4]}, "output": 19}, {"input": {"nums": [-12, -6, -6]}, "output": -6}]
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": "45334dfb5f59412f"}
vcr_76d004d42788
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": 4, "b": 88}, "output": 4}, {"input": {"a": 128, "b": 48}, "output": 16}, {"input": {"a": 8, "b": 152}, "output": 8}]
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": "7813d084086d1eda"}
vcr_2ccce642f332
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [6, 13, 19, 1, 6, 5, 19, 5, 27, 1, 0, 28]}, "output": 105}, {"input": {"nums": [11, 18, 3, 14, 2, 14, 1, 14]}, "output": 60}, {"input": {"nums": [20, 18, 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": "f8ba2775a9c2de1a"}
vcr_d27b0801529e
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, 13, 16, 20, 22, 23, 27, 31, 42, 43, 45], "target": 33}, "output": 8}, {"input": {"nums": [1, 3, 5, 8, 11, 16, 25, 27, 30, 31], "target": 9}, "output": 4}, {"input": {"nums": [3, 16, 17, 19, 23, 26, 28, 29, 31, 32, 35, 39], "target": 38}, "output": 11}]
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": "a1c8593377d078ea"}
vcr_75bc662747ee
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, -28, -8, 15, 25, 26, 28, 48, 49], "target": 63}, "output": [3, 7]}, {"input": {"nums": [-19, -15, -12, 7, 21, 26, 27, 36, 39], "target": 21}, "output": [1, 7]}, {"input": {"nums": [-12, 1, 2, 10, 29], "target": 31}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "80c9fb35c05e81f1"}
vcr_f813389c4341
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": [39, 86, 27, 121, 30, 173, 85]}, "output": false}, {"input": {"nums": [6, 20, 3, 25, 25, 15, 11, 16, 5, 22]}, "output": true}, {"input": {"nums": [3, 18, 11, 17, 4, 7, 12, 8, 17, 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": "b2446ac5ff722848"}
vcr_d74e3aa0f0d2
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, 5, 15, 6, 6, -15, 10]}, "output": [7, 12, 27, 33, 39, 24, 34]}, {"input": {"nums": [16, -7, -12, 5, -12]}, "output": [16, 9, -3, 2, -10]}, {"input": {"nums": [-4, 18, 12, 17, -7, -12, 15, 12]}, "output": [-4, 14, 26, 43, 36, 24, 39, 51]}]
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": "10f320760c46a766"}
vcr_c4f57c39fcd6
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 1, 3, 5, 14]}, "output": [1, 3, 5, 14]}, {"input": {"nums": [2, 3, 4, 8, 11, 12]}, "output": [2, 3, 4, 8, 11, 12]}, {"input": {"nums": [3, 6, 8]}, "output": [3, 6, 8]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "f3b6958b6539df09"}
vcr_25c5e1b4aeb4
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, 4, 4, 5, 5, 6, 9, 15]}, "output": [0, 2, 4, 5, 6, 9, 15]}, {"input": {"nums": [1, 7, 7, 8, 8, 8, 9, 9, 11, 12]}, "output": [1, 7, 8, 9, 11, 12]}, {"input": {"nums": [1, 7, 9, 10, 12]}, "output": [1, 7, 9, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "b012b0175815dd46"}
vcr_882bfd68389d
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": [25, 7, 1, 25, 3, 3, 31, 52, 13, 7, 13, 1, 31]}, "output": 52}, {"input": {"nums": [25, 2, 9, 20, 6, 48, 2, 9, 25, 13, 13, 6, 20]}, "output": 48}, {"input": {"nums": [3, 3, 11, 29, 11, 5, 29, 57, 5]}, "output": 57}]
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": "9c03ee27908f2911"}
vcr_f68d2c764752
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, 11, 27, 20, 1, 12]}, "output": 60}, {"input": {"nums": [3, 1, 2, 17, 12, 14, 17, 9]}, "output": 43}, {"input": {"nums": [20, 6]}, "output": 20}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "b77d805b04b5fa32"}
vcr_66bb4b5ca481
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["aa", "aa", "acac", "cbbb", "baa", "bcba", "bbbc", "acbc", "cc"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["abb", "bba", "bba", "aaa", "abb"]}, "output": [1, 4]}, {"input": {"strs": ["bab", "abb", "aab"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "40da4016cb7531fd"}
vcr_3477c879b1c7
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [11, 19, -18, 2, -13, 13, -14, 11, -15]}, "output": 30}, {"input": {"nums": [0, -12, -9]}, "output": 0}, {"input": {"nums": [-1, 4, 10, 14, 0, 8, -12]}, "output": 36}]
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": "4fd46a12b9869054"}
vcr_5435c08f51b8
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": "bdc bcae eddda ae ccc"}, "output": "ccc ae eddda bcae bdc"}, {"input": {"s": "bba bba aab"}, "output": "aab bba bba"}, {"input": {"s": "abb bbab b"}, "output": "b bbab abb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "f211ca0475fb05c7"}
vcr_00e172b74aca
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, 13, 24, 27, 35, 38], "target": 18}, "output": 2}, {"input": {"nums": [0, 21, 26, 36], "target": 20}, "output": 1}, {"input": {"nums": [2, 3, 12, 17, 20, 29, 30, 31, 39], "target": 22}, "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": "3e36ecb7bbde6bc2"}
vcr_54120fe0e501
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 8, 17, 21, 23, 28, 35, 42, 45], "target": 30}, "output": 6}, {"input": {"nums": [0, 10, 27, 31, 34, 37], "target": 26}, "output": 2}, {"input": {"nums": [3, 4, 24, 25, 27, 28, 37, 38], "target": 19}, "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": "2b009f26f10f7c71"}
vcr_01a07c3982e3
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, -18, 8, 8, 3, 22, 20, -18, 11]}, "output": [-6, -24, -16, -8, -5, 17, 37, 19, 30]}, {"input": {"nums": [-5, -12]}, "output": [-5, -17]}, {"input": {"nums": [9, 2, -2, 1, 17]}, "output": [9, 11, 9, 10, 27]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "b8fd901521aea485"}
vcr_583c0839155c
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, 16, 12, 17, -9, -9]}, "output": 67}, {"input": {"nums": [5, -8, 3, 6, 3, 12]}, "output": 24}, {"input": {"nums": [3, 6, 8, 1, -6, -1, 6, 9]}, "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": "6f19d63e0a064b54"}
vcr_1f8736ff18a9
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": [41, 114, 22, 137, 91]}, "output": false}, {"input": {"nums": [20, 24, 14, 2, 17]}, "output": false}, {"input": {"nums": [7, 8, 7, 0, 9, 6, 8, 4, 0, 19]}, "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": "7ae62c74fce85336"}
vcr_b62cf35681c7
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": [18, 16, 5, 23]}, "output": 41}, {"input": {"nums": [20, 10, 12, 10, 13, 8, 16, 14, 15, 19]}, "output": 80}, {"input": {"nums": [0, 8, 20, 8, 6, 3, 14, 11, 1]}, "output": 41}]
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": "f83f82bdf60761b2"}
vcr_cd0fd0f5a539
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": ["bccc", "cb", "abca", "caca"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "bbb", "bba", "baa", "bbb", "bbb", "aaa"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["aaa", "bab", "baa", "aab", "bab", "bbb", "bab"]}, "output": [1, 1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "a6ba1c17f232b899"}
vcr_3d9d7d8c9382
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [7, 7, 0, 19, 21, 19, 24, 1]}, "output": 52}, {"input": {"nums": [0, 8, 5, 12, 3, 7]}, "output": 27}, {"input": {"nums": [8, 4, 20, 20]}, "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": "621bfbcbe2984c07"}
vcr_f5cc6334f132
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": "dacdcaaddcbdbbc"}, "output": false}, {"input": {"s": "bbacccba"}, "output": false}, {"input": {"s": "ccaab"}, "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": "383ba616afd04f7d"}
vcr_7d67e45eee72
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, 3, 4, 0, 4, 3, 0, 0]}, "output": true}, {"input": {"nums": [1, 2, 1, 3, 1]}, "output": true}, {"input": {"nums": [1, 1, 0, 2, 2, 1, 2, 0, 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": "5659c256db60d245"}
vcr_8b04586c1828
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 1, 4, 5, 4, 6, 6, 5]}, "output": [14400, 28800, 7200, 5760, 7200, 4800, 4800, 5760]}, {"input": {"nums": [3, 5, 2]}, "output": [10, 6, 15]}, {"input": {"nums": [2, 5, 4, 4, 2]}, "output": [160, 64, 80, 80, 160]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "4492db75b5a442a4"}
vcr_c6833595927d
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, 6, 7, 10, 11, 12, 13, 14, 15, 23, 25, 33, 36, 42], "target": 28}, "output": 11}, {"input": {"nums": [4, 7, 11, 15, 23, 28, 31, 38], "target": 30}, "output": 6}, {"input": {"nums": [2, 8, 9, 16, 30], "target": 1}, "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": "47e2a8134dfacfe7"}
vcr_c92de0779450
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, 20, 26, 31, 36], "target": 38}, "output": 5}, {"input": {"nums": [2, 10, 29, 33], "target": 32}, "output": 3}, {"input": {"nums": [2, 10, 15, 18, 22, 23, 30, 33], "target": 30}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "ea5b67c6c25e9904"}
vcr_2d86df8156d9
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": [71, 20, 69, -2, -9, 49, 27, -7, -22], "target": -29}, "output": [7, 8]}, {"input": {"nums": [4, -7, -12, 37, 17, -2], "target": 10}, "output": [1, 4]}, {"input": {"nums": [7, 27, 0, -30, -4, 38, 49, 22], "target": 29}, "output": [0, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "6e652e7155461208"}
vcr_a81404d42e41
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, 1, 5, 7, 1, 1]}, "output": [35, 105, 21, 15, 105, 105]}, {"input": {"nums": [1, 3, 3, 5]}, "output": [45, 15, 15, 9]}, {"input": {"nums": [5, 2, 3]}, "output": [6, 15, 10]}]
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": "243667957a56a6da"}
vcr_e4df9139bf56
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, 1, 4, 2, 3, 1, 2, 2, 0]}, "output": true}, {"input": {"nums": [3, 2, 0, 2, 2]}, "output": true}, {"input": {"nums": [1, 3, 3, 0, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "283d61aaac7c7df8"}
vcr_b81f77957900
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": "dcced acdae ce bbc ddac"}, "output": "ddac bbc ce acdae dcced"}, {"input": {"s": "bb abbb abaa b"}, "output": "b abaa abbb bb"}, {"input": {"s": "baa b aab ab"}, "output": "ab aab b 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": "2882ab6b6824d363"}
vcr_0ef6bf450382
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": [4, 29, 43, 27, 31, 9, 11, 19, 18, 4]}, "output": true}, {"input": {"nums": [23, 24, 1, 14]}, "output": false}, {"input": {"nums": [20, 22, 20, 7]}, "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": "acb5d789ec1ce140"}
vcr_2df125a29dda
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": "baaccabab"}, "output": 7}, {"input": {"s": "aabbcbbbc"}, "output": 5}, {"input": {"s": "aaaacaaaa"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "a3658223805ecf2a"}
vcr_4ce1b12265fa
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": "adac cd ecabdb adaed abdb ccaac e"}, "output": "e ccaac abdb adaed ecabdb cd adac"}, {"input": {"s": "baa bbba"}, "output": "bbba baa"}, {"input": {"s": "b bab"}, "output": "bab 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": "85176f023ee944a0"}
vcr_d67ea61cfe48
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": [73, 13, 13, 39, 10, 39, 10]}, "output": 73}, {"input": {"nums": [54, 14, 3, 15, 16, 3, 2, 2, 15, 14, 16]}, "output": 54}, {"input": {"nums": [21, 13, 21, 1, 13, 46, 1, 24, 24]}, "output": 46}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "a3857ef4f80b081f"}
vcr_5355564a33fd
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, 34, 3]}, "output": [16, 50, 53]}, {"input": {"nums": [-7, 4, 2, -5, 6, -2, -5, -8, -2, 1]}, "output": [-7, -3, -1, -6, 0, -2, -7, -15, -17, -16]}, {"input": {"nums": [-4, -2, 3, 3, 15]}, "output": [-4, -6, -3, 0, 15]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "8b9769068dd72a4f"}
vcr_b06b6bb00b25
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": [1, 74, 56, -12, 61, 58, -26], "target": 57}, "output": [0, 2]}, {"input": {"nums": [47, -10, 14, 24, -13], "target": 14}, "output": [1, 3]}, {"input": {"nums": [7, 18, 30, -13, 47, 2, 11, 31, 9, -21, 6], "target": 54}, "output": [0, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "1ff48f2050cc3edf"}
vcr_5bf848fb1d0e
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": [-20, 24, 5, -6]}, "output": [-20, 4, 9, 3]}, {"input": {"nums": [0, 10, 2, -1, -6, 18, -9, 3, 12]}, "output": [0, 10, 12, 11, 5, 23, 14, 17, 29]}, {"input": {"nums": [4, 6, 10]}, "output": [4, 10, 20]}]
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": "32ef75aab3158ee4"}
vcr_0794b0026967
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": 51, "b": 79}, "output": 1}, {"input": {"a": 113, "b": 7}, "output": 1}, {"input": {"a": 88, "b": 4}, "output": 4}]
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": "9b740b950bbdbfd6"}
vcr_62771c7326f6
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": "eababd dccdd bc cbdae b ddca"}, "output": "ddca b cbdae bc dccdd eababd"}, {"input": {"s": "baa ba"}, "output": "ba baa"}, {"input": {"s": "baa bbba"}, "output": "bbba 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": "2bfc1b1bdbfc4026"}
vcr_e0e60cc14a09
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": "bbbaabbbbcccaa"}, "output": 5}, {"input": {"s": "bbbacac"}, "output": 5}, {"input": {"s": "cbbcbaca"}, "output": 7}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "670f4b2580fba798"}
vcr_c25ed62244a6
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, 2, 3, 4, 7, 9, 9, 10, 11, 12, 15]}, "output": [0, 2, 3, 4, 7, 9, 10, 11, 12, 15]}, {"input": {"nums": [6, 6, 6, 8, 11, 11, 11]}, "output": [6, 8, 11]}, {"input": {"nums": [2, 4, 5, 7, 9, 9, 11, 11]}, "output": [2, 4, 5, 7, 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": "b1cbcb1722fe4501"}
vcr_da5b502338fc
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [14, 22, 27]}, "output": 41}, {"input": {"nums": [15, 11, 7, 11, 19, 1]}, "output": 41}, {"input": {"nums": [0, 19, 1, 19, 14, 4]}, "output": 42}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "d8d6d7f12fab2096"}
vcr_a0ddad4340d9
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": [184, 107, 74, 80, 91, 94, 191, 68, 199, 28, 67, 169, 54, 192, 122, 55]}, "output": false}, {"input": {"nums": [24, 10, 12, 24, 15, 12, 11, 23, 10, 24, 10, 11]}, "output": true}, {"input": {"nums": [24, 18, 11, 23, 20, 20, 17, 25, 20, 18]}, "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": "93c63ce20cf5a0c2"}
vcr_851243b880f1
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": [5, 14, -2, 12, 23, 3, -9, 10, 20, 19, -12, -17, 28]}, "output": [5, 19, 17, 29, 52, 55, 46, 56, 76, 95, 83, 66, 94]}, {"input": {"nums": [16, 16, 3, -5, 17, -5, -1]}, "output": [16, 32, 35, 30, 47, 42, 41]}, {"input": {"nums": [-2, -2, -2, 3, 8]}, "output": [-2, -4, -6, -3, 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": "299058621e75bf10"}