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_37942cb9b4f7
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [24, 31, 29, 30]}, "output": 7}, {"input": {"prices": [15, 7, 9, 40, 4, 39, 12]}, "output": 35}, {"input": {"prices": [20, 27, 39, 5, 3, 16, 26]}, "output": 23}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "46b20a54264d1603"}
vcr_04dbe1cd7133
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": [54, 8, 37, 6, 4, 12, 8, 25, 52]}, "output": 48}, {"input": {"prices": [6, 40, 30]}, "output": 34}, {"input": {"prices": [20, 27, 29, 4, 35, 14, 2]}, "output": 31}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "f4a943ea5101e461"}
vcr_a14c15eef40b
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, 0, 13, 25, 18, 5]}, "output": 53}, {"input": {"nums": [13, 2]}, "output": 13}, {"input": {"nums": [19, 5, 14, 10, 19, 5, 18, 3, 6, 4]}, "output": 76}]
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": "7b27f4ddcef04ea2"}
vcr_342c0893aed1
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, 6, 24, 27, 32, 35, 42, 45], "target": 41}, "output": 6}, {"input": {"nums": [1, 9, 31, 32], "target": 34}, "output": 4}, {"input": {"nums": [10, 14, 15, 24], "target": 21}, "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": "446833344dc113f4"}
vcr_eddf23f2570f
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, 0, 1, 0, 1, 4, 4, 3, 4, 0]}, "output": false}, {"input": {"nums": [3, 3, 3, 0, 2]}, "output": true}, {"input": {"nums": [1, 1, 1, 3, 0, 1, 3, 1, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "f5c6396a991d5aed"}
vcr_942bf055449e
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, 21, 46, 54, 2, 19, 32, 34, 58, 31, 51, 35, 31, 36, 27, 2]}, "output": 56}, {"input": {"prices": [8, 13, 23]}, "output": 15}, {"input": {"prices": [13, 30, 25, 7, 11]}, "output": 17}]
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": "29271f45485f728f"}
vcr_ede74693bf31
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "cb ad d dcab bbdadb ecaabb"}, "output": "ecaabb bbdadb dcab d ad cb"}, {"input": {"s": "b aba abb aa aab"}, "output": "aab aa abb aba b"}, {"input": {"s": "baaa aa"}, "output": "aa baaa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "0d9f46ddf22c1b18"}
vcr_4655d8b8753b
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": "aaabcbbbbcac"}, "output": 7}, {"input": {"s": "abbbbbb"}, "output": 2}, {"input": {"s": "caa"}, "output": 2}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "d0bec357fb25a4f5"}
vcr_5e6f346dbd97
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, 2, 31, -25, 23, -15, 32, -21, 16, 6, 15, 10, 30]}, "output": [-19, -17, 14, -11, 12, -3, 29, 8, 24, 30, 45, 55, 85]}, {"input": {"nums": [-11, 13]}, "output": [-11, 2]}, {"input": {"nums": [0, 4, 11, -3, -9, 4, 7, 2, 11, 18]}, "output": [0, 4, 15, 12, 3, 7, 14, 16, 27, 45]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "ce154ca220329674"}
vcr_efea0636e99c
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 4, 3, 4, 2, 3, 4, 0]}, "output": true}, {"input": {"nums": [3, 2, 1, 0]}, "output": true}, {"input": {"nums": [0, 1, 1, 0]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "26e5a93e6a6a84ab"}
vcr_d782fd48adfc
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": ["baba", "cc", "cc", "bb", "ccc", "aab", "bc"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "aba", "baa", "baa", "bab", "abb", "aab"]}, "output": [3, 4]}, {"input": {"strs": ["aaa", "bab", "aaa", "bba"]}, "output": [2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "20d9c573c965fea0"}
vcr_45b189134d66
medium
binary_search
Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing.
def first_bad_version(n: int, bad: int) -> int:
Lower-bound binary search on the is_bad predicate.
def first_bad_version(n: int, bad: int) -> int: def is_bad(v): return v >= bad lo, hi = 1, n while lo < hi: mid = (lo + hi) // 2 if is_bad(mid): hi = mid else: lo = mid + 1 return lo
[{"input": {"n": 38, "bad": 28}, "output": 28}, {"input": {"n": 33, "bad": 4}, "output": 4}, {"input": {"n": 7, "bad": 5}, "output": 5}]
true
python
73591920a83cf64d
### Problem Versions 1..n. All versions >= bad are bad. Find the first bad version. Parameter bad is provided for testing. ### Reasoning Lower-bound binary search on the is_bad predicate. ### Solution ```python def first_bad_version(n: int, bad: int) -> int: def is_bad(v): return v >= bad lo, hi = 1, ...
{"v": "ok", "content_hash": "4430cdea29b7f48e"}
vcr_4aef9153bd1f
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": "aaacaabbaacaaa"}, "output": true}, {"input": {"s": "cbcaaa"}, "output": false}, {"input": {"s": "baccabbbcb"}, "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": "ce67bd3395c17e76"}
vcr_6fae46cc5537
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": [13, 29, 22, 27, 26, 7, 3, 8, 29, 23]}, "output": 94}, {"input": {"nums": [11, 16, 19, 2, 17, 13, 9, 13, 10]}, "output": 66}, {"input": {"nums": [7, 16, 5, 13, 12]}, "output": 29}]
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": "b0ac72cd2f11ca70"}
vcr_deb3093f8410
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "dec be"}, "output": "be dec"}, {"input": {"s": "ab a bb"}, "output": "bb a ab"}, {"input": {"s": "aaa a aab aaaa b"}, "output": "b aaaa aab a 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": "74bfc17347dd7e89"}
vcr_b03b347d9683
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, 4, 16, 27, 30, 31, 36, 37, 47], "target": 49}, "output": 9}, {"input": {"nums": [6, 9, 10, 12, 14, 29, 30, 31, 37, 39], "target": 5}, "output": 0}, {"input": {"nums": [5, 12, 25], "target": 40}, "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": "ef73f711dd1d85f8"}
vcr_1ed78fa19aa1
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": [26, 67, 48, -8], "target": 40}, "output": [2, 3]}, {"input": {"nums": [7, 1, 17, -7, 22, 29, 43, 27, 32, 40], "target": 29}, "output": [0, 4]}, {"input": {"nums": [-27, -1, 39, 1, -10, 17, 32, 38], "target": 55}, "output": [5, 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": "857164433900cd14"}
vcr_424d8f90432d
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 1, 1, 3, 3, 3, 6, 8, 10, 13, 14]}, "output": [1, 3, 6, 8, 10, 13, 14]}, {"input": {"nums": [0, 1, 11]}, "output": [0, 1, 11]}, {"input": {"nums": [0, 2, 4, 4, 5, 6, 7, 8, 8, 10, 12]}, "output": [0, 2, 4, 5, 6, 7, 8, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "838eb40dc94084e6"}
vcr_171842c40623
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, 22, 31, 39], "target": 17}, "output": 2}, {"input": {"nums": [4, 12, 14, 15, 26, 33, 35], "target": 32}, "output": 5}, {"input": {"nums": [7, 11, 21, 38], "target": 28}, "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": "c0508722e906448e"}
vcr_efa94cbbcd66
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": [10, 17, 25, 31, 49], "target": 2}, "output": 0}, {"input": {"nums": [1, 7, 10, 19, 26, 27, 29, 30, 31], "target": 5}, "output": 1}, {"input": {"nums": [3, 8, 10, 17, 25, 28, 30, 31, 33, 35, 38, 39], "target": 7}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "8672fb56d03baa59"}
vcr_937381b59f2f
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, -5, -9, -13, -17, 3]}, "output": 3}, {"input": {"nums": [-12, 12, -1, -7, -6, 0, 12, -4, 3]}, "output": 12}, {"input": {"nums": [2, 7, 5, 14, -11, 9, -8]}, "output": 28}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "d87001935923ae4d"}
vcr_1d5cb1d2055a
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 2, 4, 0, 4, 2, 0]}, "output": true}, {"input": {"nums": [3, 3, 0, 2, 0]}, "output": true}, {"input": {"nums": [0, 0, 2, 0, 3, 1]}, "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": "9c835b6c0001a6cb"}
vcr_2020e888b136
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": "ccaabcacb"}, "output": 7}, {"input": {"s": "cbcaccacc"}, "output": 7}, {"input": {"s": "caacababb"}, "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": "e8445743e2447f79"}
vcr_0087703a0881
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": 1033}, "output": 3}, {"input": {"n": 7774}, "output": 9}, {"input": {"n": 5499}, "output": 9}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "d7419f2d2989fee1"}
vcr_a373edeb8a13
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": 163, "b": 37}, "output": 1}, {"input": {"a": 144, "b": 166}, "output": 2}, {"input": {"a": 52, "b": 4}, "output": 4}]
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": "c4126b61b02aeb17"}
vcr_ce113499b37d
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 2, 3, 4, 3, 0, 4, 0]}, "output": true}, {"input": {"nums": [1, 2, 1, 0, 1, 3]}, "output": false}, {"input": {"nums": [3, 2, 2]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "c743da16e02a8b01"}
vcr_9c17926f2df8
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 8, 14, 29], "target": 4}, "output": 1}, {"input": {"nums": [0, 3, 6, 13, 17, 19, 29, 34, 37], "target": 19}, "output": 5}, {"input": {"nums": [0, 8, 9, 10, 12, 14, 17, 19, 26, 30, 36, 39], "target": 26}, "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": "80f85ecfd29e4b14"}
vcr_f0234e435e95
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-11, -15, 9, -11, -13, 22]}, "output": 22}, {"input": {"nums": [-9, 12, 6, 6, -3, -12, 8, -7, -3, 5, 0]}, "output": 24}, {"input": {"nums": [-12, 10, -9, 8, 8, 9]}, "output": 26}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "bb4872b9971d24a8"}
vcr_873ba3f4eb44
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 5, 8, 9, 12, 12, 13, 13, 14]}, "output": [1, 5, 8, 9, 12, 13, 14]}, {"input": {"nums": [0, 2, 4, 9, 10, 10, 11, 12]}, "output": [0, 2, 4, 9, 10, 11, 12]}, {"input": {"nums": [1, 9, 9, 10, 12, 12, 12]}, "output": [1, 9, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "2f00444171fc9902"}
vcr_8e6920af2bb7
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": [8, 30, 46, 10, 40, 18, 20, 53, 20, 13, 16, 35]}, "output": 45}, {"input": {"prices": [19, 35, 16, 12]}, "output": 16}, {"input": {"prices": [31, 22, 37, 4, 29, 38]}, "output": 34}]
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": "c6bf4ae9b9479dbe"}
vcr_2f45c69946a7
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": 42, "b": 128}, "output": 2}, {"input": {"a": 147, "b": 11}, "output": 1}, {"input": {"a": 95, "b": 93}, "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": "ea6f80469de699e0"}
vcr_12df006f23d6
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": [12, 29, 29, 12, 36, 3, 36, 3, 17, 78, 17]}, "output": 78}, {"input": {"nums": [19, 1, 6, 20, 1, 19, 20, 26, 49, 13, 6, 26, 13]}, "output": 49}, {"input": {"nums": [2, 23, 2, 46, 23]}, "output": 46}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "68da3959b3f431a6"}
vcr_89837f32feea
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": "becac e ec deaab de cabe"}, "output": "cabe de deaab ec e becac"}, {"input": {"s": "abab aaa baab bbab"}, "output": "bbab baab aaa abab"}, {"input": {"s": "aab aab a bba aaaa"}, "output": "aaaa bba a aab aab"}]
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": "11fe6d815caf2e14"}
vcr_e5a7b1d38b8e
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-25, 31, 30, 32, 14, 19, 28, 29, 15, 14, 15, 30, -18, -21]}, "output": [-25, 6, 36, 68, 82, 101, 129, 158, 173, 187, 202, 232, 214, 193]}, {"input": {"nums": [0, 5]}, "output": [0, 5]}, {"input": {"nums": [0, 13, 16, -5, -1, 16, -4, 4]}, "output": [0, 13, 29, 24, 23, 39, 35, 39]}]
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": "839973e0534e8592"}
vcr_4d788b30e1a6
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [34, 7, 9, 9, 25, 33, 33, 7, 13, 34, 68, 39, 25, 13, 39, 11, 11]}, "output": 68}, {"input": {"nums": [16, 16, 26, 26, 9, 9, 14, 45, 14]}, "output": 45}, {"input": {"nums": [4, 4, 59, 7, 7]}, "output": 59}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "75823ab2177371fe"}
vcr_2ddebe79055d
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": [3, 0, 23, 27, 15]}, "output": 41}, {"input": {"nums": [7, 11, 11, 15, 4, 15, 20, 20]}, "output": 61}, {"input": {"nums": [10, 13, 17, 16, 20, 17, 16, 13, 14]}, "output": 77}]
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": "20d79b31bd207666"}
vcr_5c8c3f4f84f2
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": ["aca", "cb", "cccb", "aaac", "ccb", "abbc", "bbac", "cacc", "cbc"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["bba", "aab", "baa", "bba", "abb", "aaa"]}, "output": [1, 2, 3]}, {"input": {"strs": ["bab", "aba", "abb", "aaa"]}, "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": "707f9c2e644d5745"}
vcr_83f35288444c
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": "aaacacbdcdacdaa"}, "output": false}, {"input": {"s": "ca"}, "output": false}, {"input": {"s": "aabac"}, "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": "0924849e24703c41"}
vcr_15392eb7bba8
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": [7, 7, 7, 9, 12, 13, 14]}, "output": [7, 9, 12, 13, 14]}, {"input": {"nums": [2, 5, 8, 10, 11]}, "output": [2, 5, 8, 10, 11]}, {"input": {"nums": [2, 3, 4, 5, 6, 7, 7, 7, 8]}, "output": [2, 3, 4, 5, 6, 7, 8]}]
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": "e953334c42db95b2"}
vcr_5b057b88169a
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 4, 0, 3, 4, 2, 1, 0, 2, 0]}, "output": true}, {"input": {"nums": [2, 1, 2, 2]}, "output": true}, {"input": {"nums": [1, 0, 3]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "a0553dfd208db729"}
vcr_b22aa8497b21
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": 55291}, "output": 13}, {"input": {"n": 14212}, "output": 7}, {"input": {"n": 4950}, "output": 7}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "49d2157d13ade40a"}
vcr_477734ba4b71
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 1, 2, 1, 0, 1, 0, 0]}, "output": false}, {"input": {"nums": [2, 0, 2, 0, 0, 0, 3, 2, 1]}, "output": false}, {"input": {"nums": [0, 0, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "1ee71b69eb718eab"}
vcr_eb199518c9e4
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [46, 42, 27, 16, 3, 6, 46, 40, 21, 10]}, "output": 43}, {"input": {"prices": [15, 39, 28, 35, 32, 24, 33, 12]}, "output": 24}, {"input": {"prices": [17, 29, 30, 13]}, "output": 13}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "75344c87e8ad4b8c"}
vcr_571dd5328023
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, -10, 4, 18, 1, -10, 3], "k": 3}, "output": [1, -10, 3, -20, -10, 4, 18]}, {"input": {"nums": [10, 5, -4, -7, 9, -7, 3, 1, 5, 9], "k": 11}, "output": [9, 10, 5, -4, -7, 9, -7, 3, 1, 5]}, {"input": {"nums": [-9, 4, -8, 6, 9, -3, -7, 5, 5], "k": 4}, "output": [-3, -7, 5, 5, -9, 4, -8, 6, 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": "396c924ac50ef102"}
vcr_709bb3d02352
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": ["bcbc", "aaa", "cacc", "accb", "bb", "acb"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "bbb", "baa", "aba"]}, "output": [1, 3]}, {"input": {"strs": ["aab", "abb", "abb", "aaa"]}, "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": "9195a2219bd549bd"}
vcr_dcc33a44ceb4
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": 52856}, "output": 9}, {"input": {"n": 7342}, "output": 8}, {"input": {"n": 11594}, "output": 7}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "ffd618b53da84ec0"}
vcr_0054399d4182
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": [6, 21, 55, 2, 21, 24, 24, 2, 6]}, "output": 55}, {"input": {"nums": [43, 15, 21, 15, 28, 28, 19, 21, 4, 19, 4, 17, 17]}, "output": 43}, {"input": {"nums": [22, 41, 14, 21, 8, 21, 8, 14, 22]}, "output": 41}]
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": "117a24febddca8e5"}
vcr_e8bb2b5f1ea3
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 1, 3, 4, 5, 6, 7, 9, 10, 14, 14]}, "output": [1, 3, 4, 5, 6, 7, 9, 10, 14]}, {"input": {"nums": [4, 5, 6, 7, 12]}, "output": [4, 5, 6, 7, 12]}, {"input": {"nums": [5, 7, 10, 10, 12]}, "output": [5, 7, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "dd93e17e8c99fd8d"}
vcr_5a5ccb1ed2d5
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [12, 15, 11, 12, 13, 16, 24, 28, 26, 19, 19]}, "output": 105}, {"input": {"nums": [5, 20, 14]}, "output": 20}, {"input": {"nums": [12, 18, 0]}, "output": 18}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "a2540d0f360b2104"}
vcr_168f0be3460c
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": 206, "b": 127}, "output": 1}, {"input": {"a": 150, "b": 18}, "output": 6}, {"input": {"a": 158, "b": 144}, "output": 2}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "a68ba18062ae7dcf"}
vcr_4c2f58eb77ef
medium
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "cabbddaddbbac"}, "output": true}, {"input": {"s": "cba"}, "output": false}, {"input": {"s": "bccbbaaa"}, "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": "90d190a2415f5077"}
vcr_7174c402a7f8
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 3, 5, 6, 13, 14, 22, 26, 30, 33, 34, 41, 48, 49], "target": 9}, "output": 4}, {"input": {"nums": [10, 15, 21, 29], "target": 18}, "output": 2}, {"input": {"nums": [0, 2, 5, 6, 10, 14, 24, 25, 29, 33, 37], "target": 32}, "output": 9}]
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": "60d3bc13802a3d5e"}
vcr_1723d66b45d8
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [2, -11, 17, -3, -4, 17, 13, 19, -19, 10], "k": 14}, "output": [13, 19, -19, 10, 2, -11, 17, -3, -4, 17]}, {"input": {"nums": [1, -10, 10, -4, 7], "k": 7}, "output": [-4, 7, 1, -10, 10]}, {"input": {"nums": [-1, 4, -8, -7, 10, 3, 7, -8, 2], "k": 18}, "output": [-1, 4, -8, -7, 10, 3, 7, -8, 2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "ad363e038a04aa52"}
vcr_1fb5cc837fa7
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": [27, 24, 27, 24, 50, 17, 17, 39, 39]}, "output": 50}, {"input": {"nums": [24, 59, 28, 11, 28, 11, 20, 20, 24]}, "output": 59}, {"input": {"nums": [9, 53, 25, 10, 8, 9, 25, 8, 10]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "727a92f7f769e628"}
vcr_8f25a0553e35
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": [13, 18, 4, 1, 29, 29, 32, 33, 45, 48, 29, 38, 9, 17, 37]}, "output": true}, {"input": {"nums": [25, 15, 22, 18, 12, 9, 2, 16]}, "output": false}, {"input": {"nums": [22, 11, 24, 13, 22, 22]}, "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": "3a6632fcb9df2940"}
vcr_1c98d01b0af3
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 2, 3, 3, 3, 4, 7, 7, 11, 11]}, "output": [0, 2, 3, 4, 7, 11]}, {"input": {"nums": [0, 3, 5, 6, 7, 7, 8, 9, 10]}, "output": [0, 3, 5, 6, 7, 8, 9, 10]}, {"input": {"nums": [5, 11, 12]}, "output": [5, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "bc8f36df42a479a8"}
vcr_fbbd57978516
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [35, 13, 31, 29, 27, 22, 20, 35]}, "output": true}, {"input": {"nums": [9, 21, 8, 16, 24, 8, 21, 0, 24, 13, 6, 19]}, "output": true}, {"input": {"nums": [20, 4, 22]}, "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": "a005d87be62eb2aa"}
vcr_c386aaf9c813
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": [3, 36, 9, 21, 36, 9, 3, 73, 21]}, "output": 73}, {"input": {"nums": [22, 19, 22, 6, 19, 23, 23, 6, 46]}, "output": 46}, {"input": {"nums": [59, 19, 19, 2, 2]}, "output": 59}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "cdd743370d75165f"}
vcr_60759fa40f66
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": 170, "b": 4}, "output": 2}, {"input": {"a": 116, "b": 116}, "output": 116}, {"input": {"a": 75, "b": 179}, "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": "9b67aa1057285f7f"}
vcr_43e5e41cdea1
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [8, -16, -1, -1, 16, -5, 10, -15, 5, 14], "k": 0}, "output": [8, -16, -1, -1, 16, -5, 10, -15, 5, 14]}, {"input": {"nums": [9, 7, 3, 2, 5, -5], "k": 4}, "output": [3, 2, 5, -5, 9, 7]}, {"input": {"nums": [5, -3, 0, 1, 8, 7, 5], "k": 20}, "output": [-3, 0, 1, 8, 7, 5, 5]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "2d7228797eb1daec"}
vcr_fb2139993ff6
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [37, 28, 48, 23, 38, 35, 48, 58]}, "output": 35}, {"input": {"prices": [5, 17, 20, 40, 2, 33, 14, 26, 33, 28]}, "output": 35}, {"input": {"prices": [7, 1, 8, 18, 7, 34, 11, 6, 33]}, "output": 33}]
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": "df554391ca269d10"}
vcr_9180054b50bb
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": 44, "b": 239}, "output": 1}, {"input": {"a": 144, "b": 154}, "output": 2}, {"input": {"a": 25, "b": 70}, "output": 5}]
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": "b17e7429e8c671a4"}
vcr_a87bf4ceba93
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": [4, 22, 6, 5, 27]}, "output": 49}, {"input": {"nums": [3, 6, 17, 6, 10, 3, 11]}, "output": 41}, {"input": {"nums": [20, 16, 12, 2, 0, 3, 9, 16, 6]}, "output": 51}]
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": "0deb2369c1eeb06e"}
vcr_fed66ba6194d
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, -15, -7, -1, 9, 12, 14, 15, 33, 43, 45, 46], "target": 20}, "output": [0, 11]}, {"input": {"nums": [-18, -12, 2, 4, 5, 11, 20, 24, 33], "target": 44}, "output": [5, 8]}, {"input": {"nums": [-13, -8, -7, 18, 19, 27], "target": 45}, "output": [3, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "d9c754d652dbc15d"}
vcr_7ebe0edb7cc0
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [5, 6, -5, 20, 6, -12, 1, 6, 18, 12, -14], "k": 11}, "output": [5, 6, -5, 20, 6, -12, 1, 6, 18, 12, -14]}, {"input": {"nums": [-9, -9, -3, 4, 9, 9, 5, 5, -7], "k": 19}, "output": [-7, -9, -9, -3, 4, 9, 9, 5, 5]}, {"input": {"nums": [-7, -7, -4, -6, 7, 8, -10, 1, -3, -7], "k": 9}, "output": [-7, -4, ...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "f5c2cfeec0e68681"}
vcr_3bad258a48a6
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, 0, 3, 3, 0, 3, 1, 4, 0]}, "output": false}, {"input": {"nums": [3, 3, 3, 1, 2, 3, 0]}, "output": true}, {"input": {"nums": [0, 3, 2, 0, 2, 1, 1]}, "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": "c4fc1f2fe36e9081"}
vcr_1c1fd5dfb2e4
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": 9765}, "output": 6}, {"input": {"n": 10903}, "output": 8}, {"input": {"n": 16006}, "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": "e8584fa66125da50"}
vcr_02f603384009
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, -9, -6, 4, 24], "target": -2}, "output": [2, 3]}, {"input": {"nums": [-18, 8, 9, 11, 18, 25, 26], "target": 36}, "output": [3, 5]}, {"input": {"nums": [-14, -9, -1, 9, 22, 30, 38], "target": 8}, "output": [0, 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": "1c33e608c6412dfb"}
vcr_f2d4a0c5be04
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, 4, 7, 3, 7]}, "output": [588, 0, 0, 0, 0]}, {"input": {"nums": [1, 5, 2, 5]}, "output": [50, 10, 25, 10]}, {"input": {"nums": [1, 5, 1, 2, 2, 3, 4]}, "output": [240, 48, 240, 120, 120, 80, 60]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "a34aa703e53a8232"}
vcr_11be97bb35e8
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": [120, 111, 150, 175, 8, 85, 75, 166, 174, 171, 110, 112, 126, 50, 195, 106]}, "output": false}, {"input": {"nums": [14, 18, 20, 11, 4, 23, 8, 0, 11, 20, 18]}, "output": true}, {"input": {"nums": [21, 10, 17, 2, 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": "da1b467311ec9de5"}
vcr_15ad1cb959db
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": 153, "b": 96}, "output": 3}, {"input": {"a": 175, "b": 161}, "output": 7}, {"input": {"a": 106, "b": 41}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "7d3fc25b9b23b8ae"}
vcr_7cd1892cf258
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, 6, 3, 2, 12, 12, 28, 0]}, "output": 64}, {"input": {"nums": [15, 7, 2, 2, 2, 14, 6, 4, 5, 17]}, "output": 52}, {"input": {"nums": [4, 1, 16]}, "output": 20}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "cd83e1749deeb504"}
vcr_be2b680956cd
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [18, 13, -1, 17, 9, -1], "k": 15}, "output": [17, 9, -1, 18, 13, -1]}, {"input": {"nums": [7, 2, -1, -3, -4, 0, -6], "k": 19}, "output": [-1, -3, -4, 0, -6, 7, 2]}, {"input": {"nums": [2, -5, 10, 3, -3, -9, -8], "k": 20}, "output": [-5, 10, 3, -3, -9, -8, 2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "2683607e3bc5167f"}
vcr_21d5a64ff9e5
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": ["cbc", "ca", "bb", "bba"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["baa", "baa", "baa", "bbb", "aab"]}, "output": [1, 4]}, {"input": {"strs": ["bba", "aab", "bbb", "abb", "bab", "aaa"]}, "output": [1, 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": "e25ac97f6a2e9741"}
vcr_20576b2b6091
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, -12, -5, 13, 15, 32], "target": 27}, "output": [2, 5]}, {"input": {"nums": [-17, 1, 3, 10, 18, 19, 27, 28, 34, 35, 36], "target": 45}, "output": [3, 9]}, {"input": {"nums": [-16, -11, 2, 7, 13, 29, 32, 34], "target": 23}, "output": [1, 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": "2a50ba0bc527c0b6"}
vcr_640994b7dab2
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [12, -12, 26, -8, 13, 31, 12, 29, 22, -10, -5]}, "output": [12, 0, 26, 18, 31, 62, 74, 103, 125, 115, 110]}, {"input": {"nums": [11, -6, -6]}, "output": [11, 5, -1]}, {"input": {"nums": [6, 7, -1, -4, -4, 1, -12, 4]}, "output": [6, 13, 12, 8, 4, 5, -7, -3]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "bbb6b5a0d37d87cb"}
vcr_508972603114
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, 3, 6, 7, 8, 9, 11, 12, 13, 14, 15]}, "output": [2, 3, 6, 7, 8, 9, 11, 12, 13, 14, 15]}, {"input": {"nums": [1, 1, 3, 4, 4, 5, 7, 8, 11]}, "output": [1, 3, 4, 5, 7, 8, 11]}, {"input": {"nums": [4, 5, 8]}, "output": [4, 5, 8]}]
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": "773caeab1ba2a436"}
vcr_68500f75d76a
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [25, 23, 10]}, "output": 35}, {"input": {"nums": [5, 16, 4, 5, 2, 6, 12]}, "output": 33}, {"input": {"nums": [10, 0]}, "output": 10}]
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": "e3fbc41a1cc59875"}
vcr_8e1a038b4f20
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": [156, 1, 184, 33, 100, 188, 106, 53]}, "output": false}, {"input": {"nums": [2, 10, 0, 5, 13, 1, 7, 9]}, "output": false}, {"input": {"nums": [4, 25, 19, 19, 17, 1, 5]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "ff6bde9a209fc567"}
vcr_772178b9e992
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": [38, 44, 44, 56, 14, 10, 15, 18, 31, 53, 52]}, "output": 43}, {"input": {"prices": [27, 18, 28]}, "output": 10}, {"input": {"prices": [17, 24, 35]}, "output": 18}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "9d542834b0ef3ed5"}
vcr_27f8026f24eb
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": [-20, -19, -9, 6, 13, 14, 25, 26, 33, 34, 35], "target": -3}, "output": [2, 3]}, {"input": {"nums": [-10, -9, 23, 33], "target": 56}, "output": [2, 3]}, {"input": {"nums": [-16, -7, 9, 38, 39], "target": 2}, "output": [1, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "63c6801afc0c9b82"}
vcr_a2710446e46d
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [8, 18, 64, 30, 18, 8, 30]}, "output": 64}, {"input": {"nums": [29, 2, 29, 3, 20, 20, 21, 21, 2, 11, 45, 11, 3]}, "output": 45}, {"input": {"nums": [49, 18, 7, 18, 7]}, "output": 49}]
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": "6279631ea212f04a"}
vcr_44be69d96696
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 ecbabb ebcddd bedc"}, "output": "bedc ebcddd ecbabb c"}, {"input": {"s": "bb a ba aaa"}, "output": "aaa ba a bb"}, {"input": {"s": "abb baaa"}, "output": "baaa abb"}]
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": "b69a93a82e755d07"}
vcr_37864e8bd6cc
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": "dbcaca ed dbb"}, "output": "dbb ed dbcaca"}, {"input": {"s": "a bbb bbab abb bbb"}, "output": "bbb abb bbab bbb a"}, {"input": {"s": "bb baab a"}, "output": "a baab 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": "17cc2d4e2b642701"}
vcr_6dfe98cadbd6
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, 5, 5, 5, 6, 6, 7, 10, 10, 12, 14, 14]}, "output": [0, 2, 5, 6, 7, 10, 12, 14]}, {"input": {"nums": [7, 7, 8, 8, 8, 9]}, "output": [7, 8, 9]}, {"input": {"nums": [0, 1, 5, 5, 6, 6, 6, 7, 7, 10, 12]}, "output": [0, 1, 5, 6, 7, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "69426e7cb9ba6b97"}
vcr_ceacc93de8ff
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": "babbbbbccbbcb"}, "output": 7}, {"input": {"s": "abaaa"}, "output": 3}, {"input": {"s": "aaaaa"}, "output": 1}]
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": "a7e74e5cb1790959"}
vcr_e6e24729ab12
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 7, 10, 13, 19, 20, 24, 28, 34, 37, 38], "target": 21}, "output": 6}, {"input": {"nums": [0, 16, 35], "target": 37}, "output": 3}, {"input": {"nums": [2, 8, 11, 38], "target": 13}, "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": "5e998b1a18e42ee9"}
vcr_fd95061351d5
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": [14, 15, 21, 22, 25, 28, 35, 41], "target": 37}, "output": [1, 3]}, {"input": {"nums": [-20, -10, 0, 27, 31], "target": 58}, "output": [3, 4]}, {"input": {"nums": [-20, -14, -10, -7, 10, 16, 19, 28, 29, 31], "target": 24}, "output": [3, 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": "1d4d0347fa112be3"}
vcr_a473dc799af2
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, 21, 2, 21, 13, 1, 12, 22, 26, 16]}, "output": 84}, {"input": {"nums": [2, 19, 16, 5, 13, 0, 5, 20, 4]}, "output": 52}, {"input": {"nums": [1, 7, 5, 4, 1]}, "output": 11}]
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": "3f2d49d5121bb8b5"}
vcr_3b04e393807b
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": "aad eedbe ec edba"}, "output": "edba ec eedbe aad"}, {"input": {"s": "abab aa a abba"}, "output": "abba a aa abab"}, {"input": {"s": "b a a"}, "output": "a a b"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "a0a808186702369c"}
vcr_15ceb53dcb29
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 5, 2, 5, 2, 0, 2]}, "output": [0, 0, 0, 0, 0, 600, 0]}, {"input": {"nums": [5, 5, 4, 5, 5, 1]}, "output": [500, 500, 625, 500, 500, 2500]}, {"input": {"nums": [3, 5, 4, 2]}, "output": [40, 24, 30, 60]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "0468ce13cc638e59"}
vcr_6dc95a28f441
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [16, 15, 2, 12, 16, 5, -12, -16, 12, -5, 11, 6, -15, 5, 4]}, "output": 66}, {"input": {"nums": [7, -6, -2, 9, 4, 0, 2]}, "output": 15}, {"input": {"nums": [3, 2, 8, 2, 9, 4, 8, -10, 4, 11]}, "output": 41}]
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": "756ae1940f9352e9"}
vcr_fa8e9b1bbab0
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [18, -5, -4, -20], "k": 11}, "output": [-5, -4, -20, 18]}, {"input": {"nums": [-4, 6, -7, -2], "k": 8}, "output": [-4, 6, -7, -2]}, {"input": {"nums": [3, -1, 9, 0, 0, 3], "k": 0}, "output": [3, -1, 9, 0, 0, 3]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "d06adcf56083897b"}
vcr_2e157621cbba
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": [171, 72, 16, 35, 85, 122, 114, 41, 177, 47, 129, 56]}, "output": false}, {"input": {"nums": [7, 12, 23, 14, 23, 20, 19, 6, 15, 19, 2, 6]}, "output": true}, {"input": {"nums": [11, 20, 14, 20, 21, 9, 7, 10, 6, 1]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "1894e50640fea433"}
vcr_d9da403d9bc5
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-17, 1, -3, -14, -15, 22]}, "output": 22}, {"input": {"nums": [-11, 7, 8, 8, -11, 6, 8, 5]}, "output": 31}, {"input": {"nums": [1, -12, 3, -10, -12, 5, 4, -11, 0, 4, -12]}, "output": 9}]
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": "c3d30a5338977948"}
vcr_db79bd65651a
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": "aaacbbac"}, "output": 5}, {"input": {"s": "acbbbbaacbba"}, "output": 7}, {"input": {"s": "accaac"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "c25c111302298cbe"}
vcr_19482f9ba032
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": [44, 37, 34, 49, 24, 8, 43, 0, 18, 44]}, "output": true}, {"input": {"nums": [25, 6, 16, 5, 15, 16, 13]}, "output": true}, {"input": {"nums": [4, 3, 8, 1]}, "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": "cad1e89da4824942"}
vcr_a51250076903
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": "aaababbacaa"}, "output": 7}, {"input": {"s": "cacbabac"}, "output": 8}, {"input": {"s": "cabbaaacabca"}, "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": "e28bfd2566fab974"}
vcr_cf37f337f4f1
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 12, 13, 22, 31, 39, 41, 45], "target": 45}, "output": 7}, {"input": {"nums": [5, 14, 15, 19, 24, 27, 28, 33, 35, 38], "target": 36}, "output": 9}, {"input": {"nums": [1, 3, 4, 6, 10, 12, 19, 30, 33, 38], "target": 19}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "fd0f503efbe03473"}
vcr_c7cad2783750
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": [14, 13, 39, 12, 14]}, "output": true}, {"input": {"nums": [3, 21, 22, 18, 2, 20, 4]}, "output": false}, {"input": {"nums": [16, 16, 15]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "5ebe487864511424"}