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_79cfc8a16be0
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [29, 8, 47, 49, 8, 45, 17, 0, 14, 8, 38, 27]}, "output": true}, {"input": {"nums": [8, 6, 12]}, "output": false}, {"input": {"nums": [1, 1, 15, 22, 4, 3, 24, 1, 2]}, "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": "81dbc6f4fce7acff"}
vcr_469386c58bd0
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 4, 9, 13, 14, 18, 19, 23, 27, 31, 33, 45, 47, 49], "target": 34}, "output": 11}, {"input": {"nums": [16, 25, 30, 35, 37], "target": 3}, "output": 0}, {"input": {"nums": [1, 6, 8, 13, 18, 26, 29, 37], "target": 32}, "output": 7}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "f7c50d195cc9d393"}
vcr_827d9a6201f9
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, -5, 22, -16, -6, 8, -3, 9, -16]}, "output": 37}, {"input": {"nums": [11, -5, 1, -8]}, "output": 11}, {"input": {"nums": [9, 6, -6, -4, 2, 7, 14, -9, 3, -10]}, "output": 28}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "893b58a243ed0d2f"}
vcr_dacdc3ca4ab3
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": "bcabac"}, "output": false}, {"input": {"s": "bbcaab"}, "output": false}, {"input": {"s": "aa"}, "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": "6810eb743cc48f09"}
vcr_71d2242512e3
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 10, 15, 16, 17, 19, 26, 29, 30, 39, 46], "target": 39}, "output": 9}, {"input": {"nums": [4, 8, 13, 34, 39], "target": 36}, "output": 4}, {"input": {"nums": [0, 1, 5, 7, 10, 18, 25, 33], "target": 38}, "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": "ea76260fa80281f8"}
vcr_906a2d999127
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, 24, 29, 10]}, "output": 35}, {"input": {"nums": [20, 19, 15, 3]}, "output": 35}, {"input": {"nums": [19, 16, 14, 18, 3, 8, 19, 2, 4]}, "output": 60}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "59471b2100e19fc0"}
vcr_845ff1ac595c
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, 6, 10, 11, 15]}, "output": [0, 6, 10, 11, 15]}, {"input": {"nums": [0, 2, 2, 11]}, "output": [0, 2, 11]}, {"input": {"nums": [0, 4, 6, 8]}, "output": [0, 4, 6, 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": "ee97a2288a72145d"}
vcr_63acb53c86f7
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, 68, -38, 46], "target": 114}, "output": [1, 3]}, {"input": {"nums": [35, -9, 2, 39, -30, 38, -3, 0, -22, 34, -24], "target": -30}, "output": [4, 7]}, {"input": {"nums": [-16, -19, -27, 27, -20, -4, -29, -22, -17, -18, 33], "target": -20}, "output": [0, 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": "185d16f5e42a339a"}
vcr_015df4375d67
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, -21, -11, 2, 6, 47], "target": 26}, "output": [1, 5]}, {"input": {"nums": [-14, -3, -2, 19, 24, 39], "target": 16}, "output": [1, 3]}, {"input": {"nums": [-15, -13, -6, -2, 2, 9, 19, 25, 28], "target": 0}, "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": "b00c520e0da9b6f8"}
vcr_0e29ed13755c
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": "cccbaac"}, "output": 4}, {"input": {"s": "cba"}, "output": 3}, {"input": {"s": "ccabbccabc"}, "output": 7}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "854686c3351a5581"}
vcr_4b0eca309213
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": 17729}, "output": 5}, {"input": {"n": 8035}, "output": 9}, {"input": {"n": 5068}, "output": 7}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "7e8416282a0df68d"}
vcr_b73302168ff5
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 216, "b": 98}, "output": 2}, {"input": {"a": 155, "b": 115}, "output": 5}, {"input": {"a": 67, "b": 45}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "a94903c7ec74b27f"}
vcr_8aa6208fc02a
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 4, 7, 16, 18, 26, 32, 38, 39, 42, 49], "target": 6}, "output": 2}, {"input": {"nums": [8, 20, 29], "target": 26}, "output": 2}, {"input": {"nums": [2, 7, 9, 14, 18, 21, 29, 33], "target": 15}, "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": "0f2e45604a08cda4"}
vcr_0baf9df182d2
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": "bcabcbacbb"}, "output": 9}, {"input": {"s": "acab"}, "output": 4}, {"input": {"s": "cbcac"}, "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": "69454a51fbcf03da"}
vcr_4270478937d7
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": [15, 35, 13, 16, 40, 37, 15]}, "output": true}, {"input": {"nums": [17, 14, 8, 19, 8, 16, 20, 20, 18, 23]}, "output": true}, {"input": {"nums": [6, 15, 21]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "dcd1830d84c869b8"}
vcr_c999e495785f
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": "ae ccd dade"}, "output": "dade ccd ae"}, {"input": {"s": "ab abbb"}, "output": "abbb ab"}, {"input": {"s": "bbba ba bb aaab"}, "output": "aaab bb ba bbba"}]
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": "7b415df26b53a6a1"}
vcr_b34f301a83fd
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [19, 0, 2, 12, 17], "k": 14}, "output": [0, 2, 12, 17, 19]}, {"input": {"nums": [2, -3, 10, 0, 4, -5, 8, 0, 8, 1], "k": 15}, "output": [-5, 8, 0, 8, 1, 2, -3, 10, 0, 4]}, {"input": {"nums": [-8, -8, 6, -5, -4, 3, -6], "k": 3}, "output": [-4, 3, -6, -8, -8, 6, -5]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "ce81550a545c6d05"}
vcr_0c4218d826fd
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": [-14, 6, 12, -13, 20, -10, -3, 1, 13], "k": 0}, "output": [-14, 6, 12, -13, 20, -10, -3, 1, 13]}, {"input": {"nums": [6, 3, -2, 2], "k": 16}, "output": [6, 3, -2, 2]}, {"input": {"nums": [4, -7, 3, 0, 4, -4, 3], "k": 9}, "output": [-4, 3, 4, -7, 3, 0, 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": "a503350f387e8ff4"}
vcr_e63af8078b82
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, 6, 6, 7, 9, 10, 10, 12, 14]}, "output": [0, 1, 4, 6, 7, 9, 10, 12, 14]}, {"input": {"nums": [1, 1, 3, 4, 5, 6, 10, 12]}, "output": [1, 3, 4, 5, 6, 10, 12]}, {"input": {"nums": [0, 0, 5, 7, 10, 12, 12]}, "output": [0, 5, 7, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "542d20e3de06071a"}
vcr_35b7b8f0907a
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": "cbbcacbbabbbc"}, "output": 9}, {"input": {"s": "bbbbaabb"}, "output": 3}, {"input": {"s": "baacbbcabcca"}, "output": 9}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "053778578d8b56b2"}
vcr_48941688a566
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": [16, 16, 1, 50, 32, 32, 1]}, "output": 50}, {"input": {"nums": [29, 25, 20, 5, 29, 47, 20, 12, 12, 5, 25]}, "output": 47}, {"input": {"nums": [8, 18, 54, 19, 16, 18, 8, 20, 12, 16, 12, 20, 19]}, "output": 54}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "6f104352fb1b22cf"}
vcr_f45405cae509
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, 4, 5, 6, 6, 7, 8, 8, 10, 11, 15]}, "output": [2, 4, 5, 6, 7, 8, 10, 11, 15]}, {"input": {"nums": [2, 2, 6, 7, 11]}, "output": [2, 6, 7, 11]}, {"input": {"nums": [1, 2, 4, 4, 5, 6, 7, 7, 8, 10, 11, 11]}, "output": [1, 2, 4, 5, 6, 7, 8, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "c86271758e1aabc3"}
vcr_c911dea7630c
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": "bababaaabccca"}, "output": 9}, {"input": {"s": "abcabacc"}, "output": 7}, {"input": {"s": "ccba"}, "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": "f553a9944925cf2d"}
vcr_f2492113ab28
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": "ee aace cee d"}, "output": "d cee aace ee"}, {"input": {"s": "b bba ba a aaaa"}, "output": "aaaa a ba bba b"}, {"input": {"s": "ba aaa aa b bbba"}, "output": "bbba b aa aaa ba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "244ba47013500a1c"}
vcr_056c9a156cb3
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": [39, 10, 39, 10, 9, 77, 14, 9, 28, 14, 28, 25, 25]}, "output": 77}, {"input": {"nums": [15, 18, 15, 20, 41, 5, 5, 20, 9, 9, 22, 18, 22]}, "output": 41}, {"input": {"nums": [19, 18, 27, 43, 19, 27, 18]}, "output": 43}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "7f12f999a891273f"}
vcr_0b3b58b94e0e
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-16, -5, 9, -9, 21, 12, -11, -18]}, "output": 33}, {"input": {"nums": [-2, 11, -4]}, "output": 11}, {"input": {"nums": [-3, -12, 7]}, "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": "7f4451b4c4aece52"}
vcr_7914fcd8b37c
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [13, 14, 24, 14, 5, 4, 13, 3, 16]}, "output": 71}, {"input": {"nums": [20, 0, 16, 11, 5, 6]}, "output": 42}, {"input": {"nums": [20, 20, 15, 19, 7, 9, 17, 3]}, "output": 59}]
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": "592d9e4fcf2f5cad"}
vcr_e02908710c25
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, 1, 4, 2, 3, 3, 3, 2, 0]}, "output": true}, {"input": {"nums": [1, 1, 3, 1, 2, 1, 2, 0]}, "output": true}, {"input": {"nums": [1, 1, 2, 0, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "69435c500f7724f3"}
vcr_e180aac3febe
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": [32, 28, 32, 77, 16, 16, 28, 6, 6]}, "output": 77}, {"input": {"nums": [4, 19, 22, 22, 26, 4, 18, 26, 19, 47, 18]}, "output": 47}, {"input": {"nums": [25, 25, 12, 53, 27, 12, 27]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "b567a56fb0f87c26"}
vcr_958ea8ecc000
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-8, 8, 5, -15, 16, 10, -10, 1, 14, 11], "k": 15}, "output": [10, -10, 1, 14, 11, -8, 8, 5, -15, 16]}, {"input": {"nums": [-3, 5, 10, -6], "k": 17}, "output": [-6, -3, 5, 10]}, {"input": {"nums": [-7, -3, 7, 7], "k": 5}, "output": [7, -7, -3, 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": "ebd5245c0f782d8f"}
vcr_19b37cec809d
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": [40, 41, 24, 22, 64, 3, 11, 54, 50, -32, 72, 29, 79, 57], "target": 27}, "output": [2, 5]}, {"input": {"nums": [14, -3, -20, -18, 35], "target": -21}, "output": [1, 3]}, {"input": {"nums": [-2, 0, -7, -16, 42, 39, 41, 40], "target": 80}, "output": [5, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "1837e0e5c6323f3b"}
vcr_34ecf436d49a
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, 3, 1, 6, 4, 6, 7]}, "output": [3024, 12096, 4032, 12096, 2016, 3024, 2016, 1728]}, {"input": {"nums": [3, 3, 1, 5, 1, 3, 2]}, "output": [90, 90, 270, 54, 270, 90, 135]}, {"input": {"nums": [5, 3, 5, 2, 5]}, "output": [150, 250, 150, 375, 150]}]
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": "d27c4ebccfa81332"}
vcr_9ce7cb7ed0ba
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 3, 5, 5, 6, 12, 14, 15, 15]}, "output": [1, 2, 3, 5, 6, 12, 14, 15]}, {"input": {"nums": [2, 4, 10]}, "output": [2, 4, 10]}, {"input": {"nums": [0, 1, 3, 4, 5, 6, 7, 8, 9, 11]}, "output": [0, 1, 3, 4, 5, 6, 7, 8, 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": "210138e8e5b2a7a6"}
vcr_7c78b079dcd3
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["abbc", "cac", "ab", "baca", "bcac", "cbba", "cc", "acbc", "cbcc"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["bba", "aaa", "aab", "bbb", "aaa", "bba"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["abb", "bba", "bba", "bab", "baa"]}, "output": [1, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "4837fd0eaf0ec5c1"}
vcr_51b6952fde64
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": [18, 43, 7, 50]}, "output": 43}, {"input": {"prices": [34, 31, 37, 36, 30, 15, 2, 20, 16, 6, 19]}, "output": 18}, {"input": {"prices": [12, 7, 36, 40, 3, 9, 33, 5, 32, 7, 15]}, "output": 33}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "0fad1d4f0c3edcc8"}
vcr_3732b8cf2740
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, 3, 10, 31, 39, 46], "target": 56}, "output": [2, 5]}, {"input": {"nums": [-17, -15, -14, -9, -7, -3, -2, 0, 16, 24], "target": 21}, "output": [5, 9]}, {"input": {"nums": [-19, -11, -5, -1, 7, 13, 20, 21, 29, 30], "target": 1}, "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": "3996ec9d0763a715"}
vcr_e5678960ad20
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 8, 12, 20, 21, 24, 29, 32, 41], "target": 12}, "output": 2}, {"input": {"nums": [2, 5, 7, 18, 21, 23, 27, 32], "target": 37}, "output": 8}, {"input": {"nums": [4, 20, 21, 24, 31, 34, 35, 38], "target": 19}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "460b6dd4483b6911"}
vcr_1131c1efd3a1
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, 4, 9, 11, 13, 15, 25, 27, 32, 48], "target": 5}, "output": 2}, {"input": {"nums": [3, 6, 8, 9, 10, 12, 15, 18, 22, 23, 30, 37], "target": 35}, "output": 11}, {"input": {"nums": [0, 2, 5, 8, 10, 15, 16, 17, 19, 21, 25, 28], "target": 14}, "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": "c3e189b87f1f31cc"}
vcr_349d28efc1af
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [5, 12, 21, 25, 33, 38, 39, 44, 48], "target": 24}, "output": 3}, {"input": {"nums": [32, 33, 35], "target": 12}, "output": 0}, {"input": {"nums": [6, 19, 27], "target": 14}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "ca980427df11b54a"}
vcr_db475a28faef
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": [-3, 2, -37, 40, -9, -12, 72, 7, 67, 37, -2, 58, 13], "target": -2}, "output": [4, 7]}, {"input": {"nums": [27, 32, 40, 39, -5, 4, -7, -18, 17], "target": 25}, "output": [1, 6]}, {"input": {"nums": [31, -1, -29, 9, 14, 28, 0, 7, 33, -2], "target": 7}, "output": [6, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "cadf50a0349f7e1a"}
vcr_f65f567289f0
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": [6, 4, 2, 5, 6, 2]}, "output": [480, 720, 1440, 576, 480, 1440]}, {"input": {"nums": [5, 1, 5, 5]}, "output": [25, 125, 25, 25]}, {"input": {"nums": [1, 4, 5, 2, 2]}, "output": [80, 20, 16, 40, 40]}]
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": "d87734885dbeb1a7"}
vcr_3da62f5a01b9
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [60, 54, 37, 36, 40, 1, 53, 34, 59]}, "output": 58}, {"input": {"prices": [17, 6, 23, 36, 25, 8]}, "output": 30}, {"input": {"prices": [21, 31, 15, 10, 31, 33, 20, 14]}, "output": 23}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "07ae78e579ac6130"}
vcr_744c366b5c26
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, 1, -1, -16, -6, 1, 15, 9, 18, 6, -6]}, "output": 49}, {"input": {"nums": [5, -7, 7, 9, 10, -4, -4, -10, 8, -12]}, "output": 26}, {"input": {"nums": [0, -6, -7, -2, 3, -8, 4]}, "output": 4}]
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": "dba1cbe4bc1a05c1"}
vcr_3ffed1eb25ca
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [40, 29, 60, 19, 28, 2, 10, 7, 46, 20, 48, 28, 45]}, "output": 46}, {"input": {"prices": [17, 16, 5, 19, 29]}, "output": 24}, {"input": {"prices": [2, 14, 31, 29, 4, 4, 37, 11]}, "output": 35}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "d85f7eb3fa7ddc20"}
vcr_cf477b2135e9
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 2, 3, 3, 3, 2, 0, 4, 2, 0]}, "output": true}, {"input": {"nums": [1, 3, 1, 3, 2]}, "output": true}, {"input": {"nums": [0, 1, 2, 3, 1, 1, 3, 3, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "3772e44e690b1955"}
vcr_b4372e2475eb
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, -24, -19, -12, -10, -4, 9, 17, 39], "target": -34}, "output": [0, 5]}, {"input": {"nums": [-20, -16, -12, 6, 17, 19, 26, 32], "target": 38}, "output": [3, 7]}, {"input": {"nums": [-19, -16, -15, -10, -8, 4, 5, 8, 9, 32], "target": 1}, "output": [4, 8]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "cefd6450f5786c8e"}
vcr_4b25cd4e8d88
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [6, 10, 33, 37], "target": 41}, "output": 4}, {"input": {"nums": [6, 7, 9, 24, 25, 28, 30], "target": 38}, "output": 7}, {"input": {"nums": [4, 6, 8, 17, 19, 33, 38], "target": 25}, "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": "b734ab784db003fc"}
vcr_bef5f474f5ab
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [27, 4, 1, 37, 42, 5, 39, 4, 46, 37]}, "output": true}, {"input": {"nums": [21, 4, 22, 6, 13, 19, 20, 19, 23, 6, 4]}, "output": true}, {"input": {"nums": [18, 24, 18, 8, 25, 20, 7, 20, 24, 23, 14, 21]}, "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": "70920440164d524b"}
vcr_f2ec3f5ae5d6
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [6, 8, 10, 18, 23, 30, 39], "target": 16}, "output": 3}, {"input": {"nums": [5, 11, 12, 14, 22, 31, 35, 38], "target": 0}, "output": 0}, {"input": {"nums": [4, 5, 7, 9, 18, 23], "target": 37}, "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": "504357c8da5deb2f"}
vcr_bf1d15438f1d
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-8, -7, 35, 37, 40], "target": 30}, "output": [1, 3]}, {"input": {"nums": [-6, 20, 22, 26], "target": 16}, "output": [0, 2]}, {"input": {"nums": [-18, -14, -7, 0, 3, 4, 20, 22, 25, 26], "target": -25}, "output": [0, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "6ab5128c65251c68"}
vcr_ffa43a4ade75
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [30, 51, 2, 45, 44, 35]}, "output": 43}, {"input": {"prices": [21, 38, 10, 40]}, "output": 30}, {"input": {"prices": [7, 26, 8, 35, 8, 13, 10, 1, 26, 24, 28, 25]}, "output": 28}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "94a33c1afb862fd6"}
vcr_aea9b6500750
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": [-13, -9, -5, 9, 11, 13, 20, 27, 33, 35, 41, 43], "target": 4}, "output": [1, 5]}, {"input": {"nums": [-20, -2, 3, 29, 31, 34, 35, 37, 38], "target": 9}, "output": [0, 3]}, {"input": {"nums": [-6, 10, 16, 17], "target": 27}, "output": [1, 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": "0948b9517ca1cc2c"}
vcr_5179113af628
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": "e bdbdeb dddea badd addcbe"}, "output": "addcbe badd dddea bdbdeb e"}, {"input": {"s": "ba baa"}, "output": "baa ba"}, {"input": {"s": "ba b"}, "output": "b ba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "c76122c3cfd3c097"}
vcr_e49a50c62a3f
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 3, 14, 16, 25, 35, 36], "target": 48}, "output": 7}, {"input": {"nums": [2, 4, 6, 12, 25, 26, 28, 31, 33, 34, 39], "target": 18}, "output": 4}, {"input": {"nums": [4, 17, 18, 30], "target": 3}, "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": "a74115f39cf084ca"}
vcr_6ae54f491921
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 4, 0, 4, 0, 4, 0, 0]}, "output": false}, {"input": {"nums": [1, 3, 2, 1, 2, 3, 1, 3, 1]}, "output": true}, {"input": {"nums": [0, 2, 1, 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": "dc4184746301d06b"}
vcr_b2daf717436f
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": [4, 39, 51, 5, 1, 10, 45, 24, 21, 25, 60, 10, 26, 33, 9]}, "output": 59}, {"input": {"prices": [1, 25, 17, 13, 21, 16, 12, 4, 20]}, "output": 24}, {"input": {"prices": [1, 9, 38]}, "output": 37}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "6e4504ab0131539f"}
vcr_fbcac3543b82
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, -25, -12, -11, -8, 12, 24, 26, 37, 41], "target": 16}, "output": [1, 9]}, {"input": {"nums": [-2, 20, 22, 31], "target": 18}, "output": [0, 1]}, {"input": {"nums": [-9, 5, 9, 12, 14, 18, 25], "target": 34}, "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": "98406b2180e13ffd"}
vcr_ee04b048d957
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 6, 11, 12, 14, 34, 43, 44, 46, 49], "target": 17}, "output": 5}, {"input": {"nums": [3, 4, 6, 7, 10, 11, 21, 30, 32, 34, 35, 39], "target": 27}, "output": 7}, {"input": {"nums": [0, 1, 4, 8, 15, 22, 28, 29, 32, 38], "target": 18}, "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": "bd2ab1dc30866857"}
vcr_91593f73e934
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": 39657}, "output": 9}, {"input": {"n": 7747}, "output": 7}, {"input": {"n": 13513}, "output": 7}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "f7ec80092553619f"}
vcr_9410e5880034
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": "aaaabacaab"}, "output": 6}, {"input": {"s": "aaaacbcc"}, "output": 4}, {"input": {"s": "abbbaaabc"}, "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": "d4dde356c710a0b0"}
vcr_4aaf7bac5c6b
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": ["bbb", "bcbc", "aa", "ab", "abba", "cbc", "cac", "baa"]}, "output": [1, 1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["bba", "aaa", "baa", "baa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aba", "baa", "abb", "baa", "bbb"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "a3cebc427d68899b"}
vcr_eb02167ae657
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [37, 14, 52, 19, -28, 36], "target": -9}, "output": [3, 4]}, {"input": {"nums": [-6, -11, 41, -18, -16, 26, 0, 16], "target": 23}, "output": [2, 3]}, {"input": {"nums": [21, 39, 17, 19, 2, 47, 10, 27], "target": 49}, "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": "04f6805c09bad0c7"}
vcr_2cd916d9e8ee
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, 3, 4, 5, 5, 7, 8, 9, 11, 11, 13]}, "output": [0, 2, 3, 4, 5, 7, 8, 9, 11, 13]}, {"input": {"nums": [0, 0, 3, 5, 10, 10, 12, 12]}, "output": [0, 3, 5, 10, 12]}, {"input": {"nums": [1, 8, 10]}, "output": [1, 8, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "8ffb2c001f478a97"}
vcr_0ffce37212a3
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, -7, -6, 7, 39, 45], "target": 33}, "output": [3, 5]}, {"input": {"nums": [-16, -13, 7, 32, 37], "target": -6}, "output": [1, 2]}, {"input": {"nums": [-15, -12, -10, -6, 12, 16], "target": -22}, "output": [1, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "10535ee343c2a71d"}
vcr_11ead86d47dc
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [2, 13, 5, 1, 23, 19, 3, 14]}, "output": 50}, {"input": {"nums": [1, 10, 14, 18, 11, 11, 6, 2]}, "output": 41}, {"input": {"nums": [6, 2, 13, 3, 9]}, "output": 28}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "fd184c513566c51b"}
vcr_565123cebe5f
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [1, 2, -4, -11, 7, -16, 15, -4, 17, 2, -10, -9, 11, 9, 4]}, "output": 35}, {"input": {"nums": [2, 0, 5, 9, 14, 14]}, "output": 44}, {"input": {"nums": [-4, 8, 6, 10, -3, -5]}, "output": 24}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "c85143601dfe68e7"}
vcr_2759038d5a56
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": [17, -10, 4, -24, 7, -9, 23, 28, 30, 9]}, "output": [17, 7, 11, -13, -6, -15, 8, 36, 66, 75]}, {"input": {"nums": [6, -12, 10, 14, 5, 12]}, "output": [6, -6, 4, 18, 23, 35]}, {"input": {"nums": [3, 13, -10, 10, -1]}, "output": [3, 16, 6, 16, 15]}]
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": "9d57bcc03a2709fb"}
vcr_81497a543ab9
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, -24, -20, -15, -14, -3, 5, 8, 14, 34, 41], "target": -34}, "output": [2, 4]}, {"input": {"nums": [-9, -3, 10, 16, 23, 26, 32, 33], "target": 48}, "output": [3, 6]}, {"input": {"nums": [-20, -5, -2, 2, 4, 8, 13, 18], "target": 3}, "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": "656ec84436175f72"}
vcr_045343c21eb7
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [7, 3, 6, 1, 5]}, "output": [90, 210, 105, 630, 126]}, {"input": {"nums": [4, 4, 4]}, "output": [16, 16, 16]}, {"input": {"nums": [5, 4, 3]}, "output": [12, 15, 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": "28691241dac1a12e"}
vcr_4c634a70dc58
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": "cbaac ebccb b"}, "output": "b ebccb cbaac"}, {"input": {"s": "ab baab a bb"}, "output": "bb a baab ab"}, {"input": {"s": "ab aa a bbb"}, "output": "bbb a aa 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": "95afef3ba4b86198"}
vcr_9f70b87d22fb
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [56, 48, 32, 40, 44, 20, 51, 45, 1, 24, 58, 42, 51]}, "output": 57}, {"input": {"prices": [32, 31, 27, 5, 10, 3, 28, 7, 10, 30, 39]}, "output": 36}, {"input": {"prices": [29, 11, 8, 26]}, "output": 18}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "f11e330425e7179e"}
vcr_4d026b0c9d36
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, 2, 3, 5, 6, 6, 9, 10, 10, 12, 13, 14]}, "output": [0, 1, 2, 3, 5, 6, 9, 10, 12, 13, 14]}, {"input": {"nums": [1, 2, 3, 4, 5, 5, 7, 8, 8, 9, 11]}, "output": [1, 2, 3, 4, 5, 7, 8, 9, 11]}, {"input": {"nums": [1, 1, 2, 5, 9, 9, 10, 10, 11]}, "output": [1, 2, 5, 9, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "250a346cc276170a"}
vcr_56f633b1d834
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, 14, 2, 24, 3, 24, 26, 23]}, "output": 85}, {"input": {"nums": [16, 13, 6, 13, 10]}, "output": 32}, {"input": {"nums": [18, 19, 11, 11, 14, 2, 0, 1, 4, 18]}, "output": 62}]
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": "fd6db15e1074dd44"}
vcr_3c902a424681
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-10, -9, 15, -12, 8, 8, -1, -5, 19, 19, 9], "k": 23}, "output": [9, -10, -9, 15, -12, 8, 8, -1, -5, 19, 19]}, {"input": {"nums": [4, -4, 7], "k": 20}, "output": [-4, 7, 4]}, {"input": {"nums": [-6, -4, 6, 9, 8, 6, 4, -6], "k": 20}, "output": [8, 6, 4, -6, -6, -4, 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": "5b5845f8cad9a2e2"}
vcr_f790acb5cd97
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 83, "b": 154}, "output": 1}, {"input": {"a": 156, "b": 143}, "output": 13}, {"input": {"a": 147, "b": 65}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "e339c0d33de65fe1"}
vcr_ca8942bfbd3f
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 3, 4, 6, 10, 12, 15]}, "output": [0, 1, 3, 4, 6, 10, 12, 15]}, {"input": {"nums": [0, 4, 4, 5, 6, 6, 7, 8, 9, 9, 12]}, "output": [0, 4, 5, 6, 7, 8, 9, 12]}, {"input": {"nums": [1, 5, 5, 6, 6, 10, 12, 12]}, "output": [1, 5, 6, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "f6b32ee8ba02e883"}
vcr_96efc34fa510
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 1, 1, 3, 4, 4, 0]}, "output": true}, {"input": {"nums": [2, 0, 2, 0, 3]}, "output": true}, {"input": {"nums": [0, 3, 2, 2, 2, 2, 1, 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": "c754690a6ae45bc9"}
vcr_933fee0028f2
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [3, 2, 6, 6, 0, 3, 1]}, "output": [0, 0, 0, 0, 648, 0, 0]}, {"input": {"nums": [2, 2, 4, 1]}, "output": [8, 8, 4, 16]}, {"input": {"nums": [2, 1, 2]}, "output": [2, 4, 2]}]
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": "83f3cfc6edec02b9"}
vcr_2fb6d504113c
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 1, 5, 5, 3]}, "output": [75, 375, 75, 75, 125]}, {"input": {"nums": [1, 4, 5]}, "output": [20, 5, 4]}, {"input": {"nums": [2, 5, 2, 3, 1]}, "output": [30, 12, 30, 20, 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": "50a044174f9688ea"}
vcr_88ddb791b713
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 188, "b": 185}, "output": 1}, {"input": {"a": 39, "b": 43}, "output": 1}, {"input": {"a": 19, "b": 55}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "66d1f181c15f7679"}
vcr_171dda51e7fc
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, 1, 2, 16, 19, 20, 29, 32, 36, 38, 40, 42, 43], "target": 10}, "output": 3}, {"input": {"nums": [1, 3, 8, 10, 15, 16, 18, 23, 31], "target": 12}, "output": 4}, {"input": {"nums": [5, 6, 7, 13, 14, 15, 18, 30, 35], "target": 3}, "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": "75bc63f2ba217292"}
vcr_2d89e0b1a82d
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [12, 10, -12, -15, 31]}, "output": [12, 22, 10, -5, 26]}, {"input": {"nums": [5, -7, 16, 15, 8, 3]}, "output": [5, -2, 14, 29, 37, 40]}, {"input": {"nums": [-10, -6, 10, 18, -5, 12, 16, -10]}, "output": [-10, -16, -6, 12, 7, 19, 35, 25]}]
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": "e7ff734b050aa4af"}
vcr_8ebd366111b9
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, 4, 5, 7, 9, 9, 14, 15]}, "output": [1, 4, 5, 7, 9, 14, 15]}, {"input": {"nums": [0, 0, 7, 8, 11]}, "output": [0, 7, 8, 11]}, {"input": {"nums": [1, 6, 12]}, "output": [1, 6, 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": "8db81a65d525194e"}
vcr_bd00410cb6af
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": 60067}, "output": 9}, {"input": {"n": 14419}, "output": 7}, {"input": {"n": 8290}, "output": 4}]
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": "1de1f55e174809f5"}
vcr_35d49ba0774a
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": [32, -13, 3, 0, 6]}, "output": [32, 19, 22, 22, 28]}, {"input": {"nums": [1, 8, -6, -9, -1, 9, -1]}, "output": [1, 9, 3, -6, -7, 2, 1]}, {"input": {"nums": [18, 1, 2, 18, -3, 10, -10, 6, 11]}, "output": [18, 19, 21, 39, 36, 46, 36, 42, 53]}]
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": "57e54b4dedd2ae69"}
vcr_dc975cb7e0a8
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [12, 55, 34, 19, 20, 20, 19, 24, 12, 32, 34, 28, 32, 28, 24]}, "output": 55}, {"input": {"nums": [43, 26, 26, 25, 25, 29, 29]}, "output": 43}, {"input": {"nums": [29, 8, 6, 6, 28, 29, 41, 8, 28]}, "output": 41}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "2a28f7d228a8dd88"}
vcr_620bb7251dd9
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, 15, 24, 28, 30, 35, 40], "target": 35}, "output": 5}, {"input": {"nums": [6, 9, 22, 26, 30, 35, 36], "target": 11}, "output": 2}, {"input": {"nums": [3, 10, 14], "target": 24}, "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": "0682a105b2010034"}
vcr_c0e4b06f7f3c
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": [33, 13, 25, 16, 44, 43, 12, 27, 43, 4, 43, 24, 37]}, "output": true}, {"input": {"nums": [13, 3, 5, 8, 9, 12, 11]}, "output": false}, {"input": {"nums": [18, 13, 5, 1, 18, 22, 18, 1]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "94144a22bdc3360f"}
vcr_e34589ba8d4f
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, 4, 4, 4, 6, 6, 7, 11, 11, 14, 14, 14, 15]}, "output": [1, 4, 6, 7, 11, 14, 15]}, {"input": {"nums": [1, 3, 3, 5, 6, 8, 9, 10, 12, 12]}, "output": [1, 3, 5, 6, 8, 9, 10, 12]}, {"input": {"nums": [1, 2, 2, 4, 4, 6, 6, 8, 12]}, "output": [1, 2, 4, 6, 8, 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": "583e0a67aef57190"}
vcr_ccd66794b4e2
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-15, 1, -12, 10, -3, 9, 20], "k": 6}, "output": [1, -12, 10, -3, 9, 20, -15]}, {"input": {"nums": [-1, -1, 8, -10], "k": 8}, "output": [-1, -1, 8, -10]}, {"input": {"nums": [3, 8, -9, 0, 0, -6, 10, -10], "k": 18}, "output": [10, -10, 3, 8, -9, 0, 0, -6]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "5b2de2d763b18632"}
vcr_5e5341da02d8
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 7, 4, 2, 6]}, "output": [336, 48, 84, 168, 56]}, {"input": {"nums": [1, 2, 2]}, "output": [4, 2, 2]}, {"input": {"nums": [2, 5, 2]}, "output": [10, 4, 10]}]
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": "6180ec2a2a0f53fd"}
vcr_e1c3c3bafc5a
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": 26196}, "output": 7}, {"input": {"n": 12003}, "output": 9}, {"input": {"n": 4764}, "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": "b9f16a4a115dc24e"}
vcr_8c2727a41081
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [11, 17, 19, 22, 40], "target": 14}, "output": 1}, {"input": {"nums": [2, 4, 8, 14, 17, 25, 27, 29, 36, 38], "target": 38}, "output": 9}, {"input": {"nums": [5, 8, 9, 11, 14, 15, 19, 25, 30, 31, 32, 36], "target": 19}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "f9c900f91945eb38"}
vcr_c93b39c21390
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": [26, 20, 0, 21, 23]}, "output": 49}, {"input": {"nums": [3, 8, 18, 12, 0, 15, 11, 20, 3, 15]}, "output": 71}, {"input": {"nums": [19, 18, 12, 20, 17, 7, 2]}, "output": 50}]
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": "6a379e094bce50df"}
vcr_69f41103d892
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-9, 14, -11, 6, 3, 4, 19, 4, 5, -2, -6, 9, 19, 17]}, "output": 81}, {"input": {"nums": [-9, 11, -6, 11, 6, 3, 2, 2, -1]}, "output": 29}, {"input": {"nums": [9, 6, -11, 4, -6, 5]}, "output": 15}]
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": "0e10bb40116b063c"}
vcr_2b68091cbc7f
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": "abbaaaabcbbcba"}, "output": 9}, {"input": {"s": "bacabca"}, "output": 7}, {"input": {"s": "babbccccbac"}, "output": 7}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "b53c10b77c11b52f"}
vcr_422c63738a3a
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, -25, -12, 2, 7, 19, 35], "target": -18}, "output": [1, 4]}, {"input": {"nums": [-14, -11, -2, 29], "target": -25}, "output": [0, 1]}, {"input": {"nums": [-17, -16, -15, -8, 0, 16, 22, 24, 29, 32], "target": 15}, "output": [0, 9]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "e5f7d0feb6d443aa"}
vcr_6c0130b543c7
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": [50, 9, 29, 37, 58, 54]}, "output": 49}, {"input": {"prices": [11, 6, 26, 13, 33, 5, 14, 39]}, "output": 34}, {"input": {"prices": [18, 28, 5, 17, 15, 37, 15]}, "output": 32}]
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": "e09536fd3c9ce6ea"}
vcr_30309de6a3c7
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-2, -11, -8, 20, 15, -4, -11, 17], "k": 6}, "output": [-8, 20, 15, -4, -11, 17, -2, -11]}, {"input": {"nums": [4, -10, -10, -7], "k": 6}, "output": [-10, -7, 4, -10]}, {"input": {"nums": [10, -4, -7, 9], "k": 18}, "output": [-7, 9, 10, -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": "6366f62e646d808f"}
vcr_c83fc72a27bd
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [36, 5, 19, 8, 28, 19, 36, 17, 74, 8, 1, 1, 4, 5, 4, 17, 28]}, "output": 74}, {"input": {"nums": [9, 3, 19, 53, 3, 19, 24, 9, 24]}, "output": 53}, {"input": {"nums": [12, 13, 47, 13, 26, 12, 26]}, "output": 47}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "4b54c01c00f0e5f8"}