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_15de43790f03
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [2, 24, 6]}, "output": 24}, {"input": {"nums": [19, 8, 12, 7, 11]}, "output": 42}, {"input": {"nums": [14, 14, 5, 15, 7, 3, 11, 3, 15]}, "output": 55}]
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": "2827a320dc00c056"}
vcr_1bff4c2696a5
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": [-34, 52, -28, 20], "target": 24}, "output": [1, 2]}, {"input": {"nums": [41, 31, 34, 17], "target": 65}, "output": [1, 2]}, {"input": {"nums": [16, 26, -2, 37, -1, -9, 49, 14, 23], "target": 53}, "output": [0, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "3bf5aa9b19804772"}
vcr_0732989b1ea2
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 3, 5, 6, 7, 7, 7, 8, 9, 12, 14, 14, 15]}, "output": [0, 2, 3, 5, 6, 7, 8, 9, 12, 14, 15]}, {"input": {"nums": [1, 6, 7, 11]}, "output": [1, 6, 7, 11]}, {"input": {"nums": [1, 1, 2, 9, 12, 12]}, "output": [1, 2, 9, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "1c5cf2b04bc6db5e"}
vcr_8fb3519ea6f8
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": [2, 16, 3, 10, 19, 31, 11, 45, 41, 12, 50, 50, 10, 22, 36, 37, 11, 12]}, "output": true}, {"input": {"nums": [5, 19, 17, 22, 14, 1, 25, 25, 22, 11, 14]}, "output": true}, {"input": {"nums": [9, 18, 3]}, "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": "3b79f1bbe59fe8bb"}
vcr_2c1b5972a4b6
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": "abeeca da aabede aebe ee"}, "output": "ee aebe aabede da abeeca"}, {"input": {"s": "abba bbbb b"}, "output": "b bbbb abba"}, {"input": {"s": "bb bba b bb ba"}, "output": "ba bb b bba bb"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "233faefec3c347ac"}
vcr_29dc83a3d609
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": [-19, -17, -13, -9, 1, 6, 19, 37, 39, 48], "target": 54}, "output": [5, 9]}, {"input": {"nums": [-17, -10, -6, 13, 18, 25, 36], "target": 38}, "output": [3, 5]}, {"input": {"nums": [-10, -9, 13, 26, 28], "target": 54}, "output": [3, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "157fd911f28703eb"}
vcr_9909c1783992
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": "ddaccccadd"}, "output": true}, {"input": {"s": "abbccbb"}, "output": false}, {"input": {"s": "ccacbcab"}, "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": "3bc39171b52b873b"}
vcr_0328b7e9a22d
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [60, 19, 49, 50, 18, 45, 38, 26]}, "output": 31}, {"input": {"prices": [9, 20, 37, 18]}, "output": 28}, {"input": {"prices": [20, 21, 17, 35, 4, 40]}, "output": 36}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "b255ab7a265b7470"}
vcr_fd2428ec9dd8
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": ["ac", "aa", "aabb", "aac", "bacc", "cba", "bca"]}, "output": [1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "baa", "baa", "aab"]}, "output": [1, 3]}, {"input": {"strs": ["bba", "bab", "bab", "bab", "bab"]}, "output": [5]}]
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": "4ad7ebdc0156da0b"}
vcr_cc742886008c
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": [-13, -8, 18, -13, 18, -17, -3, 18, 8, 10, 17], "k": 5}, "output": [-3, 18, 8, 10, 17, -13, -8, 18, -13, 18, -17]}, {"input": {"nums": [0, 3, -9, -1], "k": 0}, "output": [0, 3, -9, -1]}, {"input": {"nums": [-7, 4, -9, -2], "k": 17}, "output": [-2, -7, 4, -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": "ed12720f71024889"}
vcr_1bbb16a6e860
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, 13, 18, 25, 2]}, "output": 38}, {"input": {"nums": [10, 13]}, "output": 13}, {"input": {"nums": [12, 0, 15]}, "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": "d8101acf48ecca78"}
vcr_9c507582a2a6
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": [19, 169, 67, 117, 28, 46, 18, 165, 29, 118, 128, 106, 62, 50, 64, 121, 78]}, "output": false}, {"input": {"nums": [21, 11, 3]}, "output": false}, {"input": {"nums": [8, 13, 5, 2, 8]}, "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": "b139e0f6fdb5d792"}
vcr_ecb8b80cca42
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": [8, 42, 2, 3, 19, 5, 17, 7, 35, 47, 50, 32, 4, 8]}, "output": true}, {"input": {"nums": [12, 11, 17, 21, 1, 17, 4, 24, 23, 22, 20]}, "output": true}, {"input": {"nums": [5, 19, 22]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "8d0a4c9085fa5904"}
vcr_0f609d2687fa
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-14, -20, 2, -15, 33, -9, -13, 21, -19, -12, 2, -8, 35]}, "output": [-14, -34, -32, -47, -14, -23, -36, -15, -34, -46, -44, -52, -17]}, {"input": {"nums": [5, -7]}, "output": [5, -2]}, {"input": {"nums": [0, -10, 3, -8, -4, 3]}, "output": [0, -10, -7, -15, -19, -16]}]
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": "8422e2d08b061bd7"}
vcr_4dd23c407d26
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [11, 14, 15, 21, 30, 34, 37, 49], "target": 21}, "output": 3}, {"input": {"nums": [3, 6, 10, 11, 18, 21, 27, 28, 34], "target": 28}, "output": 7}, {"input": {"nums": [4, 6, 18, 25], "target": 10}, "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": "87d7b4708e36e47e"}
vcr_ea5823f43073
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": [11, 35, 56, 41, 59, 32, 17, 29, 5, 49, 20]}, "output": 48}, {"input": {"prices": [11, 33, 7, 1, 13, 26, 23, 21, 22, 21, 38]}, "output": 37}, {"input": {"prices": [11, 34, 20, 33, 12, 14]}, "output": 23}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "895489dc7194bacb"}
vcr_8cbdace29b65
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": [33, 3, 40, 44]}, "output": 41}, {"input": {"prices": [16, 6, 6, 11, 27, 18, 16, 21, 30, 3]}, "output": 24}, {"input": {"prices": [23, 34, 15, 27, 26, 2, 25, 22]}, "output": 23}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "581f065320cf0b1e"}
vcr_83443e07ec36
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, 6, 7, 7, 8, 8, 9, 9, 12, 12, 14, 15, 15]}, "output": [3, 6, 7, 8, 9, 12, 14, 15]}, {"input": {"nums": [3, 8, 8, 12]}, "output": [3, 8, 12]}, {"input": {"nums": [9, 10, 10]}, "output": [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": "ba1d1e8b2a587c3c"}
vcr_ae7c1f133988
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 1, 1, 3, 8, 9, 10, 10, 12, 12, 13, 14, 15]}, "output": [0, 1, 3, 8, 9, 10, 12, 13, 14, 15]}, {"input": {"nums": [0, 4, 4, 4, 5, 7, 7, 8, 9, 9, 10]}, "output": [0, 4, 5, 7, 8, 9, 10]}, {"input": {"nums": [3, 4, 4, 4, 9, 11]}, "output": [3, 4, 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": "b9f56b58eaf5391a"}
vcr_a2d062987038
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": [9, 25, 38, 27, 46, 5, 6, 23, 54, 16, 44, 24, 46, 48]}, "output": 49}, {"input": {"prices": [25, 38, 18, 11]}, "output": 13}, {"input": {"prices": [37, 24, 33, 38, 16, 34, 9, 5, 20, 1, 5, 24]}, "output": 23}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "958b349e971478f2"}
vcr_346801e65132
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": "bcdbcbcaacbcbdcb"}, "output": true}, {"input": {"s": "cac"}, "output": true}, {"input": {"s": "ac"}, "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": "6817acd1e0da83e0"}
vcr_2d1baa812d0c
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": 71, "b": 210}, "output": 1}, {"input": {"a": 126, "b": 116}, "output": 2}, {"input": {"a": 53, "b": 166}, "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": "11830ef3e3206c14"}
vcr_d7ffdcfe410f
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": [24, 4, 29, 29, 16, 21, 25, 24, 21, 56, 16, 4, 25]}, "output": 56}, {"input": {"nums": [3, 3, 22, 23, 40, 2, 2, 23, 22]}, "output": 40}, {"input": {"nums": [27, 23, 14, 27, 2, 52, 23, 2, 14]}, "output": 52}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "40cdb32819ee36da"}
vcr_142d98adb62a
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [28, 27, 50, -10, 35, -29, -8, -38, 26, 16, 12, -30, -34, 21], "target": 16}, "output": [3, 8]}, {"input": {"nums": [38, 32, -28, -17, 8, -11, 5, 41, 25, 20], "target": 28}, "output": [4, 9]}, {"input": {"nums": [-24, 16, 21, 13, 0, -4, 29, 31, -21, 34, 49], "target": 16}, "output": [1, 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": "1193be95f1d32d39"}
vcr_6338387f5a88
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [14, -22, 12, 34, -14, 32, 9, 19, 33, 24, 13, 22, -18, -16]}, "output": [14, -8, 4, 38, 24, 56, 65, 84, 117, 141, 154, 176, 158, 142]}, {"input": {"nums": [5, -1, 0, -10]}, "output": [5, 4, 4, -6]}, {"input": {"nums": [4, 14, 16, 4, 3, -7, 16, 5, -8, 4]}, "output": [4, 18, 34, 38, 41, 34, 50, 55, 47...
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": "17703f82f618566b"}
vcr_583894eaac6b
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -22, -21, -16, -13, -1, 10, 14, 15, 31, 35], "target": 34}, "output": [5, 10]}, {"input": {"nums": [-17, -16, 24, 36, 39], "target": 23}, "output": [1, 4]}, {"input": {"nums": [-18, -17, -7, 1, 3, 8, 9, 19, 28, 34], "target": -24}, "output": [1, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "ec37fb8beb916e1a"}
vcr_64f339db5ec2
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [27, 30, 29, 21, 9, 6, 11, 2, 12, 24, 7]}, "output": 100}, {"input": {"nums": [0, 10, 0, 10, 8, 0, 1, 15, 7, 14]}, "output": 49}, {"input": {"nums": [19, 5, 20, 19, 0]}, "output": 39}]
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": "c6c8f5ab59e7cc52"}
vcr_7e088555cbcd
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [12, 12, -6, 17], "k": 10}, "output": [-6, 17, 12, 12]}, {"input": {"nums": [-6, 1, 5, 3, 4], "k": 12}, "output": [3, 4, -6, 1, 5]}, {"input": {"nums": [-10, 5, 8, 9, 8, -9, 8, 2], "k": 13}, "output": [9, 8, -9, 8, 2, -10, 5, 8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "ecdbad68667bfbc1"}
vcr_3ee981eb172f
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, 11, 13, 15, 26, 29, 39], "target": 29}, "output": 7}, {"input": {"nums": [0, 4, 13, 19, 20, 22, 36], "target": 31}, "output": 6}, {"input": {"nums": [6, 9, 12, 13, 25, 26, 29, 33, 34, 37], "target": 36}, "output": 9}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "b7addd946738b4a5"}
vcr_7dd28dbf5870
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": 18563}, "output": 5}, {"input": {"n": 7426}, "output": 5}, {"input": {"n": 7601}, "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": "aae5461e8f732214"}
vcr_c909143aa736
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["ccc", "bac", "cca", "ba"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aab", "bbb", "abb", "aab", "aba", "aaa", "bbb"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["abb", "abb", "bab"]}, "output": [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": "229338016b10a1d7"}
vcr_27e6f2aac0ef
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [7, 7, 8, 1, 6, 27, 16, 2, 9, 17, 23, 26]}, "output": 87}, {"input": {"nums": [11, 9, 2, 5, 8, 2, 18, 6, 12]}, "output": 51}, {"input": {"nums": [4, 20, 11, 18, 1, 19, 4, 11, 20, 19]}, "output": 87}]
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": "abdba407e883c7d2"}
vcr_2d7af818b894
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -27, -19, -18, -8, -3, -2, -1, 12, 14, 18], "target": -10}, "output": [4, 6]}, {"input": {"nums": [-14, -13, -7, -4, -2, 2, 4, 7, 8, 15], "target": -6}, "output": [0, 8]}, {"input": {"nums": [-1, 7, 18, 22, 34, 39], "target": 25}, "output": [1, 2]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "ba370d274e881990"}
vcr_aa17792d606d
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [14, 14, 9]}, "output": 23}, {"input": {"nums": [5, 4, 14, 8, 7, 8, 8, 19]}, "output": 46}, {"input": {"nums": [14, 16, 9, 18, 5, 9]}, "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": "b7e195c3e10e19dc"}
vcr_544e72368dc4
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": [51, 37, 45, 32, 6, 41, 37]}, "output": 35}, {"input": {"prices": [3, 14, 18, 39, 36, 19]}, "output": 36}, {"input": {"prices": [38, 2, 24, 36]}, "output": 34}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "1d15fdd1ace95859"}
vcr_36d97ecd646a
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": 8680}, "output": 6}, {"input": {"n": 1679}, "output": 7}, {"input": {"n": 3527}, "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": "8dee4dda0e28d974"}
vcr_b97c992adbc3
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": [20, 17, -11, -25, 74, 52, 49, -31, 76, -22, 50], "target": 24}, "output": [3, 6]}, {"input": {"nums": [-23, -8, -30, -25, 11, 14, 32, 26, 49, -27, 6], "target": -38}, "output": [1, 2]}, {"input": {"nums": [-18, -9, 46, 29, 12, 37], "target": 19}, "output": [0, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "5c6eb53c79ff590f"}
vcr_fd12a005368b
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": [33, 33, 14, 68, 34, 34, 14, 22, 22, 3, 3]}, "output": 68}, {"input": {"nums": [14, 29, 14, 4, 4, 17, 17, 59, 29]}, "output": 59}, {"input": {"nums": [18, 21, 44, 2, 18, 21, 13, 2, 3, 3, 25, 25, 13]}, "output": 44}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "f53d4914107a2305"}
vcr_0d2071f129db
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": [17, 7, 9, 26, 22, 17, 18, 21, 15, 16, 24]}, "output": 105}, {"input": {"nums": [4, 8, 12, 6, 18]}, "output": 34}, {"input": {"nums": [11, 9, 5, 9, 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": "51e516d1e1582f79"}
vcr_86e9c9758855
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, 12, 18, 24, 39, 43, 46], "target": 49}, "output": 7}, {"input": {"nums": [4, 11, 18, 34, 36, 39], "target": 4}, "output": 0}, {"input": {"nums": [6, 14, 17, 18, 28, 34, 38], "target": 0}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "9ea72edb2699df9b"}
vcr_739faa8a8c06
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-8, -15, 8, -15], "k": 3}, "output": [-15, 8, -15, -8]}, {"input": {"nums": [9, -10, 7, -3, -9, -10, -9], "k": 14}, "output": [9, -10, 7, -3, -9, -10, -9]}, {"input": {"nums": [7, 7, -4, 10, 8, 3, 7, -6], "k": 5}, "output": [10, 8, 3, 7, -6, 7, 7, -4]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "83451d29ba049197"}
vcr_a47d129b8dd6
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-2, 2, 13, -3, 22, 6, 17, 13, 13, -9, 10, -7, -1, -17, -6]}, "output": 84}, {"input": {"nums": [-12, -9, 7, -6, -10, 4, -8]}, "output": 7}, {"input": {"nums": [-7, 0, 0, 0, 11, 5, 6, -6]}, "output": 22}]
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": "83b20fa365d1ee2b"}
vcr_c9a00d324032
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": ["cbb", "caab", "aba", "ccab", "bbbb", "bbba", "bcab", "cbab"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["aab", "aaa", "aaa", "aab", "baa", "aba", "aba"]}, "output": [2, 5]}, {"input": {"strs": ["bba", "aab", "baa", "abb", "aba"]}, "output": [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": "7e0b8ee18d6eeef5"}
vcr_7cddab288a1b
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, 10, 11, 41], "target": 27}, "output": 4}, {"input": {"nums": [5, 6, 17, 27, 32, 38], "target": 7}, "output": 2}, {"input": {"nums": [18, 27, 37], "target": 20}, "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": "ac5d20f6b4ce7dcf"}
vcr_282b4fd2fdc0
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": [-18, 2, 16, 23, 26, 33, 42, 43, 44, 45], "target": 27}, "output": [0, 9]}, {"input": {"nums": [-17, -10, -7, 14, 20, 22, 23, 25, 26, 28], "target": 18}, "output": [1, 9]}, {"input": {"nums": [-14, -5, 6, 13, 18, 35, 38], "target": 48}, "output": [3, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "bf87b263c03791c0"}
vcr_6317fbd8417c
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 2, 2, 2, 5, 9, 9, 10, 12, 14, 15]}, "output": [0, 1, 2, 5, 9, 10, 12, 14, 15]}, {"input": {"nums": [0, 1, 2, 2, 3, 5, 5, 5, 8, 9]}, "output": [0, 1, 2, 3, 5, 8, 9]}, {"input": {"nums": [0, 3, 3, 3, 4, 4, 5, 8, 9, 10, 12, 12]}, "output": [0, 3, 4, 5, 8, 9, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "2a58e2c920780527"}
vcr_e3be1dcb0a54
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [25, 4, 16, 14, 19, 9, 21, 29, 30, 10, 16, 4]}, "output": 127}, {"input": {"nums": [16, 17, 8, 6, 4, 10, 20, 5, 17, 20]}, "output": 68}, {"input": {"nums": [0, 3]}, "output": 3}]
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": "fa42ef4c6981c092"}
vcr_fa1150551b8e
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, -21, 3, 28, 41, 48], "target": 51}, "output": [2, 5]}, {"input": {"nums": [-14, -8, -5, 24, 27, 33], "target": 19}, "output": [0, 5]}, {"input": {"nums": [-14, -7, -1, 24, 35], "target": 34}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "20864358623bda63"}
vcr_67a1dead17f8
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": [-23, -20, -11, -3, 2, 24, 31, 32, 34], "target": 13}, "output": [2, 5]}, {"input": {"nums": [-12, -7, -6, 5, 18, 20, 26, 28], "target": 48}, "output": [5, 7]}, {"input": {"nums": [-7, -1, 3, 5, 14, 15, 20, 21, 27], "target": 20}, "output": [0, 8]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "6385ab85d28d4ba7"}
vcr_e3a70d517c6e
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [2, 2, 4, 5, 6, 12, 12]}, "output": [2, 4, 5, 6, 12]}, {"input": {"nums": [5, 6, 9, 10, 10, 11, 11]}, "output": [5, 6, 9, 10, 11]}, {"input": {"nums": [3, 5, 5, 7, 8, 9]}, "output": [3, 5, 7, 8, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "062f4a23d707cc99"}
vcr_8e33c3214416
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [11, 25, 34, 36, 41, 44, 49], "target": 27}, "output": 2}, {"input": {"nums": [13, 14, 17, 23, 29, 31, 32, 36], "target": 25}, "output": 4}, {"input": {"nums": [0, 15, 16, 18, 21, 27, 30, 36], "target": 0}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "91ed7adb40bf2193"}
vcr_657baa833619
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": "dcbddddddddbcd"}, "output": true}, {"input": {"s": "acacbcab"}, "output": false}, {"input": {"s": "babcacabb"}, "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": "9ac6e4dbe9a20973"}
vcr_acf647502aca
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": [148, 34, 174, 63, 103, 62, 119, 61, 77, 127, 145, 71, 175, 97, 198, 141, 44]}, "output": false}, {"input": {"nums": [11, 13, 4, 10, 22, 13, 3, 4, 12, 25, 1, 15]}, "output": true}, {"input": {"nums": [17, 8, 9, 17, 7, 20, 22, 16]}, "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": "1caee3699a7add7d"}
vcr_be5633de924a
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, 4, 5, 5, 5, 6, 8, 13, 13, 14]}, "output": [3, 4, 5, 6, 8, 13, 14]}, {"input": {"nums": [0, 1, 2, 6, 6, 8, 11, 11, 12]}, "output": [0, 1, 2, 6, 8, 11, 12]}, {"input": {"nums": [3, 4, 8, 8, 8, 11, 12]}, "output": [3, 4, 8, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "9f938ecaa9cf042c"}
vcr_ea72a7ad3f7e
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": "abaadcacacdaaba"}, "output": true}, {"input": {"s": "cbccca"}, "output": false}, {"input": {"s": "cab"}, "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": "2502fbb4a0b8cfbb"}
vcr_714e645f69ef
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": [23, 12, 20, 3, 0, 13, 24, 24, 9, 12, 8]}, "output": 92}, {"input": {"nums": [3, 11, 4, 4, 1, 15, 17, 8, 12]}, "output": 44}, {"input": {"nums": [14, 11, 12, 6, 11, 17, 15, 6, 15]}, "output": 67}]
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": "45d3a0dd4a25302d"}
vcr_e1987e87b20e
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 6, 7, 15, 16, 20, 25, 34, 42, 47, 49], "target": 29}, "output": 7}, {"input": {"nums": [0, 3, 14, 17, 24, 35], "target": 33}, "output": 5}, {"input": {"nums": [21, 23, 24, 26, 29, 32, 33, 35, 37], "target": 16}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "e7fb79feb4ba278c"}
vcr_a1167d0cbe27
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [4, 6, 8, 16, 22, 24, 27, 29, 30, 35, 40, 41, 42, 47], "target": 27}, "output": 6}, {"input": {"nums": [4, 12, 18, 21, 29, 31, 36, 38], "target": 31}, "output": 5}, {"input": {"nums": [1, 17, 25, 27], "target": 37}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "b7bd69a20dfc2162"}
vcr_b16baedf06b5
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 3, 4, 5, 11, 11, 12, 15]}, "output": [1, 2, 3, 4, 5, 11, 12, 15]}, {"input": {"nums": [0, 4, 5]}, "output": [0, 4, 5]}, {"input": {"nums": [0, 1, 3, 7, 8, 10]}, "output": [0, 1, 3, 7, 8, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "8cbe41d77242e5ca"}
vcr_093cf63d0f62
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [16, 5, 47, 40, 14, 37, 4, 28, 32, 52, 29, 51, 5]}, "output": 48}, {"input": {"prices": [23, 3, 8, 37, 24]}, "output": 34}, {"input": {"prices": [12, 24, 35, 40, 40, 32, 25, 14, 4, 23]}, "output": 28}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "69abdd3cd9f2abb4"}
vcr_57a8c8418cb4
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -25, -22, 13, 18, 19, 31, 33, 35, 38], "target": -6}, "output": [1, 5]}, {"input": {"nums": [-19, -13, -2, 3, 7, 12, 14, 23, 30, 34], "target": 26}, "output": [3, 7]}, {"input": {"nums": [-10, 0, 1, 4, 36, 38], "target": 4}, "output": [1, 3]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "07e3689da6cd18e0"}
vcr_649d928d0ece
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": [22, 19, 16, 35, 16, 14, 23, 60, 23, 34, 35, 19, 29, 34, 22, 14, 29]}, "output": 60}, {"input": {"nums": [26, 28, 11, 7, 26, 17, 11, 7, 28, 8, 8, 53, 17]}, "output": 53}, {"input": {"nums": [22, 18, 24, 42, 6, 24, 22, 18, 6]}, "output": 42}]
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": "667a026a6a5c3ad4"}
vcr_24fdae80abd8
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": [21, 2, -3, 0, -16, 21, 13, 20, -7, -11, -8, 21, 2]}, "output": 58}, {"input": {"nums": [-6, 4, 13, 2, 2, -5]}, "output": 21}, {"input": {"nums": [-6, 2, 13, 4]}, "output": 19}]
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": "57229c5ee560071f"}
vcr_726af298629a
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [13, -13, 10, -4, -7, 4]}, "output": 13}, {"input": {"nums": [-1, 14, -6, -11, 6, 8]}, "output": 14}, {"input": {"nums": [-3, -5, 12, 10, -12, -10, 1, 11, -6]}, "output": 22}]
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": "ea21cd560d7992ca"}
vcr_b037a262094a
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": ["ac", "acc", "cbb", "bba", "acab"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bba", "bab", "baa", "baa"]}, "output": [1, 2, 2]}, {"input": {"strs": ["bba", "aab", "aab", "aab"]}, "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": "dd876e82f1bd70e2"}
vcr_40a294780b36
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": 238, "b": 231}, "output": 7}, {"input": {"a": 174, "b": 113}, "output": 1}, {"input": {"a": 137, "b": 135}, "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": "f487b23a6af1a233"}
vcr_72b789aa3a77
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": [58, 27, 21, 7, 2, 21, 32, 32, 7, 2, 27]}, "output": 58}, {"input": {"nums": [16, 11, 5, 28, 48, 28, 11, 16, 5]}, "output": 48}, {"input": {"nums": [8, 15, 27, 19, 27, 47, 19, 15, 8]}, "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": "87b76f371163cbff"}
vcr_cfaf3c370995
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [46, 2, 38, 12, 44, 57, 59, 30, 57, 47, 5, 25]}, "output": 57}, {"input": {"prices": [4, 12, 32, 17, 34, 16, 30, 18, 23]}, "output": 30}, {"input": {"prices": [4, 24, 30, 15, 7, 23, 4]}, "output": 26}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "420c5106debff965"}
vcr_5416e0ad8394
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": [-8, 11, -16, 16, 12, 78, 59, 56, 1, 37, -25, 15, -3, 18], "target": -19}, "output": [2, 12]}, {"input": {"nums": [-13, -2, 9, 4, -7, 42, 37, -27, 2, -3, 1], "target": -11}, "output": [0, 8]}, {"input": {"nums": [-26, -5, -16, -20, -8, 30], "target": -21}, "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": "7d082fa3bff03e00"}
vcr_830c83e03567
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, 21, -7, -6, -10, -12, 4, 8, 4, 4, 5, -15, -2, -3, -3]}, "output": 35}, {"input": {"nums": [-8, -11, 6, 6, 5, 11, -7, 7, -8]}, "output": 28}, {"input": {"nums": [5, 10, -8, 3, -1, -6, 5, -7]}, "output": 15}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "f56907c73632d969"}
vcr_b8319e90c01c
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": 237, "b": 156}, "output": 3}, {"input": {"a": 18, "b": 13}, "output": 1}, {"input": {"a": 120, "b": 21}, "output": 3}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "6d1c01959bf76576"}
vcr_22596b768a92
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": [-6, 1, 23, 32, 46], "target": 40}, "output": [0, 4]}, {"input": {"nums": [-15, 10, 13, 25], "target": 35}, "output": [1, 3]}, {"input": {"nums": [-18, -14, -7, -6, -1, 3, 24, 27, 29, 31], "target": 26}, "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": "68640d72208be5d7"}
vcr_68082fa1a4c8
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, 22, 28, -17, 13]}, "output": [-22, 0, 28, 11, 24]}, {"input": {"nums": [0, 3, 13, -6, -7, 16, -12, 7, 2, 9]}, "output": [0, 3, 16, 10, 3, 19, 7, 14, 16, 25]}, {"input": {"nums": [4, 15, -12, -8, 5]}, "output": [4, 19, 7, -1, 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": "691c0b2744fb5cca"}
vcr_3b2c937ef985
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, 0, 0, 2, 2, 4, 0]}, "output": false}, {"input": {"nums": [3, 1, 3, 1, 1]}, "output": true}, {"input": {"nums": [0, 2, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "e36aede49552a1dc"}
vcr_1679b3c228fa
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": [-16, -2, 25, -13, -3, -11, 15, 19, 8, 15, 30, 24, 0]}, "output": [-16, -18, 7, -6, -9, -20, -5, 14, 22, 37, 67, 91, 91]}, {"input": {"nums": [8, 11, 9, 12, -6, -12, 4, 18, -4, 9]}, "output": [8, 19, 28, 40, 34, 22, 26, 44, 40, 49]}, {"input": {"nums": [16, -9, -12, 10, 10, -4, 13, -4]}, "output": [...
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": "e82a60bad509fec6"}
vcr_e98ac124056c
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": 33276}, "output": 8}, {"input": {"n": 15493}, "output": 7}, {"input": {"n": 251}, "output": 7}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "581257ed5f601ae9"}
vcr_f5f290cc9a8c
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-26, -25, -21, -20, -10, 16, 19, 33, 37, 39, 42, 45], "target": -4}, "output": [3, 5]}, {"input": {"nums": [-20, -6, -3, -1, 20, 31], "target": -26}, "output": [0, 1]}, {"input": {"nums": [-5, 0, 1, 5, 7, 20, 27], "target": 20}, "output": [1, 5]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "35da7669b3e8c315"}
vcr_2a062ecb1260
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": [155, 28, 24, 194, 127, 150, 14]}, "output": false}, {"input": {"nums": [3, 1, 17, 0, 10, 10, 6]}, "output": true}, {"input": {"nums": [22, 20, 2, 1, 9, 19]}, "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": "3886783871db67bc"}
vcr_c57fd80161c0
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, 6, 12, 16, 18, 25, 26, 28, 38, 41, 48], "target": 5}, "output": 1}, {"input": {"nums": [1, 7, 9, 16, 19, 22, 25, 26, 27, 31, 33, 36], "target": 4}, "output": 1}, {"input": {"nums": [1, 2, 10, 12, 17, 25, 32, 38], "target": 36}, "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": "03f6dda91f0f97c7"}
vcr_8084c214ab06
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": [18, 8, 23, 15, 18, -16, 5, -25, -17]}, "output": [18, 26, 49, 64, 82, 66, 71, 46, 29]}, {"input": {"nums": [5, -12, -4, -4, -6, -8, 12, -8, 17, 12]}, "output": [5, -7, -11, -15, -21, -29, -17, -25, -8, 4]}, {"input": {"nums": [15, 13, 5, 18, 12]}, "output": [15, 28, 33, 51, 63]}]
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": "8d7be78050029cfe"}
vcr_695c202d8852
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": "baabaaabcaccba"}, "output": 10}, {"input": {"s": "acaabccaa"}, "output": 6}, {"input": {"s": "bbcc"}, "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": "852a372a3d2cb6a9"}
vcr_d647bd677560
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": [13, 55, 43, 23, 55, 29, 26, 54, 45, 6, 25, 30, 4, 31, 21, 3]}, "output": 42}, {"input": {"prices": [8, 4, 17, 8, 18]}, "output": 14}, {"input": {"prices": [23, 20, 40, 4, 32, 32, 9, 24, 33, 34]}, "output": 30}]
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": "d2fd361cb9dd63d2"}
vcr_cc0414853855
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": 50, "b": 32}, "output": 2}, {"input": {"a": 68, "b": 60}, "output": 4}, {"input": {"a": 174, "b": 112}, "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": "754cd69651ac62ae"}
vcr_b31ac3a0e843
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": [41, 7, 16, 49, 43, 43, 38, 37, 53, 16, 48]}, "output": 46}, {"input": {"prices": [8, 3, 10, 29, 26, 39, 5, 1, 35]}, "output": 36}, {"input": {"prices": [16, 4, 36, 11, 28, 29, 16, 25]}, "output": 32}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "8ac760b1c990a955"}
vcr_a910836fb34b
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": [2, 5, 2, 7, 2, 5]}, "output": [700, 280, 700, 200, 700, 280]}, {"input": {"nums": [1, 3, 5, 4, 2, 4, 3]}, "output": [1440, 480, 288, 360, 720, 360, 480]}, {"input": {"nums": [2, 5, 4]}, "output": [20, 8, 10]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "98849a0bfbd86b5d"}
vcr_6fbc6264a273
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": [-24, -3, -25, 0, 10, 23, 22, 0, -4, 28, -2, 13, -1, -9]}, "output": [-24, -27, -52, -52, -42, -19, 3, 3, -1, 27, 25, 38, 37, 28]}, {"input": {"nums": [-8, 15, 6, 16, -9, -9, 4]}, "output": [-8, 7, 13, 29, 20, 11, 15]}, {"input": {"nums": [4, -9, -7, -11, 17, -8, -8, 13, -1, 4]}, "output": [4, -5, -...
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": "885450f2080f0e10"}
vcr_d44bda8cbf31
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [6, 40, 13, 32, 51, 47, 38, 12, 19, 41, 24, 17, 35, 32]}, "output": 45}, {"input": {"prices": [32, 6, 31, 32, 36]}, "output": 30}, {"input": {"prices": [35, 8, 29, 17]}, "output": 21}]
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": "6ef423b77bc4dcf6"}
vcr_504378dec89b
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [5, 18, 9, -6, 17, 16, 14, -11, -15, 1]}, "output": 73}, {"input": {"nums": [6, -11, -9, -7, -7, -10, 14, 1]}, "output": 15}, {"input": {"nums": [8, -1, 12, -2, 5, 8, -9, 4, -12, -11, -4]}, "output": 30}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "eda289871dee6b3b"}
vcr_4fbc611dd1bd
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, 20, 26, 28, 31, 33, 35, 43, 46, 49], "target": 49}, "output": 10}, {"input": {"nums": [0, 8, 9, 14, 16, 20, 24, 27, 29, 30, 32, 34], "target": 14}, "output": 3}, {"input": {"nums": [6, 11, 17], "target": 39}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "576a3643943115d7"}
vcr_a31567efe22c
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": [23, 25, 24, 23, 18, 16, 26, 12, 7, 24, 7]}, "output": 115}, {"input": {"nums": [13, 15]}, "output": 15}, {"input": {"nums": [9, 9, 19, 16, 15, 15, 12, 11, 8, 8]}, "output": 63}]
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": "fedad122e61df952"}
vcr_61be95c6fb58
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": [37, 35, 51, 18, 37, 32, 32, 35, 18]}, "output": 51}, {"input": {"nums": [14, 27, 8, 1, 14, 54, 27, 1, 8]}, "output": 54}, {"input": {"nums": [19, 56, 24, 29, 19, 24, 29, 22, 22]}, "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": "3f6a1be155575167"}
vcr_d4ca7e2154be
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": 4169}, "output": 4}, {"input": {"n": 13748}, "output": 8}, {"input": {"n": 10527}, "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": "031248093cc1b46e"}
vcr_8345aff47122
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": [5, 21, 33, 21, 75, 7, 6, 4, 6, 30, 4, 2, 33, 5, 7, 30, 2]}, "output": 75}, {"input": {"nums": [9, 16, 29, 29, 9, 56, 1, 1, 4, 16, 4]}, "output": 56}, {"input": {"nums": [15, 1, 10, 29, 1, 10, 11, 15, 26, 29, 11, 26, 59]}, "output": 59}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "793071b32d778315"}
vcr_29550167998d
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["caa", "ca", "bcbb", "caba"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["aab", "aba", "aab", "bba"]}, "output": [1, 3]}, {"input": {"strs": ["aab", "aab", "bbb"]}, "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": "3a716ce3dc259a72"}
vcr_49ae17dc097a
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": ["ac", "bc", "bca", "aca"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bba", "aba", "bbb", "bba", "abb", "aaa"]}, "output": [1, 1, 1, 3]}, {"input": {"strs": ["aba", "abb", "aab", "bab", "bba"]}, "output": [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": "10ba9bb04d952ab8"}
vcr_9aeacb8ae267
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [38, 47, 23, 58, 45, 49, 39, 15, 20, 17, 12, 50, 59, 50, 2]}, "output": 47}, {"input": {"prices": [4, 27, 6, 3, 18, 20]}, "output": 23}, {"input": {"prices": [29, 39, 7, 34, 40, 15]}, "output": 33}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "f73ee0178a47a3e1"}
vcr_8728baff4492
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-30, -12, 44, 4, 78, -16, 3, -6], "target": 66}, "output": [1, 4]}, {"input": {"nums": [12, -25, 42, 41, 48, 38], "target": 80}, "output": [2, 5]}, {"input": {"nums": [30, -24, 9, -20, 31, -25, 41, 27], "target": 61}, "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": "3064eb500abb3263"}
vcr_651357ef0862
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, 11, 12, 12]}, "output": [0, 1, 4, 11, 12]}, {"input": {"nums": [0, 0, 4, 4, 5, 6, 8, 8, 8, 9, 12]}, "output": [0, 4, 5, 6, 8, 9, 12]}, {"input": {"nums": [1, 2, 6, 7, 7, 7, 7, 10, 10, 12, 12, 12]}, "output": [1, 2, 6, 7, 10, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "4c07f146acbf4654"}
vcr_00f73177d05d
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-21, -20, -18, -12, -9, 0, 13, 21, 25, 33, 36, 38], "target": -21}, "output": [0, 5]}, {"input": {"nums": [-12, -10, -2, 7, 15, 18, 24], "target": 31}, "output": [3, 6]}, {"input": {"nums": [-10, 1, 14, 27], "target": 28}, "output": [1, 3]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "4c8d01709d68e3b7"}
vcr_ee110933e0e8
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": [9, 1, 10, 7, 6, 23]}, "output": 42}, {"input": {"nums": [3, 6]}, "output": 6}, {"input": {"nums": [6, 1, 11]}, "output": 17}]
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": "f90f8405bf207c14"}