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_aad9d635ad41
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": [19, 0, 7, 34, 15, 31, 19]}, "output": true}, {"input": {"nums": [18, 0, 2, 9, 5]}, "output": false}, {"input": {"nums": [9, 25, 24, 20, 7, 18, 21, 14, 5, 6]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "c7f2b2da281ae13f"}
vcr_daa482bc32c2
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [1, 24, 29, 25, 10, 27, 28, 1]}, "output": 77}, {"input": {"nums": [11, 2, 11, 18, 4, 6, 12, 19, 3]}, "output": 54}, {"input": {"nums": [2, 16, 13, 18]}, "output": 34}]
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": "ac8acddfff3b94c3"}
vcr_1bd06ae99f57
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, 10, 2, 2, 26, 15, 6, 30, 16, 25, 10, 20]}, "output": 127}, {"input": {"nums": [20, 16, 9, 7, 15, 4, 12]}, "output": 56}, {"input": {"nums": [13, 11, 2, 17, 16, 4, 10]}, "output": 41}]
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": "9ed8b1671734f86f"}
vcr_6e22b91b5ecd
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, 11, 12, 14, 19, 21, 23, 30, 32, 37, 38, 44, 47], "target": 3}, "output": 0}, {"input": {"nums": [1, 2, 7, 8, 10, 14, 25, 32, 34, 38], "target": 24}, "output": 6}, {"input": {"nums": [7, 18, 27], "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": "198ca561a8b47955"}
vcr_dc1e6b9d3753
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-18, 18, 30, 33, 39], "target": 15}, "output": [0, 3]}, {"input": {"nums": [-5, 17, 21, 25, 30, 31], "target": 47}, "output": [1, 4]}, {"input": {"nums": [-18, -12, -4, 20, 29, 31, 33], "target": 51}, "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": "76c764560337905b"}
vcr_3a8b746e8fec
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": [38, 7, 43, 7, 2, 7]}, "output": true}, {"input": {"nums": [11, 16, 8, 14, 24, 15, 8, 12]}, "output": true}, {"input": {"nums": [17, 19, 19, 2, 12, 17, 7, 23, 13, 3, 9, 15]}, "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": "ac0b69247383f73f"}
vcr_72db76067887
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [35, 21, 29, 37, 13, 34, 33, 2, 35]}, "output": true}, {"input": {"nums": [22, 14, 17, 4]}, "output": false}, {"input": {"nums": [10, 11, 5, 3, 8, 0, 3, 10, 6, 5, 12, 19]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "3056ab64b9b460ef"}
vcr_193c1e0e1277
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": 166, "b": 34}, "output": 2}, {"input": {"a": 33, "b": 125}, "output": 1}, {"input": {"a": 167, "b": 104}, "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": "e6d6830b1ecdaec1"}
vcr_2bda605a2362
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, 3, 5, 5, 6]}, "output": [1, 2, 3, 5, 6]}, {"input": {"nums": [4, 5, 8, 9, 12]}, "output": [4, 5, 8, 9, 12]}, {"input": {"nums": [2, 8, 10]}, "output": [2, 8, 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": "87200b5d502f1980"}
vcr_e157f57d64aa
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, -20, -15, -8, 1, 18, -11, -6, -2, -9, 17, -15], "k": 24}, "output": [-18, -20, -15, -8, 1, 18, -11, -6, -2, -9, 17, -15]}, {"input": {"nums": [7, -10, 4, -5, -5, -6], "k": 20}, "output": [-5, -6, 7, -10, 4, -5]}, {"input": {"nums": [-7, -7, 5, 6, -1], "k": 6}, "output": [-1, -7, -7, 5, 6]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "7269579f5712cab1"}
vcr_7ed97d62bb51
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": [26, 14, -7, -3, -25, 15, 30, 10, 18, -16, -10, 14]}, "output": [26, 40, 33, 30, 5, 20, 50, 60, 78, 62, 52, 66]}, {"input": {"nums": [-5, 13, 10, 18, 3, 14, 10, 12, 11, 14]}, "output": [-5, 8, 18, 36, 39, 53, 63, 75, 86, 100]}, {"input": {"nums": [12, -9, 3, 10, -6, -12, -3, -3, 9, 9]}, "output": [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": "b4234945fdda5299"}
vcr_c26fb5382487
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-15, -9, 13, -15, 6], "k": 3}, "output": [13, -15, 6, -15, -9]}, {"input": {"nums": [9, -10, 10, -3], "k": 12}, "output": [9, -10, 10, -3]}, {"input": {"nums": [9, 8, -3, -1, -3, 0, -2, -2, -3, 7], "k": 2}, "output": [-3, 7, 9, 8, -3, -1, -3, 0, -2, -2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "fcc154c8631c4eb4"}
vcr_d1b9f935c86b
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, 4, -13, 1, 15, -14, -14, -14, 18]}, "output": 18}, {"input": {"nums": [5, 0, 11, -4, -12, 8]}, "output": 16}, {"input": {"nums": [-12, -3, -11, 3]}, "output": 3}]
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": "4c40a12c9968a147"}
vcr_f5f1e54128f3
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 1, 6, 4, 5, 1, 3]}, "output": [360, 1080, 180, 270, 216, 1080, 360]}, {"input": {"nums": [1, 2, 2]}, "output": [4, 2, 2]}, {"input": {"nums": [4, 2, 2, 4, 5, 1]}, "output": [80, 160, 160, 80, 64, 320]}]
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": "d53290172acb040e"}
vcr_9540c905b504
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, 23, 26, 39, 45], "target": 11}, "output": 2}, {"input": {"nums": [4, 10, 20, 25], "target": 9}, "output": 1}, {"input": {"nums": [4, 15, 33, 36], "target": 16}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "f30520397dd7c08c"}
vcr_0a15d196f7a4
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": [59, 1, 141, 119, 132, 170, 198, 61, 116, 32, 49, 172]}, "output": false}, {"input": {"nums": [10, 21, 0, 2, 12]}, "output": false}, {"input": {"nums": [12, 5, 23, 9, 5]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "3ac4aecb0bfd23a2"}
vcr_6b7b122ad7e4
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": "aaa baee cbae"}, "output": "cbae baee aaa"}, {"input": {"s": "baa abb"}, "output": "abb baa"}, {"input": {"s": "aaba bbba baab"}, "output": "baab bbba aaba"}]
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": "e46de9226ddaa3e9"}
vcr_87a61e12ae13
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": [10, -5, -14, 9, 32, 6, 19, 31, -17]}, "output": [10, 5, -9, 0, 32, 38, 57, 88, 71]}, {"input": {"nums": [3, 16, -10, 6, 13, 16, 12, -1]}, "output": [3, 19, 9, 15, 28, 44, 56, 55]}, {"input": {"nums": [-2, -8]}, "output": [-2, -10]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "ffc221add676c7bf"}
vcr_349cd97c63fd
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [5, 5, 7, 19, 21, 12]}, "output": 36}, {"input": {"nums": [8, 8, 9]}, "output": 17}, {"input": {"nums": [15, 3, 4, 2, 11, 19]}, "output": 38}]
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": "f2eeba1ce5290fd7"}
vcr_f0a4545c7f85
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": [30, 9, 21, 28, 27, 9, 2]}, "output": 80}, {"input": {"nums": [3, 1, 6]}, "output": 9}, {"input": {"nums": [7, 4, 3, 16, 13, 3]}, "output": 26}]
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": "b3d4cf45245aa4d4"}
vcr_f27db354f2d4
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [44, 34, 24, 11, 59, 3, 10, 47, 54]}, "output": 51}, {"input": {"prices": [6, 3, 25, 7]}, "output": 22}, {"input": {"prices": [28, 13, 26, 11, 10, 1, 23, 23, 26, 39, 1, 8]}, "output": 38}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "6b1a4725afcef910"}
vcr_3fef1c02c48b
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": [7, 7, 2, 5, 7]}, "output": [490, 490, 1715, 686, 490]}, {"input": {"nums": [5, 3, 4, 1, 1]}, "output": [12, 20, 15, 60, 60]}, {"input": {"nums": [4, 4, 5, 2, 4, 2, 4]}, "output": [1280, 1280, 1024, 2560, 1280, 2560, 1280]}]
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": "7928af2048753b6e"}
vcr_66a220198525
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 4, 2, 0, 2, 0, 0]}, "output": true}, {"input": {"nums": [3, 0, 3, 1, 0, 0]}, "output": true}, {"input": {"nums": [0, 3, 1, 3, 2, 0, 0, 1, 0]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "844f3fc6eec1bbd2"}
vcr_c248091b64ac
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [12, 25, 25, 6]}, "output": 37}, {"input": {"nums": [12, 3, 8, 5]}, "output": 20}, {"input": {"nums": [3, 7, 18, 13, 1]}, "output": 22}]
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": "c7dfaabc715dbf94"}
vcr_c3b7eca8df2b
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [3, -18, -14, 8, 17, 9, -9, -6, 17, -4], "k": 17}, "output": [8, 17, 9, -9, -6, 17, -4, 3, -18, -14]}, {"input": {"nums": [-7, -9, -8, 9, 6, 2, 1, -1], "k": 2}, "output": [1, -1, -7, -9, -8, 9, 6, 2]}, {"input": {"nums": [7, -4, 6, -3, 6, -8], "k": 13}, "output": [-8, 7, -4, 6, -3, 6]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "7f195ca7350dd4a7"}
vcr_646bb2a9448b
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": [23, -5, 22, 13, -15, 20, 34, -20, -21, -17, 28, 21, 25]}, "output": [23, 18, 40, 53, 38, 58, 92, 72, 51, 34, 62, 83, 108]}, {"input": {"nums": [-7, -11, 14, 1, -9, -11, 9, 11]}, "output": [-7, -18, -4, -3, -12, -23, -14, -3]}, {"input": {"nums": [4, 11, 2, 10, -4, 8, 15]}, "output": [4, 15, 17, 27,...
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": "5423445d92a17e95"}
vcr_8f4e3af8ad8d
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 3, 10, 11, 12, 13, 14, 15, 23, 31, 46, 47], "target": 9}, "output": 2}, {"input": {"nums": [1, 4, 6, 11, 12, 13, 19, 30, 39], "target": 13}, "output": 5}, {"input": {"nums": [4, 14, 16, 24], "target": 17}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "89e115d9bf7d5b09"}
vcr_e65e6a8a94f1
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 6, 7, 14, 19, 22, 26, 28, 29, 33, 40, 41, 43], "target": 33}, "output": 9}, {"input": {"nums": [2, 8, 10, 17, 22, 23, 25, 26, 34, 37], "target": 29}, "output": 8}, {"input": {"nums": [3, 7, 9, 12, 14, 33, 36], "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": "33d132b7be710b94"}
vcr_4c7b72ad22eb
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 6, 7, 2, 3, 1, 3]}, "output": [756, 378, 324, 1134, 756, 2268, 756]}, {"input": {"nums": [3, 1, 5, 1, 5, 1]}, "output": [25, 75, 15, 75, 15, 75]}, {"input": {"nums": [3, 4, 4, 5, 4, 2, 2]}, "output": [1280, 960, 960, 768, 960, 1920, 1920]}]
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": "ee16e5c7a618c493"}
vcr_cee9779b73b9
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-21, 4, 21, 16, 30, 14, 2, -7, 13, 25, 10]}, "output": [-21, -17, 4, 20, 50, 64, 66, 59, 72, 97, 107]}, {"input": {"nums": [-6, 1, -5, 5, 0, -11, 7, 6, 15]}, "output": [-6, -5, -10, -5, -5, -16, -9, -3, 12]}, {"input": {"nums": [-6, 2, 4, 5, -1, 15, 1, 0, 14, -8]}, "output": [-6, -4, 0, 5, 4, 19, 2...
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "74ef5db7174149df"}
vcr_6222af42a8ab
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": [11, 33, 37, 40, 49], "target": 40}, "output": 3}, {"input": {"nums": [6, 9, 16, 23, 31], "target": 20}, "output": 3}, {"input": {"nums": [1, 10, 11, 23, 27, 29, 35, 37], "target": 17}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "50f2a1e8c5522f12"}
vcr_95bdafa0fdcf
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, 5, 30, 7]}, "output": 54}, {"input": {"nums": [19, 7, 9, 11, 14, 11, 8, 2]}, "output": 50}, {"input": {"nums": [11, 15, 15, 6, 14, 18]}, "output": 44}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "7c8c207fc41b6327"}
vcr_e829b8fd221c
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": 105, "b": 40}, "output": 5}, {"input": {"a": 55, "b": 32}, "output": 1}, {"input": {"a": 90, "b": 112}, "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": "7f33b362585041b9"}
vcr_ef5dce39e17d
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, 7, 6, 4, 3, 7, 5]}, "output": [17640, 88200, 12600, 14700, 22050, 29400, 12600, 17640]}, {"input": {"nums": [5, 3, 2, 4, 2, 3]}, "output": [144, 240, 360, 180, 360, 240]}, {"input": {"nums": [5, 4, 1, 1, 5, 1]}, "output": [20, 25, 100, 100, 20, 100]}]
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": "88b0d8f47198b09d"}
vcr_1f3b1bb427ae
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [14, -6, -4, 18, 0, 19, -18, 3, -3, -3, -8]}, "output": 41}, {"input": {"nums": [9, -4, 8, -6, 0, 6]}, "output": 13}, {"input": {"nums": [4, 13, -10, 1, -12, 10, -9, 7, 5]}, "output": 17}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "6e814177a8d47df1"}
vcr_46daea64c189
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, 22, 19, 27, 23, 1, 24, 16, 7, 4]}, "output": 87}, {"input": {"nums": [8, 19, 20, 1, 5, 5, 9, 6, 3]}, "output": 45}, {"input": {"nums": [10, 20, 7, 14, 17]}, "output": 37}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "e3b3f42dc05efa5c"}
vcr_28d0ef809c1b
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-20, 25, 24, 10]}, "output": [-20, 5, 29, 39]}, {"input": {"nums": [-12, 1]}, "output": [-12, -11]}, {"input": {"nums": [9, -6, 9, -10, -9, 1, -4, 4, 13, -10]}, "output": [9, 3, 12, 2, -7, -6, -10, -6, 7, -3]}]
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": "62aac432c0ed86bc"}
vcr_3bd2289f08c3
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 8, 10, 13, 15, 16], "target": 7}, "output": 1}, {"input": {"nums": [4, 6, 7, 18, 20, 26, 33], "target": 22}, "output": 5}, {"input": {"nums": [5, 10, 21, 22], "target": 11}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "581613f940133183"}
vcr_a39572e4e9a8
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", "bca", "baa", "cbca"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["baa", "abb", "bbb", "bbb", "aab", "aaa"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["bab", "bba", "baa", "bbb", "baa"]}, "output": [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": "8b88ddd0a614ea3b"}
vcr_4959919889ad
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": [186, 43, 107, 84, 15, 111, 177, 149, 180, 56, 102, 158, 3, 85, 183, 178, 124]}, "output": false}, {"input": {"nums": [6, 13, 6, 14, 11, 4]}, "output": true}, {"input": {"nums": [1, 23, 11, 3, 7, 21]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "7f186e84871f1af3"}
vcr_2cb6d9ff6a4f
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": "cb eacced becbc ea bdb cac a"}, "output": "a cac bdb ea becbc eacced cb"}, {"input": {"s": "a bbb babb b bbab"}, "output": "bbab b babb bbb a"}, {"input": {"s": "aabb bbaa baa aaab"}, "output": "aaab baa bbaa aabb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "0965b5950b6a0a8d"}
vcr_26adb8e7771e
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": 12, "b": 218}, "output": 2}, {"input": {"a": 122, "b": 31}, "output": 1}, {"input": {"a": 71, "b": 25}, "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": "d80ecafa5bd9705e"}
vcr_85490a1991cf
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [18, 30, 1, 30, 9, 18, 21, 12, 8, 27, 26, 26]}, "output": 143}, {"input": {"nums": [13, 4, 13, 6]}, "output": 26}, {"input": {"nums": [15, 9, 15, 20]}, "output": 35}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "bc68abbf883acfcc"}
vcr_a63d04f8d2c3
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-20, 56, -39, 7, 8, -36, 67, 68, -30, 59, -38, -26, 13, 37], "target": 80}, "output": [6, 12]}, {"input": {"nums": [38, 1, -19, 15, 40, 48, 44], "target": 25}, "output": [2, 6]}, {"input": {"nums": [43, -19, 0, 29, 31, 48, 1, -5], "target": -24}, "output": [1, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "8e8fda9c5a19f4d4"}
vcr_d1c5bc464a10
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, 24, 29, 33, 37, 44], "target": 18}, "output": 1}, {"input": {"nums": [15, 16, 19, 30, 33, 34, 37], "target": 3}, "output": 0}, {"input": {"nums": [15, 21, 35], "target": 6}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "9b07495070df88e7"}
vcr_0a955f5f578e
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, 1, 2, 7, 12, 12, 13, 14, 14, 14]}, "output": [0, 1, 2, 7, 12, 13, 14]}, {"input": {"nums": [1, 3, 4, 11, 12]}, "output": [1, 3, 4, 11, 12]}, {"input": {"nums": [0, 1, 3, 3, 5, 6, 7, 8, 9, 10, 12, 12]}, "output": [0, 1, 3, 5, 6, 7, 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": "d5f4a957edb70343"}
vcr_2bb0dd461241
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [29, 10, 41, 40, 24, 45]}, "output": 35}, {"input": {"prices": [7, 27, 22, 31, 3, 5, 13, 12]}, "output": 24}, {"input": {"prices": [33, 13, 8, 4, 7, 27]}, "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": "a19f863223f02034"}
vcr_dee50e8bf841
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": "daccdcccaaabcbbd"}, "output": false}, {"input": {"s": "bbbacbb"}, "output": false}, {"input": {"s": "aaaccb"}, "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": "77c625089cae6d89"}
vcr_142ef5bf3420
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": ["bcba", "aa", "bcc", "abc", "cab", "bac", "baab", "bb"]}, "output": [1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bba", "aaa", "abb", "bab", "aab", "bbb"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["aba", "aba", "aba", "aba", "aba"]}, "output": [5]}]
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": "939971a25b310d47"}
vcr_ba7bd56c5b56
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 3, 0, 1, 4, 4, 4, 0, 0, 0]}, "output": true}, {"input": {"nums": [2, 2, 1, 3, 3, 3]}, "output": true}, {"input": {"nums": [3, 3, 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": "4bff078c5e98b405"}
vcr_d2ba91483eae
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [64, 45, 68, 78], "target": 113}, "output": [1, 2]}, {"input": {"nums": [-24, 29, 30, -10], "target": -34}, "output": [0, 3]}, {"input": {"nums": [-9, 38, 20, -14, 42, -11, -18, 12, 21, -2], "target": 10}, "output": [5, 8]}]
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": "40467e3d30e651cc"}
vcr_459d93c9815f
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": [5, 6, 13, -19, -19, 6, -15, -13, -11, 5, 14, 16], "k": 6}, "output": [-15, -13, -11, 5, 14, 16, 5, 6, 13, -19, -19, 6]}, {"input": {"nums": [-4, 10, 10, 6, -6], "k": 18}, "output": [10, 6, -6, -4, 10]}, {"input": {"nums": [0, 9, 7, 9, 0, 6, 7, 6, -4], "k": 10}, "output": [-4, 0, 9, 7, 9, 0, 6, 7, 6...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "545e1d506cde94eb"}
vcr_9c0050fb2882
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -25, -24, -21, -17, -10, -8, -7, 14, 25, 39], "target": -17}, "output": [5, 7]}, {"input": {"nums": [-15, -13, 0, 1, 10, 21, 26, 29, 32, 34, 35], "target": 22}, "output": [1, 10]}, {"input": {"nums": [-19, -18, -2, 12, 14, 15, 19, 21, 22, 31, 36], "target": 2}, "output": [0, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "b0fd9afd5aa4c477"}
vcr_31100c0c8d36
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": [46, 36, 43, 51, 7, 30, 29, 56, 59, 9, 56, 60, 33, 39, 20, 15]}, "output": 53}, {"input": {"prices": [16, 1, 36, 12, 30, 39, 25, 23, 10, 37, 5, 22]}, "output": 38}, {"input": {"prices": [16, 22, 11, 15]}, "output": 6}]
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": "b166d7e80f54455f"}
vcr_9ddd14083951
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 2, 10, 20, 27, 36, 40, 44, 49], "target": 46}, "output": 8}, {"input": {"nums": [8, 14, 30, 32, 33, 35], "target": 31}, "output": 3}, {"input": {"nums": [17, 26, 30], "target": 24}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "4e389ae6d46e12a7"}
vcr_f67b3e35e120
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [41, -3, 66, 76, 15, -8], "target": 63}, "output": [1, 2]}, {"input": {"nums": [37, -23, 19, 41, 24, 2, -6, -7], "target": 35}, "output": [3, 6]}, {"input": {"nums": [35, 16, 47, -19, -20, 33, -15, 9], "target": 14}, "output": [3, 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": "453ad42a6baa16a3"}
vcr_20bc57a144b5
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [32, 15, 51, 28, 14, 32]}, "output": 36}, {"input": {"prices": [29, 17, 37, 13]}, "output": 20}, {"input": {"prices": [9, 40, 38, 20, 4, 6, 11]}, "output": 31}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "236f2cda128e2b96"}
vcr_c1f4ea1d1abd
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 41, "b": 217}, "output": 1}, {"input": {"a": 84, "b": 56}, "output": 28}, {"input": {"a": 172, "b": 38}, "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": "4aca921d617e20ad"}
vcr_a98dcfac9bf6
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-18, -10, -8, -1, 7, 8, 10, 20, 22, 37, 39, 46], "target": 28}, "output": [0, 11]}, {"input": {"nums": [-15, -10, 21, 37], "target": 11}, "output": [1, 2]}, {"input": {"nums": [-10, -4, 7, 8, 9, 10, 13, 25, 31, 37], "target": 21}, "output": [0, 8]}]
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": "4c62a1532b1f27dc"}
vcr_cd4b731065af
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": [-8, 15, 16, 29, 34, 48], "target": 64}, "output": [2, 5]}, {"input": {"nums": [-20, -16, -15, -14, -8, -7, 22, 30, 35, 36, 38], "target": 22}, "output": [1, 10]}, {"input": {"nums": [-20, -15, -10, -9, -8, 7, 18, 27, 32, 38, 39], "target": 34}, "output": [5, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "502aa688f0397293"}
vcr_3803118f6b1b
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": "aaacbcbbcaa"}, "output": 7}, {"input": {"s": "acbcabacbaa"}, "output": 10}, {"input": {"s": "bcba"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "27a103486e4bdcfd"}
vcr_c266a5a610b0
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": [133, 152, 12, 138, 105, 99, 136, 187, 140, 188, 91, 195, 157, 93, 199, 134, 98]}, "output": false}, {"input": {"nums": [23, 6, 4]}, "output": false}, {"input": {"nums": [20, 13, 14]}, "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": "1ec25dd52908f942"}
vcr_df4f0d95f927
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": [-3, 8, 0, -6, -9]}, "output": 8}, {"input": {"nums": [10, -7, 6, -8, 10]}, "output": 11}, {"input": {"nums": [12, 1, -5, 10, 10, 3, 2, -5, -3, 1, 2]}, "output": 33}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "cf0a9b69eeacd7cd"}
vcr_0b953bbb6792
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, 34, 47, 23, 48, 18]}, "output": true}, {"input": {"nums": [20, 22, 13, 14, 9, 7]}, "output": false}, {"input": {"nums": [10, 17, 12, 0]}, "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": "10dbfb39559922c5"}
vcr_32f7de2cd395
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 2, 5, 5, 7, 12, 12, 13]}, "output": [0, 2, 5, 7, 12, 13]}, {"input": {"nums": [0, 2, 3, 4, 7, 10]}, "output": [0, 2, 3, 4, 7, 10]}, {"input": {"nums": [0, 3, 4, 6, 7, 9, 9, 11]}, "output": [0, 3, 4, 6, 7, 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": "ab89c957471b2aea"}
vcr_d869ff9969d4
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, 39, 57, 5, 43, 56, 42, 47, 5]}, "output": 51}, {"input": {"prices": [13, 29, 12, 28, 4, 27, 16, 33]}, "output": 29}, {"input": {"prices": [13, 2, 22, 18, 28, 21, 9, 13, 4, 29, 32]}, "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": "19a0ede22ea0417f"}
vcr_26b7210fbe42
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 11, 20, 23, 42], "target": 26}, "output": 4}, {"input": {"nums": [6, 11, 24, 27, 30, 32, 35, 36, 39], "target": 8}, "output": 1}, {"input": {"nums": [5, 13, 21, 29], "target": 21}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "cb3e72c8066ca365"}
vcr_849927ba297a
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": "bbbcacaacabb"}, "output": 8}, {"input": {"s": "abaacbacb"}, "output": 8}, {"input": {"s": "cbcbcac"}, "output": 7}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "448f97cb896d864b"}
vcr_4b3554ba9b74
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": 23304}, "output": 6}, {"input": {"n": 6525}, "output": 9}, {"input": {"n": 1476}, "output": 5}]
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": "86a1451d8c95066a"}
vcr_d1807db874a1
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": [67, 78, -38, 1, -36, -31, -8, 71], "target": -67}, "output": [4, 5]}, {"input": {"nums": [-20, 38, -24, 6, 41, 15, 28, -17, -27, -19, 39], "target": 34}, "output": [3, 6]}, {"input": {"nums": [-21, 35, -17, 48, 14, 26, 11, -1], "target": 46}, "output": [1, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "66ececf8a3d136e3"}
vcr_5b7e8b54d2b7
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [15, 21, 6, 2]}, "output": 23}, {"input": {"nums": [3, 2, 10, 11, 11, 14, 1, 9, 3, 19]}, "output": 56}, {"input": {"nums": [1, 1, 2, 20, 16]}, "output": 21}]
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": "d9499d1855cde035"}
vcr_8a98bf76e1ae
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": "bbb ac d aac eceaa ebbec"}, "output": "ebbec eceaa aac d ac bbb"}, {"input": {"s": "baa b baa b baa"}, "output": "baa b baa b baa"}, {"input": {"s": "ba aaaa bbb b ab"}, "output": "ab b bbb aaaa ba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "2bf4178f45f45450"}
vcr_22ff7c0d1e52
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [1, 23, 19, 16, 22, 4, 23, 19, 13]}, "output": 81}, {"input": {"nums": [17, 13, 14, 8]}, "output": 31}, {"input": {"nums": [1, 6, 12, 12, 4, 2, 10, 20, 15, 13]}, "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": "183deb5f972350ae"}
vcr_0951d83394fe
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [24, 42, 57, 4, 46, 12, 52, 37, 15]}, "output": 48}, {"input": {"prices": [2, 2, 15, 10, 17, 6]}, "output": 15}, {"input": {"prices": [36, 29, 24, 24, 13, 38, 26]}, "output": 25}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "0f4bc01c6bffab8b"}
vcr_893d7e518ba1
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": 46226}, "output": 7}, {"input": {"n": 4500}, "output": 5}, {"input": {"n": 13956}, "output": 6}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "38fb6eb6c019d12f"}
vcr_b19ac31a67d0
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": [22, 4, 19, 39, 39, 15, 37, 37, 22, 15, 17, 66, 17, 36, 4, 19, 36]}, "output": 66}, {"input": {"nums": [25, 47, 9, 9, 25]}, "output": 47}, {"input": {"nums": [21, 47, 8, 13, 27, 21, 18, 8, 13, 27, 18, 6, 6]}, "output": 47}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "f14178ccf6c5246e"}
vcr_bcbcbea9514b
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-17, 13, -17, 13, 18], "k": 0}, "output": [-17, 13, -17, 13, 18]}, {"input": {"nums": [10, 7, -7, -5, 6], "k": 1}, "output": [6, 10, 7, -7, -5]}, {"input": {"nums": [-9, 3, 5, 9, -4, 5, -9], "k": 4}, "output": [9, -4, 5, -9, -9, 3, 5]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "71350c43622dc24f"}
vcr_92445fae77f2
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [18, 20, 0, -16, 19, -12, 18, -8, 22, 8, 1, 15, -8, -4]}, "output": 85}, {"input": {"nums": [9, 1, 9]}, "output": 19}, {"input": {"nums": [8, -9, 0]}, "output": 8}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "c295ac1b1bdf14dd"}
vcr_36f1aedb5f3d
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [2, 16, 14, -13, 11, 5, -11, -12, 20, -17], "k": 14}, "output": [-11, -12, 20, -17, 2, 16, 14, -13, 11, 5]}, {"input": {"nums": [-2, -9, 8], "k": 14}, "output": [-9, 8, -2]}, {"input": {"nums": [-4, 9, -7, -5, 1, -5, 5, -9], "k": 16}, "output": [-4, 9, -7, -5, 1, -5, 5, -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": "ee5e4fe0dc927e23"}
vcr_2cb5a0af977a
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [11, 27, 25, 4, 5, 15, 24, 14, 23]}, "output": 88}, {"input": {"nums": [12, 19, 7, 16, 8]}, "output": 35}, {"input": {"nums": [20, 12, 4, 4, 13, 15]}, "output": 39}]
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": "72991e098a615277"}
vcr_ea869084967d
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": "bcabbbabbb"}, "output": 6}, {"input": {"s": "bbbcbcacacba"}, "output": 10}, {"input": {"s": "abc"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "658810bfffecc4b0"}
vcr_35b161ae14c7
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": "d beca abcddb c e d"}, "output": "d e c abcddb beca d"}, {"input": {"s": "ba ba"}, "output": "ba ba"}, {"input": {"s": "bab ba"}, "output": "ba bab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "5ac4875bdaa6d0dc"}
vcr_916c532360cc
easy
matrix
Return the sum of all elements in a 2D matrix.
def matrix_sum(mat: list[list[int]]) -> int:
Sum each row, then sum those totals.
def matrix_sum(mat: list[list[int]]) -> int: return sum(sum(row) for row in mat)
[{"input": {"mat": [[2, 1], [0, 5]]}, "output": 8}, {"input": {"mat": [[0, 1, 2], [4, 4, 2], [3, 6, 0], [2, 5, 6]]}, "output": 35}, {"input": {"mat": [[2, 2], [1, 7], [1, 2]]}, "output": 15}]
true
python
1b4635892b4bdc45
### Problem Return the sum of all elements in a 2D matrix. ### Reasoning Sum each row, then sum those totals. ### Solution ```python def matrix_sum(mat: list[list[int]]) -> int: return sum(sum(row) for row in mat) ```
{"v": "ok", "content_hash": "e7f32934c5eda1de"}
vcr_d94f287a93a1
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [25, 28, 18, 1, 18]}, "output": 61}, {"input": {"nums": [8, 1, 18]}, "output": 26}, {"input": {"nums": [17, 9, 15, 20]}, "output": 37}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "64bae42c07b2c5fa"}
vcr_f9ab2e88cf52
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": [7, 63, 35, 11, 1, 26, 3, 39, 7, 1, 26, 11, 39, 14, 14, 3, 35]}, "output": 63}, {"input": {"nums": [15, 3, 17, 3, 4, 9, 4, 9, 44, 17, 15]}, "output": 44}, {"input": {"nums": [4, 59, 25, 25, 4]}, "output": 59}]
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": "e474212801381beb"}
vcr_1998485cbf55
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [16, 2, 8, 24]}, "output": 40}, {"input": {"nums": [13, 0, 14]}, "output": 27}, {"input": {"nums": [1, 12, 7, 18]}, "output": 30}]
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": "9b3dcfdd1f14b21f"}
vcr_a3c84bb3ec11
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": [41, 39, 136, 60, 92, 196, 109, 76, 189, 14, 86, 130, 90, 121]}, "output": false}, {"input": {"nums": [21, 9, 22, 5, 22, 18, 16, 19, 23, 22, 9]}, "output": true}, {"input": {"nums": [14, 17, 1, 9, 15]}, "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": "a0070cd9055d1786"}
vcr_2a5d5ce86aae
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": [37, 58, 53, 18, 43, 58, 40, 35, 48, 47, 7, 18, 27, 54, 6, 34]}, "output": 47}, {"input": {"prices": [7, 34, 35, 37, 31]}, "output": 30}, {"input": {"prices": [1, 8, 26, 14, 19, 31, 14, 9, 4, 37, 35]}, "output": 36}]
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": "047c282dff5d4726"}
vcr_41e9a5981401
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, 1, 6, 7, 4]}, "output": [1008, 840, 5040, 840, 720, 1260]}, {"input": {"nums": [5, 1, 1, 5, 5, 3]}, "output": [75, 375, 375, 75, 75, 125]}, {"input": {"nums": [2, 2, 5, 3, 5]}, "output": [150, 150, 60, 100, 60]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "9c4704f173b1848b"}
vcr_3a9ed77f27cd
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [32, 26, 57, 41, 41, 52, 3, 7, 53, 60, 51, 58, 39, 52, 1, 45]}, "output": 57}, {"input": {"prices": [6, 27, 16]}, "output": 21}, {"input": {"prices": [8, 11, 2]}, "output": 3}]
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": "722149863012bb20"}
vcr_5c65d6f5374f
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": 65104}, "output": 9}, {"input": {"n": 2977}, "output": 6}, {"input": {"n": 10557}, "output": 8}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "68528c1a4f161544"}
vcr_d4cc5a3427f0
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": [-10, 19, -16, 4, -16, 19, 9, 6, -1], "k": 24}, "output": [4, -16, 19, 9, 6, -1, -10, 19, -16]}, {"input": {"nums": [7, 0, -1, 3, -7, 6, 7, -2], "k": 6}, "output": [-1, 3, -7, 6, 7, -2, 7, 0]}, {"input": {"nums": [3, 10, -9, -2, 3, -6, 7], "k": 5}, "output": [-9, -2, 3, -6, 7, 3, 10]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "763b6db189d3b35c"}
vcr_bd8fbed741d9
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": [29, 4, 26, 28, 27]}, "output": 82}, {"input": {"nums": [7, 6, 11, 3, 19, 4, 18, 16, 16, 2]}, "output": 71}, {"input": {"nums": [18, 0, 1, 9, 0, 19]}, "output": 46}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "a0a874954bcb6bd0"}
vcr_b3556ee16b79
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-23, 5, 6, 14, 27, 37, 48], "target": 33}, "output": [2, 4]}, {"input": {"nums": [-18, -6, 6, 10, 12, 13, 14, 18, 21, 26, 33], "target": 47}, "output": [6, 10]}, {"input": {"nums": [4, 16, 25, 36], "target": 61}, "output": [2, 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": "f9a009c2cf1ee4e0"}
vcr_b3d801214448
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": [59, 48, -35, 63, -21], "target": 27}, "output": [1, 4]}, {"input": {"nums": [25, -13, -19, -2, 4, 12], "target": -9}, "output": [1, 4]}, {"input": {"nums": [36, 12, -9, -25, 25], "target": 37}, "output": [1, 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": "86456d0221cbbf83"}
vcr_633e1e8317cf
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [0, 1, 14]}, "output": 14}, {"input": {"nums": [11, 3, 5]}, "output": 16}, {"input": {"nums": [12, 0]}, "output": 12}]
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": "b4f5bfb2dad28a76"}
vcr_efe32bb10c7e
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-8, -19, -17, 13, -5, 4, 8], "k": 14}, "output": [-8, -19, -17, 13, -5, 4, 8]}, {"input": {"nums": [0, 5, -8, 2, 5, -6, 8, 2, -3], "k": 15}, "output": [2, 5, -6, 8, 2, -3, 0, 5, -8]}, {"input": {"nums": [10, 9, -4, -10, 4, -1, 4, -7, -9, -8], "k": 13}, "output": [-7, -9, -8, 10, 9, -4, -10, 4, -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": "cf8f6fb92e2e3115"}
vcr_228dbcc6cfeb
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": 4228}, "output": 3}, {"input": {"n": 15800}, "output": 9}, {"input": {"n": 8480}, "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": "b64935a9b02a0210"}
vcr_4b6eb12675bb
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": 90, "b": 166}, "output": 2}, {"input": {"a": 154, "b": 88}, "output": 22}, {"input": {"a": 142, "b": 74}, "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": "b521094fa5955837"}
vcr_bfd540d1880a
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": [-1, -19, -18, 5], "k": 5}, "output": [5, -1, -19, -18]}, {"input": {"nums": [-1, -5, 5, 4, -10], "k": 0}, "output": [-1, -5, 5, 4, -10]}, {"input": {"nums": [3, 7, 0, 0, 0, -10], "k": 6}, "output": [3, 7, 0, 0, 0, -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": "90f5cc7edf62d909"}