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_3e331689d29f
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["cc", "bbab", "bccb", "ca"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aab", "aaa", "bab", "baa", "abb"]}, "output": [1, 2, 2]}, {"input": {"strs": ["bbb", "bab", "aba", "baa", "baa"]}, "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": "3d0b48a70f84957e"}
vcr_617afcb08015
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": "cbcadcdacbc"}, "output": true}, {"input": {"s": "bccba"}, "output": false}, {"input": {"s": "babcaaba"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "72ac5160b4257f44"}
vcr_b14ea9067a43
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": 246, "b": 223}, "output": 1}, {"input": {"a": 91, "b": 21}, "output": 7}, {"input": {"a": 75, "b": 3}, "output": 3}]
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": "d9999e3bdfa0db06"}
vcr_890a1d96a02a
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -23, 8, 14, 16, 41, 42], "target": -7}, "output": [1, 4]}, {"input": {"nums": [-15, -9, 20, 21, 36, 37], "target": 21}, "output": [0, 4]}, {"input": {"nums": [-11, -8, 4, 31, 33], "target": 23}, "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": "40bac25f73bdb6ce"}
vcr_0a9aad48536b
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, -26, -20, 16, 21, 39, 45], "target": 19}, "output": [1, 6]}, {"input": {"nums": [-4, 1, 4, 19, 20], "target": 0}, "output": [0, 2]}, {"input": {"nums": [7, 20, 27, 39], "target": 66}, "output": [2, 3]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "c87402c5a810a79b"}
vcr_a4a867f42486
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": ["acb", "ca", "aabc", "abbb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["abb", "aba", "bbb", "abb", "bbb", "aba"]}, "output": [2, 2, 2]}, {"input": {"strs": ["abb", "bab", "aab", "bab", "baa", "bab", "aba"]}, "output": [3, 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": "f0ef799765c7c8af"}
vcr_966e36886f5a
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, 43, 14, 56, 27, 40, 41, 35]}, "output": 42}, {"input": {"prices": [19, 2, 14, 8, 4, 18, 29, 4, 20]}, "output": 27}, {"input": {"prices": [6, 25, 38, 11]}, "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": "2e4a7ddfb3e27f1e"}
vcr_d64f932146e8
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": 6109}, "output": 10}, {"input": {"n": 1407}, "output": 9}, {"input": {"n": 13820}, "output": 10}]
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": "d456fa360a8a0b40"}
vcr_32598e9016ef
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, 2, 6, 7, 3, 4, 6]}, "output": [6048, 18144, 6048, 5184, 12096, 9072, 6048]}, {"input": {"nums": [3, 1, 3, 4, 5]}, "output": [60, 180, 60, 45, 36]}, {"input": {"nums": [2, 1, 3]}, "output": [3, 6, 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": "674a6de645480269"}
vcr_24f71bf3c02f
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 4, 3, 0, 4, 2, 1, 4, 4, 0]}, "output": true}, {"input": {"nums": [3, 0, 2]}, "output": true}, {"input": {"nums": [0, 3, 3, 0, 1, 3, 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": "9a506c1b40774f66"}
vcr_6872cbac46b9
medium
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "ddaccabaccadd"}, "output": true}, {"input": {"s": "cbabcbbcab"}, "output": false}, {"input": {"s": "bbcab"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "34927636e64e7eaf"}
vcr_b3c3c7d2a5ca
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, -3, -11, -10, 16, 18, -8, -8, -14]}, "output": 34}, {"input": {"nums": [13, 1, 0, 9, -2, 4, 0, 0, 7, 9, -6]}, "output": 41}, {"input": {"nums": [-2, -11, 8, -12, -10, 3, -6]}, "output": 8}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "a972e3d5d40a52fd"}
vcr_d4a70919844f
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, -28, -18, -13, -12, -6, -5, 2, 12, 17, 25, 39], "target": 4}, "output": [3, 9]}, {"input": {"nums": [-19, -11, 6, 7, 9, 10, 15, 28, 29, 30, 32], "target": 21}, "output": [1, 10]}, {"input": {"nums": [-16, -13, -1, 1, 4, 6, 16, 20, 21, 27], "target": -12}, "output": [0, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "b9b080e903358bb0"}
vcr_582ca9616eca
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": [69, 135, 43, 143, 45, 114, 104, 25, 159, 168, 137, 123]}, "output": false}, {"input": {"nums": [10, 8, 12]}, "output": false}, {"input": {"nums": [19, 9, 4, 24, 4, 19, 6, 16, 21, 22, 21, 9]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "7cf3b00704b4aa36"}
vcr_724247866242
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [7, 28, 5, 29, 3, 5, 16, 8, 25, 5, 27]}, "output": 125}, {"input": {"nums": [11, 0, 20, 17, 10, 1, 20, 0, 16]}, "output": 77}, {"input": {"nums": [1, 2, 1]}, "output": 2}]
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": "19b29404e1d02c19"}
vcr_8f64a84257ff
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["ab", "ccb", "bbc", "aaba", "acb", "aba", "cc"]}, "output": [1, 1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aba", "aba", "abb", "aaa", "bba", "abb"]}, "output": [1, 2, 3]}, {"input": {"strs": ["aab", "aab", "bab", "bbb", "aab", "aba", "bab"]}, "output": [1, 2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "a88b54293c663618"}
vcr_44bbac166daa
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "addcacaabcdcadcd"}, "output": false}, {"input": {"s": "caccbc"}, "output": false}, {"input": {"s": "aaaca"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "b2a6aedbe8b792a6"}
vcr_44583ecc4bf3
medium
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "ddabdddbadd"}, "output": true}, {"input": {"s": "cbccbb"}, "output": false}, {"input": {"s": "caacabcaa"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "dfdfa9e184a2ad30"}
vcr_b1416db818dd
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [21, 50, 39, 0, 5, 28, 15, 33, 17, 50, 8]}, "output": true}, {"input": {"nums": [11, 22, 5, 10, 22, 22, 23, 23, 2, 2, 10]}, "output": true}, {"input": {"nums": [6, 0, 9, 21, 24, 22]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "e743cbb777777fa7"}
vcr_a2c035e0e34e
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": [0, 7, 23, 9, 1, 26, 26, 16, 29]}, "output": 79}, {"input": {"nums": [5, 19, 10, 15, 13, 12, 4]}, "output": 46}, {"input": {"nums": [16, 2, 18, 1, 14, 1]}, "output": 48}]
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": "ab86809569e7dec8"}
vcr_c6ba7ec24952
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": "aabbbabbbabcbb"}, "output": 8}, {"input": {"s": "cbbccbca"}, "output": 6}, {"input": {"s": "acbaa"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "9c1d05b7b4ba745e"}
vcr_d71c585dbf3a
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [1, 1, 21, 23, 19, 3, 3, 7]}, "output": 48}, {"input": {"nums": [7, 5, 0, 14, 14, 9, 5, 12, 13]}, "output": 43}, {"input": {"nums": [18, 8, 7, 1, 8]}, "output": 33}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "c90b0f93dcf953dc"}
vcr_94a59e43ab3c
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [9, 10, 14, 25, 26, 28, 37, 39, 42], "target": 1}, "output": 0}, {"input": {"nums": [1, 4, 16, 26, 31], "target": 30}, "output": 4}, {"input": {"nums": [1, 2, 10, 15, 17, 22, 24, 25, 36, 38], "target": 23}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "7817f52a4c655623"}
vcr_3766d0c573af
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": 33050}, "output": 5}, {"input": {"n": 15248}, "output": 7}, {"input": {"n": 5255}, "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": "c4eb875f31b27796"}
vcr_fb702433d2ff
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": [-21, -11, -10, -1, 2, 3, 5, 31, 35, 48], "target": 25}, "output": [2, 8]}, {"input": {"nums": [-11, -8, -4, 13, 16, 19, 28, 34], "target": 12}, "output": [2, 4]}, {"input": {"nums": [-8, -4, 0, 7, 11, 19, 20, 24, 29, 30, 31], "target": 16}, "output": [0, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "33c8d70720ded6ba"}
vcr_151b4644d888
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 5, 5, 10, 12, 15, 15]}, "output": [3, 5, 10, 12, 15]}, {"input": {"nums": [0, 5, 6, 9, 11]}, "output": [0, 5, 6, 9, 11]}, {"input": {"nums": [0, 2, 5, 7, 7, 8, 11, 12]}, "output": [0, 2, 5, 7, 8, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "b9808efb97bf41d8"}
vcr_6da88683e6b7
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": [-21, 8, 14, 15, 22, 30, 33, 39, 40, 42, 49], "target": 55}, "output": [3, 8]}, {"input": {"nums": [-19, -15, -14, 25], "target": -34}, "output": [0, 1]}, {"input": {"nums": [-10, -4, -3, -2, 10, 13, 15, 22], "target": 25}, "output": [4, 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": "6695c8155fbdd533"}
vcr_b67ec65db223
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, 4, 2, 2, 5, 7, 2]}, "output": [1120, 1120, 2240, 2240, 896, 640, 2240]}, {"input": {"nums": [4, 1, 4]}, "output": [4, 16, 4]}, {"input": {"nums": [1, 4, 2, 5, 3]}, "output": [120, 30, 60, 24, 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": "c19a30ccf9745547"}
vcr_0918dd66c59e
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": [15, 25, 31, 34, 39, 45, 47], "target": 38}, "output": 4}, {"input": {"nums": [1, 4, 6, 16, 21, 27], "target": 31}, "output": 6}, {"input": {"nums": [3, 10, 20, 21, 24, 26, 30, 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": "7f1c823e532ba4ab"}
vcr_94f01d1b00b6
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, 11, 39, 45], "target": 22}, "output": 2}, {"input": {"nums": [10, 13, 16, 22, 25, 27], "target": 4}, "output": 0}, {"input": {"nums": [3, 6, 12, 16, 17, 21, 23, 24, 31, 37, 38], "target": 7}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "ab75f84f7fb18395"}
vcr_d2ff1ef6e1bb
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 3, 5, 5, 6, 6, 7, 10, 13, 13]}, "output": [0, 2, 3, 5, 6, 7, 10, 13]}, {"input": {"nums": [7, 8, 11]}, "output": [7, 8, 11]}, {"input": {"nums": [0, 1, 2, 2, 8, 8, 9, 12]}, "output": [0, 1, 2, 8, 9, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "896c3c04d36e3fef"}
vcr_53b24f861d3c
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-12, -9, 4, 16, -3, 12, 21, -10, -17, 19, 5, 22]}, "output": 69}, {"input": {"nums": [-8, 1, -10]}, "output": 1}, {"input": {"nums": [2, 5, 8, -7, -6, -5, -6, 4, 12, 13]}, "output": 29}]
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": "f8cdfa7257169b91"}
vcr_fdaca3cbc197
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [25, 21, 5, 11]}, "output": 36}, {"input": {"nums": [10, 13, 10]}, "output": 20}, {"input": {"nums": [12, 12, 0]}, "output": 12}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "8f1222a4a010dfb4"}
vcr_1b8a60935450
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, 18, 21, 35, 37, 43, 49], "target": 29}, "output": 4}, {"input": {"nums": [4, 10, 11, 13, 16, 28, 30, 32, 35], "target": 19}, "output": 5}, {"input": {"nums": [9, 11, 12, 25], "target": 19}, "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": "859b6bc24d29c7a9"}
vcr_ad640c8f0fb3
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": [13, 5, -18, 15, -2, -2, 20]}, "output": 31}, {"input": {"nums": [0, 3, 4, 1, 4, -11, -9, 5, -9, 5, -4]}, "output": 12}, {"input": {"nums": [-11, -5, 9, -3, -7, 6, 8, -2, 11]}, "output": 23}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "231e217d1aea7101"}
vcr_ad97cbdf51ed
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": [58, 49, 12, 9, 44, 6, 11, 48, 8, 43, 4, 2, 8, 29]}, "output": 42}, {"input": {"prices": [10, 36, 25]}, "output": 26}, {"input": {"prices": [2, 13, 22, 1, 12, 16, 38, 35, 36, 38, 37]}, "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": "86be6603e324c4ee"}
vcr_63895bb3533d
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": 70, "b": 123}, "output": 1}, {"input": {"a": 153, "b": 27}, "output": 9}, {"input": {"a": 157, "b": 160}, "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": "f2fab8cf6a890edb"}
vcr_a5b6e343b875
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, 16, 15, -13, -18, 10, 4, -15, 16, 17, 7, -3, 10, -1]}, "output": 67}, {"input": {"nums": [6, 10, 11, 5, 0, 6, 1, -5, 14, -4]}, "output": 48}, {"input": {"nums": [7, 0, -5]}, "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": "0ab47fb607ad104c"}
vcr_45da854fa74b
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [59, 25, 62, 18, 70, 17, -40], "target": 95}, "output": [1, 4]}, {"input": {"nums": [21, 27, 41, 46, 8, 9, -28, 4, -29], "target": -20}, "output": [4, 6]}, {"input": {"nums": [-9, -29, 28, -24, 21], "target": -53}, "output": [1, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "f66845c9feeabc04"}
vcr_1c16ec2f8a65
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [7, 11, 12, 13, 14, 15, 15]}, "output": [7, 11, 12, 13, 14, 15]}, {"input": {"nums": [0, 0, 2, 2, 4, 6, 8, 9, 9, 11, 12]}, "output": [0, 2, 4, 6, 8, 9, 11, 12]}, {"input": {"nums": [7, 8, 11]}, "output": [7, 8, 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": "934e6d8d8f4da541"}
vcr_93aa357690a2
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [12, 2, 21, 19, 9, 19]}, "output": [12, 14, 35, 54, 63, 82]}, {"input": {"nums": [11, 1, 0, 14, 1, 16, 10, 5, -5]}, "output": [11, 12, 12, 26, 27, 43, 53, 58, 53]}, {"input": {"nums": [13, -5]}, "output": [13, 8]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "f45faa5502e71f11"}
vcr_28215422011e
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": [16, 29, 42, 2, 47, 33, 7]}, "output": 45}, {"input": {"prices": [40, 6, 3, 3, 16, 13, 33]}, "output": 30}, {"input": {"prices": [22, 38, 29, 25]}, "output": 16}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "4cd19e7e2c13cb2c"}
vcr_85709cc75f54
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": [12, 190, 162, 146, 88, 35, 107, 147, 138, 108, 63, 21, 25]}, "output": false}, {"input": {"nums": [24, 9, 2, 12, 21, 21, 21, 13, 10]}, "output": true}, {"input": {"nums": [1, 4, 25, 16]}, "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": "7cc7964fa54dccc6"}
vcr_da6d541f5462
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, 9, 9, 10, 12, 13, 14, 14]}, "output": [2, 9, 10, 12, 13, 14]}, {"input": {"nums": [0, 1, 3]}, "output": [0, 1, 3]}, {"input": {"nums": [5, 5, 6, 7, 11]}, "output": [5, 6, 7, 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": "cd8dd47be43d865d"}
vcr_70b7877564a6
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": [2, -8, -8, -4, 8, 4, 8, -1, -17, 10, 16, -18, -4]}, "output": 28}, {"input": {"nums": [1, 1, 5, 9, 0, 7, -7]}, "output": 23}, {"input": {"nums": [-3, 9, 8, -3, 6, -9, 1, -4]}, "output": 20}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "d935c280cd745a86"}
vcr_7dcdf436827c
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": [-28, -27, -24, -16, -12, -6, -5, 1, 10, 15, 25], "target": -9}, "output": [2, 9]}, {"input": {"nums": [-19, -12, -7, -6, 6, 15, 26, 38], "target": 9}, "output": [3, 5]}, {"input": {"nums": [-20, -19, -7, 7, 22], "target": -39}, "output": [0, 1]}]
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": "0381adb699d0e8ea"}
vcr_1072113c3c4d
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, 11, 17, 19, 30, 31, 34, 48], "target": 47}, "output": 8}, {"input": {"nums": [4, 9, 13, 16, 19, 22, 26, 31, 35, 39], "target": 19}, "output": 4}, {"input": {"nums": [2, 3, 8, 9, 12, 17, 20, 24, 31, 35, 37], "target": 26}, "output": 8}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "16bfda9d7c7da912"}
vcr_9da5cb2af4cf
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [4, 8, 2, 20], "k": 4}, "output": [4, 8, 2, 20]}, {"input": {"nums": [6, 1, 5, -9, -2, -2, 7, -1], "k": 17}, "output": [-1, 6, 1, 5, -9, -2, -2, 7]}, {"input": {"nums": [1, 9, -1, -4, -6, 7], "k": 11}, "output": [9, -1, -4, -6, 7, 1]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "fbc25999dca9a03e"}
vcr_4f140a9d4f8a
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": [28, -25, -24, 0, 34, 15, 12, 15, 13, -15, 6, 25, 24]}, "output": [28, 3, -21, -21, 13, 28, 40, 55, 68, 53, 59, 84, 108]}, {"input": {"nums": [-2, 14, -5, 8, 17, 12]}, "output": [-2, 12, 7, 15, 32, 44]}, {"input": {"nums": [-4, -6]}, "output": [-4, -10]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "7e5a4ca517120a65"}
vcr_49951f63870d
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": [179, 109, 78, 143, 19, 188, 195, 182, 22, 96, 98, 137]}, "output": false}, {"input": {"nums": [16, 15, 1, 13, 4, 13, 12]}, "output": true}, {"input": {"nums": [5, 2, 23, 15, 10, 25, 0, 8, 9, 20, 16]}, "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": "e58870232747a294"}
vcr_aa0f14919551
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": [0, 9, 9]}, "output": 9}, {"input": {"nums": [3, 8, 2, 15]}, "output": 23}, {"input": {"nums": [0, 7, 11]}, "output": 11}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "0c4a05b0e445df9d"}
vcr_9c8a8655fae2
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": [13, 14, 16, 24, 26, 44, 45, 48], "target": 50}, "output": 8}, {"input": {"nums": [2, 3, 5, 7, 13, 16, 23, 24, 27, 28, 32, 37], "target": 37}, "output": 11}, {"input": {"nums": [1, 8, 14, 20, 21, 27, 28, 39], "target": 4}, "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": "a553ff2be7d80e5d"}
vcr_c74ad6e8f054
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, 14, 20, 59, 22, 32, 51, 39, 30, 8, 44, 7, 8]}, "output": 45}, {"input": {"prices": [34, 23, 35, 7, 34]}, "output": 27}, {"input": {"prices": [34, 20, 13, 1, 37]}, "output": 36}]
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": "93fe1f5ada6ce324"}
vcr_76e5b9ed765b
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", "bbac", "ccaa", "ccb", "ab", "ccbc", "cac", "bca", "ccc"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["abb", "aaa", "baa", "abb", "aba", "abb", "aba"]}, "output": [1, 3, 3]}, {"input": {"strs": ["aab", "bab", "bab", "aab", "aaa", "baa", "baa"]}, "output": [1, 2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "2bc164cba0fdd917"}
vcr_746d0acc71db
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [7, 14, 18, 22, 25, 27, 32, 40, 41, 42, 44], "target": 21}, "output": 3}, {"input": {"nums": [3, 8, 9, 10, 11, 25, 28, 34, 37], "target": 28}, "output": 6}, {"input": {"nums": [5, 8, 10, 25, 27], "target": 11}, "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": "f8858cc983421158"}
vcr_2147b57b27f3
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [4, -24, -22, -17, -21, -6, 30, 5, 13, -2, 23, -3, 29, -7]}, "output": [4, -20, -42, -59, -80, -86, -56, -51, -38, -40, -17, -20, 9, 2]}, {"input": {"nums": [6, 11, 6, -10, -4, 17]}, "output": [6, 17, 23, 13, 9, 26]}, {"input": {"nums": [10, 18, -5, -5, 2, 17, 16, -2, 10]}, "output": [10, 28, 23, 18...
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "0b1388ebabf9b5c2"}
vcr_9dd087ea4c59
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": [-6, 67, 47, 72, 58, 20, -3, 9, 70, 77, -24], "target": 90}, "output": [5, 8]}, {"input": {"nums": [-9, 9, 46, -2], "target": 7}, "output": [1, 3]}, {"input": {"nums": [21, 44, 46, 15, -17, 12, 30], "target": 4}, "output": [0, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "e191557cdff4d552"}
vcr_fb7ea6206558
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": 161, "b": 208}, "output": 1}, {"input": {"a": 42, "b": 167}, "output": 1}, {"input": {"a": 179, "b": 82}, "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": "ea59e3996614401f"}
vcr_586e6d4a0a68
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, 15, 21, 19, 10, 14, 7, 27, 28]}, "output": 76}, {"input": {"nums": [0, 17, 6, 11, 12, 0, 19]}, "output": 48}, {"input": {"nums": [15, 1, 3, 3, 12]}, "output": 30}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "ced78a5c2f9e2ed3"}
vcr_f3bc84b42aa7
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": [16, -19, -13, -5, -17, -3], "k": 7}, "output": [-3, 16, -19, -13, -5, -17]}, {"input": {"nums": [-6, 10, 7, 3], "k": 16}, "output": [-6, 10, 7, 3]}, {"input": {"nums": [-10, 9, -1, 6, 6], "k": 7}, "output": [6, 6, -10, 9, -1]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "099bd372c27004bc"}
vcr_637d932f4798
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": "cbbadadcacbdacb"}, "output": false}, {"input": {"s": "caacccb"}, "output": false}, {"input": {"s": "acb"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "177204f32114aa07"}
vcr_17581caeecb5
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": [192, 169, 98, 93, 177, 64, 111, 68]}, "output": false}, {"input": {"nums": [23, 25, 13, 24, 23, 14, 5, 24]}, "output": true}, {"input": {"nums": [25, 21, 12, 12, 20, 14, 15]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "a45c404138f1f217"}
vcr_51ba266daecc
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, -4, 3, 8, 29, 42, 43], "target": 30}, "output": [0, 6]}, {"input": {"nums": [13, 30, 31, 34], "target": 44}, "output": [0, 2]}, {"input": {"nums": [-17, -11, -8, -3, 6, 9, 28, 38], "target": -8}, "output": [0, 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": "8f2bbd2a9616ecef"}
vcr_938ff5d986b3
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-8, -7, 3, 2, 4, 9, -10, -3, 0, -18, 19, -11, -2]}, "output": 19}, {"input": {"nums": [1, 2, 6, 12, 1, 2, -1]}, "output": 24}, {"input": {"nums": [-5, 14, 9, 2, -9, -5, -7, -3]}, "output": 25}]
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": "0c455d58380e32d3"}
vcr_0d3325168b57
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-18, 18, -7, -16, -16, 13, -14, 18, 20, 16, -5], "k": 6}, "output": [13, -14, 18, 20, 16, -5, -18, 18, -7, -16, -16]}, {"input": {"nums": [1, 0, -1, 6], "k": 6}, "output": [-1, 6, 1, 0]}, {"input": {"nums": [2, -7, -9, -4], "k": 2}, "output": [-9, -4, 2, -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": "a245dcb984293ec7"}
vcr_a96186e89df4
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 2, 4, 3, 6]}, "output": [144, 360, 180, 240, 120]}, {"input": {"nums": [2, 3, 2, 1, 1, 3, 2]}, "output": [36, 24, 36, 72, 72, 24, 36]}, {"input": {"nums": [5, 1, 1, 4]}, "output": [4, 20, 20, 5]}]
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": "d643bc0035b5760d"}
vcr_2ed554e7a9db
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": [7, 3, 29, -39, 40, -13, 54, 49, -11, 76, 57, -9, 38, 34], "target": 114}, "output": [9, 12]}, {"input": {"nums": [19, 20, -15, -30, 26, 33, 41, 22], "target": 42}, "output": [1, 7]}, {"input": {"nums": [-10, 45, -21, 11, -20, -30], "target": -19}, "output": [3, 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": "ff58bc5071b2b650"}
vcr_5aeeac9f535f
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["baba", "ccc", "cb", "ac"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bab", "bba", "bbb", "aba", "bab"]}, "output": [1, 1, 3]}, {"input": {"strs": ["abb", "bab", "aba", "bba", "bbb", "baa"]}, "output": [1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "dce6148e16257648"}
vcr_8d27e343293c
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": [19, 20, 15, 26, 16, 9, 16, 23, 16, 11]}, "output": 89}, {"input": {"nums": [0, 20, 1, 18, 15, 0, 12, 20, 9, 13]}, "output": 71}, {"input": {"nums": [6, 0]}, "output": 6}]
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": "ea3e3e6c20633c45"}
vcr_301bb9096fca
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, 5, 5, 5, 7]}, "output": [1750, 2625, 1050, 1050, 1050, 750]}, {"input": {"nums": [4, 1, 5, 4, 1, 3, 1]}, "output": [60, 240, 48, 60, 240, 80, 240]}, {"input": {"nums": [1, 2, 1, 5, 5, 1]}, "output": [50, 25, 50, 10, 10, 50]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "069b212ff4c6818d"}
vcr_bf7c6cc748f5
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": [12, 29, 39, 41, 44, 46, 48], "target": 16}, "output": 1}, {"input": {"nums": [1, 8, 24, 25], "target": 1}, "output": 0}, {"input": {"nums": [6, 26, 32, 38], "target": 1}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "dc2c16683484459f"}
vcr_ff0a2c393d88
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 3, 4, 6, 7, 13, 22, 33, 39, 40, 44, 48], "target": 41}, "output": 10}, {"input": {"nums": [0, 6, 10, 13, 15, 21, 22], "target": 9}, "output": 2}, {"input": {"nums": [2, 4, 6, 18, 20, 25, 32, 36], "target": 30}, "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": "8132900b2fbc4943"}
vcr_3e240cbfba3c
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [8, 22, 30, 4]}, "output": 38}, {"input": {"nums": [12, 1]}, "output": 12}, {"input": {"nums": [17, 3, 16, 14, 7, 11, 8, 19, 6, 16]}, "output": 79}]
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": "439334dcee01e286"}
vcr_47d88eaad082
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": [45, 13, 13, 22, 41, 39, 59, 47, 36, 46, 8, 58, 17, 8, 43, 52]}, "output": 50}, {"input": {"prices": [22, 27, 37, 13, 7, 30, 26]}, "output": 23}, {"input": {"prices": [23, 24, 24, 33, 24, 4, 20, 8]}, "output": 16}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "77a1a9c4fd24b3a1"}
vcr_8e883358a74b
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": [-3, -14, 1, 17, 17, 19, -9]}, "output": 54}, {"input": {"nums": [-5, 7, 7]}, "output": 14}, {"input": {"nums": [6, -11, 10, -8]}, "output": 10}]
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": "a3bc857c880076c5"}
vcr_eeff7ab70bee
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": [54, 61, 52, 0, 2, -2, -19, 33, -13, 56], "target": 2}, "output": [3, 4]}, {"input": {"nums": [-28, 40, 32, -1], "target": 12}, "output": [0, 1]}, {"input": {"nums": [24, 10, -4, -26, 32, 11], "target": -16}, "output": [1, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "1ebd34b43fe62330"}
vcr_4a53a216ca71
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": [29, 12, 29, 24, 58, 30, 12, 24, 30, 39, 39]}, "output": 58}, {"input": {"nums": [18, 8, 18, 14, 29, 14, 5, 45, 8, 5, 29]}, "output": 45}, {"input": {"nums": [10, 24, 46, 24, 10]}, "output": 46}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "cd2c3d6a1bb1ba32"}
vcr_934205e114f4
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "abdcccdba"}, "output": true}, {"input": {"s": "acbbbb"}, "output": false}, {"input": {"s": "bbaaaccaba"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "631b281230971af4"}
vcr_318e6966b73d
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [43, 10, 48, 37, 6, 4]}, "output": 38}, {"input": {"prices": [16, 26, 16, 9, 7, 11, 25]}, "output": 18}, {"input": {"prices": [29, 37, 28, 15, 15, 10, 19]}, "output": 9}]
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": "eb9bfa0e208204c2"}
vcr_f6c4912240c7
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-3, -19, -8, -7, 2, 4], "k": 1}, "output": [4, -3, -19, -8, -7, 2]}, {"input": {"nums": [-8, -3, 6, -8, 4, 10, -8, 1], "k": 7}, "output": [-3, 6, -8, 4, 10, -8, 1, -8]}, {"input": {"nums": [0, -3, 7, -2, 4, 10, 10, -3, 8], "k": 9}, "output": [0, -3, 7, -2, 4, 10, 10, -3, 8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "1f3068cb7ad5e139"}
vcr_31a1c57645bc
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": [-13, 6, 15, 22, -2, -2, 5, 20, -17, -7, 6, 8, -1, 17, 8]}, "output": 78}, {"input": {"nums": [-5, 3, -7, 9]}, "output": 9}, {"input": {"nums": [-9, -6, -12, 11, -3, 1, 11, 1, -2]}, "output": 21}]
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": "e272209f2ea945e4"}
vcr_e7172090f377
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [61, 54, 80, 1, 102, 146, 184, 65, 162]}, "output": false}, {"input": {"nums": [8, 4, 0, 16, 12, 19, 24]}, "output": false}, {"input": {"nums": [6, 4, 2, 21, 5, 20, 8, 7, 15]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "1280e899195cbbf1"}
vcr_3f2e3574c17c
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [34, 36, 18, 19, 5, 14, 14, 36, 68, 34, 5, 19, 18]}, "output": 68}, {"input": {"nums": [12, 17, 12, 11, 4, 48, 17, 11, 2, 2, 4]}, "output": 48}, {"input": {"nums": [9, 9, 52, 25, 24, 25, 24]}, "output": 52}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "4224f9ad40908d86"}
vcr_8545c6272c64
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": "ad e ecdd eacace dd abcdeb ad"}, "output": "ad abcdeb dd eacace ecdd e ad"}, {"input": {"s": "aba abab aaba bba"}, "output": "bba aaba abab aba"}, {"input": {"s": "a abba abb aaa"}, "output": "aaa abb abba a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "09ff5ddb035dcc22"}
vcr_34f4bacfeec0
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": [-16, 11, -7, 20, 15, 12, 18, 11], "k": 2}, "output": [18, 11, -16, 11, -7, 20, 15, 12]}, {"input": {"nums": [5, 10, -4, 6], "k": 15}, "output": [10, -4, 6, 5]}, {"input": {"nums": [1, -6, -10, -2, 10, -10, 10, -1, -4, -10], "k": 3}, "output": [-1, -4, -10, 1, -6, -10, -2, 10, -10, 10]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "50d4769e0afdff42"}
vcr_1029dd82c7d5
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, -20, -18, -13, -10, -3, 7, 24, 25], "target": -13}, "output": [2, 7]}, {"input": {"nums": [-13, -12, -5, -4, 4], "target": -16}, "output": [1, 3]}, {"input": {"nums": [-8, -5, 12, 27], "target": 39}, "output": [2, 3]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "56ee792c61785951"}
vcr_9756e4477d4e
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 3, 4, 0, 2, 1, 2, 2, 0]}, "output": true}, {"input": {"nums": [1, 3, 2, 0]}, "output": true}, {"input": {"nums": [3, 0, 2, 0, 1, 0, 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": "6b3b9d1f04777d9d"}
vcr_2400946f2860
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": [-23, -16, -1, -2, 32]}, "output": [-23, -39, -40, -42, -10]}, {"input": {"nums": [-8, 3, -1, 3, 8, 3, -3]}, "output": [-8, -5, -6, -3, 5, 8, 5]}, {"input": {"nums": [3, -12, 6, -10, 4, -5]}, "output": [3, -9, -3, -13, -9, -14]}]
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": "0cfe689a77d91b56"}
vcr_ac76d89a7e8d
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 1, 2, 0, 1, 3, 3, 0]}, "output": true}, {"input": {"nums": [3, 3, 1, 0, 3, 2, 1, 0]}, "output": true}, {"input": {"nums": [0, 3, 0, 2, 2, 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": "8069cb805f5765d8"}
vcr_a261aa91d7e7
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": [39, 24, 58, 6, 14, 11, 29]}, "output": 34}, {"input": {"prices": [40, 14, 36, 21, 34, 1, 24]}, "output": 23}, {"input": {"prices": [5, 4, 30, 25, 13, 12, 17, 27, 25, 11, 24, 20]}, "output": 26}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "64962eb9ce915935"}
vcr_5b4a79f56a94
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": ["ccac", "ab", "ba", "ccbb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bab", "bab", "aab", "aab"]}, "output": [2, 2]}, {"input": {"strs": ["aba", "aba", "abb", "aab", "bab", "baa", "abb"]}, "output": [3, 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": "45cb368c8df7726d"}
vcr_de52b7e2947f
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": ["abbb", "bc", "caab", "aab", "abcb", "ccc", "ccbc", "abcb", "bbcb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["baa", "bba", "aab"]}, "output": [1, 2]}, {"input": {"strs": ["aaa", "abb", "bba", "bba", "aba"]}, "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": "acb77ab81c5b2747"}
vcr_8ce3476aa04b
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": [-17, 12, 15, 20, 8, 0, -2, 16, 17], "k": 25}, "output": [15, 20, 8, 0, -2, 16, 17, -17, 12]}, {"input": {"nums": [1, -2, 5, 1, -5, -8, 1], "k": 5}, "output": [5, 1, -5, -8, 1, 1, -2]}, {"input": {"nums": [9, -2, -2, 3, -9, -9], "k": 1}, "output": [-9, 9, -2, -2, 3, -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": "611457f05a27f0d2"}
vcr_b79d51790c83
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": "aaccbbbccaabcc"}, "output": 7}, {"input": {"s": "acbbaccaacc"}, "output": 7}, {"input": {"s": "cbcba"}, "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": "17ae71b354ed45bd"}
vcr_a8c00f5504f1
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [1, 1, 14, 35, 27, 11, 25, 34, 27, 14, 25, 60, 19, 34, 35, 11, 19]}, "output": 60}, {"input": {"nums": [7, 1, 24, 27, 9, 41, 7, 27, 24, 1, 13, 9, 13]}, "output": 41}, {"input": {"nums": [1, 49, 24, 24, 1, 4, 4]}, "output": 49}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "14340d33242ede95"}
vcr_363785c29722
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": [61, 20, 4, 4, 20, 23, 23]}, "output": 61}, {"input": {"nums": [20, 15, 13, 18, 50, 18, 17, 17, 13, 15, 20]}, "output": 50}, {"input": {"nums": [16, 22, 11, 44, 22, 16, 11]}, "output": 44}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "77c49e9e637b4b9b"}
vcr_63080438bf08
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "ab bbdcea ccdeb b adceda bcab c"}, "output": "c bcab adceda b ccdeb bbdcea ab"}, {"input": {"s": "aba aab aa a bbbb"}, "output": "bbbb a aa aab aba"}, {"input": {"s": "aaa aaa aa b"}, "output": "b aa aaa aaa"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "610ef0cd3e6ac9ba"}
vcr_9cf1997db998
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": [59, 11, 70, 57, 33], "target": 81}, "output": [1, 2]}, {"input": {"nums": [46, -19, 17, 0, -1, 34, -11], "target": 16}, "output": [2, 4]}, {"input": {"nums": [10, -17, -19, 19], "target": -36}, "output": [1, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "87c1c94edda87366"}
vcr_a5be19017b9d
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": 101, "b": 132}, "output": 1}, {"input": {"a": 34, "b": 26}, "output": 2}, {"input": {"a": 64, "b": 70}, "output": 2}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "acdcc5074933071c"}
vcr_9f36ff9a54ce
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-33, 77, 60, 67, -7, -6, 58, 44, 56, -26, 4], "target": 81}, "output": [1, 10]}, {"input": {"nums": [44, -6, -7, 26, 36, -17, -3], "target": -20}, "output": [5, 6]}, {"input": {"nums": [2, 47, 9, -14], "target": 11}, "output": [0, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "433175901aea30eb"}