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_2c7b00d8b923
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [9, 27, 28, 34, 42, 46, 47], "target": 0}, "output": 0}, {"input": {"nums": [0, 29, 31], "target": 17}, "output": 1}, {"input": {"nums": [2, 8, 14, 18, 20, 24, 26, 28, 31], "target": 25}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "685f428b67e225d0"}
vcr_a7b1d9e247f7
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": [18, 49, 43, 6, 18]}, "output": true}, {"input": {"nums": [18, 17, 4]}, "output": false}, {"input": {"nums": [4, 8, 6, 24, 24, 19]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "cf20acc15c450e3e"}
vcr_09b68ebd9bb8
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": [63, 33, 5, 22, 8, 33, 8, 5, 28, 28, 22]}, "output": 63}, {"input": {"nums": [59, 12, 20, 5, 5, 18, 18, 20, 12]}, "output": 59}, {"input": {"nums": [19, 19, 13, 9, 13, 29, 8, 47, 8, 9, 29]}, "output": 47}]
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": "77c252cbad074c1c"}
vcr_ce74786c6cd5
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": "baacabababb"}, "output": 9}, {"input": {"s": "bcbacbaaac"}, "output": 8}, {"input": {"s": "cbbacaa"}, "output": 5}]
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": "c5dc73ff38af5d28"}
vcr_bb8cdd18a7e0
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 2, 5, 6, 3, 6, 7]}, "output": [7560, 11340, 4536, 3780, 7560, 3780, 3240]}, {"input": {"nums": [4, 1, 4, 4, 3]}, "output": [48, 192, 48, 48, 64]}, {"input": {"nums": [5, 4, 1]}, "output": [4, 5, 20]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "138172bc0b020440"}
vcr_b052b45c6851
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, 11, 16, 23, 39, 40], "target": 15}, "output": 2}, {"input": {"nums": [10, 14, 15, 16, 17, 19, 20, 22, 24, 28, 31, 38], "target": 8}, "output": 0}, {"input": {"nums": [8, 10, 13, 15, 26, 27, 28, 31, 33, 36], "target": 6}, "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": "4ed42dd526225f91"}
vcr_f37104a0ad53
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 6, 7, 14, 17, 22, 27, 28, 30, 31, 37, 47, 49], "target": 28}, "output": 7}, {"input": {"nums": [1, 3, 4, 9, 20, 27, 35, 39], "target": 24}, "output": 5}, {"input": {"nums": [1, 5, 8, 11, 12, 22, 23, 26, 35, 36], "target": 4}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "bd7bd8ffb96076be"}
vcr_cb7f4892c4e2
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": [11, 26, 1, 25, 21, 49, 35, 17, 5, 0, 11]}, "output": true}, {"input": {"nums": [14, 17, 14, 20, 4, 23, 18, 16, 9, 23, 18, 10]}, "output": true}, {"input": {"nums": [13, 24, 25, 4, 22, 9, 2, 16, 7]}, "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": "0a5017f19a79e4a5"}
vcr_efa5d5e93675
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": "dcbbccdcdbbc"}, "output": false}, {"input": {"s": "bac"}, "output": false}, {"input": {"s": "aabbbbcbc"}, "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": "eeda77d1c9484bfb"}
vcr_80b4eb1bc07b
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": "de edec ac bd bbeea"}, "output": "bbeea bd ac edec de"}, {"input": {"s": "aa b"}, "output": "b aa"}, {"input": {"s": "baab b aabb"}, "output": "aabb b baab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "d696ec1f5028d4d9"}
vcr_ebf5a273ff9d
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [30, 73, 28, 3, 3, 15, 26, 26, 39, 29, 39, 30, 28, 35, 29, 15, 35]}, "output": 73}, {"input": {"nums": [16, 22, 10, 16, 22, 40, 10]}, "output": 40}, {"input": {"nums": [19, 15, 15, 17, 17, 28, 28, 5, 19, 54, 5]}, "output": 54}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "66b453ddfc2ca95e"}
vcr_c2a3033f324d
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": "aedeeb c eb a ad"}, "output": "ad a eb c aedeeb"}, {"input": {"s": "aba a ba abab"}, "output": "abab ba a aba"}, {"input": {"s": "ba aa bb"}, "output": "bb aa ba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "0a6b1a18f31ef307"}
vcr_e7fbfe2c31e1
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [25, 58, 24, 36, 36, 17, 25, 20, 24, 17, 20]}, "output": 58}, {"input": {"nums": [21, 29, 21, 6, 6, 53, 17, 14, 17, 14, 29]}, "output": 53}, {"input": {"nums": [44, 28, 1, 28, 11, 17, 3, 1, 11, 3, 17]}, "output": 44}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "6014f93fca2533c0"}
vcr_24fa3e2532ba
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": [12, 35, 39, 10, 40]}, "output": 30}, {"input": {"prices": [22, 2, 1, 27, 1]}, "output": 26}, {"input": {"prices": [28, 30, 8, 32, 6, 40]}, "output": 34}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "ba46b1424f238ca4"}
vcr_0d5c4e5660bb
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, -11, -4, 2]}, "output": 52}, {"input": {"nums": [6, -9, 8, 0, 13, -5, -5, 1]}, "output": 21}, {"input": {"nums": [-7, -9, 9, -6, -11, 10, -12, -12, 13, -5]}, "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": "a37f5e51505dccf5"}
vcr_022d1b37524e
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [9, 11, 19, 21, 29, 30, 35, 38, 39], "target": 26}, "output": 4}, {"input": {"nums": [13, 28, 29, 32, 37], "target": 20}, "output": 1}, {"input": {"nums": [0, 14, 15], "target": 38}, "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": "8997c9e5e43d0a1f"}
vcr_b29826b96817
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [20, -5, 29, -8]}, "output": [20, 15, 44, 36]}, {"input": {"nums": [7, 4, -9, -1, -7, 12, 12, 13, 14]}, "output": [7, 11, 2, 1, -6, 6, 18, 31, 45]}, {"input": {"nums": [-3, -3]}, "output": [-3, -6]}]
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": "f9104b3ff3e295aa"}
vcr_4fe00156075b
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [12, 7, 10, 3]}, "output": 22}, {"input": {"nums": [2, 13, 15, 5, 0, 7, 6, 16, 7]}, "output": 41}, {"input": {"nums": [0, 11, 10]}, "output": 11}]
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": "8c94d70b457991d6"}
vcr_43aae15d1bc4
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, 6, 6, 6, 7, 7, 11, 12, 13, 13, 14]}, "output": [0, 2, 3, 6, 7, 11, 12, 13, 14]}, {"input": {"nums": [2, 3, 12]}, "output": [2, 3, 12]}, {"input": {"nums": [2, 11, 12]}, "output": [2, 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": "e446e7ec23a0edba"}
vcr_775dd363cdd2
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, 7, 8, 23, 38, 46], "target": 30}, "output": 4}, {"input": {"nums": [5, 6, 14, 15, 16, 17, 20, 30, 32, 37, 38], "target": 2}, "output": 0}, {"input": {"nums": [3, 25, 28, 29, 34], "target": 19}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "8c76fce0986f8a85"}
vcr_6bc56e258b78
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, 19, 35], "target": 11}, "output": 0}, {"input": {"nums": [0, 6, 15, 18, 23, 24, 25, 28, 29, 32, 37], "target": 19}, "output": 4}, {"input": {"nums": [1, 3, 16, 22, 23, 26, 27, 28, 31, 34, 36], "target": 35}, "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": "4b86954ac8aaa9c8"}
vcr_630869769496
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [49, 33, 10, 2, 33, 1, 38, 42, 49, 3]}, "output": true}, {"input": {"nums": [13, 3, 14, 9, 13, 21, 17, 22]}, "output": true}, {"input": {"nums": [10, 18, 10, 3, 15, 21, 19, 11, 4, 1, 2, 22]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "6eac6fb4c22d36e2"}
vcr_d80472f86d95
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": [-19, 29, 77, -34, -40], "target": -53}, "output": [0, 3]}, {"input": {"nums": [-15, -24, 16, 26, -29, -2, 32, -26, 49], "target": 65}, "output": [2, 8]}, {"input": {"nums": [30, 36, -23, 23, -10, 28], "target": 0}, "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": "62c38b4c69e1301c"}
vcr_a6e729072d0c
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": 35350}, "output": 6}, {"input": {"n": 2089}, "output": 4}, {"input": {"n": 15304}, "output": 8}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "ccae7a2ff2b0a989"}
vcr_ec41759accc0
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [9, 18, 42, 44], "target": 42}, "output": 2}, {"input": {"nums": [1, 2, 8, 9, 10, 11, 17, 21, 27, 29, 31, 35], "target": 38}, "output": 12}, {"input": {"nums": [0, 2, 6, 14, 16, 27, 36], "target": 15}, "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": "62ec7ce204da55be"}
vcr_38c7778fc5a2
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, 21, 28, 21, 72, 9, 33, 9, 10, 10, 28, 14, 14, 37, 37]}, "output": 72}, {"input": {"nums": [7, 24, 17, 17, 7, 24, 47]}, "output": 47}, {"input": {"nums": [2, 12, 11, 11, 9, 29, 29, 12, 56, 2, 9]}, "output": 56}]
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": "efe9081566b0adee"}
vcr_aa3748ca865f
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": "cdbeed deacd"}, "output": "deacd cdbeed"}, {"input": {"s": "abab abba aaaa"}, "output": "aaaa abba abab"}, {"input": {"s": "aaa aaba bba bbba abaa"}, "output": "abaa bbba bba aaba aaa"}]
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": "0a6ad76825d03f8d"}
vcr_530c79c0ab02
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": [32, 26, 58, 10, 23, 29, 27, 43, 26, 40, 53, 54, 49, 14, 10, 47]}, "output": 44}, {"input": {"prices": [6, 32, 19, 35, 8, 2, 17, 27]}, "output": 29}, {"input": {"prices": [23, 9, 18, 36, 22, 2, 4, 27, 37]}, "output": 35}]
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": "23179f44669f9712"}
vcr_dd87f20b7353
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-14, 4, 20, 23, 30, 32, 44], "target": 50}, "output": [2, 4]}, {"input": {"nums": [-19, -8, -2, 3, 6, 7, 11, 12, 25, 29, 34], "target": 15}, "output": [0, 10]}, {"input": {"nums": [-17, -14, -11, -7, 16, 19, 20, 27, 34], "target": 16}, "output": [2, 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": "d6d738c4ac553b9d"}
vcr_ab3115f66e41
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 216, "b": 208}, "output": 8}, {"input": {"a": 123, "b": 79}, "output": 1}, {"input": {"a": 80, "b": 22}, "output": 2}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "492a93eaa75c919b"}
vcr_f6edc9ef218b
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": [-18, -16, -9, -3, 6, 7, 16, 21, 37, 41, 43], "target": -11}, "output": [0, 5]}, {"input": {"nums": [-12, -10, 5, 15, 24], "target": -22}, "output": [0, 1]}, {"input": {"nums": [-19, -18, -16, -13, -5, 3, 11, 16, 18, 20, 36], "target": -8}, "output": [0, 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": "d2ec5e06aebdefc5"}
vcr_09bbc3fe23de
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, 7, 7, 7, 8, 10, 11, 14, 14]}, "output": [5, 7, 8, 10, 11, 14]}, {"input": {"nums": [4, 5, 5, 8, 8, 9]}, "output": [4, 5, 8, 9]}, {"input": {"nums": [0, 1, 2, 2, 4, 7, 8, 8, 9, 11, 11, 12]}, "output": [0, 1, 2, 4, 7, 8, 9, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "df1d2330bf26e7cb"}
vcr_2aaac2b84c1f
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": [59, 33, 19, 4, 22, 38, 36, 19, 22, 36, 33, 4, 38]}, "output": 59}, {"input": {"nums": [13, 6, 7, 58, 6, 7, 9, 10, 10, 13, 9]}, "output": 58}, {"input": {"nums": [16, 2, 13, 9, 2, 16, 13, 9, 52]}, "output": 52}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "bf70565e11726765"}
vcr_3252ed7c4171
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [17, 3, 30, 5, 30, 11]}, "output": 77}, {"input": {"nums": [19, 4, 18, 18, 13, 0, 20, 5, 15, 12]}, "output": 85}, {"input": {"nums": [5, 11, 5, 7, 6, 19, 9, 6, 15, 18]}, "output": 61}]
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": "cb3437fbb2af06b5"}
vcr_0bc561d7e767
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": "abec c be cdadc ace be"}, "output": "be ace cdadc be c abec"}, {"input": {"s": "b a"}, "output": "a b"}, {"input": {"s": "a bbb"}, "output": "bbb a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "020c26148d64e684"}
vcr_18724b5ce0eb
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, 3, 4, 7, 9, 12, 24, 26, 31, 32, 41], "target": 11}, "output": 5}, {"input": {"nums": [2, 15, 17, 18, 22, 23, 25, 37], "target": 25}, "output": 6}, {"input": {"nums": [0, 2, 6, 12, 19, 21, 28, 33, 38], "target": 32}, "output": 7}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "9ca196327083fc89"}
vcr_4cf8e2216479
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": "accdcbdbc"}, "output": false}, {"input": {"s": "bccccacbbc"}, "output": false}, {"input": {"s": "ca"}, "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": "29e8540b49eb8b7c"}
vcr_baedf492dafc
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": [-1, -9, 7, 20, -7, 3, -4, -4, -7, -7]}, "output": 27}, {"input": {"nums": [-9, 0, -8, -8]}, "output": 0}, {"input": {"nums": [-12, -9, -9, 0]}, "output": 0}]
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": "1b18daac7f7c7f30"}
vcr_32ed24c1f44f
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": "cbadcacacdabc"}, "output": true}, {"input": {"s": "caacbb"}, "output": false}, {"input": {"s": "acba"}, "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": "0c78a4455b9c6f7d"}
vcr_d285844a9466
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, -24, -7, 5, 8, 14, 16, 19, 32, 35, 40], "target": 45}, "output": [3, 10]}, {"input": {"nums": [-10, 14, 20, 27], "target": 47}, "output": [2, 3]}, {"input": {"nums": [-5, -3, -2, 3, 4, 12, 16, 21, 32, 37], "target": 7}, "output": [0, 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": "f17fe380f8386155"}
vcr_8086dd38ba62
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-7, 22, -10, -11, 17, -15, 8, 13, 16, 10, -15, -4, 15]}, "output": 50}, {"input": {"nums": [9, 4, 3, 10, -6]}, "output": 26}, {"input": {"nums": [-10, 8, 9, -5, -3, -1, 6]}, "output": 17}]
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": "846e7769e0281f12"}
vcr_bc2942b4f0c7
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": [24, 15, 2, 53, 6, 38, 44, 52]}, "output": 51}, {"input": {"prices": [21, 5, 25, 28]}, "output": 23}, {"input": {"prices": [8, 22, 15, 17, 33, 30, 28, 4, 31, 36, 20, 24]}, "output": 32}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "1d3de8224989e079"}
vcr_3758036dd40a
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [35, 15, 21, 21, 15, 73, 38, 7, 38, 35, 7]}, "output": 73}, {"input": {"nums": [25, 4, 46, 23, 25, 23, 12, 4, 12]}, "output": 46}, {"input": {"nums": [44, 23, 11, 1, 11, 19, 23, 1, 19]}, "output": 44}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "65e3e7b03b3652e9"}
vcr_dcd8ca485998
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": [51, 38, 34, 50]}, "output": 16}, {"input": {"prices": [35, 18, 22, 12, 29, 33, 8, 2, 34, 23]}, "output": 32}, {"input": {"prices": [39, 2, 10]}, "output": 8}]
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": "54d21dc5ac2df9f4"}
vcr_05155d704caa
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [18, 16, 16, 29]}, "output": 47}, {"input": {"nums": [19, 17, 1, 1, 4, 1, 10, 20, 20, 9]}, "output": 54}, {"input": {"nums": [0, 20, 15, 17, 18, 12]}, "output": 49}]
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": "eaf1332c2b5ce74b"}
vcr_f5399a79e924
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [9, 17, 18, 21, 25, 36, 39], "target": 35}, "output": 5}, {"input": {"nums": [4, 22, 33], "target": 31}, "output": 2}, {"input": {"nums": [2, 3, 10, 16, 19, 24, 29, 34], "target": 4}, "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": "26b79e82cca3d908"}
vcr_748557ce979d
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, 5, 13, 3, 8, 6]}, "output": 50}, {"input": {"nums": [-1, -9, 7, -2, 7, -11, -2, -12, 7]}, "output": 12}, {"input": {"nums": [7, 3, -12, 13]}, "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": "0f37c9a455c9787e"}
vcr_46f77b92b5e8
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": [143, 103, 192, 56, 23, 6, 145, 128, 54, 76, 130, 99, 122, 21]}, "output": false}, {"input": {"nums": [2, 2, 1, 19, 2, 14, 3, 4, 25, 23, 23]}, "output": true}, {"input": {"nums": [22, 12, 21, 0, 2, 22]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "be8bed6c76c45233"}
vcr_589aaf77f599
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, 4, 2, 3, 3, 4, 4, 0]}, "output": false}, {"input": {"nums": [1, 1, 2, 0, 2, 0, 3]}, "output": true}, {"input": {"nums": [2, 0, 2, 1, 3, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "1ee91d329ca7b107"}
vcr_39c49f1a51bc
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, 14, 24, 42, 46], "target": 14}, "output": 1}, {"input": {"nums": [2, 3, 12, 14, 16, 19, 22, 24, 27, 33, 35, 36], "target": 1}, "output": 0}, {"input": {"nums": [0, 3, 16, 31, 32, 34], "target": 11}, "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": "e66d99fd0e232677"}
vcr_4596451b23a8
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, -11, -4, -3, 25, -13, -10, -4]}, "output": [-15, -26, -30, -33, -8, -21, -31, -35]}, {"input": {"nums": [4, 10, -11]}, "output": [4, 14, 3]}, {"input": {"nums": [-10, 16, 5, 2, 3, 17, -7, -11]}, "output": [-10, 6, 11, 13, 16, 33, 26, 15]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "11e1aac7b45f2619"}
vcr_a958b11bc0b7
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": [47, 17, 6, 17, 3, 15, 14, 17, 50, 39, 35, 49]}, "output": true}, {"input": {"nums": [8, 24, 4, 18, 5, 25, 14, 0, 20, 13]}, "output": false}, {"input": {"nums": [10, 23, 7, 25, 6, 8, 16, 10, 6, 25, 9, 19]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "2c7f4079e905c44b"}
vcr_aa74341abdb5
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": 70, "b": 47}, "output": 1}, {"input": {"a": 113, "b": 9}, "output": 1}, {"input": {"a": 90, "b": 149}, "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": "5005db2816170825"}
vcr_aa9495ab9173
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": [44, 50, 50, 34, 10, 48, 16, 3, 6, 36, 27, 16, 32]}, "output": true}, {"input": {"nums": [4, 10, 10, 17, 6, 6, 20]}, "output": true}, {"input": {"nums": [17, 14, 17]}, "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": "40aed1cd6bb2295f"}
vcr_c9bbdeb35b3c
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [73, 26, 36, 16, 26, 16, 12, 12, 33, 36, 33]}, "output": 73}, {"input": {"nums": [26, 19, 3, 12, 12, 43, 19, 3, 26]}, "output": 43}, {"input": {"nums": [29, 22, 29, 22, 12, 21, 19, 12, 21, 48, 19]}, "output": 48}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "aa183597284fec55"}
vcr_10aa0b861ed6
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": [9, 16, 6, 45, 35, 53, 36, 13, 19, 59]}, "output": 53}, {"input": {"prices": [21, 22, 32, 34, 13, 36, 19, 37, 31]}, "output": 24}, {"input": {"prices": [39, 30, 26, 31, 9, 21, 4, 8]}, "output": 12}]
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": "8a90e5923a30f562"}
vcr_0737cd915957
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": [37, 26, 33, 26, 23, 33, 14, 17, 37, 76, 23, 14, 17]}, "output": 76}, {"input": {"nums": [16, 2, 2, 46, 16]}, "output": 46}, {"input": {"nums": [11, 53, 11, 2, 2, 1, 1]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "0aab80d61c1a820d"}
vcr_930c16a4c949
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, 4, 2, 4, 0, 1, 0]}, "output": false}, {"input": {"nums": [2, 2, 0, 0, 1, 2, 3, 1, 0]}, "output": false}, {"input": {"nums": [3, 1, 1, 0, 2, 1, 1, 0, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "6d5ed2ca7345fb5a"}
vcr_833631cd1b9d
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": 20467}, "output": 11}, {"input": {"n": 6489}, "output": 7}, {"input": {"n": 7241}, "output": 6}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "e8cfd18d337a3f4e"}
vcr_646faa107a32
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-2, 6, -3, -19, 18, 11, 14], "k": 17}, "output": [18, 11, 14, -2, 6, -3, -19]}, {"input": {"nums": [-7, 1, -9, -6, 5, -7, -6, -1], "k": 18}, "output": [-6, -1, -7, 1, -9, -6, 5, -7]}, {"input": {"nums": [5, 4, 6, 6], "k": 4}, "output": [5, 4, 6, 6]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "31ad073b131e2d6a"}
vcr_efbd81ff04a5
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [24, 14, 10, 20, 14, 22, 19, 3]}, "output": 69}, {"input": {"nums": [20, 15, 9, 2, 4, 10, 8, 17, 19, 16]}, "output": 72}, {"input": {"nums": [3, 13, 4, 3, 2, 0, 5, 8, 16]}, "output": 37}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "8f3f76924d0d547a"}
vcr_313c7f97a58f
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": "accaaacbbcabbb"}, "output": 8}, {"input": {"s": "acbbccba"}, "output": 6}, {"input": {"s": "cccacaacaaaa"}, "output": 6}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "283fe314f18c643b"}
vcr_ed588fab17bf
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": [64, -39, -7, 79, 63, 24, 6], "target": 25}, "output": [0, 1]}, {"input": {"nums": [-13, 39, 28, 13, -26, 29, -10, 12, 27], "target": 68}, "output": [1, 5]}, {"input": {"nums": [45, 26, 11, 19, -25, -21, -16], "target": -14}, "output": [2, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "96eaeb89f3c15638"}
vcr_bcc4b9e55fd7
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [28, 6, 6, 23, 29, 23]}, "output": 74}, {"input": {"nums": [11, 0, 15, 5, 9, 12, 15, 19]}, "output": 57}, {"input": {"nums": [2, 19, 7, 5, 3, 11, 6, 18, 15, 1]}, "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": "949f5117eefbdf7f"}
vcr_ca4680515382
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-19, -18, 18, -1, -7, 13, 10, -6, 11, -2, 16, -18], "k": 22}, "output": [18, -1, -7, 13, 10, -6, 11, -2, 16, -18, -19, -18]}, {"input": {"nums": [4, -9, -1, -7, 7, 8, 8, 7, 10], "k": 6}, "output": [-7, 7, 8, 8, 7, 10, 4, -9, -1]}, {"input": {"nums": [-5, -10, -4, -10, 4, -9, 7, -8], "k": 17}, "outp...
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": "0491139e1206fea6"}
vcr_9ec62230160d
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-19, -5, 3, -15, -18, 2, 17, -19, -12, 9], "k": 0}, "output": [-19, -5, 3, -15, -18, 2, 17, -19, -12, 9]}, {"input": {"nums": [6, -5, 0, 7, 8, 8, 1, 0, 1], "k": 2}, "output": [0, 1, 6, -5, 0, 7, 8, 8, 1]}, {"input": {"nums": [10, -7, -5, -2], "k": 19}, "output": [-7, -5, -2, 10]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "25e7cae5ad56f431"}
vcr_86841b7d93a4
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": "abcababbbacc"}, "output": 9}, {"input": {"s": "cacbbbca"}, "output": 6}, {"input": {"s": "cbbcca"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "7a45c63d20f995a3"}
vcr_d857e2abe7b9
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": "aed a dc"}, "output": "dc a aed"}, {"input": {"s": "bbba a"}, "output": "a bbba"}, {"input": {"s": "bab ab a aa bbb"}, "output": "bbb aa a ab bab"}]
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": "d35d4c07d7dcbcb7"}
vcr_59f753881e3d
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": [29, 41, -2, 69, 49, 60, -7, 15, -22], "target": -9}, "output": [2, 6]}, {"input": {"nums": [31, 21, -26, -9, 30, 38, 2], "target": 5}, "output": [0, 2]}, {"input": {"nums": [-16, 45, 46, -28, 8, 21, 32, 5, 36, -25, -27], "target": 91}, "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": "a2821b3a67af75d3"}
vcr_95b91155b432
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, -14, 14, -11, 5, 6, 20, 1, -7, 7, 19, -15, 10]}, "output": 54}, {"input": {"nums": [2, -4, 1, -6, 1, 7, 4, -11, 5, -4]}, "output": 12}, {"input": {"nums": [3, -1, 14, 0, 0, -4, -4, 8, 2, 0]}, "output": 18}]
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": "218af22cb51318ab"}
vcr_2d29c8720e0e
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, 15, 1, 8, 17, 16, -5, -11, 20, 7, 0, 10], "k": 32}, "output": [17, 16, -5, -11, 20, 7, 0, 10, 11, 15, 1, 8]}, {"input": {"nums": [-4, -3, 8, 4], "k": 3}, "output": [-3, 8, 4, -4]}, {"input": {"nums": [-9, 0, -1, 4, -9, -10, -7, 8, -8, 10], "k": 4}, "output": [-7, 8, -8, 10, -9, 0, -1, 4, -9, -1...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "5dbb95751be81ed4"}
vcr_b9c2a372425f
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": [9, 16, 52, 6, 35, 46, 39, 50]}, "output": 44}, {"input": {"prices": [3, 29, 26, 28]}, "output": 26}, {"input": {"prices": [23, 34, 8, 20, 14]}, "output": 12}]
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": "408a92b091372f0c"}
vcr_63b7143e823f
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 5, 20, 27, 35, 42, 49], "target": 5}, "output": 1}, {"input": {"nums": [9, 16, 19, 20, 22, 24], "target": 17}, "output": 2}, {"input": {"nums": [10, 11, 16, 19, 26], "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": "200e032ff07fa56f"}
vcr_3ec44ada3642
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": [29, 39, 16, 53, 44, 25, 30, 47, 20, 22, 30, 53]}, "output": 37}, {"input": {"prices": [2, 18, 26, 3, 5, 15, 13]}, "output": 24}, {"input": {"prices": [30, 40, 30, 35]}, "output": 10}]
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": "b0473e78dd4f8f50"}
vcr_66aa3339f371
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": [1, 7, 3, 4, 1, 5, 2]}, "output": [840, 120, 280, 210, 840, 168, 420]}, {"input": {"nums": [1, 5, 2, 3, 1, 1]}, "output": [30, 6, 15, 10, 30, 30]}, {"input": {"nums": [2, 5, 4, 4, 3, 5]}, "output": [1200, 480, 600, 600, 800, 480]}]
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": "9be35e09585d49a4"}
vcr_ad34e01583b5
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [3, 28, -14, -9, 3, 3, 30, 21, -23]}, "output": [3, 31, 17, 8, 11, 14, 44, 65, 42]}, {"input": {"nums": [6, 3, -5, 10, -12, 17, -2, 5, -4]}, "output": [6, 9, 4, 14, 2, 19, 17, 22, 18]}, {"input": {"nums": [11, -11, -9, 8, 11, -4, 16, 16]}, "output": [11, 0, -9, -1, 10, 6, 22, 38]}]
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": "6b9c236f99cad263"}
vcr_1dea982cc1d5
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, 5, 7, 8, 8, 11, 12, 15]}, "output": [0, 1, 4, 5, 7, 8, 11, 12, 15]}, {"input": {"nums": [2, 3, 5, 9, 9, 10, 12, 12, 12]}, "output": [2, 3, 5, 9, 10, 12]}, {"input": {"nums": [1, 2, 3, 3, 9]}, "output": [1, 2, 3, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "9083f9895197a592"}
vcr_773b4d038f17
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 4, 4, 5, 6, 9, 9, 12, 14]}, "output": [3, 4, 5, 6, 9, 12, 14]}, {"input": {"nums": [2, 7, 9, 10]}, "output": [2, 7, 9, 10]}, {"input": {"nums": [2, 2, 3, 3, 4, 5, 5, 5, 10, 10, 10, 11]}, "output": [2, 3, 4, 5, 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": "646e0585617e4622"}
vcr_bca397586dc6
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", "cba", "cab", "abca", "cba", "cb", "ab", "cc"]}, "output": [1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bba", "aab", "bab", "abb", "bba"]}, "output": [1, 4]}, {"input": {"strs": ["baa", "abb", "bab"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "e48c21b9ab9ad859"}
vcr_0d4bab70ca59
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, 4, 14, -12, 3, -14]}, "output": 18}, {"input": {"nums": [-8, 11, 4, 1, -6, 1]}, "output": 16}, {"input": {"nums": [-6, 6, 6, -1]}, "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": "a085d0e8f74a67f5"}
vcr_179690e85572
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": [-13, -5, 2, -17, -8, 19, 4, -13, -10, 20], "k": 18}, "output": [2, -17, -8, 19, 4, -13, -10, 20, -13, -5]}, {"input": {"nums": [10, 8, 6, 2, 4, 7, -9, -7, 7, -9], "k": 1}, "output": [-9, 10, 8, 6, 2, 4, 7, -9, -7, 7]}, {"input": {"nums": [-2, -2, 7], "k": 10}, "output": [7, -2, -2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "606c429428d7e1e0"}
vcr_9f77aaeb4c9a
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, 4, 0, 3, 1, 0, 2, 0]}, "output": true}, {"input": {"nums": [2, 3, 3]}, "output": true}, {"input": {"nums": [0, 3, 3, 2, 0, 2, 0, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "916b7c12338f2ae1"}
vcr_47e12a276c66
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, 2, 0, 2, 3, 2, 0, 0, 0]}, "output": false}, {"input": {"nums": [0, 2, 1, 0, 0, 1]}, "output": false}, {"input": {"nums": [2, 1, 1, 0, 2, 2, 1, 3, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "59859dc1a85bdc07"}
vcr_26842ca10976
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": [7, 1, 27, 10, 30, 40, 36, 35, 24, 14, 30, 44, 32, 1]}, "output": true}, {"input": {"nums": [19, 17, 8, 25, 23, 24]}, "output": false}, {"input": {"nums": [15, 1, 4, 13]}, "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": "561ca73a3f046b9b"}
vcr_e8499c2764f4
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, 7, 9, 12, 12, 13, 15]}, "output": [0, 7, 9, 12, 13, 15]}, {"input": {"nums": [0, 3, 4, 6, 7, 7, 8, 9, 12]}, "output": [0, 3, 4, 6, 7, 8, 9, 12]}, {"input": {"nums": [3, 6, 6, 8]}, "output": [3, 6, 8]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "b18d3634e667bae7"}
vcr_6bbdfaafb6ff
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": [8, 45, 56, 73, 57, -39, -6, 20, -2], "target": 65}, "output": [0, 4]}, {"input": {"nums": [7, 13, -13, -30, 36, -29, -22, -5, -18], "target": 2}, "output": [0, 7]}, {"input": {"nums": [-18, 16, 15, 49, 48, -24, 43, 44, 45, 10], "target": 64}, "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": "1551d85d85159f79"}
vcr_8d6e2aee26f0
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": 108, "b": 131}, "output": 1}, {"input": {"a": 72, "b": 77}, "output": 1}, {"input": {"a": 77, "b": 153}, "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": "04b15e43508c3027"}
vcr_88b6ccd598ea
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [5, 13, 15, 18], "target": 3}, "output": 0}, {"input": {"nums": [1, 2, 3, 12, 20, 24, 39], "target": 6}, "output": 3}, {"input": {"nums": [2, 6, 7, 9, 12, 15, 18, 32, 33, 34, 35], "target": 34}, "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": "a753707743cb8f20"}
vcr_7134150a55f4
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 1, 4, 11]}, "output": [0, 1, 4, 11]}, {"input": {"nums": [1, 5, 9, 10]}, "output": [1, 5, 9, 10]}, {"input": {"nums": [0, 0, 2, 8, 9, 10, 12, 12]}, "output": [0, 2, 8, 9, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "14a4aed027de3445"}
vcr_e6b699a1c2bd
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, 6, 5, 6, 6, 3]}, "output": [3240, 3240, 3888, 3240, 3240, 6480]}, {"input": {"nums": [2, 2, 1, 3]}, "output": [6, 6, 12, 4]}, {"input": {"nums": [1, 5, 3, 2]}, "output": [30, 6, 10, 15]}]
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": "5efae8a88adf84bb"}
vcr_627a5a06f18e
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": "cbdae ed cdeaa abca dbdbbd aeb"}, "output": "aeb dbdbbd abca cdeaa ed cbdae"}, {"input": {"s": "b b aaab aaba"}, "output": "aaba aaab b b"}, {"input": {"s": "b baba ba baa bbb"}, "output": "bbb baa ba baba 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": "4d0e8450963f1a54"}
vcr_1ac4759bfbc6
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": [4, 7, 9, 10, 13]}, "output": [4, 7, 9, 10, 13]}, {"input": {"nums": [1, 1, 3, 5, 9]}, "output": [1, 3, 5, 9]}, {"input": {"nums": [0, 2, 2, 4, 4, 5, 12, 12]}, "output": [0, 2, 4, 5, 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": "cec4ca044f89a11d"}
vcr_0ee86848a1e5
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 6, 5, 0, 6]}, "output": [0, 0, 0, 900, 0]}, {"input": {"nums": [1, 1, 5]}, "output": [5, 5, 1]}, {"input": {"nums": [5, 4, 3, 1, 3]}, "output": [36, 45, 60, 180, 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": "c48eabafe3178a48"}
vcr_67c434dffe1e
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": ["cc", "aaba", "bba", "bcc"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["baa", "bab", "bba", "bab", "bab", "aab"]}, "output": [2, 4]}, {"input": {"strs": ["bba", "aba", "baa", "baa", "aab"]}, "output": [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": "e97faebf2a8437fb"}
vcr_37303a61a5f4
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": ["ccaa", "aba", "bbcc", "aaa", "aaaa"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bbb", "aba", "aba", "aaa", "aab", "baa"]}, "output": [1, 2, 4]}, {"input": {"strs": ["bbb", "abb", "aab", "abb", "aba", "baa", "bbb"]}, "output": [2, 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": "f00cb5ec56049efe"}
vcr_ff7da65a6d1b
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": 26264}, "output": 7}, {"input": {"n": 1088}, "output": 2}, {"input": {"n": 2124}, "output": 4}]
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": "06ab6b53625f72cb"}
vcr_f2ed8586d8db
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": [-1, 30, 12, 32, 35, 4, 11, 3, -23, 30, 7]}, "output": [-1, 29, 41, 73, 108, 112, 123, 126, 103, 133, 140]}, {"input": {"nums": [5, 11, -8, 15]}, "output": [5, 16, 8, 23]}, {"input": {"nums": [18, 9, 8]}, "output": [18, 27, 35]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "24cb7534d3adf682"}
vcr_1027bba314d8
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-22, -17, -9, 2, 4, 13, 16, 24, 28, 36, 43, 47], "target": 19}, "output": [1, 9]}, {"input": {"nums": [-18, -12, -10, -8, 2, 11, 19, 33, 37], "target": -22}, "output": [1, 2]}, {"input": {"nums": [-17, 6, 7, 26], "target": 9}, "output": [0, 3]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "4175d1cc11b5b1f1"}
vcr_6bfdc485d4bb
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": ["baa", "cbb", "ba", "bcca", "baaa", "bcb"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "aaa", "bba", "bbb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bba", "bab", "bba", "bba"]}, "output": [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": "f35bc96a16c32b5b"}
vcr_fb4bcb07f54d
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [28, 134, 115, 40, 53, 54, 199, 85, 130, 150, 70, 64, 129, 107, 166, 194, 33, 34]}, "output": false}, {"input": {"nums": [12, 18, 10, 2, 11, 2, 15, 5, 13]}, "output": true}, {"input": {"nums": [20, 6, 9, 9, 16, 5]}, "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": "8750db0c887c0ed4"}