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_042adda794b9
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": [9, 3, 17, 9, 14, 10, 55, 10, 17, 3, 14]}, "output": 55}, {"input": {"nums": [2, 11, 11, 28, 16, 7, 28, 29, 16, 7, 2, 47, 29]}, "output": 47}, {"input": {"nums": [6, 19, 47, 12, 6, 3, 19, 12, 3]}, "output": 47}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "6b1ccf20f27edc2e"}
vcr_038be6d9cde1
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": [8, 23, 4, 6, 28, 27, 12]}, "output": [8, 31, 35, 41, 69, 96, 108]}, {"input": {"nums": [18, -5]}, "output": [18, 13]}, {"input": {"nums": [14, -1, -6, 7, 4, -5, 10, 4, -8]}, "output": [14, 13, 7, 14, 18, 13, 23, 27, 19]}]
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": "e2d95e5d4ed09fdd"}
vcr_9ae8268eff43
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [18, 19, 2, 22, 22, 12, 9, 18, 12, 22, 27]}, "output": 98}, {"input": {"nums": [6, 11, 18, 4, 2, 19, 0, 19]}, "output": 62}, {"input": {"nums": [0, 10, 6, 15, 12, 19, 17, 15]}, "output": 59}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "b78f77b4852e1378"}
vcr_9ec2546afde2
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, 0, 0, 3, 3, 3, 2, 3, 1, 0]}, "output": false}, {"input": {"nums": [1, 2, 1, 1, 0, 3, 3, 0]}, "output": false}, {"input": {"nums": [3, 0, 3, 0, 2, 2, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "8b4550d6bafcc743"}
vcr_4572c550c6e4
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, 1, 2, 7, 1, 6]}, "output": [168, 504, 1008, 504, 144, 1008, 168]}, {"input": {"nums": [5, 4, 3, 3]}, "output": [36, 45, 60, 60]}, {"input": {"nums": [2, 3, 2, 2, 2, 3]}, "output": [72, 48, 72, 72, 72, 48]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "02e1d8ab95cc99fa"}
vcr_b505d177746e
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [3, 5, 29, 15, 18, 4, 4, 18, 14, 15, 18, 11]}, "output": 94}, {"input": {"nums": [19, 13, 15, 20, 5, 2]}, "output": 41}, {"input": {"nums": [14, 17, 12, 20, 6, 17, 10]}, "output": 54}]
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": "1074fe06f7bb9112"}
vcr_123871ab7f59
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": "bccbabddddbabccb"}, "output": true}, {"input": {"s": "cabbca"}, "output": false}, {"input": {"s": "cbccacbb"}, "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": "a94c86aa942dedac"}
vcr_5982fc0a9a1f
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [20, 20, 6, 19, 20, 12, 30, 22, 28, 5]}, "output": 104}, {"input": {"nums": [1, 14, 6]}, "output": 14}, {"input": {"nums": [7, 18, 10, 1, 4, 8]}, "output": 27}]
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": "caa7b5f7f906b053"}
vcr_91ef826aff82
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", "cb", "bcc", "ac"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aab", "bab", "aab"]}, "output": [1, 2]}, {"input": {"strs": ["aab", "aab", "baa", "bba"]}, "output": [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": "7c5936620e242286"}
vcr_958b37db6944
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [3, 15, 16, 18, 9, 0, 1, 22]}, "output": 55}, {"input": {"nums": [1, 4, 4, 14, 4, 7, 15]}, "output": 33}, {"input": {"nums": [10, 14, 11, 4, 17, 13, 18, 17]}, "output": 56}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "78e31586fb846aaa"}
vcr_7b7b7ca6f816
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": [34, 2, 8, 24, 31, 22, -15]}, "output": [34, 36, 44, 68, 99, 121, 106]}, {"input": {"nums": [8, -11, 2, -6]}, "output": [8, -3, -1, -7]}, {"input": {"nums": [-3, -2, 3, 2, 2, 12, -7, -6]}, "output": [-3, -5, -2, 0, 2, 14, 7, 1]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "3e86c22b78f1ae2f"}
vcr_59174377b999
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 3, 8, 15, 21, 31, 35, 42, 45], "target": 45}, "output": 8}, {"input": {"nums": [7, 8, 18, 24, 28, 30, 33, 39], "target": 14}, "output": 2}, {"input": {"nums": [1, 5, 16, 17, 30, 34, 37, 39], "target": 13}, "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": "f80df867132c9fa0"}
vcr_e0426e99941f
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": 53493}, "output": 9}, {"input": {"n": 5956}, "output": 6}, {"input": {"n": 6526}, "output": 9}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "c78cb5f68b2c07da"}
vcr_b4ed5105edeb
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": "cbb aebdbd cda eddebb bae"}, "output": "bae eddebb cda aebdbd cbb"}, {"input": {"s": "b aa a ab"}, "output": "ab a aa b"}, {"input": {"s": "a baa ba a b"}, "output": "b a ba baa 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": "cf7dcf8c80e0ea6e"}
vcr_d53abd741f40
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": 8021}, "output": 9}, {"input": {"n": 12719}, "output": 9}, {"input": {"n": 15316}, "output": 9}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "ce457119d78ec916"}
vcr_329bb57d0508
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": [19, 15, 9, 63, 15, 19, 34, 9, 34, 22, 5, 39, 5, 22, 39]}, "output": 63}, {"input": {"nums": [48, 20, 23, 15, 8, 23, 11, 21, 20, 8, 15, 11, 21]}, "output": 48}, {"input": {"nums": [28, 8, 5, 13, 50, 21, 21, 8, 13, 28, 12, 12, 5]}, "output": 50}]
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": "da68819e8ac2085a"}
vcr_783642ce8272
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": ["baa", "bb", "aa", "cab", "bcb", "ccac"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["baa", "bab", "abb", "aba"]}, "output": [2, 2]}, {"input": {"strs": ["bbb", "bab", "bbb", "abb", "aab"]}, "output": [1, 2, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "8e54355f4f5d3799"}
vcr_6b32424c3dcc
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": ["ccba", "acb", "ca", "cca", "bca", "aaca", "cca"]}, "output": [1, 1, 1, 2, 2]}, {"input": {"strs": ["aba", "bba", "aaa", "abb", "bba", "abb"]}, "output": [1, 1, 4]}, {"input": {"strs": ["bba", "bab", "baa", "bab", "baa", "aaa"]}, "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": "a84fb27f0b94c865"}
vcr_da92d3700579
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": [29, 18, 30, 50]}, "output": 32}, {"input": {"prices": [3, 21, 26, 2, 40, 25, 37, 7]}, "output": 38}, {"input": {"prices": [24, 3, 14, 34, 12, 17, 8, 24, 5, 28, 16]}, "output": 31}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "36e8ba5a42122ac0"}
vcr_9042814724de
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [14, 6, 44, 5, 29, 9, 1, 25, 41, 47, 5]}, "output": true}, {"input": {"nums": [7, 4, 22, 3, 16]}, "output": false}, {"input": {"nums": [11, 18, 17]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "3969a75e017c89a7"}
vcr_093611234858
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": "eddcda eea bcbb cc ccae ebb eaded"}, "output": "eaded ebb ccae cc bcbb eea eddcda"}, {"input": {"s": "aaba baaa"}, "output": "baaa aaba"}, {"input": {"s": "b bbb aa baab ab"}, "output": "ab baab aa bbb b"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "cfbe415d27c0bb32"}
vcr_bbb12d4e28cd
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [50, 42, 42, 29, 38, 25, 1, 32, 47, 1, 39, 21, 24]}, "output": 46}, {"input": {"prices": [11, 35, 25, 18, 30, 39, 18]}, "output": 28}, {"input": {"prices": [5, 2, 18, 8, 16, 1, 8, 33, 17, 39, 21, 30]}, "output": 38}]
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": "4e7d3ebc879efbbe"}
vcr_d9303af436ab
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, 11, 14]}, "output": 22}, {"input": {"nums": [6, 15, 10]}, "output": 16}, {"input": {"nums": [13, 14, 20, 8, 2, 5, 2, 5]}, "output": 43}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "fc880079c7bf71aa"}
vcr_b40c48544bee
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": "ccbdcbddbaab"}, "output": false}, {"input": {"s": "cbb"}, "output": false}, {"input": {"s": "babcbaa"}, "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": "d495b43b3d4b0d3e"}
vcr_99a7e9ec57c5
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [4, 6, 3, 3, 4, 7, 2, 2]}, "output": [6048, 4032, 8064, 8064, 6048, 3456, 12096, 12096]}, {"input": {"nums": [3, 3, 2, 5, 1]}, "output": [30, 30, 45, 18, 90]}, {"input": {"nums": [1, 3, 5, 4]}, "output": [60, 20, 12, 15]}]
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": "59d3c10d44c6155c"}
vcr_4512b5f98628
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [54, 55, 4, 6, 60, 15, 16, 31, 22, 58, 2, 51, 42, 35, 2, 41]}, "output": 56}, {"input": {"prices": [25, 28, 16, 22, 13, 40, 7, 17, 37, 19]}, "output": 30}, {"input": {"prices": [9, 10, 4, 3]}, "output": 1}]
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": "d94fc3fec216b085"}
vcr_7c65017d879f
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": "debc ececdd bbecb"}, "output": "bbecb ececdd debc"}, {"input": {"s": "abba b b b baab"}, "output": "baab b b b abba"}, {"input": {"s": "a baa ba abbb bb"}, "output": "bb abbb ba baa 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": "57c3c8067356eee2"}
vcr_dd8b09a8dadd
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [33, -4, 11, 17, -24, -16, 5, 31, -5]}, "output": [33, 29, 40, 57, 33, 17, 22, 53, 48]}, {"input": {"nums": [6, 5, -4, -4, 2]}, "output": [6, 11, 7, 3, 5]}, {"input": {"nums": [-1, 17, -3, -12, 14]}, "output": [-1, 16, 13, 1, 15]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "09227d0d068039bf"}
vcr_cfd1fb425a95
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": ["cca", "bbb", "ab", "bac", "cba", "cbba", "bca", "aa", "ab"]}, "output": [1, 1, 1, 1, 2, 3]}, {"input": {"strs": ["abb", "bbb", "bab", "aab"]}, "output": [1, 1, 2]}, {"input": {"strs": ["bab", "aba", "aba"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "dd9929c1c62c1a79"}
vcr_1a1427629860
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": [11, 4, 48, 7, 28, 29, 48, 2, 42, 18, 39]}, "output": true}, {"input": {"nums": [25, 15, 12, 23, 12, 13, 20, 3, 10, 24]}, "output": true}, {"input": {"nums": [25, 10, 22, 22, 24, 24, 13, 12, 19, 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": "d9070cbd5cf35f51"}
vcr_517037919628
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": [5, 12, 23, 0, 29, 20]}, "output": 57}, {"input": {"nums": [9, 17, 5]}, "output": 17}, {"input": {"nums": [20, 19, 16, 8, 1, 0, 13, 1]}, "output": 50}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "827a7e598e08229a"}
vcr_ea25cae92916
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 4, 3, 2, 1, 4, 2, 0, 3, 0]}, "output": true}, {"input": {"nums": [1, 2, 2, 0, 0]}, "output": true}, {"input": {"nums": [0, 0, 3, 1, 0, 3]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "3f6e04f96d5376d8"}
vcr_67e8b23f69b4
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 0, 2, 1, 4, 4, 3, 1, 0]}, "output": false}, {"input": {"nums": [1, 2, 1]}, "output": true}, {"input": {"nums": [1, 1, 0, 1, 0, 0, 0]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "dd3e4a4ef7940f7e"}
vcr_28ccda6f6010
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": "ec dadede dedbca eeccbd"}, "output": "eeccbd dedbca dadede ec"}, {"input": {"s": "bb abaa abbb ba"}, "output": "ba abbb abaa bb"}, {"input": {"s": "bbbb b bbbb ab"}, "output": "ab bbbb b bbbb"}]
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": "521705805132feb6"}
vcr_73741653bae8
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, 3, 4, 5, 9, 13, 15, 15]}, "output": [3, 4, 5, 9, 13, 15]}, {"input": {"nums": [0, 0, 1, 2, 4, 7, 8, 11, 11, 12, 12]}, "output": [0, 1, 2, 4, 7, 8, 11, 12]}, {"input": {"nums": [7, 7, 7, 7, 8, 8, 10, 10, 10, 11]}, "output": [7, 8, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "daa65c02b39113af"}
vcr_7a03df97ccec
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 3, 1, 6, 1, 4, 2, 5]}, "output": [720, 240, 720, 120, 720, 180, 360, 144]}, {"input": {"nums": [2, 5, 2, 3, 4, 4, 5]}, "output": [2400, 960, 2400, 1600, 1200, 1200, 960]}, {"input": {"nums": [2, 4, 3]}, "output": [12, 6, 8]}]
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": "b056c1ea27e874ce"}
vcr_434710fb8214
easy
stack
Return True if a string of '(' and ')' is balanced.
def valid_parentheses(s: str) -> bool:
Maintain a running balance. Never go negative; must end at zero.
def valid_parentheses(s: str) -> bool: bal = 0 for ch in s: bal += 1 if ch == '(' else -1 if bal < 0: return False return bal == 0
[{"input": {"s": "(((())(()())))"}, "output": true}, {"input": {"s": "((()))()"}, "output": true}, {"input": {"s": "))))()"}, "output": false}]
true
python
5d0846a128e0bec5
### Problem Return True if a string of '(' and ')' is balanced. ### Reasoning Maintain a running balance. Never go negative; must end at zero. ### Solution ```python def valid_parentheses(s: str) -> bool: bal = 0 for ch in s: bal += 1 if ch == '(' else -1 if bal < 0: return False ...
{"v": "ok", "content_hash": "bc5ef69c17576fe7"}
vcr_e55b06cf22ec
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": [22, -20, 34, -22, -2, -21, 10, 35, 41, 66, 8, -37, 57, 30], "target": 98}, "output": [8, 12]}, {"input": {"nums": [5, 49, -16, -3, -14, 4, 15, -12], "target": 54}, "output": [0, 1]}, {"input": {"nums": [9, 13, 14, 10, 49, 29], "target": 63}, "output": [2, 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": "29915eff3b4d6052"}
vcr_9dfc3bfab38a
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, 17, -13, -13, 0, 3, -4]}, "output": 17}, {"input": {"nums": [2, 4, 9, -4, 0, 3]}, "output": 15}, {"input": {"nums": [10, 12, 11, 14, 10, 4, -7]}, "output": 61}]
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": "201104ef9647e49a"}
vcr_69b79a8ba1e8
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": 19, "b": 39}, "output": 1}, {"input": {"a": 110, "b": 114}, "output": 2}, {"input": {"a": 20, "b": 86}, "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": "4421f8f8001f6dad"}
vcr_879b37e10490
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": 234, "b": 122}, "output": 2}, {"input": {"a": 38, "b": 106}, "output": 2}, {"input": {"a": 161, "b": 18}, "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": "0053cc4a338854f3"}
vcr_b38e7818e602
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, 10, 11, 16, 18, 19, 20, 21, 23, 27, 37, 40, 46], "target": 46}, "output": 12}, {"input": {"nums": [0, 7, 9, 16, 17, 27, 29, 32, 36, 38], "target": 20}, "output": 5}, {"input": {"nums": [3, 5, 15, 16, 21, 25, 33], "target": 40}, "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": "39fd2ff05ebed15a"}
vcr_4cece3b498e0
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [6, 16, -6, -16], "k": 4}, "output": [6, 16, -6, -16]}, {"input": {"nums": [1, 9, 3, -3, 7, -2], "k": 16}, "output": [3, -3, 7, -2, 1, 9]}, {"input": {"nums": [10, -7, -9, -5], "k": 19}, "output": [-7, -9, -5, 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": "172cca2435f41e29"}
vcr_14518e58ae90
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": "cbaccbcd"}, "output": false}, {"input": {"s": "cacac"}, "output": true}, {"input": {"s": "bbbcac"}, "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": "2c2f89fa68e00275"}
vcr_28e3c73fb5ff
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": [7, 9, 12, -17, -2, 22, 14, 2, 8, 22, -12, -10]}, "output": 77}, {"input": {"nums": [-9, -7, -1]}, "output": -1}, {"input": {"nums": [-8, -11, -2, 0, 12, -3, 10, 7, -3, -11]}, "output": 26}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "c4185783b3780658"}
vcr_2bd8b1292f8f
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": 20, "b": 52}, "output": 4}, {"input": {"a": 87, "b": 178}, "output": 1}, {"input": {"a": 35, "b": 112}, "output": 7}]
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": "4cc4ce1cde7574f4"}
vcr_c4c0333ae7d8
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["baaa", "cc", "ac", "abba", "ccba", "ca", "cbaa"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bbb", "baa", "bbb", "baa", "aba", "bab", "baa"]}, "output": [1, 2, 4]}, {"input": {"strs": ["bbb", "abb", "aaa", "abb"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "288b5ea55a072ef4"}
vcr_dc9df5c67627
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, -26, -22, -11, 0, 1, 24], "target": -4}, "output": [0, 6]}, {"input": {"nums": [-18, -16, -9, 2, 7, 9, 15, 19, 32, 34], "target": 25}, "output": [2, 9]}, {"input": {"nums": [-2, 6, 28, 31, 33], "target": 4}, "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": "5c72c7381c8b1728"}
vcr_6420948fe669
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": 8950}, "output": 8}, {"input": {"n": 2158}, "output": 6}, {"input": {"n": 13658}, "output": 8}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "f9f5fb12cbf997aa"}
vcr_bf2f861c28d1
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": "bcbc adeee"}, "output": "adeee bcbc"}, {"input": {"s": "baab a"}, "output": "a baab"}, {"input": {"s": "a baba abb ab abb"}, "output": "abb ab abb baba 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": "b82514b0acd416c4"}
vcr_2480b88196a5
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [4, 29, 13, 25, 5, 7, 10, 10, 13, 4, 16]}, "output": 93}, {"input": {"nums": [3, 19, 12, 12, 9]}, "output": 31}, {"input": {"nums": [10, 5, 15, 19]}, "output": 29}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "f8297cdc31819579"}
vcr_f64678348f17
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, 15, 34, 15, 12, 69, 12]}, "output": 69}, {"input": {"nums": [50, 4, 4, 27, 27]}, "output": 50}, {"input": {"nums": [2, 2, 56, 9, 28, 22, 22, 28, 20, 9, 20]}, "output": 56}]
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": "3b519bae0f5a4f42"}
vcr_f4d12e138938
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": 171, "b": 86}, "output": 1}, {"input": {"a": 75, "b": 127}, "output": 1}, {"input": {"a": 112, "b": 62}, "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": "d4bb0586455edd62"}
vcr_6c4087445ebf
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": "aabbacaacbc"}, "output": 8}, {"input": {"s": "caccaaabb"}, "output": 5}, {"input": {"s": "accca"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "6f22ddaed1a7e3fd"}
vcr_4561aeb8748f
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [2, 15, -16, 12, 19, 10, 1], "k": 19}, "output": [-16, 12, 19, 10, 1, 2, 15]}, {"input": {"nums": [4, 0, 4, -9, 4], "k": 19}, "output": [0, 4, -9, 4, 4]}, {"input": {"nums": [3, -10, 3, -4, -8, -10, 6], "k": 18}, "output": [-4, -8, -10, 6, 3, -10, 3]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "80183a2c9d7bbf34"}
vcr_3c3205017884
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 5, 6, 10, 11, 11, 12, 13]}, "output": [1, 2, 5, 6, 10, 11, 12, 13]}, {"input": {"nums": [2, 2, 8, 9]}, "output": [2, 8, 9]}, {"input": {"nums": [2, 3, 3, 4, 4, 4, 6, 7, 8, 9, 10]}, "output": [2, 3, 4, 6, 7, 8, 9, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "8c522d28a5871137"}
vcr_50000e2fceb6
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": [-18, 17, 35, 73, 46, -10, -7, 36, 33], "target": 79}, "output": [4, 8]}, {"input": {"nums": [19, -23, 8, 31, -5, 34, 21, -22, 20], "target": 9}, "output": [3, 7]}, {"input": {"nums": [-7, 8, -3, 4, 5, 39, 15], "target": -2}, "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": "08184c0013880376"}
vcr_d8d2e1f08a59
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [17, 19, 22, 24, 10, -19, 19, 34]}, "output": [17, 36, 58, 82, 92, 73, 92, 126]}, {"input": {"nums": [1, 2, 10]}, "output": [1, 3, 13]}, {"input": {"nums": [-4, -3]}, "output": [-4, -7]}]
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": "c3b74527f129180b"}
vcr_b803f6df8b2b
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", "ba", "bbb", "cac", "bcab", "ba", "acc", "cbcb", "bcba"]}, "output": [1, 1, 2, 2, 3]}, {"input": {"strs": ["bab", "baa", "baa", "aaa", "baa", "aaa", "bbb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["bab", "bab", "bbb", "bbb", "bba", "abb"]}, "output": [2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "6552b43c034f2f07"}
vcr_73f451c152ab
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": 59481}, "output": 8}, {"input": {"n": 6674}, "output": 5}, {"input": {"n": 13320}, "output": 4}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "05cb013abad0019d"}
vcr_906533ca9622
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, 13, 1, 16]}, "output": [-4, 9, 10, 26]}, {"input": {"nums": [11, 15, 5, 6]}, "output": [11, 26, 31, 37]}, {"input": {"nums": [12, -2, 1, 10, 18, -2, -3, -2, 17]}, "output": [12, 10, 11, 21, 39, 37, 34, 32, 49]}]
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": "876cbf17cae7db6c"}
vcr_630e53d65539
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": ["bab", "ba", "acb", "acbb", "ac", "cab"]}, "output": [1, 1, 1, 1, 2]}, {"input": {"strs": ["aaa", "aba", "bba", "aba"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aba", "aba", "abb", "baa", "aba", "bba"]}, "output": [2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "1e883fec945d8542"}
vcr_bcc0d7662e5b
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [11, 13, -6, 11, 5, -18, 5, -14, -15], "k": 16}, "output": [-6, 11, 5, -18, 5, -14, -15, 11, 13]}, {"input": {"nums": [-5, 9, -8, 5, -1, -3, 8, 1, 5, 1], "k": 13}, "output": [1, 5, 1, -5, 9, -8, 5, -1, -3, 8]}, {"input": {"nums": [-2, -1, -9], "k": 6}, "output": [-2, -1, -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": "824843c3b77efd11"}
vcr_b4ff037cc58f
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 1, 2, 4, 2, 2, 0, 3, 0, 0]}, "output": true}, {"input": {"nums": [3, 2, 0]}, "output": true}, {"input": {"nums": [2, 1, 1, 3, 1, 0, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "5bdc02423a18a121"}
vcr_98f2d1bb3938
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, 3, 16, 11, 21, 17, 7]}, "output": 52}, {"input": {"nums": [20, 13, 10, 7, 18, 19, 3, 17, 13]}, "output": 66}, {"input": {"nums": [19, 4, 13, 12, 19, 19, 8, 10]}, "output": 61}]
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": "352ae6190e82b26a"}
vcr_914b9d3d929d
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-20, 0, 8, 21, 24, 32, 39], "target": 29}, "output": [2, 3]}, {"input": {"nums": [-15, -7, -5, -1, 8, 15, 25, 26, 36, 38], "target": 51}, "output": [5, 8]}, {"input": {"nums": [-19, -17, -8, 2, 27, 33], "target": -6}, "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": "0560681e54b6d45d"}
vcr_cdbf04c878c7
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", "bbaa", "ba", "aa"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "abb", "bbb", "aaa"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aaa", "bab", "aab", "aaa"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "cb6b1df0ddc7c7ab"}
vcr_cc7b0de78992
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 4, 4, 6, 8, 8, 8, 9, 9, 10, 11, 13, 15]}, "output": [0, 1, 4, 6, 8, 9, 10, 11, 13, 15]}, {"input": {"nums": [0, 6, 6]}, "output": [0, 6]}, {"input": {"nums": [0, 0, 2, 2, 3, 4, 4, 8, 9, 9, 11]}, "output": [0, 2, 3, 4, 8, 9, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "2acb792300d41c11"}
vcr_2647cc7a337e
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": "ccccaabbbbacab"}, "output": 7}, {"input": {"s": "aabcaaa"}, "output": 4}, {"input": {"s": "abbbbb"}, "output": 2}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "248218f6c185c450"}
vcr_144aa0f5c1c8
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [22, 2, 19, 8, 29, 15, 6, 10]}, "output": 80}, {"input": {"nums": [12, 7, 14, 3, 20, 7, 1, 2, 19]}, "output": 66}, {"input": {"nums": [10, 16, 4, 12]}, "output": 28}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "466f8a28d8ddfbfd"}
vcr_9c12fb44907f
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": [22, -24, 9, 22, -22, 18, -22, 19, 1, 35]}, "output": [22, -2, 7, 29, 7, 25, 3, 22, 23, 58]}, {"input": {"nums": [-3, -3, 4, -12, -2, 1, 8]}, "output": [-3, -6, -2, -14, -16, -15, -7]}, {"input": {"nums": [15, -11]}, "output": [15, 4]}]
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": "8ccfdbd4eb3c0440"}
vcr_5630921f99ea
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": "bdadbdacdb"}, "output": false}, {"input": {"s": "bababab"}, "output": true}, {"input": {"s": "cccccccbb"}, "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": "930649a4b4d9dfbe"}
vcr_a1f1c6aa0eb8
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 1, 5, 25, 30, 40], "target": 41}, "output": 6}, {"input": {"nums": [1, 4, 13, 15, 21, 22, 26, 27, 32, 33, 35], "target": 38}, "output": 11}, {"input": {"nums": [9, 15, 29], "target": 21}, "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": "c024323703347f82"}
vcr_d5363d7d86d1
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": [45, 22, 33, 14, 12, 46, 2, 23, 45]}, "output": true}, {"input": {"nums": [2, 13, 18, 9, 15, 7, 23, 6]}, "output": false}, {"input": {"nums": [2, 15, 24, 18, 0, 15, 9, 21, 22]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "302777cd83bbe021"}
vcr_0ab44b03a9a8
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 7, 9, 12, 16, 25, 27, 29, 31, 35, 36, 44, 45], "target": 21}, "output": 5}, {"input": {"nums": [5, 11, 19, 23, 32], "target": 18}, "output": 2}, {"input": {"nums": [0, 10, 11, 16, 22, 30, 32, 33, 35], "target": 23}, "output": 5}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "181322ecbc71c3c0"}
vcr_f9cca3a83902
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": [59, 60, 2, 26, 58, 15, 11, 27, 43, 20]}, "output": 56}, {"input": {"prices": [35, 7, 4, 15, 3, 24, 27, 1]}, "output": 24}, {"input": {"prices": [36, 1, 4, 28, 26, 34]}, "output": 33}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "b33f28b81cce5087"}
vcr_b3664754c3a8
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 4, 1, 2, 2, 0, 0]}, "output": true}, {"input": {"nums": [3, 1, 1]}, "output": true}, {"input": {"nums": [1, 3, 1, 1, 1, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "87a7c0cb28695fa4"}
vcr_74307eaaca42
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": [-25, -19, -11, -4, 5, 11, 26, 42, 43], "target": -15}, "output": [2, 3]}, {"input": {"nums": [-20, -17, 6, 27], "target": 7}, "output": [0, 3]}, {"input": {"nums": [-16, -15, -14, -12, -4, 1, 10, 13, 20], "target": 9}, "output": [4, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "1875bce3e2c693e9"}
vcr_ba8ea01f995e
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": [-9, 15, -10, -2, 9, 0, -4, 1, 13, -8, -7, -1], "k": 34}, "output": [-10, -2, 9, 0, -4, 1, 13, -8, -7, -1, -9, 15]}, {"input": {"nums": [10, 8, 5, 0, 6, -6, 1, 2, 6, -6], "k": 3}, "output": [2, 6, -6, 10, 8, 5, 0, 6, -6, 1]}, {"input": {"nums": [-9, -10, 1, -2, 0, -10, 1, 8, -5], "k": 8}, "output": ...
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": "15e17cf8aad9eeb2"}
vcr_1cd84b4dfeb9
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [4, 30, 7, 12]}, "output": 42}, {"input": {"nums": [15, 0, 5, 12, 8, 19, 6]}, "output": 46}, {"input": {"nums": [16, 6, 2, 9, 20, 3, 12, 4]}, "output": 50}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "f6fd5b8769b5c7cc"}
vcr_cf7f2fc93fe4
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": ["aaba", "ccac", "bc", "abbb", "aa", "baaa", "aab", "cac", "cca"]}, "output": [1, 1, 1, 1, 1, 2, 2]}, {"input": {"strs": ["bbb", "bba", "bba"]}, "output": [1, 2]}, {"input": {"strs": ["abb", "bbb", "bab", "aba", "aab", "bab"]}, "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": "9564a1a126033176"}
vcr_54cc229196ea
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 4, 17, 32, 35, 38, 44, 46, 47], "target": 32}, "output": 3}, {"input": {"nums": [4, 11, 18, 19, 22, 30, 32, 36], "target": 17}, "output": 2}, {"input": {"nums": [7, 9, 16, 18, 19, 20, 30, 33, 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": "2c16a49ab66c3478"}
vcr_bf9a5fdcd6b4
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, 57, 49, 8, 50]}, "output": 42}, {"input": {"prices": [4, 20, 15, 23, 21, 33]}, "output": 29}, {"input": {"prices": [24, 30, 21, 31, 19, 18, 3, 3, 37, 29, 38, 26]}, "output": 35}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "f4191b4bba4d1f83"}
vcr_e682b9486c20
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 7, 8, 10, 12, 14, 19, 28, 30, 39, 44], "target": 42}, "output": 10}, {"input": {"nums": [3, 14, 17, 19, 20, 21, 28, 29, 36, 37, 38], "target": 3}, "output": 0}, {"input": {"nums": [7, 17, 27, 39], "target": 9}, "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": "0ca3e6be37626293"}
vcr_71e12fbec550
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [8, -9, -9, 8, 4, -2, 20, 16, 9]}, "output": 55}, {"input": {"nums": [-3, 10, -3, -9, 1, 1, 6]}, "output": 10}, {"input": {"nums": [8, -4, 12, -9]}, "output": 16}]
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": "5bb7d0ca400b46ed"}
vcr_a954a0371f0a
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, 6, 17, 15]}, "output": [-23, -17, 0, 15]}, {"input": {"nums": [4, 7, -5, 2]}, "output": [4, 11, 6, 8]}, {"input": {"nums": [14, -4, -5, 14, 12, 15]}, "output": [14, 10, 5, 19, 31, 46]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "1970ab0761bc83cf"}
vcr_f252be117db9
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": 10129}, "output": 7}, {"input": {"n": 1422}, "output": 6}, {"input": {"n": 10657}, "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": "bb6fcadcec73e1c4"}
vcr_78f2b060d1d6
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": 230, "b": 36}, "output": 2}, {"input": {"a": 102, "b": 137}, "output": 1}, {"input": {"a": 176, "b": 74}, "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": "539a4e26096555e7"}
vcr_a6270b62aab6
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": "bcbabcbacbc"}, "output": 11}, {"input": {"s": "cbcbbbc"}, "output": 5}, {"input": {"s": "bbabbbabccca"}, "output": 7}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "903725c905bbc469"}
vcr_e8199d4a687f
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": 80, "b": 227}, "output": 1}, {"input": {"a": 89, "b": 91}, "output": 1}, {"input": {"a": 174, "b": 58}, "output": 58}]
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": "e69ae8598dbc675e"}
vcr_a095c4e8a4dd
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": ["bb", "babc", "bab", "abb", "bcb", "bbaa", "aaab"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bab", "bba", "bab", "bab"]}, "output": [4]}, {"input": {"strs": ["bbb", "bbb", "bba", "aba", "aab", "baa", "abb"]}, "output": [2, 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": "8fcad0cde29bf975"}
vcr_843c4cc238ed
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 4, 17, 22, 24, 35], "target": 21}, "output": 3}, {"input": {"nums": [5, 20, 27, 30, 37], "target": 21}, "output": 2}, {"input": {"nums": [1, 12, 18, 20, 25, 32, 35, 37, 38], "target": 33}, "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": "c09f30b19f077d67"}
vcr_ce589f6bc5a4
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-35, 30, -36, 6, -10, -23, -17, 47], "target": 24}, "output": [5, 7]}, {"input": {"nums": [9, -23, -10, 40, 23, 39, -9, 46], "target": -1}, "output": [0, 2]}, {"input": {"nums": [15, 23, 36, 41, 4, 8, 14, -22, 7], "target": 59}, "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": "f82e6e2c5909ac80"}
vcr_030b712e6a44
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": 42080}, "output": 5}, {"input": {"n": 15617}, "output": 6}, {"input": {"n": 3846}, "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": "c1cd5139bb0328ac"}
vcr_3052ce4951b8
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 0, 3, 3, 2, 4, 3, 3, 0]}, "output": false}, {"input": {"nums": [3, 1, 1, 1, 1, 2]}, "output": true}, {"input": {"nums": [0, 0, 1, 1, 0, 0]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "5c2d0f2845068eba"}
vcr_57ad6918bf08
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": [-22, 21, 10, 0, -22, 5, -17, 32]}, "output": [-22, -1, 9, 9, -13, -8, -25, 7]}, {"input": {"nums": [15, 5, 18, 1, 15, 7, -11]}, "output": [15, 20, 38, 39, 54, 61, 50]}, {"input": {"nums": [14, -5, 2, 7, 10, 5]}, "output": [14, 9, 11, 18, 28, 33]}]
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": "93d06587e90dd3a3"}
vcr_e3f28d744451
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [20, 18, 26, 15, 25, 8, 1, 2, 8, 14]}, "output": 87}, {"input": {"nums": [1, 12, 2, 8, 1, 5, 15, 0, 4]}, "output": 39}, {"input": {"nums": [20, 15, 12, 17, 10]}, "output": 42}]
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": "7049ed015cd2826c"}
vcr_5fec36b41495
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": [-28, -36, -5, 58], "target": -41}, "output": [1, 2]}, {"input": {"nums": [12, 25, 24, 17, 10, 35, -30], "target": 36}, "output": [0, 2]}, {"input": {"nums": [42, 31, 25, 16], "target": 73}, "output": [0, 1]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "86fbb87e4eeb273b"}
vcr_0f692b8119eb
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": [-6, -1, 7, -18, -9, 4, -17, -9, 18, 18]}, "output": 36}, {"input": {"nums": [10, 14, 8, -1, 7, 12, 5, 9, -6, -8, 6]}, "output": 64}, {"input": {"nums": [1, -6, 9, 14]}, "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": "fe1c08313672b443"}
vcr_dc9a33307cda
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": [-11, 30, 2, -3, 10, 35, 20, 32, 23, 12, -20, 22]}, "output": [-11, 19, 21, 18, 28, 63, 83, 115, 138, 150, 130, 152]}, {"input": {"nums": [12, -5, 5, -9, 8, 16, -4]}, "output": [12, 7, 12, 3, 11, 27, 23]}, {"input": {"nums": [-9, 16, -5, 17, -9]}, "output": [-9, 7, 2, 19, 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": "c9e1cceebfee5b67"}