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_6204657dbe62
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": "ddabbadaadabbadd"}, "output": true}, {"input": {"s": "acaab"}, "output": false}, {"input": {"s": "baaaaabc"}, "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": "0ad7ecd41c3fdcc1"}
vcr_4a70fbcf99e9
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, 3, 16, 17, -1, 12, -3], "k": 16}, "output": [12, -3, -5, 3, 16, 17, -1]}, {"input": {"nums": [1, 5, 4, 5, -3, 4, 1], "k": 17}, "output": [-3, 4, 1, 1, 5, 4, 5]}, {"input": {"nums": [-9, 4, 10], "k": 10}, "output": [10, -9, 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": "fc36bee61b65d3c0"}
vcr_86a80f70702f
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": [2, 44, 33, 48, 41, 44]}, "output": 46}, {"input": {"prices": [19, 27, 17, 5, 29, 20, 18, 15]}, "output": 24}, {"input": {"prices": [22, 34, 9, 3]}, "output": 12}]
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": "7b38a404d85a11a0"}
vcr_1a06942fe9e4
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -14, -6, 3, 17, 23, 27, 28, 31, 47], "target": -1}, "output": [0, 7]}, {"input": {"nums": [-17, -8, -1, 7, 10, 16, 22, 32, 39], "target": 2}, "output": [1, 4]}, {"input": {"nums": [-12, -9, 6, 7, 9, 17, 26], "target": 16}, "output": [3, 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": "cf99f11d22ec0760"}
vcr_3634c25da0e1
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [14, -13, 0, -14, -15, -11]}, "output": 14}, {"input": {"nums": [-4, -4, -9, -10, -7, 11, -3, 5, 14]}, "output": 27}, {"input": {"nums": [5, -9, -10, -4]}, "output": 5}]
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": "2adbe8ffbc179e73"}
vcr_9a55b9cc197f
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, 5, 8, 9, 10, 10, 10, 13, 15, 15]}, "output": [2, 3, 5, 8, 9, 10, 13, 15]}, {"input": {"nums": [1, 5, 9, 12]}, "output": [1, 5, 9, 12]}, {"input": {"nums": [0, 1, 1, 1, 2, 2, 3, 3, 8]}, "output": [0, 1, 2, 3, 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": "d871a6e1656e4cf9"}
vcr_4770979b93d0
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": [28, 38, 45, 48], "target": 49}, "output": 4}, {"input": {"nums": [5, 24, 25, 27, 35, 36], "target": 2}, "output": 0}, {"input": {"nums": [0, 1, 5, 13, 21, 23, 24, 37], "target": 39}, "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": "1983e89598dc408b"}
vcr_8ac9baf57183
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-10, 2, 1, 10, 19, -7, -7, 5, 8, -9, 15, -5]}, "output": 37}, {"input": {"nums": [5, 9, 13, -2, -6, -9]}, "output": 27}, {"input": {"nums": [7, 10, 13, -11, -10, 4, 7, -7, -7]}, "output": 30}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "48012bdd3ecb53d4"}
vcr_adb1f3d4906d
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, 26, 44, 46], "target": 5}, "output": 1}, {"input": {"nums": [14, 17, 29], "target": 29}, "output": 2}, {"input": {"nums": [12, 15, 16, 17, 20, 21, 27, 28, 29, 32, 34, 35], "target": 27}, "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": "698212e4f88da026"}
vcr_51e97bf46074
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": [25, 6, -1, 47, 65, 9, 12, 18, 20, -40, 11, 4, 0, -19], "target": 85}, "output": [4, 8]}, {"input": {"nums": [-26, 2, 38, -29, 12, 40, -20, 41, -25], "target": 50}, "output": [2, 4]}, {"input": {"nums": [1, 47, -23, -13, -3, 26], "target": 23}, "output": [4, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "36b6cd7daaeb6dad"}
vcr_411fd0db0745
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": [5, 20, -14, -2, 16, -22, 13, 15, 3, 4, -6, -21]}, "output": [5, 25, 11, 9, 25, 3, 16, 31, 34, 38, 32, 11]}, {"input": {"nums": [13, 13, 18]}, "output": [13, 26, 44]}, {"input": {"nums": [-4, 11, 6, 15, 5, 17]}, "output": [-4, 7, 13, 28, 33, 50]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "e1cce2b4d736f3d2"}
vcr_56b5074760da
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, -5, 18, 31, 33, 34, 35, 47], "target": -9}, "output": [0, 2]}, {"input": {"nums": [3, 4, 11, 13, 25], "target": 38}, "output": [3, 4]}, {"input": {"nums": [-20, -18, -9, -7, -6, -3, 11, 12, 24, 33], "target": 5}, "output": [3, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "30087b25e932c929"}
vcr_338660bf6f26
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, 16, 19, 21, 27, 31, 33, 37, 43], "target": 5}, "output": 1}, {"input": {"nums": [9, 16, 18, 19, 23, 26, 33, 37], "target": 26}, "output": 5}, {"input": {"nums": [0, 3, 13, 17], "target": 38}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "6ade17c1d29a4693"}
vcr_95a22f2df23e
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [10, 15, 28, 6, 3, 29, 24, 19, 4, 20, 27, 6]}, "output": 113}, {"input": {"nums": [5, 13, 6, 15, 6, 6]}, "output": 34}, {"input": {"nums": [16, 16, 4, 1, 20, 4, 12]}, "output": 52}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "30b686cb23bf3e9f"}
vcr_74e7d20bff59
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, 14, 41, 44, 48, 49], "target": 10}, "output": 0}, {"input": {"nums": [4, 9, 13, 20, 22, 24, 28, 29], "target": 12}, "output": 2}, {"input": {"nums": [2, 11, 13, 19, 23, 29, 34, 36, 37, 39], "target": 34}, "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": "f70d728347cddcd2"}
vcr_167aa1c05bc7
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-9, -1, 0, 15, 8, 18]}, "output": 41}, {"input": {"nums": [1, -11, -9, -1]}, "output": 1}, {"input": {"nums": [8, 1, -12, -12, 11, 9, -11, -1]}, "output": 20}]
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": "9fbbad8aefc71619"}
vcr_e98fd2b02b53
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": [4, 6, 2, 4, 3, 1, 6]}, "output": [864, 576, 1728, 864, 1152, 3456, 576]}, {"input": {"nums": [5, 5, 2, 2, 2, 3, 2]}, "output": [240, 240, 600, 600, 600, 400, 600]}, {"input": {"nums": [1, 4, 5, 3, 5]}, "output": [300, 75, 60, 100, 60]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "1139a3fa4b6dad7e"}
vcr_3626026990c5
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-27, -24, -16, -11, -5, 12, 18, 19, 27, 43], "target": -51}, "output": [0, 1]}, {"input": {"nums": [-16, -11, -10, -5, -3, 16, 17, 23, 26, 27, 34], "target": -14}, "output": [1, 4]}, {"input": {"nums": [-20, -13, -11, -1, 4, 6, 24, 28, 31, 32, 39], "target": 4}, "output": [0, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "00e998971af969a4"}
vcr_02dc2646a35c
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": "dccade bd ae deeea abc baccac"}, "output": "baccac abc deeea ae bd dccade"}, {"input": {"s": "abab bb a b"}, "output": "b a bb abab"}, {"input": {"s": "aa ba a"}, "output": "a ba aa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "4c8b77a6ac7f0fb8"}
vcr_1c754c50b49c
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, 2, 3, 4, 5, 8, 9, 10, 14]}, "output": [1, 2, 3, 4, 5, 8, 9, 10, 14]}, {"input": {"nums": [1, 1, 4, 6, 7, 7, 8, 8, 9, 11, 11, 11]}, "output": [1, 4, 6, 7, 8, 9, 11]}, {"input": {"nums": [0, 3, 4, 4, 5, 5, 7, 9, 11]}, "output": [0, 3, 4, 5, 7, 9, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "a8483435bd6fc3c8"}
vcr_1fab097c85ad
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": [2, 79, 12, 25, 16, 29, 25, 1, 1, 32, 32, 16, 29, 12, 2]}, "output": 79}, {"input": {"nums": [17, 46, 4, 7, 17, 4, 7]}, "output": 46}, {"input": {"nums": [4, 4, 51, 2, 2]}, "output": 51}]
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": "3dad95d89d73af58"}
vcr_892d942304a6
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 4, 4, 5, 6, 6, 8, 8, 9, 10, 11, 14]}, "output": [0, 1, 4, 5, 6, 8, 9, 10, 11, 14]}, {"input": {"nums": [1, 1, 3, 4, 5, 6, 7, 8, 8, 11, 12, 12]}, "output": [1, 3, 4, 5, 6, 7, 8, 11, 12]}, {"input": {"nums": [3, 4, 6, 6, 7, 8, 10, 10, 11, 12, 12]}, "output": [3, 4, 6, 7, 8, 10, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "5bc9659ff01b4ea5"}
vcr_9723ca1b9b08
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 1, 1, 1, 2, 7, 7, 8, 12, 13, 13, 14, 15]}, "output": [0, 1, 2, 7, 8, 12, 13, 14, 15]}, {"input": {"nums": [0, 4, 4, 5, 7]}, "output": [0, 4, 5, 7]}, {"input": {"nums": [0, 0, 0, 1, 1, 2, 9]}, "output": [0, 1, 2, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "233a526f5e66cfbf"}
vcr_b1e2f474a3d0
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 3, 2, 4, 4, 1, 0, 0]}, "output": true}, {"input": {"nums": [1, 2, 1, 1, 2, 0, 3]}, "output": true}, {"input": {"nums": [2, 0, 2, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "82c48a3e0cfd9815"}
vcr_4c7424ec2253
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, 53, 17, 5, 24, 20]}, "output": 29}, {"input": {"prices": [37, 26, 24, 33, 3, 16, 28, 24]}, "output": 25}, {"input": {"prices": [21, 6, 26, 30, 33]}, "output": 27}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "6c031a4acaed96c8"}
vcr_0d2a9a3a8057
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 2, 13, 20, 21, 22, 24, 31, 32, 36, 39, 40, 49], "target": 27}, "output": 7}, {"input": {"nums": [12, 15, 17, 21, 22, 23, 29, 31, 34, 37, 39], "target": 3}, "output": 0}, {"input": {"nums": [1, 11, 19, 26, 27], "target": 34}, "output": 5}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "298efb8d65c55e21"}
vcr_6cd5da3c78cc
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": "bacaaabacabba"}, "output": 10}, {"input": {"s": "cbbccbcbac"}, "output": 8}, {"input": {"s": "bcacab"}, "output": 6}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "b066be884bcdcc50"}
vcr_46d681088025
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, -1, 2, 3, 7, 18, 22, 33, 44, 48], "target": 46}, "output": [3, 9]}, {"input": {"nums": [-18, -12, 5, 33, 39], "target": -30}, "output": [0, 1]}, {"input": {"nums": [-20, -15, 4, 17, 20, 23, 29, 31], "target": 60}, "output": [6, 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": "2fafa20820027998"}
vcr_18ea72dd1c23
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, 55, 1, 38, 14, 47, 40, 37, 43, 43, 38, 57]}, "output": 56}, {"input": {"prices": [31, 29, 7, 10, 9, 2, 33, 13, 36, 20, 4, 24]}, "output": 34}, {"input": {"prices": [29, 19, 27, 7, 19, 24, 36, 15, 36]}, "output": 29}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "998a21912e545f24"}
vcr_7da924c55f87
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": "bbabbbbc"}, "output": 4}, {"input": {"s": "bbcababca"}, "output": 8}, {"input": {"s": "babbbccaaa"}, "output": 5}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "56a39502953e2224"}
vcr_bc9b5be79986
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, -14, -13, -10, 10, 12, 20, 35], "target": -24}, "output": [1, 3]}, {"input": {"nums": [-15, -12, -5, -2, 3, 9, 11, 28, 29], "target": 32}, "output": [4, 8]}, {"input": {"nums": [-19, -7, 9, 11, 23, 28], "target": 51}, "output": [4, 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": "10c9dcfe43511aa0"}
vcr_521aabbdff12
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": "ddaddaddcbdbd"}, "output": false}, {"input": {"s": "aaac"}, "output": false}, {"input": {"s": "cbc"}, "output": true}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "5d9a96c2749f4400"}
vcr_3fdac3ce55ff
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": [37, 72, 56, -18, -23, 59], "target": 96}, "output": [0, 5]}, {"input": {"nums": [-8, 1, 29, -25, 46, 32, -19, 23, 10], "target": 61}, "output": [2, 5]}, {"input": {"nums": [42, 17, -30, -28, 0, 7], "target": 7}, "output": [4, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "0b67b43ff131946c"}
vcr_6cce6bcc7bc8
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": [10, 39, -31, 74, -8, -3], "target": 84}, "output": [0, 3]}, {"input": {"nums": [-11, 36, 32, 44, -9], "target": 25}, "output": [0, 1]}, {"input": {"nums": [40, 30, 14, 8, 38, 7, -3, 6, -10], "target": 48}, "output": [0, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "b81b9fc469f46da4"}
vcr_5cd57e3ac024
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-1, -20, -2, -14, 13, -14, 18, 8, 11], "k": 24}, "output": [-14, 13, -14, 18, 8, 11, -1, -20, -2]}, {"input": {"nums": [7, 6, 10, -10, 8, -2, -6, -6], "k": 20}, "output": [8, -2, -6, -6, 7, 6, 10, -10]}, {"input": {"nums": [0, 0, 6, 9], "k": 16}, "output": [0, 0, 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": "968eb0e576039bc7"}
vcr_820181554408
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-22, -12, -11, 0, 4, 18, 21, 28, 40, 41, 43], "target": 47}, "output": [4, 10]}, {"input": {"nums": [-2, 3, 25, 36], "target": 39}, "output": [1, 3]}, {"input": {"nums": [-5, 16, 21, 31], "target": 52}, "output": [2, 3]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "6e2c75273e867e24"}
vcr_17e393973f0a
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, 5, 6, 15, 16, 30, 36, 37, 41, 45, 48], "target": 34}, "output": 6}, {"input": {"nums": [9, 12, 14, 21, 23, 28, 30], "target": 30}, "output": 6}, {"input": {"nums": [10, 11, 15, 16, 22], "target": 22}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "80fc65835e58a531"}
vcr_ceb2e6e6282f
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": [2, 4, 26, 39, 39, 2, 4, 27, 26, 60, 27]}, "output": 60}, {"input": {"nums": [47, 2, 12, 28, 2, 12, 28, 18, 18]}, "output": 47}, {"input": {"nums": [19, 28, 1, 19, 3, 29, 4, 3, 28, 4, 55, 1, 29]}, "output": 55}]
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": "a113d78f8df31e6a"}
vcr_5900666557da
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [14, 13, 26]}, "output": 40}, {"input": {"nums": [5, 6, 2, 6, 2]}, "output": 12}, {"input": {"nums": [5, 7, 13, 4, 3]}, "output": 21}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "065983b4f999c460"}
vcr_f6b6ab6483e5
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": 42167}, "output": 9}, {"input": {"n": 11049}, "output": 7}, {"input": {"n": 1714}, "output": 6}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "3cee72ac206dbd86"}
vcr_495593fb5d08
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, 6, 8, 9, 10, 35, 39, 41, 49], "target": 32}, "output": 6}, {"input": {"nums": [0, 7, 24, 29], "target": 2}, "output": 1}, {"input": {"nums": [5, 6, 14], "target": 8}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "eb2ac9e891bc3a2b"}
vcr_3a866e17ae63
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-4, 17, -9, 2, 19, 7, 13, 20, 6, 14, 20]}, "output": 109}, {"input": {"nums": [10, 12, 8, -2, 3]}, "output": 31}, {"input": {"nums": [-6, -2, -1, 3, -2, 0, 2, 7, 13]}, "output": 23}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "c8e9fc9d10826515"}
vcr_ca3f5155e18c
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-35, 61, -1, 40, -39, 5, -21, 23, 52, 1, 41], "target": -60}, "output": [4, 6]}, {"input": {"nums": [30, 2, 49, -2, 36, -24, -5, -29], "target": -26}, "output": [3, 5]}, {"input": {"nums": [21, -29, 31, -24], "target": 2}, "output": [1, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "f32331f10071022f"}
vcr_7595cc7edffc
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 1, 5, 5, 9, 10, 11, 12, 12, 14, 15]}, "output": [0, 1, 5, 9, 10, 11, 12, 14, 15]}, {"input": {"nums": [4, 5, 7]}, "output": [4, 5, 7]}, {"input": {"nums": [0, 1, 5, 8, 9, 9, 10, 11, 11, 12, 12]}, "output": [0, 1, 5, 8, 9, 10, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "2bc6b287daff3f55"}
vcr_af80247800ac
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": 55130}, "output": 10}, {"input": {"n": 14850}, "output": 5}, {"input": {"n": 13143}, "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": "e8667477055f557c"}
vcr_7de73f889631
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": [-24, -21, -19, -17, 16, 46], "target": -8}, "output": [0, 4]}, {"input": {"nums": [-20, -6, -2, 0, 3, 20, 21, 25, 32], "target": -20}, "output": [0, 3]}, {"input": {"nums": [-11, -10, -2, 0, 14, 34, 35], "target": 33}, "output": [2, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "5da229a99b1e811c"}
vcr_cf118324a4fa
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, 29, 9, 14, 20, 21, 18, 10, 3, 28, 28]}, "output": 102}, {"input": {"nums": [18, 6, 13, 7, 6, 18, 20, 16]}, "output": 65}, {"input": {"nums": [16, 14, 16, 13, 20]}, "output": 52}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "d0306ae156ce2839"}
vcr_7eddf8490811
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": [-12, 7, -6, 7, -4, 2, 5], "k": 19}, "output": [-6, 7, -4, 2, 5, -12, 7]}, {"input": {"nums": [-3, -6, 6, -8, -6, 4, 7, -5], "k": 1}, "output": [-5, -3, -6, 6, -8, -6, 4, 7]}, {"input": {"nums": [4, 7, 7, 6, -9], "k": 13}, "output": [7, 6, -9, 4, 7]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "1b32b78b35f848b3"}
vcr_081d65cb0257
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": "bbbcacb"}, "output": 5}, {"input": {"s": "bbabcc"}, "output": 4}, {"input": {"s": "cabbccbacccb"}, "output": 8}]
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": "4864b63cc7010fe9"}
vcr_2173827f2192
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "d ee bdcaca"}, "output": "bdcaca ee d"}, {"input": {"s": "aaba ab"}, "output": "ab aaba"}, {"input": {"s": "b bb bba"}, "output": "bba bb 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": "f4ff41a9a8a98cf1"}
vcr_2a984f381996
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": [3, 16, 26, 56]}, "output": 53}, {"input": {"prices": [11, 37, 38, 38, 40, 12, 29, 11]}, "output": 29}, {"input": {"prices": [40, 16, 36]}, "output": 20}]
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": "b593a8eacc85d469"}
vcr_5d0e0087c01b
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": [88, 171, 45, 131, 76, 19, 5, 142, 58, 137, 112, 22, 62, 67, 73, 36]}, "output": false}, {"input": {"nums": [17, 14, 14, 7]}, "output": true}, {"input": {"nums": [0, 25, 1, 18, 6, 2, 21, 15]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "744e2a0494c71c17"}
vcr_ffcf3e746185
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": ["caa", "aa", "abb", "acab", "aabc"]}, "output": [1, 1, 1, 2]}, {"input": {"strs": ["abb", "abb", "abb"]}, "output": [3]}, {"input": {"strs": ["aaa", "aba", "aba", "aaa", "aab", "baa"]}, "output": [2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "4cfa892094e6eb91"}
vcr_9f0a842286cb
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": [53, 6, 25, 48, 35]}, "output": 42}, {"input": {"prices": [18, 7, 37, 24, 17, 18]}, "output": 30}, {"input": {"prices": [16, 34, 10, 9, 15, 28]}, "output": 19}]
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": "0bfed6365708b962"}
vcr_faea5df7c8e4
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": ["ccc", "bc", "bb", "bba"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["abb", "bba", "aaa", "baa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bbb", "aba", "bba", "aaa"]}, "output": [1, 1, 1, 1]}]
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": "35baa10bc17d21e5"}
vcr_919342132092
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [47, 43, 45, 30, 30, 7, 22, 7]}, "output": true}, {"input": {"nums": [2, 6, 23, 20, 1, 15, 2, 8, 16, 23, 0, 5]}, "output": true}, {"input": {"nums": [0, 12, 23, 6, 18, 9]}, "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": "6ef462c61c6d9315"}
vcr_1b6b2228f1d8
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": "cccbbabbacaaa"}, "output": 7}, {"input": {"s": "cbcaabb"}, "output": 5}, {"input": {"s": "cbabba"}, "output": 5}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "0e5c9f1ecd8e900c"}
vcr_f53461105c23
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [35, 24, 64, 182, 195, 70, 94, 156, 119, 97, 33, 47, 96]}, "output": false}, {"input": {"nums": [4, 21, 3, 9, 24, 24, 22, 6, 17, 20]}, "output": true}, {"input": {"nums": [20, 11, 9, 15, 21, 14, 22, 6, 17]}, "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": "66cb1e5d57f96ff4"}
vcr_42a874277d45
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": "ccaabdadadcdbba"}, "output": false}, {"input": {"s": "cacbbc"}, "output": false}, {"input": {"s": "aabaaacba"}, "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": "17d05a9cb9882cfc"}
vcr_640e0d79ade9
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": [6, 30, -1]}, "output": [6, 36, 35]}, {"input": {"nums": [11, 0, 15, 4]}, "output": [11, 11, 26, 30]}, {"input": {"nums": [16, 10, -5, -1, 17, -12, 5, 0, 13, 3]}, "output": [16, 26, 21, 20, 37, 25, 30, 30, 43, 46]}]
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": "dd27a3b91959cef6"}
vcr_e3bca1bc16e3
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, 16, -13, 9, -2, -8, 14, -16, -10, 16, -18, -1, 8]}, "output": 21}, {"input": {"nums": [-5, 9, 5]}, "output": 14}, {"input": {"nums": [-5, 11, -5, 7, -1, 8, 7]}, "output": 27}]
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": "5cf15df28aa909f7"}
vcr_96af87b713f5
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": [20, -15, 19, 9, 9, 6, 7, 6, -2, 16, -14]}, "output": 75}, {"input": {"nums": [7, 9, 13]}, "output": 29}, {"input": {"nums": [0, 7, -2, -10]}, "output": 7}]
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": "86ec71ec2ef02191"}
vcr_c70426d7f09b
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [8, 9, 22, -9, -7, -9, -15, 14, -3]}, "output": 39}, {"input": {"nums": [-4, -5, 6, -4, 12, 11, 8, 14, 9, -6, -5]}, "output": 56}, {"input": {"nums": [13, -7, -6, -11, 10, 13]}, "output": 23}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "3016fed14520986c"}
vcr_bfa2f18f5008
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [11, -15, 3, 10, 20, 20, -12], "k": 9}, "output": [20, -12, 11, -15, 3, 10, 20]}, {"input": {"nums": [3, 4, 2, 4, 3, 6, 3], "k": 13}, "output": [4, 2, 4, 3, 6, 3, 3]}, {"input": {"nums": [3, 5, 7, -7, 2], "k": 15}, "output": [3, 5, 7, -7, 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": "5e9b77b700e1717d"}
vcr_d1f796bcff39
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 3, 3, 5, 7, 8, 9, 11, 11]}, "output": [0, 2, 3, 5, 7, 8, 9, 11]}, {"input": {"nums": [0, 0, 2, 3, 5, 6, 10, 10, 12, 12]}, "output": [0, 2, 3, 5, 6, 10, 12]}, {"input": {"nums": [1, 4, 12, 12]}, "output": [1, 4, 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": "8b73de9089d46289"}
vcr_3be4655147d2
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": "bcbbcbbbacbacb"}, "output": 11}, {"input": {"s": "bacaccabc"}, "output": 8}, {"input": {"s": "acaabbab"}, "output": 6}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "9308d0c62db17140"}
vcr_b21a84243bd0
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, 3, 20, 21, 34, 48, 49], "target": 26}, "output": 4}, {"input": {"nums": [3, 4, 5, 9, 16, 17, 20, 22, 23, 27, 33, 39], "target": 15}, "output": 4}, {"input": {"nums": [3, 18, 35, 38], "target": 36}, "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": "8c0e0f78b760dfff"}
vcr_ade795ab72c7
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 2, 4, 5, 2, 4]}, "output": [320, 800, 400, 320, 800, 400]}, {"input": {"nums": [1, 2, 1]}, "output": [2, 1, 2]}, {"input": {"nums": [2, 5, 5, 1]}, "output": [25, 10, 10, 50]}]
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": "dee7edd5ec741fc6"}
vcr_6d4bf7c68c5d
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": [10, 46, 24, 31, 19, 10]}, "output": true}, {"input": {"nums": [13, 7, 16, 18, 18, 16]}, "output": true}, {"input": {"nums": [23, 24, 10, 1, 24, 16, 23]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "f78b5edaaf9ac31a"}
vcr_d97c0ac594a4
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": 3960}, "output": 8}, {"input": {"n": 10268}, "output": 5}, {"input": {"n": 5221}, "output": 6}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "45834ff612e50911"}
vcr_27864d5b5616
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": "bacacacbabaa"}, "output": 11}, {"input": {"s": "bcaacbcaacb"}, "output": 9}, {"input": {"s": "abccccbb"}, "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": "3b86988cb2c612b7"}
vcr_eb8c5fef1ae2
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": [6, 16, 29, 24, 21, 24, 11]}, "output": 67}, {"input": {"nums": [8, 7, 14, 2]}, "output": 22}, {"input": {"nums": [0, 12, 10, 0]}, "output": 12}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "0301a87855f9d846"}
vcr_4055d1f475f6
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [28, 1, 19, 0]}, "output": 47}, {"input": {"nums": [2, 19, 15, 15, 18]}, "output": 37}, {"input": {"nums": [3, 14]}, "output": 14}]
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": "5d30ad5601b4e1d3"}
vcr_40db42b929e1
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": "ddb c debcbb acdc d da bdabd"}, "output": "bdabd da d acdc debcbb c ddb"}, {"input": {"s": "ba ba"}, "output": "ba ba"}, {"input": {"s": "ab b"}, "output": "b ab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "dc0550ef75f47ee3"}
vcr_aae3c9845766
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [20, 38, 34, 19, 3, 23, 44, 59, 38]}, "output": 56}, {"input": {"prices": [18, 35, 32, 19, 19, 31]}, "output": 17}, {"input": {"prices": [15, 26, 5, 32, 19, 19]}, "output": 27}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "c522611e95bc27de"}
vcr_f07aa9f3b359
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 5, 8, 8, 10]}, "output": [3, 5, 8, 10]}, {"input": {"nums": [2, 3, 4, 5, 6, 7, 10, 12]}, "output": [2, 3, 4, 5, 6, 7, 10, 12]}, {"input": {"nums": [0, 2, 2, 3, 5, 6, 8, 8, 8, 9, 12, 12]}, "output": [0, 2, 3, 5, 6, 8, 9, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "53ebe661760c070c"}
vcr_9c5f6e5d427b
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [20, 25, 26, 30, 46], "target": 6}, "output": 0}, {"input": {"nums": [5, 21, 30, 33], "target": 17}, "output": 1}, {"input": {"nums": [1, 13, 15, 17, 19, 29, 33, 35, 38], "target": 1}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "2f8aa2e0f4eadeb2"}
vcr_b4d66d0f7cec
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-18, -5, -6, 6, -3, 13, 7, 3, 13]}, "output": 39}, {"input": {"nums": [1, -10, -5, 7, -3, 6, 14, 0]}, "output": 24}, {"input": {"nums": [8, -1, 4, 12]}, "output": 23}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "07008a5ae0e8effb"}
vcr_558404f4a3bc
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": "bccacacc"}, "output": 6}, {"input": {"s": "ccaba"}, "output": 4}, {"input": {"s": "cbca"}, "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": "14bd3081d2c62219"}
vcr_d6d9c0a17a75
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": "daddcaaaacddad"}, "output": true}, {"input": {"s": "aacaaacc"}, "output": false}, {"input": {"s": "aac"}, "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": "d3b6d3146acec1ed"}
vcr_041723309bea
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [29, 10, 29]}, "output": 58}, {"input": {"nums": [6, 13, 12, 3, 13, 17, 10]}, "output": 41}, {"input": {"nums": [10, 19]}, "output": 19}]
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": "ff3926f9f739a515"}
vcr_3de6b0376e7f
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["bc", "aa", "ba", "cbc", "bcca"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bbb", "bbb", "bab"]}, "output": [1, 3]}, {"input": {"strs": ["bba", "bab", "aab"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "df41c18de67d724a"}
vcr_a949597eea4d
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": [-23, 39, 42, 51, 61, 60, 71, -39], "target": 122}, "output": [3, 6]}, {"input": {"nums": [-14, -10, 10, -11, 44, 4, -7], "target": -25}, "output": [0, 3]}, {"input": {"nums": [-30, -12, 43, 25, -29, 41, 18], "target": -4}, "output": [3, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "1c31851bc7b2fbb1"}
vcr_d6d2137bf22d
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": 40742}, "output": 9}, {"input": {"n": 16080}, "output": 8}, {"input": {"n": 3438}, "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": "bc443d3d58648cbf"}
vcr_b9377421ed15
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [27, 29, 14, 14, 23, 26, 29]}, "output": 93}, {"input": {"nums": [8, 6, 0]}, "output": 8}, {"input": {"nums": [1, 16]}, "output": 16}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "45d862790cc87028"}
vcr_30422a9b612a
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": [4, 1, 1, 4, 1]}, "output": [4, 16, 16, 4, 16]}, {"input": {"nums": [1, 1, 5, 4, 1, 1]}, "output": [20, 20, 4, 5, 20, 20]}, {"input": {"nums": [2, 5, 2, 2]}, "output": [20, 8, 20, 20]}]
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": "c8e2da7d468119d9"}
vcr_58b0fba0c195
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, 4, 0, 3, 3, 3, 2, 3, 0]}, "output": true}, {"input": {"nums": [0, 1, 3, 2, 2, 3]}, "output": false}, {"input": {"nums": [1, 0, 1, 0, 3, 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": "d5a52b7c1bd977e2"}
vcr_8b0ae54a52f9
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, 9, 17, 8, 20, 6, 6, 3, 26, 10, 7]}, "output": 79}, {"input": {"nums": [2, 0, 20, 3]}, "output": 22}, {"input": {"nums": [18, 8, 5, 11, 10]}, "output": 33}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "d3eba63f34417cab"}
vcr_21053e3844da
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 2, 2, 4, 4, 1, 4, 0]}, "output": true}, {"input": {"nums": [0, 1, 0, 2, 0, 1, 3, 2, 1]}, "output": false}, {"input": {"nums": [2, 0, 3, 0, 3, 1, 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": "c652c7dcb6b4f29e"}
vcr_0c9ebd02050a
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": "cbbaaabcbba"}, "output": 7}, {"input": {"s": "abcabcabbac"}, "output": 10}, {"input": {"s": "cacaacbcbb"}, "output": 8}]
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": "7a9122727dfc4978"}
vcr_af6bacaebcd5
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [6, 33, 25, 34, 44, 12, 12, 38, 20, 60, 52, 45]}, "output": 54}, {"input": {"prices": [39, 19, 32]}, "output": 13}, {"input": {"prices": [18, 20, 12, 34, 36, 26, 26, 32, 33, 20]}, "output": 24}]
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": "e36a89c790c8a511"}
vcr_85504f40e0db
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-4, -14, -2, 17, 1, -8, 1, -14, -1]}, "output": 18}, {"input": {"nums": [-2, -6, 12, 0, -11, 9, 11, -11, -5, -2]}, "output": 21}, {"input": {"nums": [-9, -5, 3, 9]}, "output": 12}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "411cb73bb8464a66"}
vcr_dd334dd06ceb
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 0, 0, 1, 1, 2, 2, 3, 6, 8, 14]}, "output": [0, 1, 2, 3, 6, 8, 14]}, {"input": {"nums": [2, 5, 6, 6, 6, 7, 8, 9, 10, 10, 12]}, "output": [2, 5, 6, 7, 8, 9, 10, 12]}, {"input": {"nums": [1, 3, 4, 5]}, "output": [1, 3, 4, 5]}]
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": "4e7dc6819fcdde16"}
vcr_07529c8f44a1
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": [25, 19, 8, 0, 19, 9, 23]}, "output": true}, {"input": {"nums": [11, 14, 19, 25, 3, 25]}, "output": true}, {"input": {"nums": [5, 17, 23, 14, 16, 19, 2]}, "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": "75682afb1664e542"}
vcr_c44a094af90d
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": [7, 12, 6, 29, 23, 8, 13, 25, 9, 16, 14]}, "output": 90}, {"input": {"nums": [18, 8, 2, 7, 9, 4, 7, 1]}, "output": 36}, {"input": {"nums": [4, 7, 10, 9]}, "output": 16}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "71bd528fe391c07b"}
vcr_0006a067541a
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 9, 12, 23, 29, 34, 37, 38, 43, 44, 45, 46, 47, 48], "target": 21}, "output": 3}, {"input": {"nums": [3, 4, 5, 7, 15, 30, 36, 39], "target": 27}, "output": 5}, {"input": {"nums": [5, 10, 15, 29, 30, 31, 34, 38, 39], "target": 26}, "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": "71b8c224193a9f3c"}
vcr_4ecbe7afc61b
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -26, -22, -18, -17, -15, 8, 17, 21, 28, 31], "target": 16}, "output": [5, 10]}, {"input": {"nums": [-13, -10, -8, -5, -1, 2, 6, 12, 15, 21], "target": -4}, "output": [1, 6]}, {"input": {"nums": [-17, -13, -5, -2, 3, 7], "target": -6}, "output": [1, 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": "eeef514c35bfab8c"}
vcr_595cee267edc
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": "ccaaa"}, "output": 2}, {"input": {"s": "babaaaca"}, "output": 6}, {"input": {"s": "ccabb"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "8e44334b464e1877"}
vcr_75079a25c85e
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": [43, 67, 77, -10, 69, -2, 19, 12, -15], "target": -12}, "output": [3, 5]}, {"input": {"nums": [16, 10, 30, 27, 41, 13, -3, 7, 32], "target": 71}, "output": [2, 4]}, {"input": {"nums": [46, 20, 16, -14, -2], "target": 32}, "output": [0, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "a40d14fe7c641b76"}
vcr_bb87be5d2e82
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": [14, 23, -24, 17, 12, -5, 25, -14, -25, -15, -4, -1, 2, 26]}, "output": [14, 37, 13, 30, 42, 37, 62, 48, 23, 8, 4, 3, 5, 31]}, {"input": {"nums": [16, -5, 0, 17, -7, 2, 9, 12, -1]}, "output": [16, 11, 11, 28, 21, 23, 32, 44, 43]}, {"input": {"nums": [14, -1, -10, 9, 1, 13, -6]}, "output": [14, 13, 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": "68ae5c3a4b582b98"}