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_9670dff329d9
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": "eaad c"}, "output": "c eaad"}, {"input": {"s": "bbb a a a aab"}, "output": "aab a a a bbb"}, {"input": {"s": "abbb a"}, "output": "a abbb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "0757a014c36c98e6"}
vcr_891d167b95e4
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": [3, 16, 2, 25, 12, 3, 13]}, "output": 54}, {"input": {"nums": [1, 0, 17]}, "output": 18}, {"input": {"nums": [3, 11, 13, 9, 5, 0, 11, 4]}, "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": "a1c7f847fe2ea431"}
vcr_29f8cc4c6b79
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-28, -21, -18, -16, -2, 17, 20, 28, 36, 37, 48], "target": -34}, "output": [2, 3]}, {"input": {"nums": [-17, -16, -6, 27, 28], "target": -22}, "output": [1, 2]}, {"input": {"nums": [-12, -6, 6, 10, 23, 24, 27, 32, 33, 34], "target": 42}, "output": [3, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "e41d021716380f86"}
vcr_3d7ad9aeacce
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 2, 5, 21, 28, 29], "target": 17}, "output": 3}, {"input": {"nums": [1, 8, 9, 13, 16, 24, 29, 30, 33, 36], "target": 29}, "output": 6}, {"input": {"nums": [3, 9, 27], "target": 39}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "2fd08fdc02fe47de"}
vcr_f5d7ec49b8ac
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [30, -20, -7, -18, 21, -25, 16, 23, 24, 33, 25, 29]}, "output": [30, 10, 3, -15, 6, -19, -3, 20, 44, 77, 102, 131]}, {"input": {"nums": [-4, 0, 3, -5, 15, 3, -11]}, "output": [-4, -4, -1, -6, 9, 12, 1]}, {"input": {"nums": [7, 17]}, "output": [7, 24]}]
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": "862a444e78371b0a"}
vcr_1a4e952407fc
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [8, 12, 13, 14, 17, 19, 20, 26, 28, 31, 46, 47], "target": 2}, "output": 0}, {"input": {"nums": [11, 16, 17, 21, 27, 28, 31, 33, 35, 37], "target": 26}, "output": 4}, {"input": {"nums": [5, 8, 12, 16, 18, 20, 21, 25, 27, 28, 33, 34], "target": 32}, "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": "71c53937fe8aa0e3"}
vcr_80e3d5c9ff18
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [22, 16, 15, 6, 10, 28, 19]}, "output": 66}, {"input": {"nums": [2, 15]}, "output": 15}, {"input": {"nums": [9, 15, 3, 1, 8, 8, 9, 16]}, "output": 40}]
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": "df59e746cb82519c"}
vcr_b7b21c1f381c
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": [11, 1, 79, -19, 62, 40, -36, 49, -22], "target": -58}, "output": [6, 8]}, {"input": {"nums": [-1, 21, 18, -29, -12, -6, 46, 40, 23, 13], "target": 59}, "output": [6, 9]}, {"input": {"nums": [11, 17, 28, 0, -26], "target": 45}, "output": [1, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "11d9a57367d38b11"}
vcr_f23aa72977f4
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": "bbbaabbbbbbbb"}, "output": 3}, {"input": {"s": "bbcb"}, "output": 3}, {"input": {"s": "ccaaacccc"}, "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": "e248cd19c0835184"}
vcr_54bfc339cedb
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [10, 12, 14, 22, 30, 31, 42, 48], "target": 40}, "output": 6}, {"input": {"nums": [15, 18, 20, 22, 25, 26], "target": 3}, "output": 0}, {"input": {"nums": [5, 10, 11, 18, 28, 39], "target": 31}, "output": 5}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "b9cbb3d855e37a0e"}
vcr_a657431b9d7e
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": 132, "b": 3}, "output": 3}, {"input": {"a": 84, "b": 170}, "output": 2}, {"input": {"a": 179, "b": 134}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "fa4319436b01052e"}
vcr_6d3d9b465d31
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": "dabdcadbabdccdb"}, "output": false}, {"input": {"s": "ccabc"}, "output": false}, {"input": {"s": "bcccbbb"}, "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": "4ec0ae4ca05ee895"}
vcr_2f5ddea7c60c
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": [33, 28, 26, 28, 24, 33, 26, 69, 24]}, "output": 69}, {"input": {"nums": [1, 15, 24, 15, 18, 54, 1, 18, 4, 24, 27, 27, 4]}, "output": 54}, {"input": {"nums": [59, 26, 28, 3, 28, 26, 3]}, "output": 59}]
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": "1c1c35dc78b0c168"}
vcr_49a309d0cae3
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, -22, -21, -19, -9, -7, 2, 9, 29, 42, 43], "target": 13}, "output": [0, 10]}, {"input": {"nums": [-19, -15, 2, 3, 6, 22, 27, 36, 39], "target": 5}, "output": [2, 3]}, {"input": {"nums": [0, 4, 21, 26, 30, 36], "target": 62}, "output": [3, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "82eec4f474c18510"}
vcr_ded5286580db
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": ["ba", "ab", "cca", "ca", "aa", "bcab", "acaa", "ccc", "cb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "aab", "aab", "abb", "bba"]}, "output": [1, 2, 2]}, {"input": {"strs": ["baa", "aaa", "aba", "bbb", "aab", "aaa", "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": "b7627012d54bde98"}
vcr_89284c4856e1
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": [37, 2, 22, 35, 47, 20, 37]}, "output": true}, {"input": {"nums": [18, 21, 5, 24, 15, 18, 24, 4, 1, 2, 16, 5]}, "output": true}, {"input": {"nums": [8, 21, 12]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "16b49cb5ac0880ca"}
vcr_4f06b4c7a176
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["caba", "aaa", "babc", "ba", "cc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bbb", "aaa", "bbb", "bba", "bba"]}, "output": [1, 2, 3]}, {"input": {"strs": ["bba", "abb", "aab", "abb"]}, "output": [1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "a4188967c3739daa"}
vcr_eedf310c4523
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": [19, 25, 74, 11, 19, 25, 17, 23, 28, 17, 11, 23, 28]}, "output": 74}, {"input": {"nums": [7, 6, 7, 19, 27, 44, 27, 19, 6]}, "output": 44}, {"input": {"nums": [22, 13, 21, 25, 13, 59, 6, 22, 21, 6, 25]}, "output": 59}]
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": "284f93bd3b121119"}
vcr_862a34707696
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, 4, 4, 6, 3]}, "output": [1440, 576, 720, 720, 480, 960]}, {"input": {"nums": [5, 5, 4, 4]}, "output": [80, 80, 100, 100]}, {"input": {"nums": [5, 3, 5, 5, 3, 1]}, "output": [225, 375, 225, 225, 375, 1125]}]
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": "f09a8735360fbfe5"}
vcr_9e23d8c04cba
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 7, 15, 22, 32, 33, 34, 43, 44, 45], "target": 17}, "output": 3}, {"input": {"nums": [6, 7, 12, 15], "target": 38}, "output": 4}, {"input": {"nums": [2, 3, 5, 10, 17, 19, 24, 27, 30, 32, 38], "target": 31}, "output": 9}]
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": "baa3fdaf38d6d88e"}
vcr_eea26453b26f
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": [14, 18, 20, 1, -2, 10, -10, 1, -3, 12, -14, 12, 0, 1]}, "output": 61}, {"input": {"nums": [-8, 2, 14, -12, 2, -2, -8, 5]}, "output": 16}, {"input": {"nums": [6, 9, -6, 13, 14, -8, 12, -8, -5, 7, -12]}, "output": 40}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "d755e70170359117"}
vcr_5d613540306c
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, -12, -10, 8, 9, -4]}, "output": 17}, {"input": {"nums": [2, 11, 5, 1, -7, -3, 0, 5]}, "output": 19}, {"input": {"nums": [5, -12, -8, -5, -6, 9, -2, -3, 9, -4]}, "output": 13}]
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": "7ab9a434d6556731"}
vcr_83e26555d25c
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": [-16, 8, 17, -19, -15, -19, -10, 11], "k": 7}, "output": [8, 17, -19, -15, -19, -10, 11, -16]}, {"input": {"nums": [0, -1, -10, -6, 9, -3, 0], "k": 18}, "output": [-6, 9, -3, 0, 0, -1, -10]}, {"input": {"nums": [0, -7, -7], "k": 10}, "output": [-7, 0, -7]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "80ecfc2da2d3fe96"}
vcr_f842c85b3497
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": [-23, -13, 10, 25, 32, 44], "target": 9}, "output": [0, 4]}, {"input": {"nums": [-19, -18, -4, 0, 8, 15, 24, 29, 36, 38], "target": 38}, "output": [3, 9]}, {"input": {"nums": [-9, -8, -6, 10, 17, 21, 27, 29, 34, 38], "target": 48}, "output": [3, 9]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "9394e3c6c7dba9d2"}
vcr_60879b47bd85
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, 34, 35, 24, 2]}, "output": [15, 49, 84, 108, 110]}, {"input": {"nums": [9, 16, 6, -11, -1, 1, -5, -10, 4, -8]}, "output": [9, 25, 31, 20, 19, 20, 15, 5, 9, 1]}, {"input": {"nums": [-9, 9]}, "output": [-9, 0]}]
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": "87ff1c9f962e4e88"}
vcr_f02574a9c06b
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [23, 3, 14, 17]}, "output": 40}, {"input": {"nums": [8, 15, 14]}, "output": 22}, {"input": {"nums": [5, 20, 13, 8, 11, 19]}, "output": 47}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "85d35600b662f77f"}
vcr_ca637af9538d
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": [5, 0, 18, 16, 11]}, "output": 34}, {"input": {"nums": [9, 17, 3]}, "output": 17}, {"input": {"nums": [1, 20, 2, 16, 9, 18]}, "output": 54}]
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": "26884395060c8175"}
vcr_17e428d0ca5d
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": [25, 10, 1, 25, 34]}, "output": true}, {"input": {"nums": [13, 21, 23]}, "output": false}, {"input": {"nums": [23, 17, 2, 0, 13, 10, 12, 22, 2]}, "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": "f71a25ce317c22d6"}
vcr_49f14080cc59
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-7, 13, 59, -15, 27, 30, 64, 12, -4, -31, -3, 68, 49, 46], "target": 89}, "output": [2, 5]}, {"input": {"nums": [23, 29, 10, -14, 9, 26, 36, 2, 7, -24], "target": 49}, "output": [0, 5]}, {"input": {"nums": [4, -9, -15, 39, 27, 12, 35, 11, 49, -6, -8], "target": 2}, "output": [1, 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": "8b4a2d8b3e8da94f"}
vcr_0f05074cf4ab
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": "bc be ae edbce ce adbbb"}, "output": "adbbb ce edbce ae be bc"}, {"input": {"s": "aabb aba bab a"}, "output": "a bab aba aabb"}, {"input": {"s": "b bb babb bb ab"}, "output": "ab bb babb bb 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": "b07bfb0d3fe6a1f6"}
vcr_66cd5d939433
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, 21, 22, 49], "target": 34}, "output": 3}, {"input": {"nums": [1, 2, 12, 24, 26, 38], "target": 18}, "output": 3}, {"input": {"nums": [4, 6, 11, 19, 22, 25, 28, 29, 36, 37], "target": 35}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "843917699e7d7d36"}
vcr_d3a9f3d46435
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, 2, -2, 1, 8, -12, -10], "k": 9}, "output": [-12, -10, 1, 2, -2, 1, 8]}, {"input": {"nums": [-6, -2, -5, 1, 1, -5, 0, 4, 2], "k": 14}, "output": [1, -5, 0, 4, 2, -6, -2, -5, 1]}, {"input": {"nums": [2, -3, -5, 8, 5], "k": 6}, "output": [5, 2, -3, -5, 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": "5831b517acfdd450"}
vcr_3db6498095cf
medium
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "bbcaadbbbdaacbb"}, "output": true}, {"input": {"s": "accacaab"}, "output": false}, {"input": {"s": "ababa"}, "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": "f375708e518c5cf4"}
vcr_ba05fd20f7da
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, 4, 4, 5, 5, 5, 6, 10, 12, 12, 14]}, "output": [2, 4, 5, 6, 10, 12, 14]}, {"input": {"nums": [0, 1, 2, 5, 8, 10, 11]}, "output": [0, 1, 2, 5, 8, 10, 11]}, {"input": {"nums": [0, 1, 6]}, "output": [0, 1, 6]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "87910bd00de4561c"}
vcr_c96b819e024f
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, 17, 40, 43], "target": 46}, "output": 4}, {"input": {"nums": [14, 15, 20, 23], "target": 21}, "output": 3}, {"input": {"nums": [2, 9, 11, 12, 13, 17, 18, 20, 21, 22, 26, 28], "target": 13}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "ff3ab47ccaaedcbf"}
vcr_78d3d81cfa7b
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-3, 19, -11, 12, 3, -17, -18], "k": 0}, "output": [-3, 19, -11, 12, 3, -17, -18]}, {"input": {"nums": [6, 6, -10, 0, -3, -7, -10], "k": 20}, "output": [6, -10, 0, -3, -7, -10, 6]}, {"input": {"nums": [9, -3, 1, 1, 6, 4], "k": 18}, "output": [9, -3, 1, 1, 6, 4]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "c6b73b9447c368c0"}
vcr_c73ee5c31770
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, 8, 27, 32, 36, 37, 39], "target": 37}, "output": 5}, {"input": {"nums": [7, 16, 23, 39], "target": 36}, "output": 3}, {"input": {"nums": [0, 8, 9, 15, 19, 35], "target": 14}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "fc6496c80edd54b5"}
vcr_68865e5f4bcb
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 230, "b": 201}, "output": 1}, {"input": {"a": 107, "b": 156}, "output": 1}, {"input": {"a": 170, "b": 37}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "a8ebad42f0e5943c"}
vcr_98965ad31d96
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": ["aaa", "aa", "bb", "aba", "cbb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "aba", "baa"]}, "output": [1, 2]}, {"input": {"strs": ["aaa", "aab", "aba", "abb", "aab"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "44a9b9d74272797c"}
vcr_f47663c81686
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": "db adacce"}, "output": "adacce db"}, {"input": {"s": "baa bba bab bbba aaaa"}, "output": "aaaa bbba bab bba baa"}, {"input": {"s": "b ba aab a b"}, "output": "b a aab 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": "c91e54af3247c44b"}
vcr_80ffdbac26b2
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": [15, 14, 9, -9, -11, -8, 10, -8]}, "output": 38}, {"input": {"nums": [6, 7, -12, -2, -7, -11, -6]}, "output": 13}, {"input": {"nums": [13, 8, -5, 7, 2, 6, 6, -2, -6, -1, -9]}, "output": 37}]
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": "cf9f32cbba78b9df"}
vcr_21eb22e3c4e7
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, 13, 14, 20, 21, 25, 27, 36, 38, 41, 45, 46, 49], "target": 34}, "output": 7}, {"input": {"nums": [0, 7, 11, 14, 18, 19, 23, 29, 32], "target": 37}, "output": 9}, {"input": {"nums": [13, 18, 20, 36, 39], "target": 3}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "35abcbc4adadbabe"}
vcr_d1eb889c3764
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": [-20, 18, 37, -17, -39, 76, -21, 25, -23], "target": 53}, "output": [5, 8]}, {"input": {"nums": [-21, 26, 16, 3, -30, 14, 5, 9, -7], "target": 40}, "output": [1, 5]}, {"input": {"nums": [-17, 29, 30, 42], "target": 72}, "output": [2, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "153364ed133345c1"}
vcr_33ba1e6e9040
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 2, 4, 5, 7, 7, 10]}, "output": [0, 1, 2, 4, 5, 7, 10]}, {"input": {"nums": [0, 1, 3, 5, 6, 8, 10, 11]}, "output": [0, 1, 3, 5, 6, 8, 10, 11]}, {"input": {"nums": [3, 4, 9, 12]}, "output": [3, 4, 9, 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": "b28e4b05a22b0287"}
vcr_8c25bced5f6f
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [11, -5, -10, -13, -18, 0, -4, -16, -4, -5], "k": 9}, "output": [-5, -10, -13, -18, 0, -4, -16, -4, -5, 11]}, {"input": {"nums": [2, 9, 8, -1], "k": 20}, "output": [2, 9, 8, -1]}, {"input": {"nums": [6, -9, 4, 1, 3, -3, -4, 6], "k": 12}, "output": [3, -3, -4, 6, 6, -9, 4, 1]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "f04db9b63c5598b5"}
vcr_88ea414175a0
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": "ebd abdaac ccbbd bb bbaa"}, "output": "bbaa bb ccbbd abdaac ebd"}, {"input": {"s": "ab abba aa aab aba"}, "output": "aba aab aa abba ab"}, {"input": {"s": "bbba aaa"}, "output": "aaa bbba"}]
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": "7f296fd5fbdb918f"}
vcr_bad68162d54f
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": [12, 13, 15, 23, 25, 41], "target": 44}, "output": 6}, {"input": {"nums": [4, 9, 12, 20, 25], "target": 34}, "output": 5}, {"input": {"nums": [11, 15, 20, 23, 28, 31, 33], "target": 10}, "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": "c3b72e262bebf58b"}
vcr_1bb3ed8701f1
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 2, 2, 1, 4, 4, 4, 1, 1, 0]}, "output": false}, {"input": {"nums": [1, 3, 2, 3, 3, 0]}, "output": true}, {"input": {"nums": [3, 0, 0, 2, 1, 2, 3, 1, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "106ad16bb30a5998"}
vcr_757d75967602
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": ["bbbc", "cc", "cb", "ccb", "ac", "cb", "ca", "ac"]}, "output": [1, 1, 1, 2, 3]}, {"input": {"strs": ["bab", "aaa", "aab", "abb", "abb"]}, "output": [1, 1, 3]}, {"input": {"strs": ["baa", "aaa", "bba", "abb", "bbb", "baa", "baa"]}, "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": "c320eb65a89f6f03"}
vcr_7e04d1d0970c
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 4, 4, 9, 13]}, "output": [0, 1, 4, 9, 13]}, {"input": {"nums": [1, 3, 3, 5, 5, 8]}, "output": [1, 3, 5, 8]}, {"input": {"nums": [0, 0, 1, 1, 4, 7, 8, 10, 10, 10, 10, 11]}, "output": [0, 1, 4, 7, 8, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "83531c0277b13dbc"}
vcr_86a973615b04
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": [124, 133, 46, 197, 173, 67, 136, 70, 63, 79, 90, 169, 1]}, "output": false}, {"input": {"nums": [16, 21, 0, 18, 7, 2, 14, 21, 21]}, "output": true}, {"input": {"nums": [17, 25, 7, 4, 25, 3, 6, 21, 23]}, "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": "753bf646b7fd1e49"}
vcr_2412f589ec68
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [8, 11, 12, 13, 15, 17, 19, 30, 32, 35, 37, 41, 45, 47], "target": 12}, "output": 2}, {"input": {"nums": [3, 28, 29, 38], "target": 33}, "output": 3}, {"input": {"nums": [1, 8, 9, 12, 13, 22, 23, 25, 31, 36, 37, 39], "target": 14}, "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": "62f8147026dc7d2b"}
vcr_1511c0ff10b7
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [22, 17, 46, 20, 173, 87, 153, 25, 151, 167, 97, 76, 183, 194, 131, 182, 101, 125]}, "output": false}, {"input": {"nums": [17, 7, 23, 13, 7, 14, 2, 21]}, "output": true}, {"input": {"nums": [22, 12, 6, 18, 16, 23, 12]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "c65a24359ae9bc97"}
vcr_bcc02774707b
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-30, -4, -33, -1, 52, 48, 68, -23, 56, -25], "target": 51}, "output": [3, 4]}, {"input": {"nums": [48, -25, -19, 13, -23, 28, 37], "target": -42}, "output": [2, 4]}, {"input": {"nums": [32, 40, -20, 29, -25, 12], "target": 44}, "output": [0, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "0eb9628737362918"}
vcr_d106868f7568
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, 18, -6, 20, 2, -7]}, "output": 34}, {"input": {"nums": [4, 4, -11, 3, 9, 6, 10, -2]}, "output": 28}, {"input": {"nums": [7, -2, -11]}, "output": 7}]
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": "7fead3904623a434"}
vcr_c7d07f11d0a4
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, 15, 25, 27, 33, 38, 39], "target": 39}, "output": 6}, {"input": {"nums": [0, 2, 4, 6, 8, 9, 10, 14, 22, 28, 32, 33], "target": 9}, "output": 5}, {"input": {"nums": [2, 10, 25, 31, 37], "target": 27}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "a2b3a5bcee6ee56b"}
vcr_5ce3e11abc4b
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [27, 2, 9, 4, 24, 2, 24, 9, 15, 15, 4, 27, 19, 19, 20, 20, 79]}, "output": 79}, {"input": {"nums": [1, 18, 18, 47, 1]}, "output": 47}, {"input": {"nums": [19, 29, 8, 8, 43, 29, 3, 3, 19]}, "output": 43}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "e85b5cf85298f122"}
vcr_4a835f8801aa
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": 165}, "output": 4}, {"input": {"n": 1119}, "output": 7}, {"input": {"n": 512}, "output": 1}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "78a71af2510ae61d"}
vcr_4ed2b78822d5
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, 12, 13, 22, 28, 30, 43, 44], "target": 44}, "output": 7}, {"input": {"nums": [0, 1, 16, 18, 23, 28, 29, 30, 33, 34, 39], "target": 21}, "output": 4}, {"input": {"nums": [2, 4, 6, 17, 24, 27, 28, 33, 36, 39], "target": 15}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "f7f2d9e6e9f1d51c"}
vcr_7bb3fc0a593d
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": ["cab", "aaa", "cbb", "cb", "aab"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["abb", "aaa", "bba"]}, "output": [1, 2]}, {"input": {"strs": ["aaa", "aab", "bba", "aab", "baa"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "ec02aa36ced95e77"}
vcr_e5d0ada66906
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-5, 45, -16, -17, 59, 26, -13, 43, 54], "target": 113}, "output": [4, 8]}, {"input": {"nums": [-9, 8, 39, 9], "target": -1}, "output": [0, 1]}, {"input": {"nums": [34, -28, 12, 49], "target": 46}, "output": [0, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "acb2a0d5fb930bf2"}
vcr_e78f67b2edc9
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": [-5, -17, -18, -12, -18, 12, 14, -3, 7, 21, 6, -9, -2]}, "output": 57}, {"input": {"nums": [-12, -7, -1, -7, -3, 8]}, "output": 8}, {"input": {"nums": [4, -3, -1, 3, 12, 10, 13, 6, 4, 0]}, "output": 48}]
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": "463636fd3effd13b"}
vcr_ca584e334885
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-22, 11, 24, -9, 19, -14, -2, -21, 22, 0, -6, 16]}, "output": [-22, -11, 13, 4, 23, 9, 7, -14, 8, 8, 2, 18]}, {"input": {"nums": [-9, 6, -9, -4]}, "output": [-9, -3, -12, -16]}, {"input": {"nums": [16, 14, -3, -8, -9]}, "output": [16, 30, 27, 19, 10]}]
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": "413af4c243096a0f"}
vcr_02e131682b3a
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": [-4, 18, -19, 1, -14, 27, -6, 30, 18, -16, -7, -21, -8, 10]}, "output": [-4, 14, -5, -4, -18, 9, 3, 33, 51, 35, 28, 7, -1, 9]}, {"input": {"nums": [9, 0, -10, 15]}, "output": [9, 9, -1, 14]}, {"input": {"nums": [18, 5, 10, -8, 16, 12, -2, -4]}, "output": [18, 23, 33, 25, 41, 53, 51, 47]}]
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": "d4db75ef16e69eeb"}
vcr_7b4c6937fd5a
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, 2, 1, 3, 7]}, "output": [42, 42, 84, 28, 12]}, {"input": {"nums": [2, 3, 2, 5, 3, 2, 1]}, "output": [180, 120, 180, 72, 120, 180, 360]}, {"input": {"nums": [4, 2, 5, 5, 2, 4, 1]}, "output": [400, 800, 320, 320, 800, 400, 1600]}]
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": "fbf1afe57da71475"}
vcr_031eea374a99
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 4, 3, 2, 1, 1, 2, 1, 3, 0]}, "output": true}, {"input": {"nums": [3, 1, 2, 2]}, "output": true}, {"input": {"nums": [0, 1, 1, 0, 0, 3]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "a69acf12a3aa1a68"}
vcr_3d091c2fcbdf
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": [-19, 8, 33, 6, -21, 13, 11]}, "output": [-19, -11, 22, 28, 7, 20, 31]}, {"input": {"nums": [4, -4, 14, 0, 3, -6, -9, 11, -2]}, "output": [4, 0, 14, 14, 17, 11, 2, 13, 11]}, {"input": {"nums": [13, 0, -9, -1, 0, 16]}, "output": [13, 13, 4, 3, 3, 19]}]
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": "670bf69a54350602"}
vcr_c0b524f171d4
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": [-24, -23, -16, -15, -3, 7, 12, 20, 27, 34, 40], "target": 67}, "output": [8, 10]}, {"input": {"nums": [-20, -14, -12, -9, 2, 5, 9, 15, 22, 28, 34], "target": -26}, "output": [1, 2]}, {"input": {"nums": [-12, 6, 13, 17, 18, 21, 25, 38], "target": 43}, "output": [4, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "0b934d6666707428"}
vcr_cca61af9f08c
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, 2, 4, 3, 2, 1, 1, 1, 0, 0]}, "output": false}, {"input": {"nums": [3, 3, 1, 1, 0, 2, 0]}, "output": false}, {"input": {"nums": [0, 2, 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": "902d98f835490dce"}
vcr_2bcd4cd085e2
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [5, 8, 8, 9, 12, 14, 15]}, "output": [5, 8, 9, 12, 14, 15]}, {"input": {"nums": [1, 4, 5, 5, 7, 9, 9, 9]}, "output": [1, 4, 5, 7, 9]}, {"input": {"nums": [2, 6, 7, 7, 9, 9]}, "output": [2, 6, 7, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "1355083e804e22bd"}
vcr_5e9baa635ef1
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 3, 3, 4, 13, 13]}, "output": [0, 2, 3, 4, 13]}, {"input": {"nums": [1, 1, 5, 5, 5, 6, 7, 7, 7, 12, 12]}, "output": [1, 5, 6, 7, 12]}, {"input": {"nums": [1, 2, 5, 7, 7, 8, 11, 12, 12]}, "output": [1, 2, 5, 7, 8, 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": "2ed094e85257e537"}
vcr_de49831d7c2f
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-3, -11, 2, 10, -15, 33, -21]}, "output": [-3, -14, -12, -2, -17, 16, -5]}, {"input": {"nums": [11, -11, -10, -9]}, "output": [11, 0, -10, -19]}, {"input": {"nums": [8, 13, -5, 8, 14, -11, 6, 1]}, "output": [8, 21, 16, 24, 38, 27, 33, 34]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "08dfe3cc895c678a"}
vcr_5a309625461e
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": [68, 178, 161, 26, 133, 11, 58, 17, 192, 73, 27]}, "output": false}, {"input": {"nums": [20, 2, 10, 3, 11, 19, 12, 16, 16, 12]}, "output": true}, {"input": {"nums": [21, 16, 19, 15, 1, 1, 23, 20]}, "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": "33b718ea3ef0ecce"}
vcr_57bf13962480
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": 53, "b": 112}, "output": 1}, {"input": {"a": 180, "b": 98}, "output": 2}, {"input": {"a": 40, "b": 175}, "output": 5}]
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": "e0551d770a8e233d"}
vcr_b87c9393c0c6
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": [4, 37, 15, 8, 24, 62, 4, 37, 8, 15, 35, 24, 35]}, "output": 62}, {"input": {"nums": [27, 27, 46, 19, 19]}, "output": 46}, {"input": {"nums": [43, 25, 25, 20, 20]}, "output": 43}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "e10b05850c7f2df7"}
vcr_4123bbfbea74
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [8, 9, 12, 15, 18, 20, 25, 28, 42, 44, 45, 46], "target": 23}, "output": 6}, {"input": {"nums": [13, 19, 26, 39], "target": 9}, "output": 0}, {"input": {"nums": [14, 17, 30], "target": 20}, "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": "d1e925917307c756"}
vcr_90ddc586f58c
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, 31, 33, 38, 46], "target": 32}, "output": 2}, {"input": {"nums": [5, 14, 16, 18, 21, 22, 28, 29, 30, 31, 36], "target": 20}, "output": 4}, {"input": {"nums": [9, 12, 18, 25, 30, 31, 36], "target": 5}, "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": "5464a9b9fe17e805"}
vcr_608ae9e6f985
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [55, 37, 43, 7, 49, 38, 50, 7, 21, 5]}, "output": 43}, {"input": {"prices": [11, 5, 34, 24, 16, 39, 7, 8, 11, 18, 10, 25]}, "output": 34}, {"input": {"prices": [25, 15, 11, 10, 15, 32, 8, 16, 35, 18, 11, 31]}, "output": 27}]
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": "3069386cd111e24e"}
vcr_28e0a33d2b4f
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [3, 10, 4, 7, 2, -8, -17], "k": 8}, "output": [-17, 3, 10, 4, 7, 2, -8]}, {"input": {"nums": [-3, 3, 6, -3], "k": 4}, "output": [-3, 3, 6, -3]}, {"input": {"nums": [6, 4, 8], "k": 0}, "output": [6, 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": "0b986ae644bfef07"}
vcr_f67d002e200f
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [4, -18, 8, -16, 0, -11, 15, 7, 13, -15], "k": 6}, "output": [0, -11, 15, 7, 13, -15, 4, -18, 8, -16]}, {"input": {"nums": [4, -4, -10, -6], "k": 5}, "output": [-6, 4, -4, -10]}, {"input": {"nums": [5, -5, -1, 0, 8, -1, -1], "k": 19}, "output": [-1, 0, 8, -1, -1, 5, -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": "f641f782c9012968"}
vcr_17b62f100c07
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, 12, 27, 26, 17, 25, 20, 20, 4, 18, 3]}, "output": 108}, {"input": {"nums": [17, 8, 18, 17, 5, 18, 10]}, "output": 53}, {"input": {"nums": [13, 20, 15, 17, 14]}, "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": "894f5b467a906909"}
vcr_a5a482b1874a
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [2, 3, 4, 4, 4, 6, 8, 11, 14, 15, 15]}, "output": [2, 3, 4, 6, 8, 11, 14, 15]}, {"input": {"nums": [2, 4, 8, 10, 12]}, "output": [2, 4, 8, 10, 12]}, {"input": {"nums": [0, 5, 10, 10]}, "output": [0, 5, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "f5e62b6b43c07b30"}
vcr_5752e8ec96e2
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [9, 27, 26, 13, 15, 26, 10, 9, 23, 30]}, "output": 105}, {"input": {"nums": [10, 7, 18, 3]}, "output": 28}, {"input": {"nums": [19, 9, 6, 5]}, "output": 25}]
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": "4ae1af6751d50be9"}
vcr_40699249fdf6
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": ["abaa", "abaa", "bca", "cc", "bc", "ab", "aa", "caba", "bb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "bbb", "aaa", "bba", "baa"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["abb", "aaa", "bba"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "ee850147143c0a2e"}
vcr_66a7ad4d8aab
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-17, 38, 52, 70, 19, 60, -14, -40, 14, 25, 21, 6, -9, 39], "target": 95}, "output": [3, 9]}, {"input": {"nums": [45, 23, 3, -21, -26, -15, 44], "target": 2}, "output": [1, 3]}, {"input": {"nums": [42, 29, 35, -29, 24, -7, 39, -4], "target": -11}, "output": [5, 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": "4ec2f3931255f7a6"}
vcr_62e41dee3dde
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, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 15]}, "output": [2, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15]}, {"input": {"nums": [0, 0, 1, 3, 3, 6, 8, 10, 10, 11, 12, 12]}, "output": [0, 1, 3, 6, 8, 10, 11, 12]}, {"input": {"nums": [0, 4, 5, 8, 10, 10]}, "output": [0, 4, 5, 8, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "684c367633297117"}
vcr_b97b65896992
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": ["abc", "cca", "bc", "bcbc", "cc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "bba", "bbb"]}, "output": [1, 2]}, {"input": {"strs": ["aba", "aba", "abb", "bbb"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "7cab1b2f9d63271a"}
vcr_fddceb78927b
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [11, 6, 3, 17, -22, -17, -15, -12, -23, 1, 19]}, "output": [11, 17, 20, 37, 15, -2, -17, -29, -52, -51, -32]}, {"input": {"nums": [18, 11, 2, -9]}, "output": [18, 29, 31, 22]}, {"input": {"nums": [3, -5, 9, 17, 13, 3, -1, -1, -2, -5]}, "output": [3, -2, 7, 24, 37, 40, 39, 38, 36, 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": "1b163b1e6a5f813f"}
vcr_5ad549f33d34
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": [5, 7, 7, 2, 5, -7, -5, 2, -16, 10]}, "output": 26}, {"input": {"nums": [3, 11, -8, 10, -10, -7, 6, 7, -7]}, "output": 16}, {"input": {"nums": [-3, 0, 12, -9, 6, -9, -6, -10, -11, -7]}, "output": 12}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "0e8a806a4886967b"}
vcr_2cfb5790c46c
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [22, 0, 15, 16, 6]}, "output": 43}, {"input": {"nums": [13, 2, 8, 17]}, "output": 30}, {"input": {"nums": [9, 9, 9]}, "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": "3f884526462306cb"}
vcr_4c795344ba01
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [15, 71, 28, 14, 15, 26, 30, 14, 4, 26, 4, 28, 30]}, "output": 71}, {"input": {"nums": [14, 15, 54, 10, 26, 15, 10, 26, 14]}, "output": 54}, {"input": {"nums": [15, 19, 15, 2, 11, 14, 14, 19, 2, 58, 11]}, "output": 58}]
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": "d9b3f26e666d8b90"}
vcr_96ca2e3c380e
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": ["bc", "bbb", "bbc", "ba"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bab", "bbb", "aaa"]}, "output": [1, 1, 1]}, {"input": {"strs": ["baa", "baa", "aba", "aab", "aaa", "bab"]}, "output": [1, 1, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "6291a8d1d0659701"}
vcr_7daf9e7e66f4
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": 56016}, "output": 8}, {"input": {"n": 3368}, "output": 5}, {"input": {"n": 1547}, "output": 5}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "c3259f6dfb0a7e3a"}
vcr_5117cc6a7dda
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -21, -20, -16, -9, -2, 12, 17, 43], "target": -9}, "output": [0, 7]}, {"input": {"nums": [-10, -5, 19, 20, 27, 34], "target": 24}, "output": [0, 5]}, {"input": {"nums": [-19, -17, 1, 6, 8, 9, 11, 26, 35], "target": 9}, "output": [1, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "fb694792cd748441"}
vcr_bbea592a68df
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 2, 0, 6]}, "output": [0, 0, 60, 0]}, {"input": {"nums": [5, 2, 3, 1, 2, 4, 5]}, "output": [240, 600, 400, 1200, 600, 300, 240]}, {"input": {"nums": [4, 3, 5, 1]}, "output": [15, 20, 12, 60]}]
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": "13158d8d535b4f37"}
vcr_f0aa5565a2c6
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": [17, 15, 31, 22, 62, -30, 39, 29], "target": 51}, "output": [3, 7]}, {"input": {"nums": [3, 17, -4, 18], "target": -1}, "output": [0, 2]}, {"input": {"nums": [38, 17, 15, -7, 42, 19, -13, 13, -3, 27, 39], "target": 66}, "output": [9, 10]}]
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": "299fd4446fd55fec"}
vcr_0139032b27bb
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["aca", "abc", "bb", "cc"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["abb", "abb", "abb", "bba", "bbb"]}, "output": [1, 4]}, {"input": {"strs": ["abb", "bab", "aaa"]}, "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": "c139b99f18ecc6be"}
vcr_c430bff9da21
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": "bdcdca cecab a eda"}, "output": "eda a cecab bdcdca"}, {"input": {"s": "b baaa ba bbb"}, "output": "bbb ba baaa b"}, {"input": {"s": "aa a b ba"}, "output": "ba b a 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": "d0150c19ce68061e"}
vcr_4ec02f9a6398
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, 0, 2, 4, 7, 4, 1, 5]}, "output": [0, 6720, 0, 0, 0, 0, 0, 0]}, {"input": {"nums": [3, 3, 5, 5, 4]}, "output": [300, 300, 180, 180, 225]}, {"input": {"nums": [2, 1, 4]}, "output": [4, 8, 2]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "064ea6f9a6794ca1"}
vcr_72f44b2ebdc1
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [7, 18, 24, 41, 47], "target": 6}, "output": 0}, {"input": {"nums": [3, 6, 30], "target": 5}, "output": 1}, {"input": {"nums": [6, 8, 13, 17, 25, 31, 37], "target": 3}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "faa615394e1dfd9a"}