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_429a538310c7
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": 37}, "output": 1}, {"input": {"a": 155, "b": 74}, "output": 1}, {"input": {"a": 171, "b": 56}, "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": "c5335e3ef6b34167"}
vcr_a2e6ed729fdf
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [8, 12, 21, 24, 25, 28, 34, 41], "target": 23}, "output": 3}, {"input": {"nums": [3, 4, 5, 6, 11, 16, 19, 20, 23, 28, 30, 36], "target": 20}, "output": 7}, {"input": {"nums": [2, 4, 7, 8, 16, 19, 20, 26, 27, 29, 32, 38], "target": 24}, "output": 7}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "a98646dbb8b4170f"}
vcr_816d401e5636
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": [25, 29, 46, 37, 43, 14, 14, 12, 42, 7, 22, 51]}, "output": 44}, {"input": {"prices": [12, 29, 5, 25, 35, 8, 38, 10]}, "output": 33}, {"input": {"prices": [22, 23, 13, 8, 28, 22, 32, 31, 34]}, "output": 26}]
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": "8fcd11e790b96641"}
vcr_eca9236571f1
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": 34531}, "output": 8}, {"input": {"n": 5545}, "output": 7}, {"input": {"n": 4105}, "output": 3}]
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": "a3bb501891eb7b48"}
vcr_974d3be978a6
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": [-15, -13, -3, 7, 9, 16, 25, 28, 32, 37, 39, 43], "target": 40}, "output": [2, 11]}, {"input": {"nums": [-12, -8, 0, 10, 18, 23, 33, 36, 37, 38], "target": 47}, "output": [3, 8]}, {"input": {"nums": [-12, -4, -1, 6, 13, 21, 35], "target": 12}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "831df554d8d9117a"}
vcr_020ad00423eb
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, 9, 4, -2, -7], "k": 5}, "output": [-13, 9, 4, -2, -7]}, {"input": {"nums": [6, -8, 6, -1, 7, 1, -5], "k": 2}, "output": [1, -5, 6, -8, 6, -1, 7]}, {"input": {"nums": [-1, 7, 4, -7, 3, -4, -9], "k": 6}, "output": [7, 4, -7, 3, -4, -9, -1]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "31747212406191f5"}
vcr_540ce4d21b7a
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [5, 8, 31, 36, 44, 46], "target": 16}, "output": 2}, {"input": {"nums": [9, 10, 11, 16, 18, 19, 26, 29, 33, 38], "target": 23}, "output": 6}, {"input": {"nums": [0, 1, 5, 6, 15, 17, 21, 24, 26, 33, 37, 39], "target": 39}, "output": 11}]
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": "8a726ee437fa4f9f"}
vcr_69c08eaeb522
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [6, 9, 22, 34, 43, 45], "target": 46}, "output": 6}, {"input": {"nums": [6, 10, 12, 13, 15, 39], "target": 20}, "output": 5}, {"input": {"nums": [4, 5, 6, 8, 19, 35, 38], "target": 36}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "d4e763841691a414"}
vcr_8cccc67b9800
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, 10, 14, 19, 29, 33, 46], "target": 33}, "output": 5}, {"input": {"nums": [6, 11, 18, 27, 32, 34], "target": 34}, "output": 5}, {"input": {"nums": [0, 6, 16, 23, 28, 29, 32], "target": 29}, "output": 5}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "2e4a3b0673e15e84"}
vcr_a2addd893a68
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": [28, 55, 19, 21]}, "output": 27}, {"input": {"prices": [23, 25, 10, 9]}, "output": 2}, {"input": {"prices": [32, 9, 6, 35, 28, 33, 27, 20, 24, 29, 21, 26]}, "output": 29}]
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": "6cabcea55bf7d001"}
vcr_7fa54929d4d5
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [28, 27, 12, 15, 24, 10, 4, 18, 12, 14, 19]}, "output": 101}, {"input": {"nums": [2, 4, 9, 7, 17, 16, 9, 4]}, "output": 37}, {"input": {"nums": [10, 3, 16, 8, 13, 5, 12, 13, 11]}, "output": 62}]
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": "c07f28533b07ad1f"}
vcr_a7df1d83ea0f
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 2, 3, 3, 5, 6, 8, 9, 10, 11, 13, 15, 15]}, "output": [1, 2, 3, 5, 6, 8, 9, 10, 11, 13, 15]}, {"input": {"nums": [0, 4, 6, 11]}, "output": [0, 4, 6, 11]}, {"input": {"nums": [0, 2, 6, 6, 7, 9, 10, 10]}, "output": [0, 2, 6, 7, 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": "e7f63ef0005be837"}
vcr_bf291044c028
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": [-7, 5, 13, 1, -24, -23, -13, 23, 20, 27]}, "output": [-7, -2, 11, 12, -12, -35, -48, -25, -5, 22]}, {"input": {"nums": [2, 7, -8, -1, 1, -9, 1, 3, -5]}, "output": [2, 9, 1, 0, 1, -8, -7, -4, -9]}, {"input": {"nums": [3, -8, 6]}, "output": [3, -5, 1]}]
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": "99e834ec4b0cd10a"}
vcr_3a11484f4547
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": [20, 21, 29, 32], "target": 7}, "output": 0}, {"input": {"nums": [16, 20, 23], "target": 39}, "output": 3}, {"input": {"nums": [0, 1, 5, 10, 15, 16, 21, 23, 24, 28, 29, 34], "target": 0}, "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": "e168b4dea3d2fe74"}
vcr_57fa818556e8
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": "bcabccbbbacab"}, "output": 10}, {"input": {"s": "cabca"}, "output": 5}, {"input": {"s": "babbbaacbcba"}, "output": 9}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "a0d373c2965368cc"}
vcr_dbd117669366
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, 1, 4, 3, 2]}, "output": [24, 24, 6, 8, 12]}, {"input": {"nums": [4, 2, 2, 5, 1, 3]}, "output": [60, 120, 120, 48, 240, 80]}, {"input": {"nums": [3, 4, 4]}, "output": [16, 12, 12]}]
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": "905d3f6ebca7569d"}
vcr_38900219ecb3
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": [13, 26, 28, 40, 42, 49], "target": 42}, "output": 4}, {"input": {"nums": [2, 5, 7, 14, 32], "target": 15}, "output": 4}, {"input": {"nums": [8, 9, 11, 17, 25, 26, 29, 32], "target": 37}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "3481affa60ca4953"}
vcr_94417c772575
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [21, 27, 24, 3, 18, 21, 5, 21, 5, 28, 15, 2]}, "output": 117}, {"input": {"nums": [2, 17, 20, 2]}, "output": 22}, {"input": {"nums": [12, 2, 15]}, "output": 27}]
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": "23c196dcb853a2e7"}
vcr_0c9194bb614c
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [7, 17, 28, 35, 38, 48], "target": 34}, "output": 3}, {"input": {"nums": [3, 4, 8, 12, 19, 21, 23, 24, 25, 35, 38, 39], "target": 27}, "output": 9}, {"input": {"nums": [13, 16, 26, 28, 30, 32, 38], "target": 33}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "f11ebd3893cd0b71"}
vcr_0cb085feebe5
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 1, 3, 5, 3, 2, 4, 1]}, "output": [360, 1800, 600, 360, 600, 900, 450, 1800]}, {"input": {"nums": [3, 3, 2, 1, 5]}, "output": [30, 30, 45, 90, 18]}, {"input": {"nums": [2, 4, 2, 5, 3, 2]}, "output": [240, 120, 240, 96, 160, 240]}]
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": "1c355f11ee9b4610"}
vcr_fba7aa7e7e50
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [13, -1, 1, -16], "k": 11}, "output": [-1, 1, -16, 13]}, {"input": {"nums": [-8, -2, 1], "k": 15}, "output": [-8, -2, 1]}, {"input": {"nums": [-10, 8, 8, 9, -4, -3, 2, -2, 0, -7], "k": 10}, "output": [-10, 8, 8, 9, -4, -3, 2, -2, 0, -7]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "07e851716b80e2d7"}
vcr_ffb47eb4509e
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": [-19, -18, -12, -11, 18, 26, 35, 47], "target": -1}, "output": [0, 4]}, {"input": {"nums": [-20, -16, -9, 0, 1, 13, 17, 20, 21, 24, 29], "target": 20}, "output": [2, 10]}, {"input": {"nums": [-13, -9, -1, 14, 18], "target": 17}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "81d2c91f540ae087"}
vcr_14c36a6bcf47
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, -1, -9, -20, 18, 17, -4], "k": 4}, "output": [-20, 18, 17, -4, 18, -1, -9]}, {"input": {"nums": [3, -4, 0], "k": 11}, "output": [-4, 0, 3]}, {"input": {"nums": [-2, -4, 9, -10, 4], "k": 2}, "output": [-10, 4, -2, -4, 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": "c609e71317459157"}
vcr_cd588257c0f7
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 12, 13, 17, 18, 37, 46], "target": 7}, "output": 1}, {"input": {"nums": [8, 13, 26, 30, 32], "target": 1}, "output": 0}, {"input": {"nums": [1, 3, 6, 18, 25, 29, 34, 36], "target": 32}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "e3694a2107536ba2"}
vcr_d3e48bf53505
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": 35162}, "output": 7}, {"input": {"n": 6421}, "output": 6}, {"input": {"n": 12800}, "output": 3}]
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": "6e1061335d55ae5e"}
vcr_fbd6e6fe70bb
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": [20, -10, -25, -16, 2, -28, 26], "target": 1}, "output": [2, 6]}, {"input": {"nums": [46, 22, -13, 47], "target": 34}, "output": [2, 3]}, {"input": {"nums": [-21, 47, 49, 6, 22, 45, 30, 1, 11], "target": 94}, "output": [2, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "e28ba4371a8c2e33"}
vcr_1b07bb84b399
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [10, 28, 23, 19, 6, 5]}, "output": 52}, {"input": {"nums": [15, 12, 0, 19, 18, 11, 17, 7]}, "output": 52}, {"input": {"nums": [4, 17, 19, 13, 16, 5, 6, 17, 17, 4]}, "output": 62}]
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": "b3b9507dae602e8c"}
vcr_f2de1815b5a8
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, 6, 3, 6, 5, 0, 1, 3]}, "output": [0, 0, 0, 0, 0, 1620, 0, 0]}, {"input": {"nums": [2, 3, 5, 3, 2, 3, 1]}, "output": [270, 180, 108, 180, 270, 180, 540]}, {"input": {"nums": [4, 4, 2, 1]}, "output": [8, 8, 16, 32]}]
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": "e578fb8706caa643"}
vcr_1053197137fe
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": [60, 27, 38, 12, 52, 47]}, "output": 40}, {"input": {"prices": [4, 14, 28, 39, 24, 25, 30, 6, 33, 34]}, "output": 35}, {"input": {"prices": [1, 20, 40, 40, 31]}, "output": 39}]
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": "195980b9ecfe3aec"}
vcr_25577aa60a05
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", "baba", "caba", "ca", "aa", "aac", "caca", "cbac", "bac"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bab", "bba", "abb"]}, "output": [3]}, {"input": {"strs": ["abb", "bba", "bbb", "bbb", "bba"]}, "output": [2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "ed0a0e5846d471e6"}
vcr_4859b56001b1
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, -1, 14, 16, 28, 42], "target": 15}, "output": [0, 5]}, {"input": {"nums": [-19, -10, -4, 14, 28], "target": -14}, "output": [1, 2]}, {"input": {"nums": [-5, -3, 2, 7, 10, 19, 27, 29], "target": -8}, "output": [0, 1]}]
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": "6743e3de8afdd492"}
vcr_286f67bdd413
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": [45, 43, 13, 45, 12, 58, 42, 28, 2]}, "output": 46}, {"input": {"prices": [3, 19, 21, 25, 19, 11, 8, 37]}, "output": 34}, {"input": {"prices": [40, 2, 37, 5, 1]}, "output": 35}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "a2e1c41be662e92d"}
vcr_e93e04ecf758
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, 3, 1, 4, 2, 2, 0, 0, 3, 0]}, "output": false}, {"input": {"nums": [0, 2, 0, 2, 3]}, "output": false}, {"input": {"nums": [1, 1, 2, 1, 1, 2, 1, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "faf3f1417d6872b4"}
vcr_bc002b7459b0
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": [21, 11, 40, 44, 36, 54, 25, -1, 62, -33, 20], "target": 35}, "output": [4, 7]}, {"input": {"nums": [49, -26, 9, -14, 6, -3, 29], "target": 6}, "output": [2, 5]}, {"input": {"nums": [19, -12, -14, 34, 37, 2], "target": 56}, "output": [0, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "ec8140722804f21c"}
vcr_6999e91d43d4
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, -14, -14, 3, 12, 4, -14, -11, 13], "k": 2}, "output": [-11, 13, 3, -14, -14, 3, 12, 4, -14]}, {"input": {"nums": [9, 6, 3, 8, 0, 8, 3, -1, -6, 4], "k": 0}, "output": [9, 6, 3, 8, 0, 8, 3, -1, -6, 4]}, {"input": {"nums": [-6, 3, 4, 10, 1, 7, -7, 6, -3, -5], "k": 13}, "output": [6, -3, -5, -6, 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": "6e7b873cf6cc9ee9"}
vcr_f4a9f03c3952
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": 57, "b": 143}, "output": 1}, {"input": {"a": 53, "b": 127}, "output": 1}, {"input": {"a": 62, "b": 50}, "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": "84a9121f9e149144"}
vcr_bd5c6fe3b3b0
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": [0, -9, 17, -20, 0, 16], "k": 9}, "output": [-20, 0, 16, 0, -9, 17]}, {"input": {"nums": [10, -1, -6, -9], "k": 18}, "output": [-6, -9, 10, -1]}, {"input": {"nums": [-4, -8, -10, -8, 7, 6, 10, 8], "k": 9}, "output": [8, -4, -8, -10, -8, 7, 6, 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": "0c1cba324d66a04b"}
vcr_8996afe22f27
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [18, 28, 37, -28], "target": 46}, "output": [0, 1]}, {"input": {"nums": [-27, 46, 15, 22], "target": -12}, "output": [0, 2]}, {"input": {"nums": [36, -24, -21, 24], "target": 3}, "output": [2, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "5d904190b27eb14c"}
vcr_f3bfacffac03
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, 3, 5, 13, 23, 28, 29, 34, 44], "target": 31}, "output": [1, 5]}, {"input": {"nums": [-18, 9, 10, 12, 21, 33], "target": 19}, "output": [1, 2]}, {"input": {"nums": [-15, -9, -5, -3, 11, 20, 28, 32], "target": 25}, "output": [3, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "89207c7185d39ebe"}
vcr_32adc307e3ee
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": [18, 29, 48, 51, 9, 36, 16, 53, 1, 19, 14, 12, 58, 5]}, "output": 57}, {"input": {"prices": [32, 12, 32, 18, 15, 32, 15, 23, 8, 15, 18, 27]}, "output": 20}, {"input": {"prices": [18, 25, 24, 5, 35, 34]}, "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": "31a331d65b754692"}
vcr_34da47801746
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 3, 0, 2, 1, 1, 2, 4, 0]}, "output": true}, {"input": {"nums": [1, 3, 0, 1, 1, 0, 2]}, "output": false}, {"input": {"nums": [2, 0, 3, 0, 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": "10b60c7bfab0289c"}
vcr_5cdc96dcb4e9
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": [-10, -17, 19, -6, 21, 14, 16, -2, 12]}, "output": 74}, {"input": {"nums": [8, -9, 9, 9, 4, 10, -5, 2, 3]}, "output": 32}, {"input": {"nums": [-2, 11, -11]}, "output": 11}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "c04b870d691d088d"}
vcr_3e9f2f68d7fa
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-22, -19, -4, 20, 21, 22, 29, 43], "target": 42}, "output": [3, 5]}, {"input": {"nums": [-10, -6, -5, 6, 13, 15, 33, 35], "target": 46}, "output": [4, 6]}, {"input": {"nums": [-5, 4, 10, 17, 22, 24, 35, 38, 39], "target": 41}, "output": [3, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "1071cedb16056d10"}
vcr_e8bc7eda2a4a
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": 101, "b": 17}, "output": 1}, {"input": {"a": 34, "b": 62}, "output": 2}, {"input": {"a": 6, "b": 178}, "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": "524e39bd8577fc3f"}
vcr_064f8b4b0015
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": [-25, 10, 12, -24, 3, 34, 8, 9, -22, -10, -5, 11]}, "output": [-25, -15, -3, -27, -24, 10, 18, 27, 5, -5, -10, 1]}, {"input": {"nums": [5, 7, 9, -10, 17, 11]}, "output": [5, 12, 21, 11, 28, 39]}, {"input": {"nums": [1, 15, 17, -4, -1, 7, 15, -8, -6, 14]}, "output": [1, 16, 33, 29, 28, 35, 50, 42, 36...
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": "b7d1f86edec84f20"}
vcr_99c2a721057a
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [6, 0, 3, 5, 7]}, "output": [0, 630, 0, 0, 0]}, {"input": {"nums": [1, 4, 4, 2, 4, 1, 3]}, "output": [384, 96, 96, 192, 96, 384, 128]}, {"input": {"nums": [5, 3, 3, 2, 2, 1]}, "output": [36, 60, 60, 90, 90, 180]}]
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": "fb5b1d71dc470b7f"}
vcr_2fcd0b69084d
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [9, 19, 32, 27, 42, 13, 35, 12, 30, 37, 24, 45, 9]}, "output": true}, {"input": {"nums": [2, 25, 1, 24, 11, 12, 12, 17, 2]}, "output": true}, {"input": {"nums": [0, 18, 24, 16, 19, 3, 16, 22, 22, 4, 4, 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": "78d313929b6ed691"}
vcr_1679c82c6b2e
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": ["bbba", "caa", "ac", "baab", "acbb", "acbb", "aaa", "bbaa", "abc"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["abb", "aba", "aaa", "baa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "bab", "bba", "bba", "bbb", "abb", "baa"]}, "output": [1, 2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "07bcbd3d217ceb29"}
vcr_54c28b044618
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, 2, 3, 3, 7, 10, 10, 12]}, "output": [0, 2, 3, 7, 10, 12]}, {"input": {"nums": [0, 2, 4, 5, 7, 8, 8, 12, 12]}, "output": [0, 2, 4, 5, 7, 8, 12]}, {"input": {"nums": [1, 2, 5, 6, 6, 10, 11]}, "output": [1, 2, 5, 6, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "90784bc67db29f8a"}
vcr_21029f801612
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": [-22, 20, -7, 27, 45, 1, -32, 14, 17, 74, 41, -24], "target": 9}, "output": [6, 10]}, {"input": {"nums": [27, 33, -12, -17, 34, 35, -19, -27, 46], "target": 27}, "output": [6, 8]}, {"input": {"nums": [39, 41, 31, -6, -28, -27], "target": 72}, "output": [1, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "5bfb234c7b036444"}
vcr_ec53f00692c5
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": "aadbbaaabbdaa"}, "output": true}, {"input": {"s": "babcbbcc"}, "output": false}, {"input": {"s": "cc"}, "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": "abd2d893d43685d4"}
vcr_f423ed977c54
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": [27, 5, 29]}, "output": 56}, {"input": {"nums": [8, 4, 10, 15, 16]}, "output": 34}, {"input": {"nums": [18, 16, 1, 1, 1]}, "output": 20}]
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": "5f08cbc0ed49541e"}
vcr_38b694c66abe
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": [-17, -9, -12, 19, 10, 9, 6, 6]}, "output": 50}, {"input": {"nums": [8, 7, 10, 9, 10, -3]}, "output": 44}, {"input": {"nums": [1, -6, 4, -11, 11, -10]}, "output": 11}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "02a3988983149368"}
vcr_0be30910fa6c
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": [31, 52, 31, 52, 46, 5, 26, 13, 45, 31, 25, 53, 2, 49]}, "output": 48}, {"input": {"prices": [1, 9, 5, 38, 32, 39, 6, 19, 9, 29, 17, 12]}, "output": 38}, {"input": {"prices": [27, 31, 32, 18, 4, 33, 28, 16, 21]}, "output": 29}]
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": "cdc684654e9e50f6"}
vcr_8c8ed1c6d756
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, -14, -17, -13, 19, -10, 22]}, "output": 31}, {"input": {"nums": [10, 4, -12, -1, -9, -4, -11, 11, 9]}, "output": 20}, {"input": {"nums": [13, 10, -9, -8, 8, 3, -3]}, "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": "0a65b2507f32b219"}
vcr_3ea6516461ec
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, 12, -11, 10, -14, 18, 5, -20, -17, 17, -20, 12], "k": 18}, "output": [5, -20, -17, 17, -20, 12, 18, 12, -11, 10, -14, 18]}, {"input": {"nums": [5, 3, 6, -8, -4, 0], "k": 16}, "output": [6, -8, -4, 0, 5, 3]}, {"input": {"nums": [-8, -6, 9, 6, -7, -2, 9, 10, -9], "k": 20}, "output": [10, -9, -8, ...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "dfd7409381a5dab5"}
vcr_9e7a567dee96
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "c eead ab bebebe ede a"}, "output": "a ede bebebe ab eead c"}, {"input": {"s": "aaab a bbbb"}, "output": "bbbb a aaab"}, {"input": {"s": "aba abb aa"}, "output": "aa abb aba"}]
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": "f9637c45d51a420a"}
vcr_a9100bdcde08
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 6, 21, 26, 28, 31, 36, 38], "target": 24}, "output": 3}, {"input": {"nums": [0, 4, 6, 12, 14, 19, 22, 33, 35, 38, 39], "target": 40}, "output": 11}, {"input": {"nums": [3, 4, 10, 12, 15, 25, 26, 27, 32], "target": 27}, "output": 7}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "592706a77ac591f5"}
vcr_73768250fb92
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": [14, 1, 17, 0, 9]}, "output": 40}, {"input": {"nums": [3, 10, 0, 13, 11, 7, 12]}, "output": 35}, {"input": {"nums": [3, 15, 18, 18, 17, 4, 16]}, "output": 54}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "b8a5b66e71e7da70"}
vcr_0dc63ed77d6a
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 4, 1, 0, 3, 3, 1, 2, 0, 0]}, "output": false}, {"input": {"nums": [0, 0, 2, 0]}, "output": false}, {"input": {"nums": [3, 0, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "42f70d4ed0e92237"}
vcr_3e5a92159ee2
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 3, 6, 10, 11, 14, 15]}, "output": [0, 1, 3, 6, 10, 11, 14, 15]}, {"input": {"nums": [1, 2, 5, 5, 11, 12]}, "output": [1, 2, 5, 11, 12]}, {"input": {"nums": [0, 5, 5, 6, 6, 8, 9, 11]}, "output": [0, 5, 6, 8, 9, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "f61a604ce70ad7b7"}
vcr_812540796935
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": [20, 29, 36, 28, 46, 25, 3, 48]}, "output": 45}, {"input": {"prices": [15, 17, 30, 23, 18, 34, 18]}, "output": 19}, {"input": {"prices": [8, 4, 39]}, "output": 35}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "d89db83ee9160208"}
vcr_a0db8f720ddd
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [17, 25, 7, 0, 26]}, "output": 51}, {"input": {"nums": [9, 13, 2]}, "output": 13}, {"input": {"nums": [7, 9, 16]}, "output": 23}]
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": "b091d32150555e85"}
vcr_e989db2b90d8
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": [33, -17, 1, 33, -22, -3, 8, 1, 21, 12, 23]}, "output": [33, 16, 17, 50, 28, 25, 33, 34, 55, 67, 90]}, {"input": {"nums": [10, 6, 3, 12, 10, -1, -2, -2, 7, -7]}, "output": [10, 16, 19, 31, 41, 40, 38, 36, 43, 36]}, {"input": {"nums": [7, 3, 16, 4, 11, 4, -3, -3, 10]}, "output": [7, 10, 26, 30, 41, 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": "3a6e01fbf68fa079"}
vcr_c875a0b546b4
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [24, 29, 26, 20]}, "output": 50}, {"input": {"nums": [4, 14, 20, 17, 17, 1, 9, 19, 8, 13]}, "output": 73}, {"input": {"nums": [9, 10, 11, 12, 17, 9, 12]}, "output": 49}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "2ea9069e019951a4"}
vcr_a64fe01d7dcf
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 0, 1, 2, 3, 0, 0]}, "output": true}, {"input": {"nums": [0, 1, 2, 2, 3, 1, 0, 0]}, "output": false}, {"input": {"nums": [3, 3, 2, 1, 1, 3, 1, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "12d8485207ffb96a"}
vcr_9c8cd82da9e5
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -19, 9, 23, 48], "target": -10}, "output": [1, 2]}, {"input": {"nums": [-16, -1, 4, 9, 12, 15, 20, 28, 29, 36], "target": 45}, "output": [3, 9]}, {"input": {"nums": [-8, 17, 26, 32], "target": 49}, "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": "859ba47b9a8b9f14"}
vcr_3043fb1e74c5
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": "aaaaaacb"}, "output": 3}, {"input": {"s": "cabcc"}, "output": 4}, {"input": {"s": "abccbabbacb"}, "output": 9}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "7d93da398e115d1f"}
vcr_9513e90a9b3e
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-12, -22, 33, 31, 10, 30, -18, -14, 31, 30, -22]}, "output": [-12, -34, -1, 30, 40, 70, 52, 38, 69, 99, 77]}, {"input": {"nums": [-5, -8, 6, 17, 15, 1]}, "output": [-5, -13, -7, 10, 25, 26]}, {"input": {"nums": [0, -11]}, "output": [0, -11]}]
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": "d6a53122154177be"}
vcr_c57367edd63b
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [10, 24, 7, 26]}, "output": 50}, {"input": {"nums": [7, 20, 1, 17, 14, 18, 14, 5, 1, 14]}, "output": 74}, {"input": {"nums": [4, 9, 10, 16, 10, 6, 14, 20, 8, 2]}, "output": 53}]
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": "65af0d03acda5976"}
vcr_6acaee9fb9eb
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": [6, 20, 29, 51, 20, 41, 56, 53, 45, 18, 30, 48]}, "output": 50}, {"input": {"prices": [9, 34, 14, 1, 32, 17, 30, 22, 34]}, "output": 33}, {"input": {"prices": [19, 24, 32, 37, 9, 12, 14]}, "output": 18}]
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": "7a8af7e22580764b"}
vcr_b389821d1f5d
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["bc", "aa", "cca", "bc"]}, "output": [1, 1, 2]}, {"input": {"strs": ["abb", "aaa", "aab"]}, "output": [1, 1, 1]}, {"input": {"strs": ["bba", "bba", "bab", "aab", "aaa"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "14e8eaed3afbdb3d"}
vcr_0cef68b15ceb
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [21, 26, 25, 2]}, "output": 46}, {"input": {"nums": [9, 3, 16]}, "output": 25}, {"input": {"nums": [0, 15, 18, 18, 2, 19, 11, 0, 1]}, "output": 53}]
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": "6543880101084ab5"}
vcr_1fc60bda4527
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [2, 2, 3, 3, 7]}, "output": [2, 3, 7]}, {"input": {"nums": [0, 1, 3, 5, 7, 8, 9, 9, 10, 11, 12]}, "output": [0, 1, 3, 5, 7, 8, 9, 10, 11, 12]}, {"input": {"nums": [3, 4, 4, 5, 8, 8, 10, 12]}, "output": [3, 4, 5, 8, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "72a692c2be257acd"}
vcr_e2aefae21a0b
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": [-4, 1, 21, 6, -1, -14, 10, 9, -18, 1, -11, -17, 2, 22]}, "output": 32}, {"input": {"nums": [4, -12, -3]}, "output": 4}, {"input": {"nums": [12, 3, -7, 13, 7, 3, 6, -9, 11, 5, 3]}, "output": 47}]
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": "009be5cac833b475"}
vcr_55c3b4027c6f
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, 0, 11, 2, 5, 4, -16, 31, 13, -11, 6, 22, -20, 8]}, "output": [13, 13, 24, 26, 31, 35, 19, 50, 63, 52, 58, 80, 60, 68]}, {"input": {"nums": [16, -9, 17, 2, 4, 7, -1, -4]}, "output": [16, 7, 24, 26, 30, 37, 36, 32]}, {"input": {"nums": [-5, -5, 7, 2]}, "output": [-5, -10, -3, -1]}]
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": "8893826a369b4199"}
vcr_ca8c8939e6bf
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, 5, 8, 14, 14]}, "output": [3, 5, 8, 14]}, {"input": {"nums": [0, 0, 0, 2, 4, 7, 9, 10, 11, 11, 11]}, "output": [0, 2, 4, 7, 9, 10, 11]}, {"input": {"nums": [2, 2, 7, 11]}, "output": [2, 7, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "68e7d4cdf9e1a391"}
vcr_ba9dc0aa67ca
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-6, 10, 20, -17, 5, 11, 4, 12, 6, -16]}, "output": 51}, {"input": {"nums": [-8, -1, -12]}, "output": -1}, {"input": {"nums": [1, 13, -10]}, "output": 14}]
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": "39bf258dc9884f4d"}
vcr_89ad3b0bf8af
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-8, 1, -18, -1, -15, 3, 6, -1, 17, 7]}, "output": 32}, {"input": {"nums": [0, 12, -3, 10, -6, -10, 5, 3, 10]}, "output": 21}, {"input": {"nums": [1, 11, -8, -4, 1, -6, 7, -7, 5, -5]}, "output": 12}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "96d8334286d1d361"}
vcr_a90df74ccf44
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-7, 8, 13, 12, -7, -8, 12, 7]}, "output": 37}, {"input": {"nums": [-8, -5, -7, -6, -7, 6, 8, -12, 2, -2]}, "output": 14}, {"input": {"nums": [4, -9, 5, 14, 5, 12, -10, 6, -1]}, "output": 36}]
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": "bc0d979e4dc03333"}
vcr_0794d535d5e6
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [18, 186, 37, 67, 23, 199, 32]}, "output": false}, {"input": {"nums": [18, 6, 12, 21, 9, 12]}, "output": true}, {"input": {"nums": [6, 24, 11, 9, 0, 24, 0, 4, 4, 10]}, "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": "a1d0a78301f5259b"}
vcr_da7da71c9264
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": "ddaacacaacacaadd"}, "output": true}, {"input": {"s": "aaca"}, "output": false}, {"input": {"s": "cbcac"}, "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": "f5095e9d2fd68e00"}
vcr_8aa12620f4a1
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, 27, -16, -18, 34, 23, 4]}, "output": [-24, 3, -13, -31, 3, 26, 30]}, {"input": {"nums": [5, 13, -10, 8, 18, 16, -2, -9, 6, -7]}, "output": [5, 18, 8, 16, 34, 50, 48, 39, 45, 38]}, {"input": {"nums": [5, -3, 7, -4, -2, 4, -11, -3]}, "output": [5, 2, 9, 5, 3, 7, -4, -7]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "ad5d6a522a816270"}
vcr_f34798f00cda
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": "bba eee adbae"}, "output": "adbae eee bba"}, {"input": {"s": "aa bab aa"}, "output": "aa bab aa"}, {"input": {"s": "ab aa"}, "output": "aa ab"}]
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": "5afc099d7d8b18fd"}
vcr_f452f23defb9
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -28, 2, 5, 6, 15, 19, 22, 39, 43, 44], "target": 11}, "output": [1, 8]}, {"input": {"nums": [-19, -11, 5, 11, 34], "target": -6}, "output": [1, 2]}, {"input": {"nums": [-13, 11, 21, 25, 35], "target": 46}, "output": [1, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "d825c3793e2ed321"}
vcr_d5f5e5f89e09
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [22, 10, 29, 12]}, "output": [22, 32, 61, 73]}, {"input": {"nums": [-8, -10, 8, 16]}, "output": [-8, -18, -10, 6]}, {"input": {"nums": [-3, 16, 12, -9, 2, -10, 10]}, "output": [-3, 13, 25, 16, 18, 8, 18]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "a78346577221308d"}
vcr_c431b759db53
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [9, 56, 8, 60, 34, 30, 41, 33, 12, 16, 53, 5]}, "output": 52}, {"input": {"prices": [14, 18, 19, 27, 35, 36]}, "output": 22}, {"input": {"prices": [35, 3, 20, 26, 24, 5, 22]}, "output": 23}]
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": "f6863e3ab82aafe6"}
vcr_ba2581c69e31
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, 7, 10, 14, 25, 29, 30, 32, 40, 42], "target": 7}, "output": 1}, {"input": {"nums": [2, 24, 28, 34], "target": 18}, "output": 1}, {"input": {"nums": [5, 10, 18, 21, 33, 36, 38, 39], "target": 30}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "3f322fb7d1755cbc"}
vcr_9ccb72482ac8
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": "bcbadabadabcb"}, "output": true}, {"input": {"s": "cbb"}, "output": false}, {"input": {"s": "bbbacb"}, "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": "e38aec41670c7f5c"}
vcr_f731dea80f10
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 2, 2, 3, 4, 4, 4, 5, 6, 10, 14]}, "output": [1, 2, 3, 4, 5, 6, 10, 14]}, {"input": {"nums": [2, 6, 8, 12]}, "output": [2, 6, 8, 12]}, {"input": {"nums": [0, 3, 4, 5, 6, 8, 8, 9, 9, 10, 12]}, "output": [0, 3, 4, 5, 6, 8, 9, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "6de6feda2f3114db"}
vcr_1a396ccba981
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 6, 4, 4, 7, 7, 6, 4]}, "output": [112896, 94080, 141120, 141120, 80640, 80640, 94080, 141120]}, {"input": {"nums": [1, 5, 4, 4]}, "output": [80, 16, 20, 20]}, {"input": {"nums": [4, 4, 5, 1, 1, 5]}, "output": [100, 100, 80, 400, 400, 80]}]
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": "03c63b83a51f72e9"}
vcr_1c256a4ae505
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": ["aabb", "cca", "bc", "cb", "cb", "acc", "ab", "cca"]}, "output": [1, 1, 3, 3]}, {"input": {"strs": ["bbb", "bab", "aab", "aaa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["abb", "bba", "bbb", "aba", "aaa", "bbb"]}, "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": "9360abfbbb404611"}
vcr_b72f3eda4453
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [4, 1, 6, 12], "k": 9}, "output": [12, 4, 1, 6]}, {"input": {"nums": [-2, -1, 7, -4], "k": 15}, "output": [-1, 7, -4, -2]}, {"input": {"nums": [8, -8, 2, 0, -2, -8, 5, -2, -9], "k": 8}, "output": [-8, 2, 0, -2, -8, 5, -2, -9, 8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "3352dcae78b2aab4"}
vcr_3e06be657b97
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 4, 3, 2, 1, 3, 0]}, "output": true}, {"input": {"nums": [2, 1, 1, 3, 3, 2, 1, 3, 1]}, "output": true}, {"input": {"nums": [3, 0, 1, 3, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "3d617eaca754173d"}
vcr_07ff298655c2
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [23, 44, 12, 27, 37, 25, 7, 48, 27, 33]}, "output": true}, {"input": {"nums": [8, 10, 23, 4, 24, 13, 22, 13, 8]}, "output": true}, {"input": {"nums": [6, 12, 13, 23, 8]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "a6a31ebe0b4b83ff"}
vcr_b12301225c8f
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [8, 1, 24, 24, 76, 31, 1, 31, 8]}, "output": 76}, {"input": {"nums": [22, 28, 18, 22, 4, 28, 18, 10, 4, 10, 13, 13, 54]}, "output": 54}, {"input": {"nums": [24, 16, 22, 6, 6, 58, 24, 22, 16]}, "output": 58}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "a81bdb10aad55de3"}
vcr_3de782752958
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 7, 4, 3, 5, 0]}, "output": [0, 0, 0, 0, 0, 420]}, {"input": {"nums": [2, 2, 4]}, "output": [8, 8, 4]}, {"input": {"nums": [4, 1, 5, 5, 1, 2, 3]}, "output": [150, 600, 120, 120, 600, 300, 200]}]
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": "a57c90436fbb163e"}
vcr_8e1449f48bc4
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-35, 0, -21, 30, 62, 47, 61, -24, -31, -39, -37, -14, 59, 27], "target": 31}, "output": [4, 8]}, {"input": {"nums": [2, 10, 40, 19, 9], "target": 42}, "output": [0, 2]}, {"input": {"nums": [47, -15, -22, -25, -4, 12, -20, 28, 44, 6], "target": 25}, "output": [0, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "d9ec7923b6967f2f"}
vcr_28426b16b39a
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": [15, 18, 29, 42, 52, 33, 27, 10, 19]}, "output": 37}, {"input": {"prices": [17, 31, 7, 24, 24, 13, 16, 39, 38]}, "output": 32}, {"input": {"prices": [6, 33, 33, 14, 38, 21, 12, 4]}, "output": 32}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "e77ce2e2b102135f"}
vcr_a37ff102a962
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": [2, 23, 2, 18, 2, 24]}, "output": 65}, {"input": {"nums": [0, 17, 3]}, "output": 17}, {"input": {"nums": [19, 5, 6, 3, 11]}, "output": 36}]
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": "72f682525ee0a9f2"}