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_6c3f030dd2e3
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": [10, 10, 48, 20, 59, 53, 54, 13]}, "output": 49}, {"input": {"prices": [33, 26, 4, 19, 35, 33, 32, 29, 27, 37, 34, 10]}, "output": 33}, {"input": {"prices": [35, 17, 2, 13, 8, 2]}, "output": 11}]
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": "ebcfccd40f9f52f4"}
vcr_f8af8f8905af
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -18, 3, 22, 49], "target": -15}, "output": [1, 2]}, {"input": {"nums": [-8, 3, 4, 6, 7, 8, 13, 19, 28, 34], "target": 36}, "output": [5, 8]}, {"input": {"nums": [-17, -7, -6, -4, -1, 0, 23, 24], "target": -8}, "output": [1, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "ec984c6e4696f82c"}
vcr_523c96eb1add
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, -17, -9, 7, 36, 39], "target": -44}, "output": [0, 1]}, {"input": {"nums": [3, 7, 8, 13], "target": 16}, "output": [0, 3]}, {"input": {"nums": [-15, -12, -5, -4, -2, 0, 3, 6, 7, 19, 29], "target": -12}, "output": [0, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "b7fa4adf7b3599c8"}
vcr_7253410aae40
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, 17, -21, -21, 4, -19, -20]}, "output": [-21, -4, -25, -46, -42, -61, -81]}, {"input": {"nums": [6, 10, -12, -7, -2, 12, 7, -11, 2]}, "output": [6, 16, 4, -3, -5, 7, 14, 3, 5]}, {"input": {"nums": [17, 13, 12, 17]}, "output": [17, 30, 42, 59]}]
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": "dc13694fb06f59bd"}
vcr_7a01f03c41e8
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": ["cba", "ba", "bc", "aa", "cabb", "bb", "bac"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "aaa", "baa", "bba", "bbb"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["aab", "abb", "aba", "bba", "bab"]}, "output": [2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "6118bfd2e8987dfa"}
vcr_5085b8f231a8
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": "aae ddded acb add bcdbaa"}, "output": "bcdbaa add acb ddded aae"}, {"input": {"s": "bb bab baba aaba"}, "output": "aaba baba bab bb"}, {"input": {"s": "bb b"}, "output": "b bb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "3b9c5d40cb370592"}
vcr_41bad8c48ba5
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": [0, 2, 7, 7]}, "output": [98, 0, 0, 0]}, {"input": {"nums": [1, 1, 4, 3, 1, 5]}, "output": [60, 60, 15, 20, 60, 12]}, {"input": {"nums": [5, 4, 2, 5, 3]}, "output": [120, 150, 300, 120, 200]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "2ed67f5cee159d05"}
vcr_60d6de661d21
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [21, 18, 16, 24, 27, 2, 0, 26, 12, 9, 24, 3]}, "output": 114}, {"input": {"nums": [1, 11, 10, 14, 18]}, "output": 29}, {"input": {"nums": [13, 0, 12, 3, 9]}, "output": 34}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "3bb98bcfc3977516"}
vcr_eafd02f59a36
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": [29, 35, 38, 44, 49, 18, 4, 11, 1, 12, 21, 1, 33]}, "output": true}, {"input": {"nums": [0, 1, 12, 24, 24, 19, 17, 21]}, "output": true}, {"input": {"nums": [20, 2, 14]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "3c6a48156eb128e9"}
vcr_56404350bb12
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": "bacadcbaabcdacab"}, "output": true}, {"input": {"s": "bbcbbcb"}, "output": false}, {"input": {"s": "babccb"}, "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": "00c30b28732a1b49"}
vcr_2698cf0541ea
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, 5, 0, 4, 8]}, "output": 19}, {"input": {"nums": [12, 19]}, "output": 19}, {"input": {"nums": [9, 9, 10, 15, 17, 0, 15, 3, 5, 1]}, "output": 56}]
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": "5e12e4305acba662"}
vcr_27431560b64e
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, 0, 0, 0, 3, 1, 0, 0]}, "output": false}, {"input": {"nums": [0, 2, 3, 2, 0]}, "output": false}, {"input": {"nums": [0, 3, 0, 3, 2, 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": "e0b76e1165ba0d42"}
vcr_6cd8fb6daa7d
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [4, 13, 6, 20, 14, 12, 11, -5, 14, -8], "k": 22}, "output": [14, -8, 4, 13, 6, 20, 14, 12, 11, -5]}, {"input": {"nums": [7, -7, -3, 4, 7, -10, 0, -7, 9], "k": 12}, "output": [0, -7, 9, 7, -7, -3, 4, 7, -10]}, {"input": {"nums": [-8, 9, 5, 8, 0, 4, -6], "k": 19}, "output": [5, 8, 0, 4, -6, -8, 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": "2a05b3da56e7abc5"}
vcr_bdd9d1078242
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [16, 7, 27, 12, 23, 6, 21, 10, 11, 7, 23]}, "output": 121}, {"input": {"nums": [2, 3, 3]}, "output": 5}, {"input": {"nums": [8, 8, 6, 7, 12, 5, 6, 6, 11, 8]}, "output": 43}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "d0820e72fd4e88ca"}
vcr_4f35b2c85e40
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [2, 6, 7, 8, 9, 15]}, "output": [2, 6, 7, 8, 9, 15]}, {"input": {"nums": [5, 6, 7, 7, 11, 12]}, "output": [5, 6, 7, 11, 12]}, {"input": {"nums": [0, 1, 3, 5, 5, 6, 10]}, "output": [0, 1, 3, 5, 6, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "a1915acc19d9dcbb"}
vcr_033ee45db0dc
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": 130, "b": 66}, "output": 2}, {"input": {"a": 154, "b": 34}, "output": 2}, {"input": {"a": 109, "b": 166}, "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": "8264c7bc3a770d75"}
vcr_6569246129c2
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": "dcdda be"}, "output": "be dcdda"}, {"input": {"s": "a aab a bb"}, "output": "bb a aab a"}, {"input": {"s": "bab a ab abbb abab"}, "output": "abab abbb ab a 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": "bcebe14c3dfadc84"}
vcr_6896f1151b3b
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": "dcbcaaccaacbcd"}, "output": true}, {"input": {"s": "bcbcabc"}, "output": false}, {"input": {"s": "abcaccca"}, "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": "287334a554a93ff5"}
vcr_43ca1367739b
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, 29, 8, 3, 0, 24, 30, 28, 23, 12]}, "output": 96}, {"input": {"nums": [14, 7, 18, 18, 3, 4]}, "output": 36}, {"input": {"nums": [2, 5, 12, 13, 18]}, "output": 32}]
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": "3bf7ffde3760e6f4"}
vcr_83e1bb1517b5
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, 9, 15, 16, 18, 24, 27, 41, 47], "target": 46}, "output": 8}, {"input": {"nums": [1, 5, 6, 10, 12, 18, 21, 24, 27, 31, 35, 39], "target": 16}, "output": 5}, {"input": {"nums": [2, 4, 7, 8, 13, 17, 18, 19, 25, 26, 38, 39], "target": 23}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "92bf4409aee909de"}
vcr_106d9f1d1054
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": [49, 61, -15, 37, 32, 65, 42, 19, -29, -40, -37], "target": 36}, "output": [5, 8]}, {"input": {"nums": [5, 34, 32, -23, 18, 26, 16, -21, 13], "target": 45}, "output": [2, 8]}, {"input": {"nums": [13, -5, -21, 26, 41, 28], "target": 8}, "output": [0, 1]}]
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": "6e0a12a0ad41537d"}
vcr_90694a954611
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": [30, 13, 52, 30, 13, 22, 22]}, "output": 52}, {"input": {"nums": [19, 40, 29, 19, 29]}, "output": 40}, {"input": {"nums": [9, 57, 25, 25, 9]}, "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": "3ca051268c713ffe"}
vcr_dc82e6dbbb0e
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [33, -23, 30, -11, 22, -11, 15]}, "output": [33, 10, 40, 29, 51, 40, 55]}, {"input": {"nums": [6, 17, -10]}, "output": [6, 23, 13]}, {"input": {"nums": [6, 4, 2, 17, 17, 10, -2, 0, -11, 7]}, "output": [6, 10, 12, 29, 46, 56, 54, 54, 43, 50]}]
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": "66f29f302764ce68"}
vcr_90bdf33febd1
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-5, 9, -9, 5, -3, -13, -9, -3]}, "output": 9}, {"input": {"nums": [9, 1, 8, 12, 5, 3, 2, -8]}, "output": 40}, {"input": {"nums": [6, 6, 14, 13, -8, 12]}, "output": 43}]
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": "f576881534a99669"}
vcr_7ed67d91044a
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-18, 13, -5, 9, -11, -8, 17, -6, 22, 6, 3]}, "output": 42}, {"input": {"nums": [-1, 3, 2, -4, 8, 8, -5, 0, -7, -4, 13]}, "output": 17}, {"input": {"nums": [-6, -6, -6, 3, 14, -10]}, "output": 17}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "d061aaaba21d5385"}
vcr_d942bfbab47f
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 2, 5, 8, 10, 11, 12, 14, 18, 19, 29, 37, 39, 42], "target": 19}, "output": 9}, {"input": {"nums": [7, 11, 19, 21, 24, 26, 27, 33], "target": 14}, "output": 2}, {"input": {"nums": [4, 13, 17, 21, 26, 31, 34, 39], "target": 40}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "51f41d1a5cddd485"}
vcr_e94d79188dd3
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, -27, 6, 37, 41], "target": -23}, "output": [0, 2]}, {"input": {"nums": [-14, -5, -4, -2, 4], "target": -9}, "output": [1, 2]}, {"input": {"nums": [-18, -12, 2, 14, 17, 19, 31, 33], "target": 5}, "output": [1, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "3cc52e77e7e680f1"}
vcr_a28af9d7aa22
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, 26, 16, 4, 25, 6, 26, 6]}, "output": 91}, {"input": {"nums": [17, 17, 16, 20, 9]}, "output": 42}, {"input": {"nums": [7, 17, 7, 11, 7, 15]}, "output": 43}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "b1d018661940d566"}
vcr_635aa4729631
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": [-17, -10, -9, -6, 2, 5, 14, 15, 17, 18, 31, 37], "target": -15}, "output": [0, 4]}, {"input": {"nums": [-18, 2, 3, 6, 7, 13, 14, 19], "target": 8}, "output": [1, 3]}, {"input": {"nums": [-16, -11, -10, -4, 3, 4, 7, 19, 29, 30], "target": 22}, "output": [4, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "f1ba2da7319dbede"}
vcr_3841ce8dfd52
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": "aaaccaaaaa"}, "output": 3}, {"input": {"s": "baabaccaabb"}, "output": 7}, {"input": {"s": "cabccaab"}, "output": 6}]
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": "5ba3f4558774dd4f"}
vcr_0e563b0b123f
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, 1, 3, 10, 12, 16, 35, 49], "target": 38}, "output": [3, 7]}, {"input": {"nums": [-20, -5, 1, 9, 14, 19, 26, 29], "target": 33}, "output": [4, 5]}, {"input": {"nums": [-13, -3, 1, 6, 8, 10, 11, 19, 29, 30], "target": 27}, "output": [1, 9]}]
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": "aecd92439f955de2"}
vcr_7980ce5a42fa
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 4, 1, 4, 0, 1, 0, 1, 4, 0]}, "output": false}, {"input": {"nums": [1, 2, 0]}, "output": true}, {"input": {"nums": [3, 3, 1, 2, 0, 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": "a67fb1e11279bfaa"}
vcr_d8fa454a589f
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-24, -6, 24]}, "output": [-24, -30, -6]}, {"input": {"nums": [3, -9, -11]}, "output": [3, -6, -17]}, {"input": {"nums": [-9, -5, 4]}, "output": [-9, -14, -10]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "7a9ab2cd97c85a75"}
vcr_c5ea018f8a46
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-15, -9, 17, -18], "k": 8}, "output": [-15, -9, 17, -18]}, {"input": {"nums": [10, 8, -6, 7, 8, 6, -5, -5, -4, 5], "k": 5}, "output": [6, -5, -5, -4, 5, 10, 8, -6, 7, 8]}, {"input": {"nums": [-4, 6, -4, 7], "k": 0}, "output": [-4, 6, -4, 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": "28208faa2f475e38"}
vcr_aad684948d91
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": "aaaaaabcabbca"}, "output": 7}, {"input": {"s": "acbaca"}, "output": 6}, {"input": {"s": "cabca"}, "output": 5}]
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": "2a26fe7f4d661c8c"}
vcr_987854ac273d
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [49, -4, 43, 11, 35, 69, -38, 55], "target": 65}, "output": [1, 5]}, {"input": {"nums": [44, -13, 7, 11, 36, 31, -12], "target": -1}, "output": [3, 6]}, {"input": {"nums": [48, 34, 11, -25, 8, 18], "target": 29}, "output": [2, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "2925ce1c03ff59c9"}
vcr_9cba28e12152
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, 48, 35, 11, 32, 48, 47]}, "output": 37}, {"input": {"prices": [2, 16, 34, 21, 25, 24]}, "output": 32}, {"input": {"prices": [34, 34, 21, 32]}, "output": 11}]
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": "ba622dceee39adee"}
vcr_997571d0a99a
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [64, 27, 23, -13, 44, 21, -28, 45], "target": 10}, "output": [2, 3]}, {"input": {"nums": [-16, 10, 11, 27, -8, 44, 43, 49, -11, 14, 17], "target": 36}, "output": [4, 5]}, {"input": {"nums": [14, 22, 46, 39, 19, 16, 20, 37, 6], "target": 59}, "output": [3, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "764a117f5d6ab42e"}
vcr_55c597ea7cbe
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 13, 15, 28, 36, 48], "target": 9}, "output": 1}, {"input": {"nums": [1, 4, 7, 9, 10, 15, 16, 21, 24, 29, 32, 36], "target": 0}, "output": 0}, {"input": {"nums": [2, 4, 5, 26, 27, 28, 30, 31, 33, 34, 37], "target": 17}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "1903deb6a781e525"}
vcr_c09767869e4f
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": ["baaa", "bcab", "abc", "ab", "caab", "abb"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "abb", "aab"]}, "output": [1, 1, 1]}, {"input": {"strs": ["bba", "bba", "bba", "bbb", "aaa"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "59838228b7e83bfe"}
vcr_dd7a2303d78f
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": "ccbce aabbd aac bca eea bbec"}, "output": "bbec eea bca aac aabbd ccbce"}, {"input": {"s": "bbbb b"}, "output": "b bbbb"}, {"input": {"s": "aa aa"}, "output": "aa aa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "cca81508292b7de3"}
vcr_316245238403
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": "daacddd"}, "output": false}, {"input": {"s": "bbb"}, "output": true}, {"input": {"s": "bbbbbaa"}, "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": "e6b6fad47cf87f8b"}
vcr_a597099b1a2c
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": "abdeca eb"}, "output": "eb abdeca"}, {"input": {"s": "aa baba bbb"}, "output": "bbb baba aa"}, {"input": {"s": "aa abb"}, "output": "abb aa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "57225cf5ff0c1064"}
vcr_44f175504560
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, 19, 20, -3, 0, -1], "k": 14}, "output": [0, -1, 20, 19, 20, -3]}, {"input": {"nums": [1, 10, -10, -1, 0, -7], "k": 1}, "output": [-7, 1, 10, -10, -1, 0]}, {"input": {"nums": [3, -2, -2, -8, -4, 4, -9, -9, -1], "k": 17}, "output": [-2, -2, -8, -4, 4, -9, -9, -1, 3]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "5c6c48d0bc6e1dc4"}
vcr_3ff9be7a8377
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": [10, 49, 43, 12, 13, 14, 60, 4, 57, 49, 34, 8, 36, 15, 58]}, "output": 54}, {"input": {"prices": [22, 28, 4]}, "output": 6}, {"input": {"prices": [14, 5, 4]}, "output": 0}]
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": "e5b204915c34c2d7"}
vcr_65a3bb3653b5
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, -11, 12, -22, -19, 35, 21, -8, 20, 9, 19, 30]}, "output": [-21, -32, -20, -42, -61, -26, -5, -13, 7, 16, 35, 65]}, {"input": {"nums": [0, 0, 7]}, "output": [0, 0, 7]}, {"input": {"nums": [-7, 2, -1]}, "output": [-7, -5, -6]}]
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": "ed6ea20693d1da05"}
vcr_67fed0d71b15
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [61, 39, 44, 38, 70, 59, 93, 51, 172]}, "output": false}, {"input": {"nums": [17, 18, 21, 22]}, "output": false}, {"input": {"nums": [7, 10, 23, 1, 20, 6]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "9d82c5386fea011b"}
vcr_2b076cf39ea4
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": [16, 17, 0, -18, 24, 26, 23, 10, -13, 23, 19, -22, -24]}, "output": [16, 33, 33, 15, 39, 65, 88, 98, 85, 108, 127, 105, 81]}, {"input": {"nums": [-4, 12, 0, -4, 17, 7, 0, 18]}, "output": [-4, 8, 8, 4, 21, 28, 28, 46]}, {"input": {"nums": [16, 5, -11, -3, -4, -4, -7]}, "output": [16, 21, 10, 7, 3, -1...
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "4250c08997d4c751"}
vcr_8029ebef0568
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [22, 7, 26, 5, 1, 6, 4, 16, 6, 9, 19]}, "output": 89}, {"input": {"nums": [11, 2, 5, 3, 8, 16]}, "output": 32}, {"input": {"nums": [17, 9, 15, 10, 5, 9, 17, 14]}, "output": 55}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "1283ab03a610e32e"}
vcr_71706b6391d1
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, 9, 22, 24, 29, 38, 43], "target": 37}, "output": 5}, {"input": {"nums": [9, 12, 18], "target": 23}, "output": 3}, {"input": {"nums": [0, 7, 9, 14, 17, 20, 23, 27, 36, 38], "target": 25}, "output": 7}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "8d73aa129fa29f27"}
vcr_08935036c20a
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-6, -6, 20, 13, -3, -6, -7, 7, 19, -10, 14, 7], "k": 4}, "output": [19, -10, 14, 7, -6, -6, 20, 13, -3, -6, -7, 7]}, {"input": {"nums": [-10, 8, -9, 4], "k": 6}, "output": [-9, 4, -10, 8]}, {"input": {"nums": [8, 5, -2, -1], "k": 16}, "output": [8, 5, -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": "ec92c0976cb08229"}
vcr_0e3e273de17b
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": "dcdacbd"}, "output": false}, {"input": {"s": "acccbacbca"}, "output": false}, {"input": {"s": "acbc"}, "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": "ebd13767d1825f4a"}
vcr_a178cb6f1c0c
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, 16, 22, 26, 28, 29, 45, 49], "target": 12}, "output": 1}, {"input": {"nums": [0, 1, 2, 13, 21, 29, 30, 32, 35], "target": 39}, "output": 9}, {"input": {"nums": [0, 1, 24, 32], "target": 33}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "73cab7a18ffca464"}
vcr_18efd33357e4
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": 1813}, "output": 6}, {"input": {"n": 4831}, "output": 9}, {"input": {"n": 14646}, "output": 8}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "6a3a0227e612b700"}
vcr_cffad3f2b056
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": [35, -1, 79, 1, -19, 40, 17, 49, 24, 76], "target": 16}, "output": [0, 4]}, {"input": {"nums": [-30, 43, -24, -2, 33, -29, 5, -6, -28, 27, -5], "target": 5}, "output": [4, 8]}, {"input": {"nums": [-3, 30, -12, 32], "target": -15}, "output": [0, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "b5981ab8561e809f"}
vcr_598f6baf0fb7
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "c eaeaa acba aa"}, "output": "aa acba eaeaa c"}, {"input": {"s": "abaa ba aa baa abba"}, "output": "abba baa aa ba abaa"}, {"input": {"s": "a ba bba a"}, "output": "a bba ba 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": "60f16eb3f26eb7df"}
vcr_334cd1c1e1c0
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": [-4, -15, 27, 0]}, "output": [-4, -19, 8, 8]}, {"input": {"nums": [9, 13, -4, 2, 12, 7, -5, 10, 4]}, "output": [9, 22, 18, 20, 32, 39, 34, 44, 48]}, {"input": {"nums": [-10, 5, 18, 3, 0, 9, -7]}, "output": [-10, -5, 13, 16, 16, 25, 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": "a88f8af7ec8050ee"}
vcr_30e85bdd430a
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-19, 11, 2, 1, -3, -20, 17, -11, 8, 11], "k": 24}, "output": [17, -11, 8, 11, -19, 11, 2, 1, -3, -20]}, {"input": {"nums": [-10, -6, 9, 7, -1, 0, 6, -2], "k": 10}, "output": [6, -2, -10, -6, 9, 7, -1, 0]}, {"input": {"nums": [-2, -8, 1, 6, 0, 4, -6, 8], "k": 15}, "output": [-8, 1, 6, 0, 4, -6, 8, -...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "9c6307b8e9e36ce3"}
vcr_0c8957244a5e
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, 1, 3, 4, 4, 5, 6, 9, 11, 12, 13, 13]}, "output": [0, 1, 3, 4, 5, 6, 9, 11, 12, 13]}, {"input": {"nums": [3, 6, 6, 7, 9, 9, 10]}, "output": [3, 6, 7, 9, 10]}, {"input": {"nums": [1, 2, 3, 3, 9, 10, 11]}, "output": [1, 2, 3, 9, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "69c25b0ed85f092d"}
vcr_a4fee2b40039
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [65, 8, 61, -38, -21, -25, -7, -13, 38, 62, -31, 69, 11], "target": 34}, "output": [0, 10]}, {"input": {"nums": [49, -8, 40, -6, -24, 13, -12, -16], "target": -40}, "output": [4, 7]}, {"input": {"nums": [18, -5, 22, 39, 13, 41, 11, 5], "target": 18}, "output": [4, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "a8f3d3ae718d9b1c"}
vcr_6872f6e7acf0
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [3, -3, 18, 11, 10, -13, -19, -16, 4], "k": 24}, "output": [11, 10, -13, -19, -16, 4, 3, -3, 18]}, {"input": {"nums": [-3, 10, 9], "k": 11}, "output": [10, 9, -3]}, {"input": {"nums": [-5, -1, 2, 1], "k": 16}, "output": [-5, -1, 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": "2aeb6ee0a34cf583"}
vcr_49e16a564bd5
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": [11, 3, 71, 35, 35, 11, 3]}, "output": 71}, {"input": {"nums": [10, 8, 8, 10, 46]}, "output": 46}, {"input": {"nums": [7, 7, 58, 14, 14]}, "output": 58}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "16000c03dae2a486"}
vcr_13cf89053f21
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": [1, 26, 24, 41, 49, 28, 16, 2, 44, 31, 21, 28]}, "output": true}, {"input": {"nums": [11, 11, 22, 7, 17, 12, 7]}, "output": true}, {"input": {"nums": [19, 14, 15, 1, 8, 21, 12, 14, 14, 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": "342b8d98543ca0d1"}
vcr_0ef7891a1465
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": "bbbababbbcaca"}, "output": 9}, {"input": {"s": "babaac"}, "output": 5}, {"input": {"s": "ccbbbcaababb"}, "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": "1334cae0de2e105a"}
vcr_abe040f8ac54
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": [1, 14, 31, 35, 45, 48], "target": 59}, "output": [1, 4]}, {"input": {"nums": [-20, -17, -16, -14, 17, 22, 32, 37], "target": 54}, "output": [4, 7]}, {"input": {"nums": [-7, -4, 1, 6, 8, 13, 21, 22, 25, 29, 31], "target": 28}, "output": [3, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "3faeacae4cc55373"}
vcr_63492ddbb984
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, -16, -15, -13, -9, 2, 3, 10, 19, 43, 49], "target": 28}, "output": [2, 9]}, {"input": {"nums": [-2, 10, 13, 20, 22, 25], "target": 33}, "output": [2, 3]}, {"input": {"nums": [-16, -11, -5, 11, 13, 20, 25, 38], "target": 8}, "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": "a5ed2fffb77c6104"}
vcr_3bea11173c82
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, 20, 2, 21, 4, 35, 18, 4, 20, 39, 2, 39, 21, 70, 18, 35, 10]}, "output": 70}, {"input": {"nums": [21, 19, 19, 43, 21]}, "output": 43}, {"input": {"nums": [12, 21, 43, 12, 21]}, "output": 43}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "b5fe28fb445596fd"}
vcr_beb21c39bb42
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": [0, 1, 21, -13, 11, -17, 11, -14]}, "output": 22}, {"input": {"nums": [14, 12, 12]}, "output": 38}, {"input": {"nums": [2, -3, -6, -4, 5, -7, 0, 8]}, "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": "1cbba2f5c6e2f941"}
vcr_4cf3bb6369e7
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 3, 3, 6, 6, 7, 8, 9, 9, 9, 13, 13, 15]}, "output": [0, 1, 3, 6, 7, 8, 9, 13, 15]}, {"input": {"nums": [0, 1, 3, 7, 7, 7, 11]}, "output": [0, 1, 3, 7, 11]}, {"input": {"nums": [2, 3, 12]}, "output": [2, 3, 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": "5b5f5e8f84e6a86c"}
vcr_123c5b469fc1
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [6, 7, 7, 9, 15]}, "output": [6, 7, 9, 15]}, {"input": {"nums": [1, 2, 4, 4, 6, 6]}, "output": [1, 2, 4, 6]}, {"input": {"nums": [0, 1, 4, 7, 12]}, "output": [0, 1, 4, 7, 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": "88d791e38cbc1725"}
vcr_c6202e704291
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, 3, 5, 7, 8, 8, 9, 11, 12, 14]}, "output": [0, 2, 3, 5, 7, 8, 9, 11, 12, 14]}, {"input": {"nums": [3, 3, 5, 5, 8, 11, 11, 12, 12]}, "output": [3, 5, 8, 11, 12]}, {"input": {"nums": [0, 1, 3, 3, 4, 7, 8, 10]}, "output": [0, 1, 3, 4, 7, 8, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "5e2c4a11249357aa"}
vcr_4703bc282913
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, 14, 15, 17, 20, 23, 24, 26, 31, 32, 35, 39, 42], "target": 6}, "output": 1}, {"input": {"nums": [2, 10, 11, 16, 20, 29, 31, 35, 37, 39], "target": 36}, "output": 8}, {"input": {"nums": [25, 28, 39], "target": 22}, "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": "3ecddc174f1d3468"}
vcr_86e1ecfb77de
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": [42, 4, 34, 2, 42, 55, 32, 47]}, "output": 53}, {"input": {"prices": [11, 14, 22, 13]}, "output": 11}, {"input": {"prices": [37, 18, 29, 22, 39, 8, 9, 29, 35]}, "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": "7b8fddce28e537ed"}
vcr_903fe024f948
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [5, 8, 9, 10, 11, 13, 15, 18, 23, 31, 35, 47, 49], "target": 23}, "output": 8}, {"input": {"nums": [0, 20, 37], "target": 31}, "output": 2}, {"input": {"nums": [3, 5, 7, 12, 20, 21, 26, 34, 37], "target": 13}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "f38db8fa517a8b8a"}
vcr_831c0469a3b7
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [14, 1, 23, 30, 22, 22, 13, 1]}, "output": 72}, {"input": {"nums": [1, 4]}, "output": 4}, {"input": {"nums": [15, 6, 6, 6, 7]}, "output": 28}]
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": "a07a377e5557f6be"}
vcr_57df91b21111
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": "abaaaababba"}, "output": 7}, {"input": {"s": "aaabcbbcbcab"}, "output": 9}, {"input": {"s": "cacababac"}, "output": 9}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "865545aad67c5a74"}
vcr_11bb0f2ca819
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, 10, 10, 11, 12, 13, 14, 15, 15, 15]}, "output": [1, 5, 8, 10, 11, 12, 13, 14, 15]}, {"input": {"nums": [0, 0, 4, 6, 8, 10, 11]}, "output": [0, 4, 6, 8, 10, 11]}, {"input": {"nums": [0, 1, 2, 2, 3, 3, 4, 6, 8, 9]}, "output": [0, 1, 2, 3, 4, 6, 8, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "41b92149838f3e03"}
vcr_9d96679422f7
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [7, 9, 11, 12, 41, 48], "target": 17}, "output": 4}, {"input": {"nums": [1, 2, 6, 20], "target": 0}, "output": 0}, {"input": {"nums": [16, 29, 32], "target": 31}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "06bcf657ae8a6d91"}
vcr_4a987748f5e2
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": [27, 13, 37, 32, 29, 10, 27]}, "output": true}, {"input": {"nums": [2, 17, 17, 1, 19, 7, 0, 3, 25, 10]}, "output": true}, {"input": {"nums": [7, 21, 11, 3, 11, 6, 13, 11, 22, 7, 17, 23]}, "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": "394ede239a29786e"}
vcr_7141144c2cf1
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, 4, 2, 4, 1, 4]}, "output": [128, 160, 320, 160, 640, 160]}, {"input": {"nums": [4, 5, 5, 1, 4]}, "output": [100, 80, 80, 400, 100]}, {"input": {"nums": [2, 1, 3, 2, 4, 1]}, "output": [24, 48, 16, 24, 12, 48]}]
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": "4cde531ca909259b"}
vcr_91d84716de05
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [11, 20, 29, 40], "target": 40}, "output": 3}, {"input": {"nums": [11, 14, 24, 28, 36, 37], "target": 16}, "output": 2}, {"input": {"nums": [0, 3, 5, 6, 11, 20, 21, 23, 24, 32, 33, 35], "target": 15}, "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": "618bb698b2550b58"}
vcr_594c762a981d
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [2, 21, 13, 4, 14, 8]}, "output": 35}, {"input": {"nums": [14, 19]}, "output": 19}, {"input": {"nums": [1, 5, 10, 6, 12, 3, 1, 3, 10]}, "output": 34}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "7fa11ebe1d11ba8b"}
vcr_cf8b89cd4264
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": 34063}, "output": 7}, {"input": {"n": 5261}, "output": 6}, {"input": {"n": 844}, "output": 5}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "9048fa6710d9b5c1"}
vcr_733730d3241c
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": [21, -13, -2, 22, 7, 17, -11, -13, 2]}, "output": 52}, {"input": {"nums": [11, 1, 13, 5, -8]}, "output": 30}, {"input": {"nums": [-1, -3, 2, 7, 13, 12, 10, 1, 7]}, "output": 52}]
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": "3d70c3a5d4ac53f8"}
vcr_df9fb069e590
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", "bc", "cab", "cc", "cbca", "bbab", "abbb", "bbba"]}, "output": [1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bab", "baa", "bbb", "aaa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "bba", "bbb", "aba"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "70ead1a6ffc0f4f3"}
vcr_4aa8be21d978
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": [34, 16, 30, 49, 9]}, "output": 33}, {"input": {"prices": [15, 19, 36, 30, 25, 17, 6, 17, 38, 5, 3]}, "output": 32}, {"input": {"prices": [5, 1, 4, 38, 6]}, "output": 37}]
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": "842c35ae721c0945"}
vcr_205cbad1b372
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": [157, 151, 127, 134, 32, 48, 132, 122, 10, 18, 69]}, "output": false}, {"input": {"nums": [3, 15, 25, 23, 0, 22, 12, 8, 20, 4, 14]}, "output": false}, {"input": {"nums": [22, 5, 24]}, "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": "4ac3f3c42f41b414"}
vcr_86c59126fc52
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": ["abbc", "aa", "ca", "aa", "acba", "bcc"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bba", "abb", "bbb", "aaa", "bba"]}, "output": [1, 1, 4]}, {"input": {"strs": ["bbb", "bbb", "aab"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "1f582489dee13c1f"}
vcr_b43642f2e48c
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, 30, 5, 13, 5]}, "output": 43}, {"input": {"nums": [0, 1, 14, 5, 17, 10]}, "output": 31}, {"input": {"nums": [13, 3, 6, 16, 7, 10]}, "output": 39}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "7abf1f9695530dd6"}
vcr_8c4f520c9a3e
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, -23, -17, -9, -1, 4, 6, 7, 20, 36, 43], "target": 42}, "output": [4, 10]}, {"input": {"nums": [-16, -14, -12, -4, 8, 13, 17, 26, 34], "target": 14}, "output": [2, 7]}, {"input": {"nums": [-16, 2, 7, 19, 30, 38], "target": 32}, "output": [1, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "93d79e95b577997b"}
vcr_47e5f7d0d1de
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, 2, 1, 0, 4, 0, 2, 4, 2, 0]}, "output": false}, {"input": {"nums": [0, 2, 2]}, "output": false}, {"input": {"nums": [2, 3, 3, 1, 2, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "2c59ccd1fc7f9abb"}
vcr_1420e08dfe11
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": "aecd cbea"}, "output": "cbea aecd"}, {"input": {"s": "aaba b"}, "output": "b aaba"}, {"input": {"s": "aaa abbb aaab"}, "output": "aaab abbb aaa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "69856442f40cd8ff"}
vcr_f7abef7bbaed
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": [47, 15, 46, 2, 159, 133, 181, 161, 40, 25, 81, 130, 105, 169, 50]}, "output": false}, {"input": {"nums": [23, 19, 24, 20]}, "output": false}, {"input": {"nums": [18, 15, 7, 17, 20, 22, 9, 25, 17]}, "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": "091c2fc66ce65112"}
vcr_2f99ef595cc9
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": "edeed ebe b bba de"}, "output": "de bba b ebe edeed"}, {"input": {"s": "b aa ab"}, "output": "ab aa b"}, {"input": {"s": "ab baa b bbb abab"}, "output": "abab bbb b baa ab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "c65b763bb32434df"}
vcr_da93e5c7e5bd
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": [113, 133, 81, 105, 73, 51, 181, 110, 103, 140, 157, 88, 161]}, "output": false}, {"input": {"nums": [8, 7, 19]}, "output": false}, {"input": {"nums": [3, 20, 17, 3, 3, 14, 25, 6, 17]}, "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": "6aef02eeab6b5eda"}
vcr_4c4476e4d0ef
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [18, 12, 30, 2, 15, 19, 30, 16, 30, 20, 23]}, "output": 146}, {"input": {"nums": [10, 7, 1, 14, 19, 14, 20, 0, 12, 18]}, "output": 68}, {"input": {"nums": [2, 6, 14, 3, 17, 17]}, "output": 33}]
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": "6bba3f3eee123756"}
vcr_87f507ab856d
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": [19, -14, 29, 4]}, "output": [19, 5, 34, 38]}, {"input": {"nums": [-4, 2, 8, 13]}, "output": [-4, -2, 6, 19]}, {"input": {"nums": [13, -3]}, "output": [13, 10]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "5a60968c95a0879f"}
vcr_347b7c454ebb
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": "bcbbabcbabaa"}, "output": 10}, {"input": {"s": "aacabcaac"}, "output": 7}, {"input": {"s": "bbcbbbab"}, "output": 5}]
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": "c8fb110e31127c02"}
vcr_9ebccd98a679
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [20, 1, 16, 3, 21, 3, 26, 26, 39, 19, 13, 7, 17, 14]}, "output": true}, {"input": {"nums": [18, 0, 25, 4, 16, 15]}, "output": false}, {"input": {"nums": [25, 18, 5]}, "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": "9c8e1c6aa714c67a"}
vcr_04fe3e482b36
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [6, 10, 13, 24, 32, 40, 42, 43, 44, 46], "target": 9}, "output": 1}, {"input": {"nums": [2, 9, 11, 15, 20, 23, 27, 28, 32, 33, 34, 39], "target": 19}, "output": 4}, {"input": {"nums": [5, 10, 33], "target": 0}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "681bd5e2432c8303"}