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_3377ab2fac03
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 4, 9, 16, 22, 23, 29, 39, 44], "target": 43}, "output": 8}, {"input": {"nums": [1, 4, 12, 19, 22, 28], "target": 14}, "output": 3}, {"input": {"nums": [9, 14, 19, 24, 27, 29], "target": 4}, "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": "7ac16ba5c6411995"}
vcr_131bb3c9985c
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 0, 4, 2, 4, 4, 2, 0, 0, 0]}, "output": true}, {"input": {"nums": [3, 0, 2]}, "output": true}, {"input": {"nums": [1, 3, 3, 0, 0, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "d950b66237e14f07"}
vcr_ce9cee9c7aae
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": [-2, 7, 2, 30, -17, -6, -16, 9, -10, 2, 3, 20, 13]}, "output": [-2, 5, 7, 37, 20, 14, -2, 7, -3, -1, 2, 22, 35]}, {"input": {"nums": [12, -12, -6, 9, 3, 3, -12, 1, 11]}, "output": [12, 0, -6, 3, 6, 9, -3, -2, 9]}, {"input": {"nums": [2, 18, 4, 3, -8]}, "output": [2, 20, 24, 27, 19]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "ff6c8ddebee01128"}
vcr_851b2ad402d5
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [16, 2, 23, 16, 4, 18, 21, 27]}, "output": 84}, {"input": {"nums": [7, 18]}, "output": 18}, {"input": {"nums": [19, 11, 0, 9, 17, 12, 17, 13, 18]}, "output": 71}]
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": "f04ba67b494a67ae"}
vcr_382e2973fd3d
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [6, 9, 10, 12, 16, 29, 34, 35, 38, 41, 46, 49], "target": 21}, "output": 5}, {"input": {"nums": [0, 1, 3, 4, 10, 16, 25, 28, 31, 33, 34, 36], "target": 27}, "output": 7}, {"input": {"nums": [4, 6, 8, 11, 13, 16, 17, 33], "target": 18}, "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": "df9d56121a8d52d3"}
vcr_19bf7ddc332c
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": [-37, 23, 33, -4, 74, 56, -40, 32, 16, 34, 72, 65, -32, -35], "target": 81}, "output": [8, 11]}, {"input": {"nums": [-19, 43, -5, 44, -16], "target": 27}, "output": [1, 4]}, {"input": {"nums": [-19, 43, 16, 46, -10, -7, -18, 26, -22], "target": -25}, "output": [5, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "73a9ac76a33e6e15"}
vcr_1cbccca12a90
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, 5, 5, 6, 8, 10, 10, 11, 12, 14, 15]}, "output": [0, 2, 5, 6, 8, 10, 11, 12, 14, 15]}, {"input": {"nums": [0, 1, 8, 11]}, "output": [0, 1, 8, 11]}, {"input": {"nums": [3, 5, 5, 6, 8, 9, 12, 12]}, "output": [3, 5, 6, 8, 9, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "a01840d15177384c"}
vcr_483f28f0153f
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": [35, 41, 26, 9, 14, 11, 29, 10, 45, 41, 48, 30, 16, 15, 24, 30]}, "output": true}, {"input": {"nums": [0, 22, 12, 15, 7, 10, 1]}, "output": false}, {"input": {"nums": [17, 21, 24, 23, 20, 1, 19, 8, 13, 10, 25, 16]}, "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": "351c505c93041384"}
vcr_76e3fe3efb52
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, 12, 13, 16, 17, 20, 22, 47], "target": 34}, "output": 9}, {"input": {"nums": [7, 8, 37, 39], "target": 24}, "output": 2}, {"input": {"nums": [2, 12, 18, 20, 24, 36], "target": 20}, "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": "bd5b3039ae9b5e23"}
vcr_e4b051e978c3
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, 3, 7, 11, 15, 15, 15]}, "output": [0, 1, 3, 7, 11, 15]}, {"input": {"nums": [3, 3, 4, 5, 5, 11]}, "output": [3, 4, 5, 11]}, {"input": {"nums": [1, 2, 4, 5, 7, 8, 8, 9, 10, 10, 11, 11]}, "output": [1, 2, 4, 5, 7, 8, 9, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "c878c7fdb5399567"}
vcr_c056f893ca6a
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [4, 9, 15, 9, 21, 6, 15, 79, 19, 4, 21, 19, 6]}, "output": 79}, {"input": {"nums": [9, 2, 3, 9, 3, 44, 2, 10, 1, 10, 1]}, "output": 44}, {"input": {"nums": [24, 56, 22, 24, 29, 22, 9, 29, 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": "1d1902b695868cdc"}
vcr_ae84a180e937
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": "bbcbacbcbbbb"}, "output": 8}, {"input": {"s": "abcaa"}, "output": 4}, {"input": {"s": "ccbaabca"}, "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": "ecc32b9d1bef3130"}
vcr_f63d0ea802bd
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [24, 24, 54, 37, 28, 37, 14, 28, 21, 14, 21]}, "output": 54}, {"input": {"nums": [4, 3, 3, 12, 4, 12, 28, 44, 28]}, "output": 44}, {"input": {"nums": [2, 55, 14, 14, 2]}, "output": 55}]
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": "f48620a9739bbaa8"}
vcr_51d4bc95125b
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["aa", "cc", "cac", "cbba", "bcc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["abb", "aba", "abb", "bab"]}, "output": [1, 3]}, {"input": {"strs": ["aab", "aaa", "aaa", "baa"]}, "output": [2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "0153bed9d0fbc34e"}
vcr_693fec291b6b
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": [198, 163, 8, 171, 57, 114, 4, 197, 78, 29, 10, 179, 63]}, "output": false}, {"input": {"nums": [21, 3, 8, 16, 8, 23, 24, 4]}, "output": true}, {"input": {"nums": [12, 14, 2, 4, 10]}, "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": "71a62e7b8db5232d"}
vcr_304043d8db3b
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, 19, 0, -13, 2]}, "output": 34}, {"input": {"nums": [14, 4, 8, -7, 7]}, "output": 26}, {"input": {"nums": [10, -5, -10, -7, 2, -5, -10]}, "output": 10}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "b6ca547a99fcb2c2"}
vcr_81189c8d7264
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": 173, "b": 94}, "output": 1}, {"input": {"a": 174, "b": 6}, "output": 6}, {"input": {"a": 2, "b": 64}, "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": "cf41cb91f15d40d3"}
vcr_fae174d36adc
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 4, 6, 16, 21, 24, 25, 26, 42, 45, 46], "target": 14}, "output": 3}, {"input": {"nums": [1, 2, 10, 13, 14, 18, 22], "target": 15}, "output": 5}, {"input": {"nums": [8, 13, 14, 18, 20, 22, 25, 26, 34], "target": 40}, "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": "9099280f51c6e261"}
vcr_7d82c8596c1b
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, 3, 4, 5, 7, 7, 10, 10, 10, 11, 13]}, "output": [0, 3, 4, 5, 7, 10, 11, 13]}, {"input": {"nums": [0, 2, 2, 2, 5, 6, 10, 10, 10]}, "output": [0, 2, 5, 6, 10]}, {"input": {"nums": [1, 3, 4, 7, 8, 11]}, "output": [1, 3, 4, 7, 8, 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": "7e2cb032c477d8f8"}
vcr_28703b72dc3a
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": "ebdda acae dc ecbacc eedb dad"}, "output": "dad eedb ecbacc dc acae ebdda"}, {"input": {"s": "babb a aba"}, "output": "aba a babb"}, {"input": {"s": "ba ba aa bbba a"}, "output": "a bbba aa ba 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": "f6561017365df0f3"}
vcr_10fb06e639aa
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [31, 31, 23, 20, 23, 4, 20, 4, 74]}, "output": 74}, {"input": {"nums": [12, 11, 12, 11, 42]}, "output": 42}, {"input": {"nums": [58, 24, 5, 25, 3, 23, 25, 24, 3, 5, 23]}, "output": 58}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "51ea326323b0b655"}
vcr_1ebbca6a85ad
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [27, 31, 57, 34, 39, 3, 54, 34, 25, 21, 43, 19]}, "output": 51}, {"input": {"prices": [14, 11, 19, 4, 23, 25]}, "output": 21}, {"input": {"prices": [40, 15, 15, 10, 33, 35, 18, 19, 3, 16, 6]}, "output": 25}]
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": "5fc16e62412f35f2"}
vcr_4dabe301d00f
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": "dababcddcbabad"}, "output": true}, {"input": {"s": "bcbacab"}, "output": false}, {"input": {"s": "aaaa"}, "output": true}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "f6b21f580347f78e"}
vcr_872e90452990
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": "cab bddd adaeea edcbe"}, "output": "edcbe adaeea bddd cab"}, {"input": {"s": "aabb bbab aba"}, "output": "aba bbab aabb"}, {"input": {"s": "a abba"}, "output": "abba 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": "ba94960cc4ea539e"}
vcr_30256060cfc4
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": 214, "b": 236}, "output": 2}, {"input": {"a": 164, "b": 70}, "output": 2}, {"input": {"a": 49, "b": 132}, "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": "7871edf3d2994dc3"}
vcr_fc2e67dd93e2
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [19, 20, 18, 8, 11, -25, 16, -22, 21, -23, 30, 13, 27]}, "output": [19, 39, 57, 65, 76, 51, 67, 45, 66, 43, 73, 86, 113]}, {"input": {"nums": [14, -8, 17]}, "output": [14, 6, 23]}, {"input": {"nums": [3, 3, 7, -4, 14]}, "output": [3, 6, 13, 9, 23]}]
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": "69c07304cca7c895"}
vcr_6fd51111dcc5
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, 30, 1, 1, 29, 21, 29, 35, 53, 38, 21, 35, 38]}, "output": 53}, {"input": {"nums": [6, 7, 9, 42, 7, 20, 23, 6, 9, 19, 19, 20, 23]}, "output": 42}, {"input": {"nums": [10, 6, 10, 12, 42, 14, 11, 11, 6, 14, 12]}, "output": 42}]
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": "e7f9b3049a2bcb7d"}
vcr_81634c2bd04e
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, 12, 3, -4, 5, -4, 3]}, "output": 23}, {"input": {"nums": [4, -10, 9, 13, 12, -2, 11, -9, 2]}, "output": 43}, {"input": {"nums": [1, -6, -8, 11, -9, 4, -3]}, "output": 11}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "6c47d8bf125ca0e5"}
vcr_453064b6b28c
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, 17, 54, 34, 36, 34, 12, 17, 37, 36, 35, 12, 37]}, "output": 54}, {"input": {"nums": [21, 16, 6, 8, 21, 16, 52, 8, 6]}, "output": 52}, {"input": {"nums": [3, 9, 2, 41, 9, 2, 8, 3, 21, 21, 8]}, "output": 41}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "15efce03acc6d5db"}
vcr_3a19587c266d
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [2, 2, 8, 8, 9, 10, 11, 12, 13]}, "output": [2, 8, 9, 10, 11, 12, 13]}, {"input": {"nums": [2, 3, 3, 4, 6, 6, 7, 10, 12]}, "output": [2, 3, 4, 6, 7, 10, 12]}, {"input": {"nums": [0, 1, 1, 2, 3, 4, 4, 7, 8, 8, 9, 11]}, "output": [0, 1, 2, 3, 4, 7, 8, 9, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "71d6adff0f1a34a5"}
vcr_42a57cf3f36b
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": [21, -14, 6, -16, 14, -14, 22, 1, 19, 10, 18, 18, -13]}, "output": 88}, {"input": {"nums": [-9, -9, -3, -3, -8, -9, -6]}, "output": -3}, {"input": {"nums": [9, -7, -2, -2, 12, 10, 2, 3, -7, 1, -7]}, "output": 27}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "7b546d57032d1821"}
vcr_c7b93ac9d14f
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [16, 9, 11, 18, 15, 42, 2, 9, 7, 9, 25, 47, 23, 24, 35, 12]}, "output": true}, {"input": {"nums": [1, 21, 17, 20, 9, 4, 5, 22, 1, 16, 14, 13]}, "output": true}, {"input": {"nums": [24, 16, 18]}, "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": "d7e598f99efeb9f3"}
vcr_e5ba379e08c8
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": "bcc e be abdb bcecee edad"}, "output": "edad bcecee abdb be e bcc"}, {"input": {"s": "aab aba"}, "output": "aba aab"}, {"input": {"s": "aaab b"}, "output": "b aaab"}]
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": "9a7874d3fa3dc733"}
vcr_76b701f0b27d
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [0, 7, 1, 1, 4, 6]}, "output": [168, 0, 0, 0, 0, 0]}, {"input": {"nums": [2, 1, 4, 4, 1]}, "output": [16, 32, 8, 8, 32]}, {"input": {"nums": [5, 1, 1, 1]}, "output": [1, 5, 5, 5]}]
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": "c31537c7d26c8265"}
vcr_1f26759a0927
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": [10, 28, 2, 30, 2, 26, 12, 30, 75, 28, 23, 12, 23, 10, 26]}, "output": 75}, {"input": {"nums": [19, 18, 20, 18, 22, 13, 20, 19, 58, 13, 22]}, "output": 58}, {"input": {"nums": [26, 41, 17, 17, 26]}, "output": 41}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "64397408b68fc9cf"}
vcr_3f917b6e1051
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": [19, 24, 10]}, "output": 29}, {"input": {"nums": [1, 20, 11, 1]}, "output": 21}, {"input": {"nums": [5, 0, 8, 4, 13, 3, 3, 16, 17]}, "output": 46}]
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": "ce2fc24546e701f6"}
vcr_918a32124256
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, -23, -18, -3, 16, 21, 32, 39, 40, 42, 48], "target": 16}, "output": [1, 7]}, {"input": {"nums": [-20, -19, -12, -8, -7, -5, -4, 3, 29, 34, 38], "target": 25}, "output": [6, 8]}, {"input": {"nums": [-17, -15, -14, -6, 30, 35, 37], "target": 31}, "output": [3, 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": "0c49568f66eb09f4"}
vcr_6911e2a3f771
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [4, 1, 3, 6, 7, 3, 3, 5]}, "output": [5670, 22680, 7560, 3780, 3240, 7560, 7560, 4536]}, {"input": {"nums": [2, 3, 1]}, "output": [3, 2, 6]}, {"input": {"nums": [5, 5, 3, 1]}, "output": [15, 15, 25, 75]}]
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": "b408769624acc582"}
vcr_8135d0b93e8a
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-18, -9, -10, 15, -14, 0, 6, -9, 12]}, "output": 15}, {"input": {"nums": [1, 0, -8, 5, 6, -3, -5, 2, -10, 3, 8]}, "output": 11}, {"input": {"nums": [2, -6, -6, 12]}, "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": "b6561cf6c28e9b6b"}
vcr_bbbf41646d02
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-18, 4, -25, 21, 19]}, "output": [-18, -14, -39, -18, 1]}, {"input": {"nums": [13, -9, 4, 7, -3]}, "output": [13, 4, 8, 15, 12]}, {"input": {"nums": [15, -11, 14]}, "output": [15, 4, 18]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "131ff0d37a4d9031"}
vcr_08b42b47ae89
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [4, 4, 26, 61, 23, 3, 19, 30, 26, 3, 30, 23, 19]}, "output": 61}, {"input": {"nums": [1, 7, 51, 8, 15, 8, 7, 18, 18, 1, 15]}, "output": 51}, {"input": {"nums": [7, 53, 11, 15, 2, 2, 15, 23, 22, 22, 23, 7, 11]}, "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": "769bf1bb925581aa"}
vcr_71f953453097
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 0, 1, 7]}, "output": [0, 14, 0, 0]}, {"input": {"nums": [4, 2, 4, 5, 3, 4, 2]}, "output": [960, 1920, 960, 768, 1280, 960, 1920]}, {"input": {"nums": [2, 1, 3, 5, 5, 1]}, "output": [75, 150, 50, 30, 30, 150]}]
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": "f75b878c5b663fd7"}
vcr_d890e26c8f82
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [3, 10, 3, -16, -11, -14, 13, 18, 20, 17, -7], "k": 13}, "output": [17, -7, 3, 10, 3, -16, -11, -14, 13, 18, 20]}, {"input": {"nums": [2, 6, -10, 7, -2, 4, 8, 0, -3, 1], "k": 11}, "output": [1, 2, 6, -10, 7, -2, 4, 8, 0, -3]}, {"input": {"nums": [-8, 1, 3, -3, 2, 2, 2, -5], "k": 8}, "output": [-8, 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": "84b445ecb781a5ff"}
vcr_ea595fd771b8
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, 16, 22, 34, 40], "target": 1}, "output": 0}, {"input": {"nums": [8, 16, 36], "target": 35}, "output": 2}, {"input": {"nums": [0, 2, 3, 6, 14, 18, 26, 27, 29, 35], "target": 36}, "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": "d7b53b77876510da"}
vcr_6e167d8ed265
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": 9005}, "output": 7}, {"input": {"n": 2422}, "output": 7}, {"input": {"n": 3356}, "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": "f402186ce931e04f"}
vcr_ac31fdf631ba
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-9, -10, -19, 14, -16, 10, 0], "k": 4}, "output": [14, -16, 10, 0, -9, -10, -19]}, {"input": {"nums": [4, -1, 5, -3, 6, -1, 10, -1], "k": 1}, "output": [-1, 4, -1, 5, -3, 6, -1, 10]}, {"input": {"nums": [4, 6, -1, 8, 1, 8, -10, 4], "k": 9}, "output": [4, 4, 6, -1, 8, 1, 8, -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": "05fcda7ac32539c4"}
vcr_33b1c2f8ff2d
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": ["cbca", "ba", "ccb", "aaca", "abac", "bb", "abac", "bab", "abc"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bbb", "aaa", "bab", "aab", "aaa"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["bab", "baa", "aba", "aba", "bbb"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "80dd49a9dd7b1077"}
vcr_3f3763f53716
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 5, 3, 7, 7]}, "output": [735, 735, 1225, 525, 525]}, {"input": {"nums": [3, 3, 5, 5, 5]}, "output": [375, 375, 225, 225, 225]}, {"input": {"nums": [3, 4, 3, 4, 4]}, "output": [192, 144, 192, 144, 144]}]
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": "615ef42b47b6cfdf"}
vcr_9e99e4935b6b
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": [35, 12, 18, 34, -1, -4, 34, 28]}, "output": [35, 47, 65, 99, 98, 94, 128, 156]}, {"input": {"nums": [-2, 6, -4, 4, -11]}, "output": [-2, 4, 0, 4, -7]}, {"input": {"nums": [-5, 8, 15, 3, 1, -3, -3, 14]}, "output": [-5, 3, 18, 21, 22, 19, 16, 30]}]
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": "8a9a65beb6f65320"}
vcr_1bdf0c34eb49
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": 88, "b": 94}, "output": 2}, {"input": {"a": 92, "b": 72}, "output": 4}, {"input": {"a": 133, "b": 107}, "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": "0cd1d4501818e0ed"}
vcr_4be0388850b4
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 1, 3, 5, 6, 10, 10, 12, 12, 13, 15, 15, 15]}, "output": [1, 3, 5, 6, 10, 12, 13, 15]}, {"input": {"nums": [0, 1, 3, 4, 4, 7, 7, 8, 10, 11, 12]}, "output": [0, 1, 3, 4, 7, 8, 10, 11, 12]}, {"input": {"nums": [1, 1, 1, 3, 3, 7, 10, 11]}, "output": [1, 3, 7, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "7d65d6451648a2f4"}
vcr_0fa7e6560fa7
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": [23, 21, 13, 11, 28, 5, 13, 21, 23, 11, 28, 61, 5]}, "output": 61}, {"input": {"nums": [9, 20, 9, 27, 27, 2, 2, 18, 3, 20, 3, 48, 18]}, "output": 48}, {"input": {"nums": [12, 12, 18, 45, 16, 18, 23, 16, 23]}, "output": 45}]
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": "8a26260897063a65"}
vcr_43b9b4db2ca2
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": 220, "b": 20}, "output": 20}, {"input": {"a": 172, "b": 113}, "output": 1}, {"input": {"a": 46, "b": 121}, "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": "9c829612351de6ec"}
vcr_66fb99eb371e
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [5, 30, 28, 4, 9, 3, 3]}, "output": 45}, {"input": {"nums": [6, 10]}, "output": 10}, {"input": {"nums": [20, 7, 4, 6, 20, 12]}, "output": 44}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "5784db1c2af89bc8"}
vcr_cbf3408a5905
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": [9, 9, 6, 69, 6, 7, 7]}, "output": 69}, {"input": {"nums": [8, 18, 18, 8, 21, 19, 1, 55, 1, 19, 21]}, "output": 55}, {"input": {"nums": [1, 15, 26, 50, 26, 1, 15]}, "output": 50}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "c5df538ccf60c69e"}
vcr_9735bccba771
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": [60, 9, 11, 9, 35, 11, 35]}, "output": 60}, {"input": {"nums": [21, 21, 16, 19, 29, 44, 13, 19, 29, 16, 13, 22, 22]}, "output": 44}, {"input": {"nums": [21, 19, 28, 21, 3, 28, 19, 47, 3]}, "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": "0be352b7b86544c6"}
vcr_f57e8fc2606a
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, 30, 48, 39, 2, 35, 27, 53, 10, 57]}, "output": 55}, {"input": {"prices": [4, 7, 27, 27, 18, 12, 36, 15, 24, 40, 20]}, "output": 36}, {"input": {"prices": [37, 19, 26, 12, 36, 27, 37, 21, 29, 23]}, "output": 25}]
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": "5dae304e6458d4fc"}
vcr_0927a413f66e
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": [-20, -17, -9, -6, 4, 14, 20, 30, 32, 36, 45, 48], "target": 18}, "output": [4, 5]}, {"input": {"nums": [-18, -10, 5, 20, 23, 30, 35], "target": 12}, "output": [0, 5]}, {"input": {"nums": [-18, -17, -11, -7, 0, 8, 9, 12, 13, 25, 31], "target": 8}, "output": [1, 9]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "4b74c3868f762154"}
vcr_eec532c165a8
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": [59, 40, 7, 12, 6, 3, 14, 10, 47, 54, 59, 42]}, "output": 56}, {"input": {"prices": [33, 35, 25]}, "output": 2}, {"input": {"prices": [36, 16, 36, 5]}, "output": 20}]
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": "d9fd2d32b416f2e1"}
vcr_9010894284fd
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-5, 8, 6, -5, -11, 19, -12, 6]}, "output": 19}, {"input": {"nums": [8, -2, 6, -2, -9, -10, 12, -1, -9, -6]}, "output": 12}, {"input": {"nums": [-8, -4, -7, 1, -5, 12, 9, 3]}, "output": 24}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "956062d3a2e8ef5a"}
vcr_c041e37bb8ed
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": [68, 38, 29, 63, -36, -6, 18, 76], "target": 101}, "output": [1, 3]}, {"input": {"nums": [-21, -8, 11, 4, -30, 33, 31, 32, 9], "target": 13}, "output": [3, 8]}, {"input": {"nums": [20, 38, 23, 29, 39, -27, -3, -28, -7, 30, 7], "target": -55}, "output": [5, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "e7adadb05b6a8b29"}
vcr_6b59218693e5
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-3, -18, 3, 9, 16, 14, -4, -3, 12], "k": 6}, "output": [9, 16, 14, -4, -3, 12, -3, -18, 3]}, {"input": {"nums": [-7, 9, -6, -8, -10, 2], "k": 7}, "output": [2, -7, 9, -6, -8, -10]}, {"input": {"nums": [8, -1, 6, 8, 9], "k": 15}, "output": [8, -1, 6, 8, 9]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "8ce2d5a2bcebac1e"}
vcr_ee4e4596cb54
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, 57, 50, 11, 12, 53, 32, 38]}, "output": 42}, {"input": {"prices": [11, 36, 19, 9, 3, 16, 28]}, "output": 25}, {"input": {"prices": [27, 9, 15, 14, 29, 5, 11, 27]}, "output": 22}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "b4089b6aba3abc59"}
vcr_4dd21208144c
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": "acd decdc c ece dcdbaa adaaee"}, "output": "adaaee dcdbaa ece c decdc acd"}, {"input": {"s": "aa baa bbab"}, "output": "bbab baa aa"}, {"input": {"s": "aabb b bab a b"}, "output": "b a bab b aabb"}]
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": "d6b9bed207b55ffd"}
vcr_0856619a45e2
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": [12, 4, 11, 11, 3, 6, 23, 6, 12, 10, 3, 64, 10, 23, 20, 4, 20]}, "output": 64}, {"input": {"nums": [20, 6, 20, 13, 13, 6, 8, 8, 47, 11, 11]}, "output": 47}, {"input": {"nums": [24, 24, 53, 25, 25, 28, 28]}, "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": "bdb3a573e0f79f29"}
vcr_2d5efb613aeb
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": ["cacb", "bb", "bac", "cc", "abb", "baa", "ccc", "acc", "bc"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["baa", "aab", "aab", "aab", "aaa"]}, "output": [1, 4]}, {"input": {"strs": ["bba", "bbb", "bbb", "bbb"]}, "output": [1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "6c17b333fdfd3122"}
vcr_bf64c0d8096f
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-18, -10, 18, 4, -11, -5, 8, 8, -5], "k": 8}, "output": [-10, 18, 4, -11, -5, 8, 8, -5, -18]}, {"input": {"nums": [-6, -6, 3, -1, 0, 6, -9, 10], "k": 13}, "output": [-1, 0, 6, -9, 10, -6, -6, 3]}, {"input": {"nums": [-7, -6, 6, 9, 8, 8, 3], "k": 9}, "output": [8, 3, -7, -6, 6, 9, 8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "9ff2f6925bb8bd6c"}
vcr_cb24a3737923
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [4, 6, 4, 1, 4, 2]}, "output": [192, 128, 192, 768, 192, 384]}, {"input": {"nums": [2, 5, 2, 5]}, "output": [50, 20, 50, 20]}, {"input": {"nums": [4, 4, 1, 3, 1, 4, 4]}, "output": [192, 192, 768, 256, 768, 192, 192]}]
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": "5604a72cdcc3f9c6"}
vcr_284d49c8b462
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-18, -15, 17, -3, 8, 1, -11], "k": 2}, "output": [1, -11, -18, -15, 17, -3, 8]}, {"input": {"nums": [-5, -9, -4, -10, -7, -9, -5, -7, 3, -5], "k": 2}, "output": [3, -5, -5, -9, -4, -10, -7, -9, -5, -7]}, {"input": {"nums": [-9, -10, 8, -9, -1, -4, 2], "k": 17}, "output": [-1, -4, 2, -9, -10, 8, -9]...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "19d98d48fabc7013"}
vcr_3bcbd9204978
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": [48, 5, 22, 16, 23, 24, 29, 47, 43, 29, 37, 8, 33, 13, 3]}, "output": 42}, {"input": {"prices": [13, 38, 25, 33, 39, 20, 27, 25, 26]}, "output": 26}, {"input": {"prices": [21, 3, 13, 18, 28, 37]}, "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": "7d5bf9a9fbe17555"}
vcr_fb6263e630c3
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": [-9, 16, 6, 8], "target": 22}, "output": [1, 2]}, {"input": {"nums": [26, -18, 38, 41, 45, -9, -7, 21], "target": 27}, "output": [1, 4]}, {"input": {"nums": [25, 6, 4, -6, 37, 36], "target": 73}, "output": [4, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "a387b422664e9736"}
vcr_f544c58e895a
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, 3, 4, 5, 6, 8, 8, 10, 10, 11, 12, 13, 14, 15]}, "output": [3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15]}, {"input": {"nums": [0, 0, 0, 4, 4, 7, 9, 10]}, "output": [0, 4, 7, 9, 10]}, {"input": {"nums": [2, 4, 6, 10, 11]}, "output": [2, 4, 6, 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": "0ae44c7745bea72e"}
vcr_bcb74cacc4c8
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": [5, 25, 5, 34]}, "output": 29}, {"input": {"prices": [11, 36, 34]}, "output": 25}, {"input": {"prices": [13, 27, 8, 39, 27]}, "output": 31}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "cd83d6a65335cc49"}
vcr_4e3ef092786c
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [11, 29, 8, 29, 0, 16, 17, 8, 3, 7, 3]}, "output": 89}, {"input": {"nums": [4, 5, 6]}, "output": 10}, {"input": {"nums": [19, 19]}, "output": 19}]
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": "c0b2176bbd5ab442"}
vcr_86ec89f5ae0b
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, 9, 12, 23, 25, 26, 28, 30, 37, 46, 49], "target": 28}, "output": 6}, {"input": {"nums": [5, 6, 15, 23, 24, 30, 39], "target": 6}, "output": 1}, {"input": {"nums": [2, 3, 8, 10, 14, 23, 24, 32, 33, 36, 38], "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": "c1e95742c14e7442"}
vcr_89bcfe85d0aa
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -16, -10, 8, 20, 27, 34, 40], "target": -18}, "output": [0, 3]}, {"input": {"nums": [-17, -12, -6, 5, 21, 22, 37], "target": -29}, "output": [0, 1]}, {"input": {"nums": [-19, -17, -16, -15, 3, 9, 16, 21, 28, 32, 33], "target": -14}, "output": [1, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "db0423d6caf34223"}
vcr_a324a3476a8c
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": 54656}, "output": 6}, {"input": {"n": 2615}, "output": 7}, {"input": {"n": 16091}, "output": 11}]
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": "b3843bf356105c5f"}
vcr_d63e5bbf4fdc
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["aaa", "aa", "cccc", "cb", "ba", "ca", "cbaa", "bb", "ab"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bbb", "aba", "aba", "baa", "aab", "bba"]}, "output": [1, 2, 4]}, {"input": {"strs": ["bab", "baa", "baa", "baa", "aab", "aaa", "bba"]}, "output": [1, 2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "1a864613346769f8"}
vcr_03d47ce85335
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": [44, 12, 27, 19, 29, 32, 1, 15, 3, 8, 3, 26, 48]}, "output": 47}, {"input": {"prices": [21, 20, 6, 9, 35, 7, 7, 21, 10, 16, 14]}, "output": 29}, {"input": {"prices": [37, 36, 22]}, "output": 0}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "e6ef7d85dd3c0b56"}
vcr_5153024c2919
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "c dadc d ceb"}, "output": "ceb d dadc c"}, {"input": {"s": "aa bbbb aaa aab baab"}, "output": "baab aab aaa bbbb aa"}, {"input": {"s": "aaab aa baa"}, "output": "baa aa aaab"}]
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": "111b786d3de1f781"}
vcr_c7cdb4facc82
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": [32, -21, 20, 13, -15, 8, -23, -11, 5, -5, 13, -25, 19]}, "output": [32, 11, 31, 44, 29, 37, 14, 3, 8, 3, 16, -9, 10]}, {"input": {"nums": [-5, -6, 10, -9]}, "output": [-5, -11, -1, -10]}, {"input": {"nums": [5, -5]}, "output": [5, 0]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "42480fc8bfea0b42"}
vcr_7abdc12bd805
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": "bbbbaaaaaa"}, "output": 2}, {"input": {"s": "caaaaab"}, "output": 3}, {"input": {"s": "baaacbbcacba"}, "output": 9}]
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": "ef3e5c5f4664e70f"}
vcr_faf42d68373c
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 9, 12, 19, 22, 24, 29, 37, 42, 44, 45, 46, 47, 48], "target": 28}, "output": 6}, {"input": {"nums": [7, 8, 9, 18, 36, 39], "target": 8}, "output": 1}, {"input": {"nums": [8, 17, 31], "target": 33}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "d699e3cf9403d34c"}
vcr_d3eb114be55a
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": [35, 30, 12, 2, 12, 4, 34, 3, 3, 46]}, "output": 44}, {"input": {"prices": [6, 21, 15, 23, 13, 9]}, "output": 17}, {"input": {"prices": [29, 2, 40, 14, 35]}, "output": 38}]
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": "ba414d8ef6adce47"}
vcr_60caf039a6b4
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [17, 18, 22, 23, 27, 28, 34, 42, 44, 46, 49], "target": 46}, "output": 9}, {"input": {"nums": [3, 9, 12, 13, 21, 22, 30, 32, 36, 37, 38, 39], "target": 13}, "output": 3}, {"input": {"nums": [2, 4, 7, 13, 15, 16, 17, 31, 34, 38], "target": 34}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "e2b4c6fe75fc4196"}
vcr_08c38ebd5ab6
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": [71, 34, 10, 10, 32, 32, 34]}, "output": 71}, {"input": {"nums": [24, 7, 25, 29, 25, 5, 29, 24, 16, 5, 57, 16, 7]}, "output": 57}, {"input": {"nums": [6, 19, 3, 4, 19, 4, 53, 3, 6]}, "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": "7065d8c9804d18a2"}
vcr_675098957623
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": [12, 7, -1, -7, -12, -25]}, "output": [12, 19, 18, 11, -1, -26]}, {"input": {"nums": [5, -7, -10, -5, 13, 10, 1, 6, -8]}, "output": [5, -2, -12, -17, -4, 6, 7, 13, 5]}, {"input": {"nums": [12, 6, -5, 12, 4, -12, 9]}, "output": [12, 18, 13, 25, 29, 17, 26]}]
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": "2deefedd3068daf0"}
vcr_ef437c0310a3
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, 2, 3, 0, 3, 2, 0]}, "output": true}, {"input": {"nums": [3, 1, 2, 3]}, "output": true}, {"input": {"nums": [0, 1, 1, 0, 1, 0, 2, 2, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "7750207a1d12fad5"}
vcr_79300baf03af
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [10, 5, 19, 16, 3, 20]}, "output": 49}, {"input": {"nums": [3, 9, 18, 13, 6, 20, 14, 2, 16]}, "output": 58}, {"input": {"nums": [7, 2, 15, 11, 13, 1, 10]}, "output": 45}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "f82610161ddaed80"}
vcr_074309a812d7
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": [21, -10, 11, -5, 8, 13, 16, -2, -4, 12, 10, -10, 4, -8]}, "output": 70}, {"input": {"nums": [-5, 13, -10, -10, 14]}, "output": 14}, {"input": {"nums": [-5, -5, 12, 3, -8, 8, -6, 3, 0, 3, 9]}, "output": 24}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "970a1bf01046cc1c"}
vcr_494cbd4e228d
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [4, 4, 4, 3, 3, 7]}, "output": [1008, 1008, 1008, 1344, 1344, 576]}, {"input": {"nums": [2, 4, 5, 3, 1]}, "output": [60, 30, 24, 40, 120]}, {"input": {"nums": [5, 3, 1, 2, 2, 5]}, "output": [60, 100, 300, 150, 150, 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": "743aa73f15be861b"}
vcr_089b89ea7359
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": [-17, 20, -5, 9, 0, -18, 10, -12, -15], "k": 13}, "output": [-18, 10, -12, -15, -17, 20, -5, 9, 0]}, {"input": {"nums": [6, -9, -5], "k": 13}, "output": [-5, 6, -9]}, {"input": {"nums": [9, 4, 3, 0, 0, 6], "k": 9}, "output": [0, 0, 6, 9, 4, 3]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "c645865ad6f9e630"}
vcr_dcae22d98f78
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["aa", "baac", "cbaa", "cacc", "bbb", "bb", "bbb", "baa"]}, "output": [1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["aab", "bbb", "bab", "aba", "aaa", "bba"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["abb", "bab", "aaa", "bab", "bbb", "aba", "bba"]}, "output": [1, 1, 1, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "774312c311b4df51"}
vcr_0ef0be0764ed
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, 2, 5, 1]}, "output": [10, 5, 2, 10]}, {"input": {"nums": [2, 3, 3, 1, 5]}, "output": [45, 30, 30, 90, 18]}, {"input": {"nums": [3, 2, 5, 5, 5, 5, 4]}, "output": [5000, 7500, 3000, 3000, 3000, 3000, 3750]}]
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": "533af6b8ec9887f5"}
vcr_8a3665c1919b
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, 19, 51, 47, 53, 20, 32, 25, 31, 30, 27, 3, 29, 29]}, "output": 34}, {"input": {"prices": [12, 18, 34, 2, 38, 5, 3, 32]}, "output": 36}, {"input": {"prices": [36, 15, 22, 31, 10, 3, 30, 35, 12, 27, 17, 20]}, "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": "42e7648ff9e781d0"}
vcr_b82376a52159
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": [12, -22, 3, -10, 32]}, "output": [12, -10, -7, -17, 15]}, {"input": {"nums": [10, -6, 8, -11, -7, 2]}, "output": [10, 4, 12, 1, -6, -4]}, {"input": {"nums": [18, -7, 5, 10, 16]}, "output": [18, 11, 16, 26, 42]}]
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": "3d911cc401fdfbf5"}
vcr_546156f2112a
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 3, 3, 0, 1, 2, 3, 1, 0]}, "output": true}, {"input": {"nums": [1, 2, 2, 2, 0, 2, 2, 1, 3]}, "output": true}, {"input": {"nums": [0, 3, 0, 1, 0, 1, 3, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "2e3e6a3054d4a5a0"}
vcr_73cfac76fd3d
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": [-17, -7, -3, 6, 2, -13, 19, 3, -3, 7, -5], "k": 3}, "output": [-3, 7, -5, -17, -7, -3, 6, 2, -13, 19, 3]}, {"input": {"nums": [-7, 2, -2, -6, -5, 9, -9], "k": 0}, "output": [-7, 2, -2, -6, -5, 9, -9]}, {"input": {"nums": [-1, -4, 7, -8, 10], "k": 7}, "output": [-8, 10, -1, -4, 7]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "02c791a41d2a8a65"}
vcr_96015f1ba74e
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": "badbbbbbbdab"}, "output": true}, {"input": {"s": "accca"}, "output": true}, {"input": {"s": "bcccbbbaac"}, "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": "0a4eb27a81aabb6c"}
vcr_b9f1808efe92
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [12, 3, -17, -16, -8]}, "output": 15}, {"input": {"nums": [-7, 8, -5, 11, 7, 2]}, "output": 23}, {"input": {"nums": [2, 2, -11, -6, 14, -8, -1, 9, -8, 10]}, "output": 16}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "9d40dc782db125f9"}