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_103489abfb15
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 2, 0, 0, 3, 1, 2, 3, 0]}, "output": false}, {"input": {"nums": [3, 1, 1]}, "output": true}, {"input": {"nums": [3, 3, 3, 2, 2, 3, 1, 0, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "be583cd959855378"}
vcr_b523c1859849
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [4, 8, 4, 26, 1, 12, 44, 44, 9, 9, 9, 40, 44, 15, 19]}, "output": true}, {"input": {"nums": [4, 0, 13, 17, 24, 19, 9, 8, 15, 20]}, "output": false}, {"input": {"nums": [13, 2, 13, 18, 19]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "c0fc53f5866b65a9"}
vcr_6598da1e4c49
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [22, 12, 0]}, "output": 22}, {"input": {"nums": [16, 10, 14, 20, 3, 10]}, "output": 46}, {"input": {"nums": [14, 1, 15, 1, 18, 3, 2, 12, 1]}, "output": 59}]
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": "98146013644aa7e0"}
vcr_7b2cdc5687e4
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": "aabaaabaabb"}, "output": 6}, {"input": {"s": "aacbc"}, "output": 4}, {"input": {"s": "cccb"}, "output": 2}]
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": "f9bfff376be7af1b"}
vcr_bedc64aa051e
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": [23, 2, 18, 24, 44, 6, 47, 9, 20, 17, 40, 1, 38, 39, 10, 47, 25]}, "output": true}, {"input": {"nums": [19, 19, 23, 20]}, "output": true}, {"input": {"nums": [1, 3, 17, 10, 24, 12, 1, 21, 23, 25, 12]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "63766c9741f3f98f"}
vcr_520acbd006c3
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": [36, 26, 5, 16, 33, 5, 26, 36, 1, 33, 66, 1, 16]}, "output": 66}, {"input": {"nums": [18, 8, 18, 15, 13, 15, 53, 13, 8]}, "output": 53}, {"input": {"nums": [44, 9, 4, 4, 9]}, "output": 44}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "c4b1b70f1e88364d"}
vcr_59b198ec7ad1
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": "eba eeca ecdade ce ecb bbe cddac"}, "output": "cddac bbe ecb ce ecdade eeca eba"}, {"input": {"s": "bab aaaa"}, "output": "aaaa bab"}, {"input": {"s": "bba b ba bb baab"}, "output": "baab bb ba b bba"}]
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": "007db6652513099e"}
vcr_a80382e86947
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, 15, 7, 8, 29, 40, 23, 34, 53, 30, 28, 48]}, "output": 46}, {"input": {"prices": [24, 27, 8, 1, 9, 9, 19, 12]}, "output": 18}, {"input": {"prices": [11, 40, 6, 38, 25, 7, 31, 4, 26]}, "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": "ebd60c63dae0f77b"}
vcr_b4ea560d593b
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": [6, -13, 12, 7, -3, 5, -15, -5, -10]}, "output": 21}, {"input": {"nums": [8, -10, -3, -1, -3, 9]}, "output": 9}, {"input": {"nums": [11, 5, -12, 4, 7, -7]}, "output": 16}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "2967a4879065ec33"}
vcr_b66867b6248b
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": [70, -6, -36, 6, 11, -9, 79, 8, 0, -40, 44, 49, -15, -21], "target": 30}, "output": [0, 9]}, {"input": {"nums": [16, 35, 7, 20, 37, 33], "target": 44}, "output": [2, 4]}, {"input": {"nums": [43, -7, -16, -5, -6, -24, 24, -12], "target": -17}, "output": [3, 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": "43201aabca433d1b"}
vcr_2de275646700
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": [13, 14, -14, 12, 0, 32, -7]}, "output": [13, 27, 13, 25, 25, 57, 50]}, {"input": {"nums": [12, -7, -12, -2, -1, -4, 9, -8, 1, -10]}, "output": [12, 5, -7, -9, -10, -14, -5, -13, -12, -22]}, {"input": {"nums": [12, -11, -1, -2, -7, 7]}, "output": [12, 1, 0, -2, -9, -2]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "6a7a1381ec85531b"}
vcr_88e87cecb7ff
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [5, 16, 34, -24]}, "output": [5, 21, 55, 31]}, {"input": {"nums": [4, 8, -8, -8, -7, 15, 0, -12]}, "output": [4, 12, 4, -4, -11, 4, 4, -8]}, {"input": {"nums": [10, 17, 6, 10, -12, -12, 3]}, "output": [10, 27, 33, 43, 31, 19, 22]}]
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": "3067583e58895230"}
vcr_b224402eef8e
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": 41, "b": 22}, "output": 1}, {"input": {"a": 1, "b": 143}, "output": 1}, {"input": {"a": 90, "b": 160}, "output": 10}]
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": "8d04172be851029c"}
vcr_e05153f0782e
hard
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "bbdccdbddddadbac"}, "output": false}, {"input": {"s": "cacabbaccc"}, "output": false}, {"input": {"s": "bac"}, "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": "6ad217843102fbbf"}
vcr_077d41ea3aeb
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [22, 1, -16, 3, -11, 22, 5, 7, -6, 12, -6, -17, 16]}, "output": 40}, {"input": {"nums": [3, -8, 6, 7, -4, -11, 2, 12, -2, 3]}, "output": 15}, {"input": {"nums": [7, -5, 8, 9, 2, -12, -8, 12, -11]}, "output": 21}]
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": "e94bc9190cca4b3f"}
vcr_ae600157ccc7
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": [0, -14, 20, -16, 35, 23]}, "output": [0, -14, 6, -10, 25, 48]}, {"input": {"nums": [-4, 2, -10, -4, 17, 5, -11, -1, 1, 3]}, "output": [-4, -2, -12, -16, 1, 6, -5, -6, -5, -2]}, {"input": {"nums": [1, -1, 16, 6]}, "output": [1, 0, 16, 22]}]
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": "f276b99c3488bf8e"}
vcr_82e13621dea1
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-19, -15, 10, 10, 20], "k": 2}, "output": [10, 20, -19, -15, 10]}, {"input": {"nums": [-10, 0, 1, -10, -9], "k": 11}, "output": [-9, -10, 0, 1, -10]}, {"input": {"nums": [10, 7, -10, 7, -2], "k": 8}, "output": [-10, 7, -2, 10, 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": "c9a094467648d6ec"}
vcr_3800ed0c7194
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-20, 11, 8, -7, 15, 6, 26, -9]}, "output": [-20, -9, -1, -8, 7, 13, 39, 30]}, {"input": {"nums": [1, -9, 7, -1, 4]}, "output": [1, -8, -1, -2, 2]}, {"input": {"nums": [16, -6, 15]}, "output": [16, 10, 25]}]
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": "028bc50dac443a7c"}
vcr_e0ec5005662b
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 2, 4, 1, 1, 1, 0, 1, 0, 0]}, "output": false}, {"input": {"nums": [1, 1, 0, 2, 3, 1, 0, 2]}, "output": false}, {"input": {"nums": [2, 3, 1, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "3cae3d9e697b5445"}
vcr_9a0b5917dc39
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": [-17, -20, -25, 23, 29, -15, -5, 17, 26, -1, 5, 25, 12, 11]}, "output": [-17, -37, -62, -39, -10, -25, -30, -13, 13, 12, 17, 42, 54, 65]}, {"input": {"nums": [-4, 10, 6, 15, -7]}, "output": [-4, 6, 12, 27, 20]}, {"input": {"nums": [13, 18, -8, 3, 10, 13, -1, 3, -4]}, "output": [13, 31, 23, 26, 36, 4...
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": "01612a255446c710"}
vcr_c79262951f5b
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 0, 1, 2, 3, 1, 1, 0]}, "output": true}, {"input": {"nums": [3, 2, 1, 3, 3, 1]}, "output": true}, {"input": {"nums": [3, 3, 1, 2, 3, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "25661ca91f45468f"}
vcr_5f365b65f6da
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [6, 4, 3, 7, 6, 5, 1]}, "output": [2520, 3780, 5040, 2160, 2520, 3024, 15120]}, {"input": {"nums": [5, 2, 5, 1]}, "output": [10, 25, 10, 50]}, {"input": {"nums": [2, 2, 5, 3]}, "output": [30, 30, 12, 20]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "9662165855badef9"}
vcr_12326cdb5d10
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, 55, 30, 55, 55, 12, 35, 41, 44, 8, 32, 36, 22, 44, 25]}, "output": 36}, {"input": {"prices": [7, 36, 1, 19, 40, 26, 31, 20, 3]}, "output": 39}, {"input": {"prices": [12, 8, 20, 34, 23, 40, 22, 1, 12]}, "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": "e1e75a5be9f801f1"}
vcr_d0a29a558dee
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-22, -13, -2, 16, 22, 41, 42], "target": 38}, "output": [3, 4]}, {"input": {"nums": [-8, -4, 1, 3, 7, 9, 32, 34, 35, 37], "target": -1}, "output": [0, 4]}, {"input": {"nums": [-9, -5, 14, 18, 27], "target": 41}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "6f6e4f1d3e85cc84"}
vcr_0d20c92a8b33
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-16, -3, 19, -7, -20, -8, 3, -13, 18, -5, -16], "k": 14}, "output": [18, -5, -16, -16, -3, 19, -7, -20, -8, 3, -13]}, {"input": {"nums": [-4, 0, 2, -3, -10, 6, 10, 10], "k": 18}, "output": [10, 10, -4, 0, 2, -3, -10, 6]}, {"input": {"nums": [-6, 2, -9, -7, -2, 0, 10], "k": 4}, "output": [-7, -2, 0,...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "a7199e1faf1cac8d"}
vcr_948cbb82eb63
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": "cccbcaca"}, "output": 6}, {"input": {"s": "accc"}, "output": 2}, {"input": {"s": "caa"}, "output": 2}]
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": "23d28652b1e35eff"}
vcr_4a8307066db9
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", "cabb", "cab", "babc", "baac", "ab", "aba", "cabc", "bb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bbb", "aaa", "baa", "abb", "abb"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["aab", "aab", "abb"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "09ed08b288bd850a"}
vcr_704767cd6a01
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [41, 46, 21, 25, 31, 13, 26, 13, 39, 19, 5, 49, 15, 4]}, "output": 44}, {"input": {"prices": [4, 21, 28, 5, 24, 17, 20, 12, 24, 25]}, "output": 24}, {"input": {"prices": [8, 9, 7, 22, 26, 25, 29, 7, 25, 23]}, "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": "8a708c763be6df0e"}
vcr_e5ebdf5a259e
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [14, 14, -18, -5, 13, 1, 3, 13, 5, -6, 5, -15, -15]}, "output": 40}, {"input": {"nums": [-8, -2, 11, -2]}, "output": 11}, {"input": {"nums": [7, -4, 5, 13, 2, 5, 12]}, "output": 40}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "d7227ea9cc7d0cb7"}
vcr_f5587068f05a
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [41, 18, 21, 37, 7, 9, 48, 47, 16, 42]}, "output": 41}, {"input": {"prices": [5, 15, 24, 3, 17, 14, 22, 10, 31, 27]}, "output": 28}, {"input": {"prices": [18, 25, 3, 36, 11, 33, 15, 24, 29, 23, 2]}, "output": 33}]
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": "cdf5ee9ec3893afa"}
vcr_8ca137730809
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, -13, -3, 0, 15, 16, 28, 32, 35, 38, 43], "target": 70}, "output": [7, 9]}, {"input": {"nums": [-15, -10, -7, -6, 13, 16, 17, 26, 31, 39], "target": 30}, "output": [4, 6]}, {"input": {"nums": [0, 11, 20, 29, 34, 38], "target": 49}, "output": [1, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "4f0f5c1ef5ec77b2"}
vcr_7b442c3ea7c6
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [22, -24, -25, -7, -16, -18, 15, 3, 15, -4]}, "output": [22, -2, -27, -34, -50, -68, -53, -50, -35, -39]}, {"input": {"nums": [-10, 13, 6, 14, 11, 17, 4, 8, -7, 8]}, "output": [-10, 3, 9, 23, 34, 51, 55, 63, 56, 64]}, {"input": {"nums": [14, 4, 16, 4]}, "output": [14, 18, 34, 38]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "3a337c0155d95a94"}
vcr_67271ac55194
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": [58, 100, 188, 70, 106, 180, 117, 82, 98, 0, 135, 152]}, "output": false}, {"input": {"nums": [3, 2, 2]}, "output": true}, {"input": {"nums": [25, 8, 7, 11, 9, 25, 18, 1, 11]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "967cd538b6cb699b"}
vcr_745bd3f135c1
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [7, 11, 23, 19, 24, 38, 26, 49, 1, 15, 19, 27, 38, 32, 46]}, "output": true}, {"input": {"nums": [3, 6, 22, 14, 11, 10, 22, 20]}, "output": true}, {"input": {"nums": [6, 19, 11, 18, 6, 19]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "4a28ba86a8893839"}
vcr_3406ab680fd0
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [24, 9, 3, 6, 16, 20, 4, 29]}, "output": 79}, {"input": {"nums": [0, 20]}, "output": 20}, {"input": {"nums": [15, 16]}, "output": 16}]
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": "09cfcf71e6af356e"}
vcr_5645462e67d5
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, 10, 12, 16, 22, 24, 25, 30, 32, 35, 40, 42, 49], "target": 43}, "output": 12}, {"input": {"nums": [7, 25, 35], "target": 29}, "output": 2}, {"input": {"nums": [6, 8, 12, 14, 17, 19, 20, 26, 27, 29, 33, 36], "target": 24}, "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": "f83aa40f09151604"}
vcr_7a53b6c1f91d
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [15, 18, -12, -8, -5], "k": 0}, "output": [15, 18, -12, -8, -5]}, {"input": {"nums": [-6, 10, 3, -9, 2, 0, -1], "k": 0}, "output": [-6, 10, 3, -9, 2, 0, -1]}, {"input": {"nums": [10, -9, 2, 10, -10, -7, 3], "k": 20}, "output": [-9, 2, 10, -10, -7, 3, 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": "a139787f8327ec4d"}
vcr_a7a0777d3227
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": [-9, 23, 3, 2, -19, -24, -14, 0, 34, 15, -7, 32, 3, 27]}, "output": [-9, 14, 17, 19, 0, -24, -38, -38, -4, 11, 4, 36, 39, 66]}, {"input": {"nums": [18, -2, 9]}, "output": [18, 16, 25]}, {"input": {"nums": [-7, 14, 2, 7, 12]}, "output": [-7, 7, 9, 16, 28]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "5250ede93a5a3bb4"}
vcr_7ed4a982e613
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [23, 30, 1, 6, 11]}, "output": 41}, {"input": {"nums": [12, 19, 11, 9, 18, 15, 18]}, "output": 59}, {"input": {"nums": [13, 4, 18, 17, 6, 7, 5, 1]}, "output": 42}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "1d6e4c4bad47de2e"}
vcr_7fba67e25acb
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": "ebbdc bbcad c b"}, "output": "b c bbcad ebbdc"}, {"input": {"s": "bb aba ab bb"}, "output": "bb ab aba bb"}, {"input": {"s": "bb bb"}, "output": "bb bb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "fe00bfae581de02d"}
vcr_d9bba28a6f27
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": [14, 4, -4, -21, -8, -14, -19, 29, 8, -18, 15, 26]}, "output": [14, 18, 14, -7, -15, -29, -48, -19, -11, -29, -14, 12]}, {"input": {"nums": [12, -9]}, "output": [12, 3]}, {"input": {"nums": [4, 4, -1]}, "output": [4, 8, 7]}]
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": "ab43aaaac010ff9e"}
vcr_f5a4a8149a6e
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": [20, 5, 33, 3, 5, 3, 20, 23, 33, 50, 23]}, "output": 50}, {"input": {"nums": [13, 8, 20, 20, 2, 4, 10, 4, 8, 10, 49, 2, 13]}, "output": 49}, {"input": {"nums": [26, 16, 26, 6, 6, 16, 58]}, "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": "5c8ba70f3353199e"}
vcr_b1f00ee0b56d
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 2, 1, 7, 6, 5]}, "output": [420, 630, 1260, 180, 210, 252]}, {"input": {"nums": [3, 1, 1, 3, 1, 4, 5]}, "output": [60, 180, 180, 60, 180, 45, 36]}, {"input": {"nums": [3, 1, 2, 5, 4, 1, 2]}, "output": [80, 240, 120, 48, 60, 240, 120]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "c0081cae30ddf0e9"}
vcr_ceab2962625f
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": [8, 20, 4, 20, 55, 8, 4]}, "output": 55}, {"input": {"nums": [19, 26, 23, 26, 23, 19, 18, 18, 22, 40, 22]}, "output": 40}, {"input": {"nums": [16, 13, 17, 43, 19, 13, 23, 23, 27, 16, 19, 27, 17]}, "output": 43}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "02e4d4be4f1151cb"}
vcr_47ccf40e0201
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": "ada d ccbe dcebea bbcaca dc"}, "output": "dc bbcaca dcebea ccbe d ada"}, {"input": {"s": "baaa b"}, "output": "b baaa"}, {"input": {"s": "bbab b ab abab"}, "output": "abab ab b bbab"}]
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": "7b78d484eb13387e"}
vcr_e26a5718d698
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": [5, 78, 17, 5, 32, 19, 1, 1, 3, 19, 3, 32, 17]}, "output": 78}, {"input": {"nums": [11, 8, 12, 10, 8, 54, 10, 22, 11, 12, 22]}, "output": 54}, {"input": {"nums": [25, 21, 29, 21, 1, 2, 2, 25, 29, 58, 1]}, "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": "e8916d3ae567b9f1"}
vcr_a94b325f054e
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "e eeaca"}, "output": "eeaca e"}, {"input": {"s": "bbb bb a"}, "output": "a bb bbb"}, {"input": {"s": "b bbaa bbb"}, "output": "bbb bbaa b"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "6424760b4be2acea"}
vcr_01b57fc44acb
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": [4, 27, 14, 0, 21, 20, 23]}, "output": 71}, {"input": {"nums": [3, 20, 8, 0, 14, 4, 20, 7]}, "output": 54}, {"input": {"nums": [17, 14, 11, 12, 9, 14, 18]}, "output": 55}]
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": "2c0c19f8863d8027"}
vcr_3687e2314229
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 2, 3, 11, 12, 14, 16, 17, 23, 25, 31, 38, 48], "target": 29}, "output": 10}, {"input": {"nums": [1, 4, 5, 6, 8, 9, 13, 22, 30, 31, 32, 38], "target": 4}, "output": 1}, {"input": {"nums": [19, 21, 25, 35], "target": 25}, "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": "e093c97eb23ea42e"}
vcr_b62aa7d949ca
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, 3, 3, 0, 6]}, "output": [0, 0, 0, 270, 0]}, {"input": {"nums": [3, 5, 4, 1, 4, 2, 2]}, "output": [320, 192, 240, 960, 240, 480, 480]}, {"input": {"nums": [3, 1, 3, 4]}, "output": [12, 36, 12, 9]}]
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": "f32d3ded84e736fb"}
vcr_496428878618
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 6, 0, 2, 1, 3, 7]}, "output": [0, 0, 1260, 0, 0, 0, 0]}, {"input": {"nums": [1, 3, 2, 2, 4]}, "output": [48, 16, 24, 24, 12]}, {"input": {"nums": [2, 4, 3, 4, 1]}, "output": [48, 24, 32, 24, 96]}]
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": "dabbb34f4e2d10ca"}
vcr_dbddaaf71ba9
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-13, -1, 5, -6, -18, -17, -7], "k": 20}, "output": [-1, 5, -6, -18, -17, -7, -13]}, {"input": {"nums": [-8, 8, -10], "k": 13}, "output": [-10, -8, 8]}, {"input": {"nums": [-10, -7, 3, -4, 0, -5, -7, -4], "k": 20}, "output": [0, -5, -7, -4, -10, -7, 3, -4]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "b6a36553e7a3abf6"}
vcr_322b84737d70
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": [0, 22, 18, 2, 20, 0]}, "output": 42}, {"input": {"nums": [18, 7, 19, 11]}, "output": 37}, {"input": {"nums": [0, 7, 17, 11, 9, 17, 2]}, "output": 35}]
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": "dd3d234d0a984347"}
vcr_482b0fcc0264
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": [-8, -17, 8, 19, 13, 5, -10, 20, -10]}, "output": 55}, {"input": {"nums": [-5, -12, 5, 8, 8, -12, 1, 3, -10, 1]}, "output": 21}, {"input": {"nums": [2, -3, -6, 6, 13, 2]}, "output": 21}]
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": "7f6a4108d6bd80f0"}
vcr_84fc96a52f6c
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": [58, 46, 13, 60, 41, 42, 35, 36, 27, 36, 3, 50]}, "output": 47}, {"input": {"prices": [9, 32, 22, 4, 16, 19, 24, 7, 28]}, "output": 24}, {"input": {"prices": [4, 6, 15, 24, 19, 22, 24, 35, 7, 11, 22]}, "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": "6fb7fd2a0884109c"}
vcr_5030aa60daaa
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-28, -33, 61, -20, -37, 2, 66, 41, 38, 42], "target": 102}, "output": [2, 7]}, {"input": {"nums": [24, 40, -30, 22, -1, 8, 45], "target": 39}, "output": [1, 4]}, {"input": {"nums": [4, -10, -19, 8, -24, -6, -29], "target": -20}, "output": [0, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "b1275a550f5a175a"}
vcr_2066d0db05d8
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": 61532}, "output": 8}, {"input": {"n": 4449}, "output": 5}, {"input": {"n": 5347}, "output": 7}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "e2abab347e80968f"}
vcr_38229a00eda5
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 4, 1, 0, 4, 1, 2, 0]}, "output": true}, {"input": {"nums": [3, 3, 2, 0, 0, 0, 3, 1]}, "output": false}, {"input": {"nums": [1, 0, 0, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "677407e46b3c2e70"}
vcr_5129bace43f1
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 3, 8, 12, 16, 19, 22, 23, 29, 36, 38, 42, 44], "target": 25}, "output": 8}, {"input": {"nums": [5, 6, 11, 20, 21, 22, 24, 38], "target": 1}, "output": 0}, {"input": {"nums": [5, 7, 8, 9, 16, 18, 25, 30, 37], "target": 35}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "07562b2ae01b5820"}
vcr_d69652dfbb74
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": "cdbaba dddca aeac"}, "output": "aeac dddca cdbaba"}, {"input": {"s": "aa bbbb a b ab"}, "output": "ab b a bbbb aa"}, {"input": {"s": "aaa bbb"}, "output": "bbb aaa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "bc1c482ce99b0575"}
vcr_25bc0cf04dde
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 3, 0, 1, 7, 7, 7, 5]}, "output": [0, 0, 15435, 0, 0, 0, 0, 0]}, {"input": {"nums": [5, 1, 4, 2]}, "output": [8, 40, 10, 20]}, {"input": {"nums": [2, 3, 4, 4, 1, 1, 1]}, "output": [48, 32, 24, 24, 96, 96, 96]}]
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": "156029db274fc088"}
vcr_a19ec06d48f7
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": ["aaab", "ac", "abcc", "cbab", "bbcb", "bc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["abb", "abb", "abb", "aab"]}, "output": [1, 3]}, {"input": {"strs": ["aab", "abb", "bba", "aab", "aab"]}, "output": [2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "159f3b6625b1e4c1"}
vcr_3640bf9f181e
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": "aaaababbbcbc"}, "output": 7}, {"input": {"s": "bcaccbbcbaa"}, "output": 8}, {"input": {"s": "bababbab"}, "output": 7}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "7c19cf8e0e100afa"}
vcr_2af9dd08d2c3
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": [136, 89, 195, 180, 178]}, "output": false}, {"input": {"nums": [10, 14, 12, 12, 16, 2, 20, 14, 19, 11, 11]}, "output": true}, {"input": {"nums": [20, 8, 6]}, "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": "c143ff2559fea103"}
vcr_1c0bed16c7a5
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": [20, 165, 43, 118, 184, 68, 189, 54, 48, 50, 92, 117, 60, 2, 121, 61]}, "output": false}, {"input": {"nums": [2, 4, 19, 18, 4, 22, 12]}, "output": true}, {"input": {"nums": [6, 15, 23, 19, 15, 17, 13, 9, 5, 20, 18, 12]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "bb1e6e9523d77d71"}
vcr_3b4065b8b8b3
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 3, 1, 4, 1, 2]}, "output": [24, 24, 72, 18, 72, 36]}, {"input": {"nums": [4, 3, 3, 1, 5]}, "output": [45, 60, 60, 180, 36]}, {"input": {"nums": [4, 5, 5, 2, 3, 5, 1]}, "output": [750, 600, 600, 1500, 1000, 600, 3000]}]
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": "c6c4460a4065ad8c"}
vcr_598470d9af92
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": [26, 30, 9, 24, 3, 25, 1]}, "output": 79}, {"input": {"nums": [2, 15, 9, 19, 18, 5, 17, 15, 8, 1]}, "output": 59}, {"input": {"nums": [12, 10, 3, 11, 12, 6, 9, 3, 18, 17]}, "output": 54}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "49ddf39fa997125e"}
vcr_95c8e7614b3b
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, -12, -13, -12, 2, -16, 6, 10, -2, -9, 2, -17, 21, -6, 17]}, "output": 32}, {"input": {"nums": [-8, 1, 3, 13, 4, 9, -8, 13, -6, -8]}, "output": 35}, {"input": {"nums": [-7, 5, 4]}, "output": 9}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "26c70860cf6018ad"}
vcr_7925b8880da4
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": 119, "b": 83}, "output": 1}, {"input": {"a": 96, "b": 136}, "output": 8}, {"input": {"a": 56, "b": 21}, "output": 7}]
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": "32b0322cacac5504"}
vcr_409df6ae9f08
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": ["ccab", "cb", "ac", "ca", "aba", "cc", "bb", "cbc"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "bab", "bab", "abb", "bbb", "bab", "aaa"]}, "output": [1, 2, 4]}, {"input": {"strs": ["bab", "aab", "aba", "abb", "bab", "aba"]}, "output": [3, 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": "42147b3026e34fe2"}
vcr_99e0c511c4d6
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-5, 31, 5, 18, -13, 13, 3, 29, 23, 18, 16, 10, 25]}, "output": [-5, 26, 31, 49, 36, 49, 52, 81, 104, 122, 138, 148, 173]}, {"input": {"nums": [1, -5, 10, -7]}, "output": [1, -4, 6, -1]}, {"input": {"nums": [9, 1]}, "output": [9, 10]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "24af2d0cdd6cf86c"}
vcr_45e8e0c867a2
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": "acbaa bc aeca c"}, "output": "c aeca bc acbaa"}, {"input": {"s": "a bbba ba"}, "output": "ba bbba a"}, {"input": {"s": "bab ba"}, "output": "ba bab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "f42861f964b98516"}
vcr_a96610abfc29
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "dbddcccbbdabd"}, "output": false}, {"input": {"s": "acbccbb"}, "output": false}, {"input": {"s": "cbbbca"}, "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": "100a808c8de79984"}
vcr_050383b1a80a
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": ["cbc", "cba", "aab", "bcaa", "ccc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bab", "bba", "aba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bbb", "aba", "aaa", "abb", "aaa", "bab", "baa"]}, "output": [1, 2, 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": "845692ca66fc39a3"}
vcr_6076ca1ef3d4
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": "acdd acc"}, "output": "acc acdd"}, {"input": {"s": "a ab abb"}, "output": "abb ab a"}, {"input": {"s": "aaaa a bbbb ba bba"}, "output": "bba ba bbbb a aaaa"}]
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": "cf8dc4237954eabb"}
vcr_55b621cc07c4
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": 22, "b": 12}, "output": 2}, {"input": {"a": 146, "b": 54}, "output": 2}, {"input": {"a": 34, "b": 174}, "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": "172cffafc0cd960c"}
vcr_817f103255b0
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 1, 1, 1, 1, 3, 3, 4, 3, 0]}, "output": true}, {"input": {"nums": [2, 0, 0, 0, 2]}, "output": false}, {"input": {"nums": [3, 1, 3, 1, 0, 1, 0, 1, 0]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "fad4d98dd50cd49d"}
vcr_82ef59f3c10c
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [7, 0, 6, 19]}, "output": 26}, {"input": {"nums": [19, 14, 18, 1, 10, 9, 6]}, "output": 53}, {"input": {"nums": [0, 6, 18, 7, 12, 7, 4, 5]}, "output": 35}]
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": "3aadf0a875d5bd20"}
vcr_1bfc570c591a
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [25, 13, 3]}, "output": 28}, {"input": {"nums": [10, 19, 7, 17, 18, 6, 14, 20]}, "output": 62}, {"input": {"nums": [17, 11, 7, 19, 3, 10, 2, 5, 3]}, "output": 51}]
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": "d0f85eb1a0768fe6"}
vcr_064b1774f5a2
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, 19, 18, 4, -6, 1, 10, -7]}, "output": 51}, {"input": {"nums": [-8, 10, 13, -8, 9, 5, 10, 8]}, "output": 47}, {"input": {"nums": [0, 11, 1, -2, -7, 1, 14]}, "output": 18}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "09306690ec954d72"}
vcr_c2f88e44f1dc
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [21, 28, 12, 23, 8, 6, 18, 18, 6, 7, 13]}, "output": 88}, {"input": {"nums": [2, 14, 5, 11, 18, 12, 12, 14]}, "output": 51}, {"input": {"nums": [5, 20, 2, 17]}, "output": 37}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "727cf589597b2f0e"}
vcr_79f68b6f232a
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": "aeedbb dabe ed eabcae b"}, "output": "b eabcae ed dabe aeedbb"}, {"input": {"s": "bbaa ab a aaaa"}, "output": "aaaa a ab bbaa"}, {"input": {"s": "bbba aaab"}, "output": "aaab bbba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "0811870b9ad886da"}
vcr_5fdc81f2715a
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, 4, 4, 4, 0, 4, 4, 1]}, "output": [0, 0, 0, 0, 2048, 0, 0, 0]}, {"input": {"nums": [3, 2, 4, 4, 3, 2]}, "output": [192, 288, 144, 144, 192, 288]}, {"input": {"nums": [5, 2, 2, 1, 2]}, "output": [8, 20, 20, 40, 20]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "89fb2370ce89f86c"}
vcr_17a73cd3f534
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": [50, 39, 71, 77, 67, 25, -18, 54, -10], "target": 57}, "output": [4, 8]}, {"input": {"nums": [32, 47, -27, 40, 36], "target": 83}, "output": [1, 4]}, {"input": {"nums": [-4, 5, -3, 41, -7, 33, -18, 19, 11], "target": 23}, "output": [3, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "c1ad3c7f0564186f"}
vcr_1312425e38c3
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-28, -18, -17, -1, 12, 17, 32, 33, 37, 44], "target": 65}, "output": [6, 7]}, {"input": {"nums": [-18, -13, 1, 2, 4, 8, 30, 38], "target": -14}, "output": [0, 4]}, {"input": {"nums": [-13, 2, 3, 10, 12, 13, 34], "target": 21}, "output": [0, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "5874aab4ef33b617"}
vcr_222106c3c5cc
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 2, 2, 2, 3, 5, 10, 10, 13, 13, 14, 14, 15]}, "output": [1, 2, 3, 5, 10, 13, 14, 15]}, {"input": {"nums": [1, 3, 5, 8, 8, 9, 12]}, "output": [1, 3, 5, 8, 9, 12]}, {"input": {"nums": [5, 7, 10, 12]}, "output": [5, 7, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "e75ed9d60cb3eb35"}
vcr_67f217a8daa3
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 1, 2, 0, 1, 1, 4, 0, 4, 0]}, "output": true}, {"input": {"nums": [1, 3, 3, 1]}, "output": true}, {"input": {"nums": [0, 3, 3]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "fa660ad7aa8947d3"}
vcr_dfd2401c205d
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": [15, 11, 25, 30]}, "output": 45}, {"input": {"nums": [2, 4, 11, 15, 1, 18, 6]}, "output": 37}, {"input": {"nums": [13, 4, 1]}, "output": 14}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "088c9f2034c089cf"}
vcr_79fbaaa8aec0
hard
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "bacadcadacdacab"}, "output": true}, {"input": {"s": "abc"}, "output": false}, {"input": {"s": "cbbaacccaa"}, "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": "99179f1c34946945"}
vcr_4cad15c22b9e
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, 4, 5, 19, 31, 32, 37, 39, 40, 45, 46, 48], "target": 32}, "output": 5}, {"input": {"nums": [1, 3, 5, 6, 11, 12, 18, 22, 26, 39], "target": 26}, "output": 8}, {"input": {"nums": [1, 3, 6, 7, 8, 13, 24, 26, 29, 34], "target": 11}, "output": 5}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "7a359dff639d3bbd"}
vcr_755f9f3fb404
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": [19, 13, -11, 6, 14, -1, 10, 13, 3, -4]}, "output": 66}, {"input": {"nums": [10, 2, -6, -7, 11, 2, -11, 12, 3]}, "output": 17}, {"input": {"nums": [5, -7, -4, 8, -3, -10, -6, -5]}, "output": 8}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "db92b3ea9dd4d77a"}
vcr_e6af448d9818
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [22, 31, 0, 42, 3, 30, 38, 47, 40, 24, 16, 48, 4, 22]}, "output": true}, {"input": {"nums": [19, 16, 21]}, "output": false}, {"input": {"nums": [9, 5, 10, 24, 4, 2, 22, 25]}, "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": "f06abf94b8bbd5df"}
vcr_757474d13624
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": [2, 25, 39, 13, 21, 11, 8, 38, 20, 13, 49, 16, 43, 25, 43]}, "output": true}, {"input": {"nums": [13, 18, 18]}, "output": true}, {"input": {"nums": [22, 14, 20, 15, 2]}, "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": "7767d50c9e118a88"}
vcr_6012dd54da22
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, -7, 20, -12, -10, -1, -9, 14, 14, -2, -5, -1, -3]}, "output": 28}, {"input": {"nums": [-10, -1, 5, 13, -8, -2, 2, 13, 13, 1]}, "output": 37}, {"input": {"nums": [-9, 12, 0, 14, -9, 7]}, "output": 26}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "2e5b5eb7d7651f82"}
vcr_b470223ca359
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": 48, "b": 203}, "output": 1}, {"input": {"a": 127, "b": 75}, "output": 1}, {"input": {"a": 5, "b": 140}, "output": 5}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "db246852f3d6a40c"}
vcr_d1dc2c89e648
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 28, 44, 48], "target": 37}, "output": 2}, {"input": {"nums": [3, 8, 9, 17, 18, 24, 32, 38], "target": 36}, "output": 7}, {"input": {"nums": [17, 27, 31, 37], "target": 25}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "ab0de2536c1868cc"}
vcr_30dea3529dab
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": [196, 185, 107, 38, 153, 116, 195, 64, 69, 98, 30, 8, 94, 67, 157, 115, 16, 138]}, "output": false}, {"input": {"nums": [14, 10, 9, 18, 6, 8, 17, 6, 2]}, "output": true}, {"input": {"nums": [17, 0, 9, 8, 13]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "b79739d5b0412c22"}
vcr_cd52a5856368
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": [122, 3, 102, 4, 58, 18, 16, 72, 116, 29, 50, 105, 79, 163, 158, 35, 156, 164]}, "output": false}, {"input": {"nums": [25, 0, 4, 12]}, "output": false}, {"input": {"nums": [15, 2, 10, 21]}, "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": "c4a04de0a7f59e65"}
vcr_70aecb2f3fd6
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": [9, 14, 20, 14, 47, 8, 49, 24, 18, 13]}, "output": true}, {"input": {"nums": [1, 14, 16, 18, 1, 5, 23, 18, 1, 7, 6, 11]}, "output": true}, {"input": {"nums": [3, 9, 13, 4, 0, 21, 25, 16, 8]}, "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": "a54e032f8feb113e"}
vcr_b7341e8fa17f
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 3, 7, 11, 13, 18, 24, 27, 31, 37, 39, 43, 46], "target": 31}, "output": 8}, {"input": {"nums": [6, 15, 24, 26, 32], "target": 0}, "output": 0}, {"input": {"nums": [11, 13, 17, 23, 24, 28, 32, 34, 35, 37, 39], "target": 7}, "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": "18a6d17716ae1f2a"}