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_0a0395c2259e
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": [8, 29, 8, 29, 7, 5, 19, 4, 15]}, "output": 92}, {"input": {"nums": [15, 9, 19, 12, 19, 7, 13]}, "output": 66}, {"input": {"nums": [17, 20, 13, 20]}, "output": 40}]
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": "3077a60ed1aeaa2d"}
vcr_c563f9a0eceb
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 2, 1, 3, 0, 0, 4, 4, 2, 0]}, "output": true}, {"input": {"nums": [2, 3, 1, 1]}, "output": true}, {"input": {"nums": [3, 2, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "cc37ed765186e693"}
vcr_78cbed5157b1
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, 8, 16, 23, 9]}, "output": 40}, {"input": {"nums": [12, 1]}, "output": 12}, {"input": {"nums": [12, 19, 18, 3]}, "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": "e904cdd5905a2ac5"}
vcr_c24e4cd13806
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": ["cc", "cba", "ab", "bcb", "aa"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bab", "baa", "abb", "bba", "aab"]}, "output": [1, 2, 3]}, {"input": {"strs": ["bbb", "bbb", "aaa", "abb", "abb", "baa", "aab"]}, "output": [1, 2, 2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "e6d5b50aac44c0eb"}
vcr_be701d7e6cf6
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, -22, -17, -8, -6, -5, -2, -1, 0, 11, 29], "target": -13}, "output": [3, 5]}, {"input": {"nums": [-4, 5, 26, 32, 38], "target": 43}, "output": [1, 4]}, {"input": {"nums": [-20, -3, 17, 23, 31, 32], "target": 48}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "6dad5ffbafd42b53"}
vcr_901367b924a5
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": [43, 8, 25, 18, 2, 5, 57, 27]}, "output": 55}, {"input": {"prices": [9, 11, 26, 10, 29, 11, 17]}, "output": 20}, {"input": {"prices": [38, 5, 4, 30, 28, 4]}, "output": 26}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "b3216547522a33eb"}
vcr_5aa5877d3fe9
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [35, 31, 27, 3, 27, 3, 35, 7, 58, 31, 7]}, "output": 58}, {"input": {"nums": [5, 8, 1, 8, 3, 58, 6, 3, 4, 5, 1, 6, 4]}, "output": 58}, {"input": {"nums": [4, 8, 2, 7, 18, 4, 44, 2, 7, 14, 14, 18, 8]}, "output": 44}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "80a1506073d29347"}
vcr_419215ce4ebe
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, 30, 28, 21, 1, 22]}, "output": 73}, {"input": {"nums": [10, 19, 12, 0, 5, 9, 3, 3]}, "output": 34}, {"input": {"nums": [10, 4, 13, 14]}, "output": 24}]
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": "bddcda347652b0ae"}
vcr_24e80e808396
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-7, -4, -13, 3, 17, -20, -18, 12]}, "output": [-7, -11, -24, -21, -4, -24, -42, -30]}, {"input": {"nums": [-3, -3]}, "output": [-3, -6]}, {"input": {"nums": [5, 7, 6]}, "output": [5, 12, 18]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "a52ae63c7226bda7"}
vcr_fc3cbaa7a031
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": ["cb", "cacb", "cba", "acb", "cb", "ca", "bbc", "ac"]}, "output": [1, 1, 2, 2, 2]}, {"input": {"strs": ["baa", "bab", "bbb", "aba", "bab"]}, "output": [1, 2, 2]}, {"input": {"strs": ["bbb", "bab", "aaa", "aba", "baa", "aba", "aba"]}, "output": [1, 1, 1, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "a51677f17ea641a4"}
vcr_6fd23ab88aff
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": "bdaa c"}, "output": "c bdaa"}, {"input": {"s": "b bb"}, "output": "bb b"}, {"input": {"s": "aaba baa"}, "output": "baa 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": "9dffab24bd6adb74"}
vcr_32be9bca089c
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, 1, 3, 3, 4, 5, 5, 9, 9, 9, 9, 12]}, "output": [0, 1, 3, 4, 5, 9, 12]}, {"input": {"nums": [1, 2, 3, 6, 6, 7, 10, 10, 11, 12]}, "output": [1, 2, 3, 6, 7, 10, 11, 12]}, {"input": {"nums": [2, 8, 9, 10]}, "output": [2, 8, 9, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "0db45efc7ef79a42"}
vcr_3eb00e077fc8
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-25, -20, 14, -8, 9, 33, -21, 35]}, "output": [-25, -45, -31, -39, -30, 3, -18, 17]}, {"input": {"nums": [-11, 3, -7, 0, 18, 7]}, "output": [-11, -8, -15, -15, 3, 10]}, {"input": {"nums": [11, 18, 14, -5, 0, 4, -11, 12, 2]}, "output": [11, 29, 43, 38, 38, 42, 31, 43, 45]}]
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": "8ee2e5397f9b96c1"}
vcr_c57e92356d24
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": [49, 7, 16, 13, 16, 16, 15, 48, 20, 36, 9, 40, 47, 1, 3, 5]}, "output": true}, {"input": {"nums": [9, 1, 24, 23, 2, 6]}, "output": false}, {"input": {"nums": [8, 17, 3, 11, 24, 10, 12]}, "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": "d5249557447a089f"}
vcr_75c000dd3875
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": 18}, "output": 2}, {"input": {"a": 72, "b": 55}, "output": 1}, {"input": {"a": 14, "b": 41}, "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": "32187a562e7ccdcd"}
vcr_4598a6978866
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": [58, 37, 3, 13, 3, 11, 20, 37, 13, 20, 11, 15, 15]}, "output": 58}, {"input": {"nums": [9, 29, 9, 46, 29, 15, 15]}, "output": 46}, {"input": {"nums": [53, 23, 10, 16, 10, 16, 23]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "a256f05cc61fcc2b"}
vcr_675abfd5f257
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, 2, 1, 0, 4, 2, 0]}, "output": false}, {"input": {"nums": [0, 1, 3, 2, 1, 0]}, "output": false}, {"input": {"nums": [0, 1, 2, 3, 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": "1f181623f58251fe"}
vcr_346fcce4a8c0
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, 7, 2, 2, 0, 2]}, "output": [0, 0, 0, 0, 168, 0]}, {"input": {"nums": [4, 1, 3, 3, 2, 1]}, "output": [18, 72, 24, 24, 36, 72]}, {"input": {"nums": [4, 4, 5, 5]}, "output": [100, 100, 80, 80]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "b607853c69ce176b"}
vcr_f5826b10da11
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [59, 58, 54, 32, 17, 54, 58, 53]}, "output": 41}, {"input": {"prices": [21, 22, 18, 26, 40, 21]}, "output": 22}, {"input": {"prices": [23, 37, 20, 35, 36, 7, 38, 33]}, "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": "5de17ee67925692d"}
vcr_d24b8be176bd
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [11, -17, 18, -12, 7, -13, 11], "k": 4}, "output": [-12, 7, -13, 11, 11, -17, 18]}, {"input": {"nums": [9, -3, 4, -8, 8], "k": 16}, "output": [8, 9, -3, 4, -8]}, {"input": {"nums": [3, 10, 10, 1, 7, 6, -2, 1, 10], "k": 1}, "output": [10, 3, 10, 10, 1, 7, 6, -2, 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": "a593f72a0787eddf"}
vcr_64b8f3e6a9ff
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": ["ac", "ab", "cb", "caa", "ac", "abbb", "bb", "bcab"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["baa", "aab", "aab", "baa"]}, "output": [4]}, {"input": {"strs": ["aba", "bbb", "aaa", "bab", "aba", "aba", "abb"]}, "output": [1, 1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "ace51a62c521735c"}
vcr_102654d6d962
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [36, 8, 21, 27, 27, 11, 8, 68, 21, 11, 36]}, "output": 68}, {"input": {"nums": [24, 14, 11, 16, 24, 46, 16, 11, 14]}, "output": 46}, {"input": {"nums": [3, 16, 27, 3, 27, 16, 57]}, "output": 57}]
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": "af6d0346fb788982"}
vcr_56f1a3293897
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, 30, 0]}, "output": 30}, {"input": {"nums": [7, 14, 3, 2]}, "output": 16}, {"input": {"nums": [5, 16, 15, 20, 18, 7, 19]}, "output": 57}]
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": "5ff142f780f3b7cb"}
vcr_3d163958b252
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": "ccedb cdd caac bceb bdad daba b"}, "output": "b daba bdad bceb caac cdd ccedb"}, {"input": {"s": "bbab babb"}, "output": "babb bbab"}, {"input": {"s": "ba baab"}, "output": "baab 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": "a16091a9688f1ac4"}
vcr_2c1e1cccf9a8
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, 7, 7, 2, 3, 1, 5]}, "output": [1470, 1050, 1050, 3675, 2450, 7350, 1470]}, {"input": {"nums": [5, 2, 4, 4]}, "output": [32, 80, 40, 40]}, {"input": {"nums": [3, 2, 3, 5, 3, 2, 5]}, "output": [900, 1350, 900, 540, 900, 1350, 540]}]
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": "2fb9254da7f7e4d4"}
vcr_ee86690a6e8c
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": 175, "b": 70}, "output": 35}, {"input": {"a": 180, "b": 84}, "output": 12}, {"input": {"a": 168, "b": 62}, "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": "7e5cb72089c7f281"}
vcr_b7f16bb2e0f9
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": "bbaaabacbabac"}, "output": 10}, {"input": {"s": "aabaacbbab"}, "output": 7}, {"input": {"s": "bbaa"}, "output": 2}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "e88a084c582d902f"}
vcr_584cc3e04285
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, 5, 8, 9, 9, 11, 12, 12, 13, 14]}, "output": [1, 5, 8, 9, 11, 12, 13, 14]}, {"input": {"nums": [1, 2, 5, 6, 7, 10]}, "output": [1, 2, 5, 6, 7, 10]}, {"input": {"nums": [0, 1, 1, 5, 7, 10, 11, 12, 12, 12]}, "output": [0, 1, 5, 7, 10, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "24877a0c86ca09bc"}
vcr_16d591bc78ef
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": [43, 3, 16, 45, 36, 32, 35, 28, 32, 52, 35, 20]}, "output": 49}, {"input": {"prices": [2, 25, 22, 25, 39, 29, 8, 18, 24]}, "output": 37}, {"input": {"prices": [24, 30, 24]}, "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": "428d8c6ae0378ae6"}
vcr_5e669d6414c4
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": ["ab", "baaa", "aa", "bbb", "abb", "ccca", "cb"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "aaa", "bbb", "abb", "bab", "abb"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["baa", "bba", "aaa", "abb", "bba"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "c21e3e40c6457693"}
vcr_c7170e02f96f
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": [25, 39, 43, 47, 48], "target": 36}, "output": 1}, {"input": {"nums": [1, 3, 9, 11, 14, 17, 19, 24, 26, 30, 31, 34], "target": 5}, "output": 2}, {"input": {"nums": [0, 1, 10, 12, 17, 21, 23, 27, 33, 37, 38, 39], "target": 23}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "dbd3ab86f21018ce"}
vcr_825871b43e10
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [20, -13, 15, 16, 8, -1, 18, 11, 1, -1, 5], "k": 6}, "output": [-1, 18, 11, 1, -1, 5, 20, -13, 15, 16, 8]}, {"input": {"nums": [-2, 6, -9, -8, 8, -6, 2, 1, 7], "k": 2}, "output": [1, 7, -2, 6, -9, -8, 8, -6, 2]}, {"input": {"nums": [-3, -9, 5, 5, -7, 4, -3, 4, 2], "k": 0}, "output": [-3, -9, 5, 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": "014aece366d17031"}
vcr_0419ebf0f833
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-12, -5, 9, -2, -5, 6, 10]}, "output": 18}, {"input": {"nums": [7, -6, 12, 1, -8, -1]}, "output": 14}, {"input": {"nums": [-1, -8, 9, -6, 13, -5, 13, 0, 12]}, "output": 36}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "b2c5ab432e280e60"}
vcr_1a9f8b1705a3
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": ["ac", "bba", "abbb", "cac", "cbb", "abab", "cca"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "aba", "baa"]}, "output": [1, 2]}, {"input": {"strs": ["aba", "baa", "aba", "bbb"]}, "output": [1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "9563678cfabcdbca"}
vcr_49d029376d1f
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": [13, 57, 27, 36, 13, 55]}, "output": 44}, {"input": {"prices": [14, 27, 17]}, "output": 13}, {"input": {"prices": [32, 26, 21, 19, 15, 31, 14]}, "output": 16}]
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": "ea65bf6a55ea5356"}
vcr_f0d255b0a65a
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-12, -4, 8, 9, 13, 27, 33, 36, 39], "target": 47}, "output": [2, 8]}, {"input": {"nums": [-14, 7, 9, 12, 17, 19, 20, 23, 27, 31, 38], "target": 29}, "output": [2, 6]}, {"input": {"nums": [-17, 8, 9, 20], "target": 29}, "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": "40e470d3956e205a"}
vcr_00dc6af0aaf6
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": 8662}, "output": 7}, {"input": {"n": 2158}, "output": 6}, {"input": {"n": 8494}, "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": "4766f4802015b9f2"}
vcr_cff400b7ddc0
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [13, 7, 12, -16, 11, -20, 13, 7], "k": 21}, "output": [-16, 11, -20, 13, 7, 13, 7, 12]}, {"input": {"nums": [5, -3, 10, -9, 1, 2, 10, -9, 4, -3], "k": 9}, "output": [-3, 10, -9, 1, 2, 10, -9, 4, -3, 5]}, {"input": {"nums": [7, 5, 3, 9, 7, 10, 1, -5, 0, 8], "k": 16}, "output": [7, 10, 1, -5, 0, 8, 7,...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "224656d323376535"}
vcr_e020c16ec8d2
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, 0, 10, 18, 13, 28, 7, 28]}, "output": 86}, {"input": {"nums": [5, 4, 18]}, "output": 23}, {"input": {"nums": [8, 14, 7, 2]}, "output": 16}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "6f0a3e62c2798874"}
vcr_e53b9c2dc0b0
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": "bcabaccbabcab"}, "output": 12}, {"input": {"s": "cabaaba"}, "output": 6}, {"input": {"s": "abbcca"}, "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": "b7729ae50592a276"}
vcr_58486954bc0f
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 4, 1, 2, 3, 4, 0, 0]}, "output": true}, {"input": {"nums": [1, 1, 2, 0]}, "output": true}, {"input": {"nums": [0, 0, 0, 0]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "6317b735951d84d7"}
vcr_33201caab252
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [7, 7, -9, -2, 20]}, "output": 23}, {"input": {"nums": [11, -1, -5, -3, -8]}, "output": 11}, {"input": {"nums": [-9, 11, -9, 5, 8, -12, 9, 11, 7, -6]}, "output": 30}]
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": "09962834731d0907"}
vcr_623ae671cfa6
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": [21, 0, 41, 13, 3, 30, 45, 32, 18, 29, 5, 29, 19, 26, 45]}, "output": true}, {"input": {"nums": [23, 3, 15, 0, 21]}, "output": false}, {"input": {"nums": [14, 7, 24, 8]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "16144997817e13b5"}
vcr_3b15872d6c24
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, 4, 19, 3, 1, 13, 18, 17, 1, 8, 21]}, "output": 95}, {"input": {"nums": [14, 14, 5]}, "output": 19}, {"input": {"nums": [1, 18]}, "output": 18}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "92c68ee7bd8d1767"}
vcr_f149b3da0807
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 2, 3, 0, 1, 3, 0]}, "output": true}, {"input": {"nums": [0, 3, 1, 2, 2, 1, 1]}, "output": false}, {"input": {"nums": [0, 3, 1, 0, 2, 0, 3]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "df0e2c5a71b3fbf2"}
vcr_337be3eb2dae
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, 7, 8, 31, 9, 5, 10, 13, 30, 4]}, "output": 25}, {"input": {"prices": [20, 29, 31, 36, 14, 18, 5]}, "output": 16}, {"input": {"prices": [20, 13, 26, 1, 16, 29, 4, 1, 6]}, "output": 28}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "6045e63ddf7f6903"}
vcr_ec68ed16817f
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 3, 5, 5, 1]}, "output": [75, 25, 15, 15, 75]}, {"input": {"nums": [4, 2, 5, 5, 4, 4, 1]}, "output": [800, 1600, 640, 640, 800, 800, 3200]}, {"input": {"nums": [1, 1, 5, 3, 3, 1]}, "output": [45, 45, 9, 15, 15, 45]}]
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": "0409c20a29c5f08d"}
vcr_ed131a0bb10e
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 1, 3, 5, 6, 7, 9, 9, 12, 15, 15, 15]}, "output": [1, 3, 5, 6, 7, 9, 12, 15]}, {"input": {"nums": [3, 5, 11]}, "output": [3, 5, 11]}, {"input": {"nums": [1, 2, 2, 3, 7, 8, 9, 12]}, "output": [1, 2, 3, 7, 8, 9, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "b0b84c3479ee6a14"}
vcr_6ac31cbed367
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, 1, 21]}, "output": 22}, {"input": {"nums": [11, 14]}, "output": 14}, {"input": {"nums": [0, 1]}, "output": 1}]
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": "01f784b18b061d43"}
vcr_72dbf7cbc4d1
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 4, 4, 4, 3, 0]}, "output": false}, {"input": {"nums": [3, 0, 3]}, "output": true}, {"input": {"nums": [1, 0, 3, 2, 1, 0, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "d0c61180c23ee9a4"}
vcr_59179bdb3392
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "aaaabdadba"}, "output": false}, {"input": {"s": "cbcabcacc"}, "output": false}, {"input": {"s": "cb"}, "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": "630e5f13f73e27c7"}
vcr_37f31e4efd2b
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 7, 8, 22, 30, 31, 34, 46, 49], "target": 21}, "output": 3}, {"input": {"nums": [7, 9, 14, 18, 19, 24, 33, 38], "target": 1}, "output": 0}, {"input": {"nums": [4, 19, 20, 22, 24, 29, 32, 33, 36], "target": 31}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "e9d3a2f6f3fae116"}
vcr_0dab0ba3beb0
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [7, 0, 13, 5, -9, 21, 10, 9, -15, -4, -12, 3]}, "output": 56}, {"input": {"nums": [11, -10, -9, 14, 10, 6, 11, 2]}, "output": 43}, {"input": {"nums": [14, 2, -3, -5]}, "output": 16}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "c5e0e3db8d8434c6"}
vcr_68045d6cf468
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-20, -4, 18, 10, -9, 11, -9, -4, -7, 5, 16, 16], "k": 26}, "output": [16, 16, -20, -4, 18, 10, -9, 11, -9, -4, -7, 5]}, {"input": {"nums": [6, -1, -8], "k": 8}, "output": [-1, -8, 6]}, {"input": {"nums": [2, 3, -1, 9, -9, 10], "k": 2}, "output": [-9, 10, 2, 3, -1, 9]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "c0c0ebe5b93ad6de"}
vcr_e94b10677cf6
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": ["cac", "cccb", "bbab", "caa", "ab", "aba", "acab", "caaa", "bccb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "aab", "aaa"]}, "output": [1, 1, 1]}, {"input": {"strs": ["abb", "abb", "bbb", "bbb", "baa", "aaa"]}, "output": [1, 1, 2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "31e69fd41ed20533"}
vcr_e25e0c8cca9e
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": "cc ddabb"}, "output": "ddabb cc"}, {"input": {"s": "baba abba"}, "output": "abba baba"}, {"input": {"s": "ba abab a"}, "output": "a abab 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": "bc437af79d8ec295"}
vcr_0fa90ca488db
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, 12, -4, -8, -8, -5, -8, -14, 8, -18, 8], "k": 0}, "output": [10, 12, -4, -8, -8, -5, -8, -14, 8, -18, 8]}, {"input": {"nums": [-6, -7, -6, -3, 2, 4, 0, -5, -1, -1], "k": 5}, "output": [4, 0, -5, -1, -1, -6, -7, -6, -3, 2]}, {"input": {"nums": [10, 8, -10, 1, -3, 9], "k": 5}, "output": [8, -10, ...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "a693409e48c40607"}
vcr_f09ba5bd9ed1
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 4, 26, 41, 43], "target": 48}, "output": 5}, {"input": {"nums": [6, 8, 15, 17, 18, 23, 34, 37, 38], "target": 33}, "output": 6}, {"input": {"nums": [3, 12, 16, 20, 21, 35, 36, 38], "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": "30e9a9fa0e8b5093"}
vcr_21e396fc84aa
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [12, 2, -7, 4, 10, -5, 32, -4, 25]}, "output": [12, 14, 7, 11, 21, 16, 48, 44, 69]}, {"input": {"nums": [7, 10, 6]}, "output": [7, 17, 23]}, {"input": {"nums": [12, 0, -6, -4, 2, 2, 2]}, "output": [12, 12, 6, 2, 4, 6, 8]}]
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": "673a0831b5a566c2"}
vcr_ed27b5f9b960
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": [6, 27, 6, 9, 6, 7, 11, 44, 26]}, "output": true}, {"input": {"nums": [6, 21, 14]}, "output": false}, {"input": {"nums": [11, 4, 4, 19, 14, 14, 21, 7, 16, 9]}, "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": "4f7420dcab627abe"}
vcr_4b40f4ba6578
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": "baccaababb"}, "output": 7}, {"input": {"s": "bbc"}, "output": 2}, {"input": {"s": "bbccababc"}, "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": "26411c8bfe7ff914"}
vcr_13e257387785
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "cddcca"}, "output": false}, {"input": {"s": "ca"}, "output": false}, {"input": {"s": "cbbaaa"}, "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": "2bfa228551177aed"}
vcr_ead97297f91f
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [6, 6, 11, -15, 10, 17, 22, 21, 22, 22, 10]}, "output": 132}, {"input": {"nums": [-7, -10, -2, 9, 9, 0, -9]}, "output": 18}, {"input": {"nums": [12, 13, 1, 4, -8]}, "output": 30}]
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": "2e5158b3b12d85ae"}
vcr_1566146df1b5
hard
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "cccbcdbabddcc"}, "output": false}, {"input": {"s": "caaaabaacc"}, "output": false}, {"input": {"s": "acca"}, "output": true}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "63cf1b7960ab1e5e"}
vcr_88c9e63555d3
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, 20, 27, 17, 26, 19, 30, 27, 0, 22, 2]}, "output": 129}, {"input": {"nums": [0, 20, 4, 12, 3, 17, 13]}, "output": 49}, {"input": {"nums": [11, 18, 4, 16, 19, 8, 7, 18, 2]}, "output": 60}]
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": "236a453c09787bd1"}
vcr_47f76e6974ea
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [10, 10, 23, 13, 23, 1, 25, 13, 55, 25, 1, 8, 8]}, "output": 55}, {"input": {"nums": [47, 3, 15, 2, 2, 4, 28, 27, 15, 3, 4, 28, 27]}, "output": 47}, {"input": {"nums": [10, 7, 52, 3, 18, 18, 19, 23, 7, 23, 3, 10, 19]}, "output": 52}]
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": "4be3cab8e839c2a3"}
vcr_115b55c8761b
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": [34, 34, 17, 9, 12, 17, 30, 2, 12, 69, 14, 14, 2, 30, 9]}, "output": 69}, {"input": {"nums": [52, 1, 4, 4, 1]}, "output": 52}, {"input": {"nums": [29, 25, 18, 46, 8, 19, 29, 18, 8, 19, 6, 25, 6]}, "output": 46}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "dc5bc6b71a3cec8a"}
vcr_e4477423e12c
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": "ab cdba dcbdb cabb dabda"}, "output": "dabda cabb dcbdb cdba ab"}, {"input": {"s": "aaa bb"}, "output": "bb aaa"}, {"input": {"s": "baaa bb b a"}, "output": "a b bb baaa"}]
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": "d366302d1f3a7a34"}
vcr_d1e0af82252a
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": "addbdddbdda"}, "output": true}, {"input": {"s": "bacaabac"}, "output": false}, {"input": {"s": "cbbabaaacc"}, "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": "afa83125be09414b"}
vcr_dfcfbd612f6c
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": [-10, -6, 5, 7, 18, 29, 44], "target": 12}, "output": [1, 4]}, {"input": {"nums": [-10, -9, -3, -2, 0, 7, 14, 17, 18, 20, 35], "target": 5}, "output": [1, 6]}, {"input": {"nums": [-18, -1, 5, 12, 13, 20], "target": 25}, "output": [2, 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": "880febfa09bb933f"}
vcr_3db81597e463
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-15, 0, 14, 10, -14, 13, 11]}, "output": 34}, {"input": {"nums": [0, 6, 2, -1, 1, -8, 11]}, "output": 11}, {"input": {"nums": [-2, 8, 0, -6]}, "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": "e7b440c4aa47d745"}
vcr_13525d0847cb
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [27, 27, 5, 7, 23, 9, 8, 9, 30, 7, 14, 12]}, "output": 107}, {"input": {"nums": [15, 16, 4, 18, 1, 18, 6, 9]}, "output": 61}, {"input": {"nums": [12, 4, 16, 8, 11, 3, 19, 14, 14, 11]}, "output": 72}]
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": "f60bdf0542460cc8"}
vcr_a98ef3c9aa5c
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 6, 8, 11, 14, 15, 18, 21, 24, 25, 28, 32, 38, 48], "target": 22}, "output": 8}, {"input": {"nums": [0, 3, 5, 7, 9, 17, 29, 35, 37], "target": 39}, "output": 9}, {"input": {"nums": [0, 2, 4, 8, 13, 17, 25, 27, 29], "target": 21}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "8170f80ac666ba7e"}
vcr_b197f7a42c22
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": [-33, 76, -31, -28, 39], "target": 45}, "output": [1, 2]}, {"input": {"nums": [31, 46, -6, 30, -22, 36, 1, -29], "target": -21}, "output": [4, 6]}, {"input": {"nums": [-9, 34, 8, 37, -4, 5], "target": 39}, "output": [1, 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": "121f2fafa1181642"}
vcr_003f2ad65886
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 6, 6, 7, 9, 14, 15]}, "output": [0, 2, 6, 7, 9, 14, 15]}, {"input": {"nums": [0, 0, 2, 5, 5, 6, 7, 8, 12]}, "output": [0, 2, 5, 6, 7, 8, 12]}, {"input": {"nums": [5, 6, 8, 10, 10, 12]}, "output": [5, 6, 8, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "1b3f57cf3a7f95dd"}
vcr_dfccbc5eeafe
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": [9, 14, -8, 2, -18, -18, 17, 17, -7, 13, -13, -14, -9, 9]}, "output": 40}, {"input": {"nums": [7, -9, -11, 11]}, "output": 11}, {"input": {"nums": [2, 3, 7]}, "output": 12}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "64ed5502c633bc44"}
vcr_dba9e6e27a2b
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": [17, 54, 59, 29, 8, 53, 23, 39]}, "output": 45}, {"input": {"prices": [18, 30, 20, 10, 25, 29, 5, 14, 31, 5]}, "output": 26}, {"input": {"prices": [1, 3, 34, 29, 20, 31, 10, 25, 30, 36, 16]}, "output": 35}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "b9d4aafefc15adf6"}
vcr_9902f6b48d07
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, 24, 50, 40, 47, 36, 0, 11, 9, 47]}, "output": true}, {"input": {"nums": [21, 1, 6, 24, 5, 23, 18, 9, 18, 2, 8]}, "output": true}, {"input": {"nums": [8, 3, 9, 21, 11, 13, 3, 11, 25, 2, 25, 7]}, "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": "1729ab9fcd5807b4"}
vcr_acb3c2076b92
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": "dbde cedbcb dacca bcdbcb eddad"}, "output": "eddad bcdbcb dacca cedbcb dbde"}, {"input": {"s": "baba ab aaa bb aaaa"}, "output": "aaaa bb aaa ab baba"}, {"input": {"s": "bab bb baa"}, "output": "baa bb 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": "feb2d69388c3711f"}
vcr_23ff143ffe5c
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, 6, 11, 0, -5, -10, -17, -10, 9, 4, 17, -8], "k": 33}, "output": [0, -5, -10, -17, -10, 9, 4, 17, -8, -1, 6, 11]}, {"input": {"nums": [5, 4, 8, -4, 0, 3], "k": 20}, "output": [0, 3, 5, 4, 8, -4]}, {"input": {"nums": [-6, -10, -2, -9, 0, -1, -8, 10, 9], "k": 0}, "output": [-6, -10, -2, -9, 0, -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": "100b9196391fa9b9"}
vcr_34d318e41f6c
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, -10, -16, 4, 16, 14, -14, -1, -5, 1, 0]}, "output": 34}, {"input": {"nums": [3, -7, -1]}, "output": 3}, {"input": {"nums": [-6, 9, 13, -5, -4, 0, -7, -5, 0, 9]}, "output": 22}]
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": "2c68a8797c92bc5b"}
vcr_11c4ebb19a00
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, 4, 2, 3, 4, 0, 0, 4, 1, 0]}, "output": true}, {"input": {"nums": [1, 0, 3, 3, 2, 1, 0, 1, 1]}, "output": false}, {"input": {"nums": [1, 2, 1, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "9cddaa1ca4da89d2"}
vcr_a14dc873a643
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-7, -6, 17, 9, -1, 13, 15, -3, -7, -3, 7, -3, -13, 11]}, "output": 53}, {"input": {"nums": [7, 8, -7, -5, 0, 6, -3, -10, -6, -11, 0]}, "output": 15}, {"input": {"nums": [-9, 13, -6, 13, 4, -1]}, "output": 24}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "41c8a3bc1d5a9c15"}
vcr_599b57340c9e
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": "bddced bcaae acebea"}, "output": "acebea bcaae bddced"}, {"input": {"s": "b abaa b"}, "output": "b abaa b"}, {"input": {"s": "a bb bb baa bbba"}, "output": "bbba baa bb bb a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "6ca766ceee87df2a"}
vcr_72c751a293f8
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [1, 24, 15, 35, 15, 30, 16, 16, 61, 30, 12, 35, 1, 24, 12]}, "output": 61}, {"input": {"nums": [28, 46, 14, 18, 3, 18, 29, 3, 28, 14, 29]}, "output": 46}, {"input": {"nums": [15, 25, 5, 5, 17, 17, 15, 25, 54]}, "output": 54}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "51d58180f3752f0c"}
vcr_1b6edb64302d
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 3, 6, 7, 10, 10, 11, 14, 14]}, "output": [0, 3, 6, 7, 10, 11, 14]}, {"input": {"nums": [0, 1, 4, 8, 9, 10, 11, 11]}, "output": [0, 1, 4, 8, 9, 10, 11]}, {"input": {"nums": [4, 10, 11, 11, 12, 12]}, "output": [4, 10, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "d4b07ea73ca40145"}
vcr_a1551ba723e3
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [17, 10, 12, 16, 5, 15, 15]}, "output": 49}, {"input": {"nums": [15, 17, 16, 12, 14, 18, 2]}, "output": 49}, {"input": {"nums": [13, 3, 17, 11, 12]}, "output": 42}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "8f5cae6878649f60"}
vcr_d3619d49cb2c
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": [30, 38, 35, 32, 44, 3, 57, 17, 46, 35, 25, 11, 44]}, "output": 54}, {"input": {"prices": [2, 20, 36]}, "output": 34}, {"input": {"prices": [15, 12, 31, 39, 8, 7]}, "output": 27}]
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": "cf9f77d2cb297e88"}
vcr_1f2065fdac02
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": "eadaed ceccaa aeecc dbdd"}, "output": "dbdd aeecc ceccaa eadaed"}, {"input": {"s": "a baaa aab aa b"}, "output": "b aa aab baaa a"}, {"input": {"s": "abaa b bba baba"}, "output": "baba bba b abaa"}]
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": "9cbb1f115aacc3fb"}
vcr_40185d223cc0
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, 4, 0, 4, 1, 1, 3, 4, 0]}, "output": true}, {"input": {"nums": [3, 3, 2]}, "output": true}, {"input": {"nums": [2, 3, 1, 1, 2, 3, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "1c03707264bb9b77"}
vcr_ee9a42bd54cd
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": [9, 10, 15, 17, 22, 26, 28, 31, 45, 46], "target": 34}, "output": 8}, {"input": {"nums": [4, 20, 36], "target": 10}, "output": 1}, {"input": {"nums": [1, 8, 12, 15, 28, 32, 35, 36], "target": 1}, "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": "a647bbbc1cda3bfe"}
vcr_fe5505594ca1
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": 27048}, "output": 7}, {"input": {"n": 6829}, "output": 8}, {"input": {"n": 14903}, "output": 9}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "dbfba4affef7b056"}
vcr_81e3b47c8ac8
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": [20, 22, 24, 5, 15, 16]}, "output": 60}, {"input": {"nums": [10, 18, 10]}, "output": 20}, {"input": {"nums": [8, 19, 3, 20, 19, 0, 13]}, "output": 52}]
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": "4f6abeb224829224"}
vcr_1ecf027f19e1
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": [56, 21, 11, 39, 54]}, "output": 43}, {"input": {"prices": [14, 13, 27, 8, 27]}, "output": 19}, {"input": {"prices": [6, 2, 8, 10, 18, 11, 4, 9, 30]}, "output": 28}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "52d67becac4e7ad8"}
vcr_cdbb65ab8a0e
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": "bbaaacbaaaaba"}, "output": 7}, {"input": {"s": "aaabaaccc"}, "output": 4}, {"input": {"s": "acaaab"}, "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": "bb67fc6a3b0f1dad"}
vcr_55189f23e427
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": [20, 25, 15, 6, 27, 19, 22, 29]}, "output": 91}, {"input": {"nums": [19, 14, 1, 20, 12]}, "output": 39}, {"input": {"nums": [3, 5, 3, 4, 8, 11]}, "output": 20}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "7d4497561affa359"}
vcr_0716a62f8e6b
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, 6, 11, 12, 22, 39, 40], "target": -18}, "output": [0, 2]}, {"input": {"nums": [-2, -1, 6, 38], "target": 37}, "output": [1, 3]}, {"input": {"nums": [-13, -8, -2, 8, 15, 18, 25, 26, 31], "target": 23}, "output": [1, 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": "e74f32272f899aaa"}
vcr_4651aeb419a5
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, -18, -6, 8, 37, 43], "target": 14}, "output": [0, 4]}, {"input": {"nums": [-16, -12, -4, -1, 3, 9, 10, 23, 30], "target": -6}, "output": [0, 6]}, {"input": {"nums": [-11, -2, 2, 3, 13, 39], "target": 42}, "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": "3652fb86746e1175"}
vcr_d6e3fb3b132d
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": [40, 30, 46, 34, 39, 14, 28, 47, 22, 43, 56]}, "output": 42}, {"input": {"prices": [2, 31, 19, 24, 40, 11, 8, 36, 39, 20, 14]}, "output": 38}, {"input": {"prices": [3, 24, 12, 6, 17, 4]}, "output": 21}]
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": "8ba91e842fbd5a35"}
vcr_87c58e847336
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, 32, 14, -23, -6, -25, 1, -5, 30, 17, 24, 16]}, "output": [21, 53, 67, 44, 38, 13, 14, 9, 39, 56, 80, 96]}, {"input": {"nums": [-10, 11, 6]}, "output": [-10, 1, 7]}, {"input": {"nums": [-10, -3, -11, -8, 18, 1]}, "output": [-10, -13, -24, -32, -14, -13]}]
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": "686e79b265b27543"}