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_a450c5867b67
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": "ecd aa cbae eaade bddaae a b"}, "output": "b a bddaae eaade cbae aa ecd"}, {"input": {"s": "b a ba b aaa"}, "output": "aaa b ba a b"}, {"input": {"s": "b b baaa"}, "output": "baaa b 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": "a66f5096698002b6"}
vcr_de5ab34030d9
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-1, -16, -11, -15, 0, 4, 14]}, "output": 18}, {"input": {"nums": [-5, -2, 3, -3, 5]}, "output": 5}, {"input": {"nums": [-10, -4, 3, 11, -4, 12, -6, 12, -10, -10, 1]}, "output": 28}]
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": "2fdf19446a862c5a"}
vcr_0ef853fc2829
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 5, 4, 3, 1, 3]}, "output": [180, 72, 90, 120, 360, 120]}, {"input": {"nums": [5, 4, 4, 5, 3, 5, 3]}, "output": [3600, 4500, 4500, 3600, 6000, 3600, 6000]}, {"input": {"nums": [4, 5, 1]}, "output": [5, 4, 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": "76dcade1fbf3d6d9"}
vcr_287dfacee58c
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 4, 6, 6, 12, 12, 13, 14, 14, 15]}, "output": [0, 2, 4, 6, 12, 13, 14, 15]}, {"input": {"nums": [2, 2, 3, 9, 9, 10, 11, 12]}, "output": [2, 3, 9, 10, 11, 12]}, {"input": {"nums": [1, 1, 2, 3, 3, 7, 8, 8, 9, 11, 11, 12]}, "output": [1, 2, 3, 7, 8, 9, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "65c779853e77120a"}
vcr_ec326efe3716
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-24, 35, 4, -7, 19, -4, -21, 8, -9, 8, -16, -16, 14]}, "output": [-24, 11, 15, 8, 27, 23, 2, 10, 1, 9, -7, -23, -9]}, {"input": {"nums": [13, 14, 1, -2, -5, -10, 1, 15, 6]}, "output": [13, 27, 28, 26, 21, 11, 12, 27, 33]}, {"input": {"nums": [-5, 5]}, "output": [-5, 0]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "d0cdf701dfec677e"}
vcr_90d9054eb99b
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": [50, 39, 37, 10, 59, 2, 30, 19, 51, 16, 29]}, "output": 49}, {"input": {"prices": [7, 5, 21, 36, 39, 7, 13]}, "output": 34}, {"input": {"prices": [26, 38, 18, 37, 2, 19, 32, 16, 10]}, "output": 30}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "c2876ae3be26b628"}
vcr_2839d619ac34
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": "ac aabedb"}, "output": "aabedb ac"}, {"input": {"s": "baab ba aaa baaa"}, "output": "baaa aaa ba baab"}, {"input": {"s": "aa bb"}, "output": "bb aa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "5696c51e827ca56e"}
vcr_331924dae405
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": 91, "b": 136}, "output": 1}, {"input": {"a": 122, "b": 83}, "output": 1}, {"input": {"a": 180, "b": 49}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "58210281e669add5"}
vcr_758f871dabd0
medium
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "bcbbbbdbbacbaa"}, "output": false}, {"input": {"s": "bbcaaa"}, "output": false}, {"input": {"s": "cbccbccbab"}, "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": "a622bb881f80b88f"}
vcr_c17400aa2076
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, 5, 2, 1, 1]}, "output": [10, 6, 15, 30, 30]}, {"input": {"nums": [3, 4, 2, 1, 5, 1]}, "output": [40, 30, 60, 120, 24, 120]}, {"input": {"nums": [3, 1, 3, 5, 3, 3]}, "output": [135, 405, 135, 81, 135, 135]}]
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": "f6f5275d71b948c4"}
vcr_5f338911ef87
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": 48694}, "output": 10}, {"input": {"n": 1784}, "output": 7}, {"input": {"n": 11106}, "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": "e70674ad408e50f0"}
vcr_096147170aaa
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [19, 2, 10, 23, 23, 21, 26, 30]}, "output": 93}, {"input": {"nums": [4, 13, 4, 5, 0, 13, 9, 13, 8, 13]}, "output": 57}, {"input": {"nums": [5, 18]}, "output": 18}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "726fa54a99744841"}
vcr_ea85444fac87
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15]}, "output": [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15]}, {"input": {"nums": [0, 3, 5, 6, 6, 7, 8, 10, 11, 12]}, "output": [0, 3, 5, 6, 7, 8, 10, 11, 12]}, {"input": {"nums": [1, 4, 6, 6, 10]}, "output": [1, 4, 6, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "8eae408ab54fec4d"}
vcr_150ef4fdd244
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": [21, 2, -10, -9, 17, 11, 19, -19, 25, 12, -3, 17, -7]}, "output": [21, 23, 13, 4, 21, 32, 51, 32, 57, 69, 66, 83, 76]}, {"input": {"nums": [10, 17, -10, -7, -10, 14, 15, -2]}, "output": [10, 27, 17, 10, 0, 14, 29, 27]}, {"input": {"nums": [-1, -8, 13]}, "output": [-1, -9, 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": "a725c0177af39246"}
vcr_c7f8d15e6341
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": [-14, 15, -1, 19], "k": 7}, "output": [15, -1, 19, -14]}, {"input": {"nums": [10, -5, -10], "k": 1}, "output": [-10, 10, -5]}, {"input": {"nums": [-4, -3, 0, 2, -2, 2, 1, -2, 3, -4], "k": 7}, "output": [2, -2, 2, 1, -2, 3, -4, -4, -3, 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": "4bef5f5580271cb3"}
vcr_69d955110985
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 1, 1, 0, 4, 2, 3, 2, 0]}, "output": false}, {"input": {"nums": [1, 1, 1, 2, 0, 0]}, "output": true}, {"input": {"nums": [2, 1, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "e7bff1d0a3e8a65d"}
vcr_4607ea58f9e3
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": 157, "b": 104}, "output": 1}, {"input": {"a": 164, "b": 96}, "output": 4}, {"input": {"a": 80, "b": 82}, "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": "3faf8fc044fed24e"}
vcr_1e11caaf7f2a
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-1, 15, 13, 18, 11]}, "output": 57}, {"input": {"nums": [10, 12, 2, 4, 1, 1]}, "output": 30}, {"input": {"nums": [5, 7, 4, 10, -6, 6, -9, -2]}, "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": "18e8f898a48f9980"}
vcr_ea49a4c1dfd4
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": 242, "b": 98}, "output": 2}, {"input": {"a": 179, "b": 139}, "output": 1}, {"input": {"a": 53, "b": 142}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "948f02ede81c1f2e"}
vcr_8d34e5ba24bc
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [24, 11, 24, 28, 37, 21, 10, 11, 21, 78, 37, 28, 10]}, "output": 78}, {"input": {"nums": [12, 47, 22, 8, 22, 8, 12]}, "output": 47}, {"input": {"nums": [1, 46, 9, 24, 9, 1, 24]}, "output": 46}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "a2e09909138aed93"}
vcr_27586433954d
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, 3, 3, 2, 4, 1]}, "output": [72, 144, 144, 216, 108, 432]}, {"input": {"nums": [5, 5, 5, 5, 4, 1, 3]}, "output": [1500, 1500, 1500, 1500, 1875, 7500, 2500]}, {"input": {"nums": [1, 2, 3, 2, 3, 3, 3]}, "output": [324, 162, 108, 162, 108, 108, 108]}]
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": "e9a34612556a7542"}
vcr_8408a7419eda
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 5, 6, 10]}, "output": [0, 5, 6, 10]}, {"input": {"nums": [0, 0, 2, 3, 9, 10, 10, 12, 12, 12]}, "output": [0, 2, 3, 9, 10, 12]}, {"input": {"nums": [0, 4, 5, 6, 8, 8, 8, 9, 9, 10, 10, 10]}, "output": [0, 4, 5, 6, 8, 9, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "28b023b50abe1977"}
vcr_841cc3589767
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": [4, 38, 8, 37, 7, 43, 54, 57, 18, 23, 36]}, "output": 53}, {"input": {"prices": [17, 40, 24, 15, 3, 29]}, "output": 26}, {"input": {"prices": [21, 9, 7, 4, 23, 12, 26, 14, 22, 11, 8, 32]}, "output": 28}]
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": "fbb63cd086e9313b"}
vcr_d7275a1d8ee0
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": 181, "b": 108}, "output": 1}, {"input": {"a": 101, "b": 96}, "output": 1}, {"input": {"a": 79, "b": 39}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "d8e51dd18dcc3556"}
vcr_ae36bff1aff2
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": [16, -10, -7, -3, -13, -1, 10, 16]}, "output": 26}, {"input": {"nums": [-6, 1, 9, 12, 9, 6, 0]}, "output": 37}, {"input": {"nums": [3, 8, -11, 9, -2, -6, 10, 1, 11, 10]}, "output": 33}]
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": "ba34e821b569211e"}
vcr_3bd30ba4ee85
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": 18759}, "output": 7}, {"input": {"n": 2021}, "output": 8}, {"input": {"n": 11509}, "output": 9}]
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": "bd0bff07b1841a08"}
vcr_0a126e1c7c2a
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": [112, 150, 46, 140, 181, 128, 32, 171, 179, 157, 42, 130, 178]}, "output": false}, {"input": {"nums": [12, 18, 24, 6, 24, 3, 8, 14, 19, 2, 22, 6]}, "output": true}, {"input": {"nums": [11, 8, 19, 16, 12, 4, 13, 12, 23, 5, 10, 20]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "a5e2bfdbd854e420"}
vcr_652aef9ae41e
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-15, -14, 10, 2, -1]}, "output": 12}, {"input": {"nums": [-10, 10, 10, 3, 1, 2, -12, 0, 3, -5, -5]}, "output": 26}, {"input": {"nums": [0, 9, -11, 5, -3, 14, 5]}, "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": "8af6ffb2db9b6d20"}
vcr_57df298937b0
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": [1, 21, 24, 28, 16]}, "output": 49}, {"input": {"nums": [1, 8, 1, 16, 7, 17, 11, 5, 8]}, "output": 49}, {"input": {"nums": [19, 3, 18, 17, 18, 17, 3, 9, 18, 19]}, "output": 83}]
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": "1648ed3540a6e8aa"}
vcr_7b7fb04b1535
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 4, 5, 5, 8, 9, 11, 12, 14]}, "output": [3, 4, 5, 8, 9, 11, 12, 14]}, {"input": {"nums": [4, 4, 5, 7, 7, 8, 9, 9, 10, 11, 11, 11]}, "output": [4, 5, 7, 8, 9, 10, 11]}, {"input": {"nums": [2, 2, 4, 4, 5, 6, 6, 7, 8, 9, 12, 12]}, "output": [2, 4, 5, 6, 7, 8, 9, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "a735879251a9b607"}
vcr_cf31fb667f05
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-18, -3, 16, 18, 14, 2, 14, -15, -14, 4, -15, 9], "k": 26}, "output": [-15, 9, -18, -3, 16, 18, 14, 2, 14, -15, -14, 4]}, {"input": {"nums": [3, -1, -6], "k": 14}, "output": [-1, -6, 3]}, {"input": {"nums": [-5, -3, 6, 10, -6], "k": 4}, "output": [-3, 6, 10, -6, -5]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "d3f5f522b7682aae"}
vcr_1b2481a75499
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-23, -17, -8, -4, 8, 15, 37, 38, 46], "target": -40}, "output": [0, 1]}, {"input": {"nums": [-18, -7, 18, 21, 23, 35], "target": 53}, "output": [2, 5]}, {"input": {"nums": [-10, -6, 4, 18, 20, 23, 30, 34, 35, 38], "target": -2}, "output": [1, 2]}]
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": "141f270bba1e7fe1"}
vcr_e5175648d700
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-11, 9, 3, 3, 16, -8, -15, 15, 6, 11, 13, 16, 3, -10]}, "output": 72}, {"input": {"nums": [1, 1, 14, 3, 14, 11, -12, -6, 1, -3, 0]}, "output": 44}, {"input": {"nums": [9, -3, -9, 4, -1, 9, 8, -11, 14]}, "output": 23}]
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": "ae9bdfc7b842b9cf"}
vcr_27faaee091db
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [10, 16, 4, 4, 10, 65, 13, 16, 3, 2, 29, 2, 3, 29, 13]}, "output": 65}, {"input": {"nums": [8, 17, 11, 17, 54, 11, 8, 26, 26]}, "output": 54}, {"input": {"nums": [26, 27, 3, 25, 29, 29, 25, 50, 27, 10, 10, 3, 26]}, "output": 50}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "1efcda74564c7e13"}
vcr_5d682ade0e10
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, 47, 11, 43, 12, 39, 12, 16, 45, 48, 15, 35, 2, 23, 15, 12, 7, 2]}, "output": true}, {"input": {"nums": [9, 10, 21, 10, 8, 14, 17, 4, 11]}, "output": true}, {"input": {"nums": [3, 19, 6, 25, 8, 24, 1, 1]}, "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": "a450a7100ecdb551"}
vcr_b5bfc50a2635
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, 2, 1, 0, 0]}, "output": false}, {"input": {"nums": [3, 3, 3, 1]}, "output": true}, {"input": {"nums": [0, 0, 3, 3, 0, 0]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "990f77dc83597182"}
vcr_a0cac16ba6f7
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": "abad d abdad d dcd daa"}, "output": "daa dcd d abdad d abad"}, {"input": {"s": "bbb bbbb ba a ba"}, "output": "ba a ba bbbb bbb"}, {"input": {"s": "babb bbba abba a aba"}, "output": "aba a abba bbba babb"}]
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": "a2aa79cb9e22e1ce"}
vcr_d0abb2f133f5
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": 20664}, "output": 6}, {"input": {"n": 13070}, "output": 7}, {"input": {"n": 11859}, "output": 8}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "dd0f97dfe307b2cb"}
vcr_a3408fa38ff6
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": ["aaaa", "cbc", "ca", "bcba", "caa", "ab", "ccba", "ccab"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aba", "aaa", "baa"]}, "output": [1, 2]}, {"input": {"strs": ["abb", "bab", "bbb", "aab"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "14318be6c399cec0"}
vcr_17eaf71768c1
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-20, -11, -18, -11, -1, -8, 9], "k": 10}, "output": [-1, -8, 9, -20, -11, -18, -11]}, {"input": {"nums": [1, -4, 10, -8, -6, 5, -10, -1, 7, 8], "k": 12}, "output": [7, 8, 1, -4, 10, -8, -6, 5, -10, -1]}, {"input": {"nums": [-2, 6, 5], "k": 17}, "output": [6, 5, -2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "e8b80989285345a8"}
vcr_9e18f4eeca8b
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -21, -11, 7, 15, 26, 31], "target": 41}, "output": [4, 5]}, {"input": {"nums": [-15, -9, 8, 20, 34, 37], "target": 45}, "output": [2, 5]}, {"input": {"nums": [-11, 7, 16, 39], "target": 46}, "output": [1, 3]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "bb566afd6b1bde16"}
vcr_b7520e604505
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": [-12, -13, -17, 19, 19, 17], "k": 18}, "output": [-12, -13, -17, 19, 19, 17]}, {"input": {"nums": [10, 3, 10], "k": 11}, "output": [3, 10, 10]}, {"input": {"nums": [10, 7, -6, -10], "k": 3}, "output": [7, -6, -10, 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": "6b44a2163a3cca32"}
vcr_da70b47f6500
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-20, -14, 15, 6, 16, 3, -1, 15, 18, 9, -13, -12], "k": 8}, "output": [16, 3, -1, 15, 18, 9, -13, -12, -20, -14, 15, 6]}, {"input": {"nums": [9, 1, 9, 0, -3, -9, -3, 9, -6], "k": 18}, "output": [9, 1, 9, 0, -3, -9, -3, 9, -6]}, {"input": {"nums": [8, 2, -7, 5, 9, 5, -4], "k": 0}, "output": [8, 2, -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": "55e1734776a79d6f"}
vcr_5c6d182d27d5
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-28, -21, -13, -7, 14, 27, 44], "target": -7}, "output": [1, 4]}, {"input": {"nums": [-18, -14, -1, 15, 22, 27, 30, 37, 39], "target": -32}, "output": [0, 1]}, {"input": {"nums": [-7, -2, 3, 21], "target": -4}, "output": [0, 2]}]
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": "8a2f844f4a28df93"}
vcr_242af8cec8ba
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [4, 4, 4, 6, 6, 6, 5, 7]}, "output": [120960, 120960, 120960, 80640, 80640, 80640, 96768, 69120]}, {"input": {"nums": [3, 3, 4]}, "output": [12, 12, 9]}, {"input": {"nums": [1, 2, 1]}, "output": [2, 1, 2]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "b327a491882f2050"}
vcr_63aa20b0fa48
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [19, 67, 19, 11, 11, 21, 21]}, "output": 67}, {"input": {"nums": [12, 18, 18, 20, 44, 20, 12, 16, 16]}, "output": 44}, {"input": {"nums": [16, 5, 12, 1, 12, 5, 16, 1, 54]}, "output": 54}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "9a7232283888faf4"}
vcr_0371475458c1
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-11, -3, 15, -15, -18, -8, -7, -7, 6, 19], "k": 10}, "output": [-11, -3, 15, -15, -18, -8, -7, -7, 6, 19]}, {"input": {"nums": [0, 7, -5, -2, -4], "k": 8}, "output": [-5, -2, -4, 0, 7]}, {"input": {"nums": [-2, -10, -8, 1, 10, -3, 8, -2, -10], "k": 2}, "output": [-2, -10, -2, -10, -8, 1, 10, -3, 8]...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "b532f697ef62ede0"}
vcr_0c67ce0abe5e
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, 5, 9, 10, 11, 12, 17, 18, 37, 40, 43, 48, 49], "target": 13}, "output": 7}, {"input": {"nums": [3, 16, 17, 34], "target": 22}, "output": 3}, {"input": {"nums": [0, 1, 2, 4, 6, 8, 16, 17, 32, 36], "target": 40}, "output": 10}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "e79dc30adc55647c"}
vcr_71c5562829da
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["ba", "aaab", "ba", "cab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "bab", "aaa", "aba", "bba", "abb", "aba"]}, "output": [1, 3, 3]}, {"input": {"strs": ["aaa", "bba", "bbb", "abb", "aba", "aaa"]}, "output": [1, 1, 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": "89ac5bf6966b72f2"}
vcr_ba912bf58966
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": "caadab"}, "output": false}, {"input": {"s": "bbcacacb"}, "output": false}, {"input": {"s": "acab"}, "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": "92d66928f4275e0b"}
vcr_d70c80ac1d7a
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": "a e a b bcc adbcb bbcaa"}, "output": "bbcaa adbcb bcc b a e a"}, {"input": {"s": "aaaa aa bbb bbba"}, "output": "bbba bbb aa aaaa"}, {"input": {"s": "a ab bab ba"}, "output": "ba bab ab a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "1eb88d4655091faa"}
vcr_5d867c09f27a
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": ["cbab", "baa", "bab", "acb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "aba", "bbb", "aab", "aaa"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aab", "bba", "aab", "baa"]}, "output": [1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "1af4bf43761c216d"}
vcr_f354dc29581e
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": 51891}, "output": 9}, {"input": {"n": 12953}, "output": 7}, {"input": {"n": 1637}, "output": 6}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "86c0b157ce413484"}
vcr_4f2f022cf687
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": "aa cb eae debeac cee d eecd"}, "output": "eecd d cee debeac eae cb aa"}, {"input": {"s": "aaaa aaab abb"}, "output": "abb aaab aaaa"}, {"input": {"s": "ba b b abba"}, "output": "abba b b ba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "994bd2b04afc6f2d"}
vcr_cb8d497d95a1
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [6, -12, 20, 8, 10, 0, -12, 3, -11, 16, -7, 5], "k": 5}, "output": [3, -11, 16, -7, 5, 6, -12, 20, 8, 10, 0, -12]}, {"input": {"nums": [2, 0, -10, -3, 7, 2], "k": 14}, "output": [7, 2, 2, 0, -10, -3]}, {"input": {"nums": [-9, -9, -1, 4, -4, -8, -6, -8, -1], "k": 5}, "output": [-4, -8, -6, -8, -1, -9...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "e7edd2719b72bbde"}
vcr_5de67b3b3fd9
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, 7, 6, 5, 5, 6]}, "output": [18900, 18900, 8100, 9450, 11340, 11340, 9450]}, {"input": {"nums": [2, 5, 2]}, "output": [10, 4, 10]}, {"input": {"nums": [2, 3, 3]}, "output": [9, 6, 6]}]
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": "01ed81057993b89a"}
vcr_652064801f98
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": "dbbbcbdddbcbbbd"}, "output": true}, {"input": {"s": "bccbaaaa"}, "output": false}, {"input": {"s": "aa"}, "output": true}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "4192c3ce7702fdb3"}
vcr_6951788eb3b0
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, 21, 12, 16, 15, 26, 5, 21, 6, 30, 8]}, "output": 115}, {"input": {"nums": [3, 14, 2]}, "output": 14}, {"input": {"nums": [13, 12, 11, 12, 7, 18, 8]}, "output": 43}]
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": "b92dd7bef3f29403"}
vcr_4828ca4bbfbd
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 4, 2, 1, 5, 6, 3]}, "output": [720, 180, 360, 720, 144, 120, 240]}, {"input": {"nums": [3, 3, 2, 3, 3, 2, 1]}, "output": [108, 108, 162, 108, 108, 162, 324]}, {"input": {"nums": [1, 3, 1, 3]}, "output": [9, 3, 9, 3]}]
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": "83540e0b9c262bb3"}
vcr_98b6fb996bad
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": [6, 9, 12, 14, 15]}, "output": [6, 9, 12, 14, 15]}, {"input": {"nums": [0, 5, 9, 9, 9, 12]}, "output": [0, 5, 9, 12]}, {"input": {"nums": [0, 2, 7, 7, 8, 9]}, "output": [0, 2, 7, 8, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "b56401ffe0b79510"}
vcr_9361a8a6bf7d
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [13, 5, 20, 15, 17, 7, 18, -11, 18, 17, -5, 11], "k": 8}, "output": [17, 7, 18, -11, 18, 17, -5, 11, 13, 5, 20, 15]}, {"input": {"nums": [3, -7, -1, -8, -4, -5, -4, -4, 9], "k": 15}, "output": [-8, -4, -5, -4, -4, 9, 3, -7, -1]}, {"input": {"nums": [1, -3, 6, 10, -5, 4, 6, 8], "k": 3}, "output": [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": "1950e09bd3d61239"}
vcr_9852ad4d5bc8
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": [129, 59, 8, 185, 102]}, "output": false}, {"input": {"nums": [15, 4, 1, 15, 8, 17, 16, 11, 1, 14, 7, 23]}, "output": true}, {"input": {"nums": [0, 17, 22, 21, 8, 13, 12, 12, 2, 15, 19, 2]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "645f912768a69428"}
vcr_4afba7030d69
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [1, 8, 19, -17, -7, -8, -15, -7, -6, 20, 16]}, "output": 36}, {"input": {"nums": [-4, 6, -4, -7]}, "output": 6}, {"input": {"nums": [4, 10, 1, 9, 14]}, "output": 38}]
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": "2dc2f16b7cb086d3"}
vcr_2a04b6c9f806
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": "dec e ca cae abc ba"}, "output": "ba abc cae ca e dec"}, {"input": {"s": "bba aaaa bb bab aa"}, "output": "aa bab bb aaaa bba"}, {"input": {"s": "baa abaa aabb bbbb"}, "output": "bbbb aabb abaa baa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "d2274f1e577b9045"}
vcr_9458ac8d799b
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": [40, 49, 48, 26, 34, 49]}, "output": true}, {"input": {"nums": [17, 2, 11, 15, 16, 0, 22]}, "output": false}, {"input": {"nums": [9, 22, 23, 6, 4, 9, 12, 21, 14, 5]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "f279b64790c1a6b3"}
vcr_6d154498096c
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": [-16, -14, -28, 52, 55, 19], "target": 71}, "output": [3, 5]}, {"input": {"nums": [-18, 9, 26, 32], "target": 58}, "output": [2, 3]}, {"input": {"nums": [2, 36, -28, 3], "target": 5}, "output": [0, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "f1e157aa463e4ddd"}
vcr_bc5321f8b3d4
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [31, 4, 24, 15, 15, 31, 4, 24, 67]}, "output": 67}, {"input": {"nums": [15, 15, 16, 22, 49, 22, 20, 16, 20]}, "output": 49}, {"input": {"nums": [54, 24, 9, 16, 9, 16, 24]}, "output": 54}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "851c5d4e4bd8d2b9"}
vcr_4f33b8eb6748
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, 23, 24, 26, 33, 36, 42], "target": 37}, "output": 6}, {"input": {"nums": [5, 9, 11, 19, 20, 30, 31, 33, 35], "target": 33}, "output": 7}, {"input": {"nums": [4, 5, 8, 18, 20, 21, 23, 27, 28, 32, 38, 39], "target": 7}, "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": "3e40332fa1fb6691"}
vcr_1be841e72507
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, 14, 23]}, "output": 23}, {"input": {"nums": [19, 10]}, "output": 19}, {"input": {"nums": [8, 17, 2, 16, 2, 9]}, "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": "6f86c28261e79217"}
vcr_d626305fbfdb
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [44, 3, 37, 39, 8, 13, 15, 9, 51, 3, 33, 34, 28]}, "output": 48}, {"input": {"prices": [9, 39, 37, 40]}, "output": 31}, {"input": {"prices": [37, 20, 14, 11, 1, 14]}, "output": 13}]
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": "3fbfdf658f2d284f"}
vcr_f204b1f8e347
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [3, 0, 20, -10, 3, -2, -16, 8, -15], "k": 1}, "output": [-15, 3, 0, 20, -10, 3, -2, -16, 8]}, {"input": {"nums": [8, 0, -10, -2, 10], "k": 9}, "output": [0, -10, -2, 10, 8]}, {"input": {"nums": [8, 9, -2, -4], "k": 7}, "output": [9, -2, -4, 8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "6d204d58eca62177"}
vcr_a8b309e7e9de
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, 13, 32, 16, -17, 27, 30, 25, 33, 34, 14, 21]}, "output": [-5, 8, 40, 56, 39, 66, 96, 121, 154, 188, 202, 223]}, {"input": {"nums": [10, -3, 13, 13, 16, -7, -12]}, "output": [10, 7, 20, 33, 49, 42, 30]}, {"input": {"nums": [10, -1, 5, -6, 17, 4, -10]}, "output": [10, 9, 14, 8, 25, 29, 19]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "4d200031182d7ed3"}
vcr_5c31fddac948
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 3, 5, 6, 10, 16, 20, 31, 38, 39, 46], "target": 19}, "output": 6}, {"input": {"nums": [0, 3, 5, 8, 12, 14, 15, 21, 22, 24, 28, 33], "target": 23}, "output": 9}, {"input": {"nums": [3, 4, 5, 11, 21, 22, 29, 30, 33, 36], "target": 2}, "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": "431ecda2ed644f3e"}
vcr_8df5909f61a3
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": ["caa", "aba", "abca", "bbb", "acaa", "bbc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "aaa", "abb", "baa", "bab", "bab"]}, "output": [1, 1, 4]}, {"input": {"strs": ["bba", "aba", "aaa", "baa", "bba", "aba"]}, "output": [1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "ebaf51873e47935a"}
vcr_f9e186c8ac28
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 5, 8, 10, 15, 16, 17, 18, 27, 31, 41, 42, 46, 48], "target": 19}, "output": 8}, {"input": {"nums": [9, 11, 24, 30], "target": 36}, "output": 4}, {"input": {"nums": [9, 11, 12, 14, 16, 18, 29, 30, 32, 38], "target": 3}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "a12d207297c0160f"}
vcr_571181284db6
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": 19870}, "output": 9}, {"input": {"n": 10499}, "output": 5}, {"input": {"n": 1643}, "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": "4e93efc766308f8f"}
vcr_00454bec05a6
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": 33799}, "output": 5}, {"input": {"n": 15867}, "output": 12}, {"input": {"n": 14293}, "output": 10}]
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": "c3b704a6b419f4f9"}
vcr_fc69bc7f0bc7
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [18, -17, -2, -4, -16, -17, -3, -7, 8, -16, -4], "k": 20}, "output": [-2, -4, -16, -17, -3, -7, 8, -16, -4, 18, -17]}, {"input": {"nums": [-8, 7, -10, 10, -1, -6, 3, 9, -5], "k": 5}, "output": [-1, -6, 3, 9, -5, -8, 7, -10, 10]}, {"input": {"nums": [9, -2, 6, 2, 2, -7, -5, -10], "k": 9}, "output": [...
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": "d5f2b4587b7a7849"}
vcr_4af11a4dbabd
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": [61, 109, 94, 34, 41, 83, 103, 159, 22, 50, 70, 180, 21]}, "output": false}, {"input": {"nums": [22, 5, 17, 3, 12, 13, 3, 1]}, "output": true}, {"input": {"nums": [6, 25, 3, 14, 16]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "2018923eba59b28e"}
vcr_b0a9dd758efc
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [1, -5, -24, 29, -9, 16, 18, -18, -2, -5, -2, -7, 26, -4]}, "output": [1, -4, -28, 1, -8, 8, 26, 8, 6, 1, -1, -8, 18, 14]}, {"input": {"nums": [-10, 2, 4]}, "output": [-10, -8, -4]}, {"input": {"nums": [16, 9, 14, -11, 0, -6]}, "output": [16, 25, 39, 28, 28, 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": "9f16da1aaee78785"}
vcr_bb4e9fb0f198
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": "cabbbabbabac"}, "output": 9}, {"input": {"s": "abbcbcca"}, "output": 6}, {"input": {"s": "aacca"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "b7e2d32abbd53b0c"}
vcr_2a181c6e059d
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": 96, "b": 4}, "output": 4}, {"input": {"a": 21, "b": 115}, "output": 1}, {"input": {"a": 111, "b": 52}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "2cfd577e0a67a76c"}
vcr_3b3f571bdfc4
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": [8, -15, 3, 12, -3, -1, 18, -16, 15, -1, 3, -11], "k": 12}, "output": [8, -15, 3, 12, -3, -1, 18, -16, 15, -1, 3, -11]}, {"input": {"nums": [9, 6, -10, 1, 9], "k": 18}, "output": [-10, 1, 9, 9, 6]}, {"input": {"nums": [7, 0, 0, -3, -4, 10, -6, 0, -7, -7], "k": 3}, "output": [0, -7, -7, 7, 0, 0, -3, ...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "b691d289825ea895"}
vcr_347b121a5268
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [15, 16, 35, 29, 20, 6]}, "output": [15, 31, 66, 95, 115, 121]}, {"input": {"nums": [-10, -7, -3, 9, -11, -4, 13, 10, 7]}, "output": [-10, -17, -20, -11, -22, -26, -13, -3, 4]}, {"input": {"nums": [17, -8, -1, 3, -3, 13]}, "output": [17, 9, 8, 11, 8, 21]}]
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": "930a0cee5457f8c3"}
vcr_817dddfc80a0
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [2, 6, 6, 7, 7, 7, 7, 8, 8, 10, 11, 14, 15]}, "output": [2, 6, 7, 8, 10, 11, 14, 15]}, {"input": {"nums": [0, 4, 5, 9]}, "output": [0, 4, 5, 9]}, {"input": {"nums": [7, 8, 9, 12]}, "output": [7, 8, 9, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "13cca5a1a70ac06f"}
vcr_c4abeaa62814
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, -5, 1, 6, 4, 6, 32, -4, 8]}, "output": [-13, -18, -17, -11, -7, -1, 31, 27, 35]}, {"input": {"nums": [6, -5, -4, 3, -5, 15, 8]}, "output": [6, 1, -3, 0, -5, 10, 18]}, {"input": {"nums": [6, 16, 1]}, "output": [6, 22, 23]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "21a33662bb8deb9d"}
vcr_8d4a5e991e3e
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [31, 33, 31, 1, 28, 27, 27, 33, 18, 18, 1, 8, 8, 28, 62]}, "output": 62}, {"input": {"nums": [45, 14, 4, 4, 14]}, "output": 45}, {"input": {"nums": [14, 26, 55, 14, 26]}, "output": 55}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "454586d66c5f9ad6"}
vcr_00f433bc1166
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": [-12, -6, -4, 18, 20, 21, 25, 32], "target": 16}, "output": [2, 4]}, {"input": {"nums": [-19, -9, -6, -2, 12, 13, 29], "target": -8}, "output": [2, 3]}, {"input": {"nums": [-18, -15, -9, -4, 3, 17, 20, 25, 29, 34], "target": -1}, "output": [0, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "a7a88e01a3079521"}
vcr_5baf8b38753e
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-2, 16, -10, -12, 20, 6, -6, 14, 2, 11, 8, 14, -17, -4, -7]}, "output": 69}, {"input": {"nums": [-8, -11, 2, -5]}, "output": 2}, {"input": {"nums": [-4, 3, 7]}, "output": 10}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "43b982c0038b593e"}
vcr_4e64744ce672
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, 8, 10, 11, 13, 18, 19, 20, 29, 33, 34, 37, 41, 44], "target": 2}, "output": 0}, {"input": {"nums": [2, 3, 6, 7, 11, 15, 17, 29], "target": 38}, "output": 8}, {"input": {"nums": [5, 10, 18, 39], "target": 33}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "289e7bb71e3f79a0"}
vcr_a25174ba40d6
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, 30, 13, -6, -3, 16]}, "output": [-5, 25, 38, 32, 29, 45]}, {"input": {"nums": [-3, 6, 8, 13, -7, -4]}, "output": [-3, 3, 11, 24, 17, 13]}, {"input": {"nums": [2, 0, 15, 2]}, "output": [2, 2, 17, 19]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "ed124919cbf49892"}
vcr_07d6ef629a3e
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [6, 16, 18, 22, 23, 30, 36, 38, 40, 42, 45], "target": 31}, "output": 6}, {"input": {"nums": [0, 6, 12, 14, 19, 30], "target": 23}, "output": 5}, {"input": {"nums": [9, 11, 18, 21, 22, 23, 28, 29, 33, 39], "target": 40}, "output": 10}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "a64eb8675fdeca9c"}
vcr_6f449bc9f80a
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": "cd cabbcc d"}, "output": "d cabbcc cd"}, {"input": {"s": "aaaa bbaa aab"}, "output": "aab bbaa aaaa"}, {"input": {"s": "aa bbb aab aaba aab"}, "output": "aab aaba aab bbb aa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "823a3bd0fa0817bd"}
vcr_0bb091660b80
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [2, 19, 20, -19, 12, 17, -11, 0, 7, 11], "k": 8}, "output": [20, -19, 12, 17, -11, 0, 7, 11, 2, 19]}, {"input": {"nums": [-1, -9, -5, -2, -3, -2, 5, 7, 2, 7], "k": 1}, "output": [7, -1, -9, -5, -2, -3, -2, 5, 7, 2]}, {"input": {"nums": [6, 8, -7, 6, -2], "k": 2}, "output": [6, -2, 6, 8, -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": "145e01006f26dcf4"}
vcr_408eceb1be98
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [7, 5, 5, -19, -9, -1, 4, 15, -17, -10], "k": 29}, "output": [5, 5, -19, -9, -1, 4, 15, -17, -10, 7]}, {"input": {"nums": [-10, -3, -8, -7, -9, 10], "k": 2}, "output": [-9, 10, -10, -3, -8, -7]}, {"input": {"nums": [7, -4, -5, 2, 6, 2, -6, -6], "k": 12}, "output": [6, 2, -6, -6, 7, -4, -5, 2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "bb62665167352e30"}
vcr_46129fe0d7d4
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": [-30, -28, -16, -9, 1, 29, 33], "target": -27}, "output": [1, 4]}, {"input": {"nums": [-15, -13, -10, 1, 2, 12, 20, 21, 37], "target": -8}, "output": [2, 4]}, {"input": {"nums": [-17, -2, -1, 3, 14, 23, 31, 35], "target": 37}, "output": [4, 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": "3018b75ea245d59f"}
vcr_fcecefc47087
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": [1, 28, 26, 36, 26, 7, 53, 23, 28, 36, 29, 1, 29, 6, 7, 6, 23]}, "output": 53}, {"input": {"nums": [12, 12, 58, 6, 6]}, "output": 58}, {"input": {"nums": [19, 18, 22, 21, 19, 22, 18, 21, 42, 9, 9]}, "output": 42}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "fd9b0f866b6d9468"}
vcr_b658ba66a910
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": 179, "b": 50}, "output": 1}, {"input": {"a": 90, "b": 85}, "output": 5}, {"input": {"a": 74, "b": 65}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "c7f5952a3a4983ee"}
vcr_69ff2e40afbc
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-18, -9, -2, -11, 18, 2, 13, 14, -12]}, "output": 47}, {"input": {"nums": [14, 8, -9, -5]}, "output": 22}, {"input": {"nums": [12, -9, -4, -11, 14, -4, 9, -8, -1, -6]}, "output": 19}]
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": "9fa1a288f6ff890c"}
vcr_0600ca4d93a3
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": "babacbcaababba"}, "output": 12}, {"input": {"s": "cbccaaccca"}, "output": 6}, {"input": {"s": "abaccbcb"}, "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": "513d960c9f9b681d"}