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_608bf472522d
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [7, 12, 20, 17]}, "output": 29}, {"input": {"nums": [15, 8]}, "output": 15}, {"input": {"nums": [7, 9, 5, 20]}, "output": 29}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "c7d0ff9da5516f88"}
vcr_ddd9d39e3d32
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": "ebddc a"}, "output": "a ebddc"}, {"input": {"s": "aabb b baa"}, "output": "baa b aabb"}, {"input": {"s": "a abba"}, "output": "abba a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "ce5d77625f5422c2"}
vcr_842933587ab5
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [12, 11, 35, 12, 23, 39, 11, 23, 75, 35, 39]}, "output": 75}, {"input": {"nums": [18, 1, 15, 1, 15, 18, 47]}, "output": 47}, {"input": {"nums": [12, 57, 27, 2, 25, 2, 12, 25, 16, 27, 16]}, "output": 57}]
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": "4d11af40491f10e1"}
vcr_8b990c716e07
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": [-9, -5, 1, 20, 25, 30], "target": 16}, "output": [0, 4]}, {"input": {"nums": [-14, -6, 2, 6, 11, 13, 26], "target": 37}, "output": [4, 6]}, {"input": {"nums": [-15, -5, 1, 3, 10, 17, 18, 23], "target": -4}, "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": "d998692f45f457ff"}
vcr_73f3984ddc76
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": [43, 19, 9, 30, 5, 22, 32, 20, 25, 43]}, "output": true}, {"input": {"nums": [12, 5, 5, 24, 6, 5, 0, 13, 14]}, "output": true}, {"input": {"nums": [18, 19, 24, 4, 0, 25, 21, 24]}, "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": "57af423f7d14bc5f"}
vcr_3811866d536a
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, 37, 3, 62, 6, 10, 33, 11, 10, 11, 3, 37, 33, 5, 6]}, "output": 62}, {"input": {"nums": [10, 19, 13, 13, 15, 11, 11, 19, 22, 47, 10, 22, 15]}, "output": 47}, {"input": {"nums": [27, 6, 55, 27, 6]}, "output": 55}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "2e176f57ed0fdbf5"}
vcr_22c3a78eaa4c
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, -20, -17, -15, -12, 33], "target": -47}, "output": [0, 2]}, {"input": {"nums": [-16, -13, -5, 0, 2, 10, 11, 24, 25, 27], "target": 19}, "output": [2, 7]}, {"input": {"nums": [-20, 3, 13, 28, 30], "target": 10}, "output": [0, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "4eb32ca2135e5f61"}
vcr_57db1e42d97f
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": [-1, 57, -26, 11, 8, 46, -40, -24, 79], "target": 39}, "output": [6, 8]}, {"input": {"nums": [7, 44, 47, -28, 26, 15, -20], "target": -13}, "output": [3, 5]}, {"input": {"nums": [12, 32, 4, 34], "target": 16}, "output": [0, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "05d2c12878307555"}
vcr_816f79e0acd6
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": 155, "b": 3}, "output": 1}, {"input": {"a": 143, "b": 146}, "output": 1}, {"input": {"a": 127, "b": 74}, "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": "f5d4a540cb6d7097"}
vcr_7566cbbf051d
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [0, 4, 3, 2, 0, 3, 0, 2, 0]}, "output": false}, {"input": {"nums": [2, 3, 0, 2, 0, 0, 0]}, "output": false}, {"input": {"nums": [2, 0, 2, 2, 1, 0]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "1c2c2c481938e478"}
vcr_2f9bebdb7690
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": [52, 51, 25, 5, 36, 1, 37, 9, 23, 49, 40, 50, 36]}, "output": 49}, {"input": {"prices": [7, 27, 8, 1, 39, 23, 18, 12, 26, 14]}, "output": 38}, {"input": {"prices": [4, 3, 35, 26, 35]}, "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": "816ec2abb1d67bd7"}
vcr_e1adef4b252a
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": [1, 4, 12, 34, 4, 44, 30, 14, 33, 30, 38, 2, 33, 49]}, "output": true}, {"input": {"nums": [24, 23, 22, 6, 19, 23, 25, 11]}, "output": true}, {"input": {"nums": [19, 0, 4, 9, 11, 15]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "cda4c98a18c74465"}
vcr_70b230e65fcc
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [20, 18, 1, 14, 11, 18, 24]}, "output": 58}, {"input": {"nums": [11, 5, 3, 2]}, "output": 14}, {"input": {"nums": [17, 9, 17, 10, 20]}, "output": 54}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "7fa325835e138484"}
vcr_55616a9443e9
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [7, 10, 13, 14, 16, 17, 20, 21, 22, 28, 37, 47], "target": 26}, "output": 9}, {"input": {"nums": [0, 5, 7, 9, 10, 13, 15, 18, 25, 30, 31, 37], "target": 3}, "output": 1}, {"input": {"nums": [5, 7, 8, 11, 20, 21, 24, 30, 33], "target": 25}, "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": "b6daa3291e846721"}
vcr_e85d2d22bcf0
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": 193, "b": 17}, "output": 1}, {"input": {"a": 36, "b": 58}, "output": 2}, {"input": {"a": 148, "b": 65}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "3c43f63b10cfa2ab"}
vcr_bff2f5661476
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [59, 54, 14, 8, 52, 15, 46, 54, 5, 53]}, "output": 48}, {"input": {"prices": [36, 39, 8, 23, 30, 24, 33, 1]}, "output": 25}, {"input": {"prices": [35, 20, 35, 30, 8, 33, 4, 26, 14, 13, 35]}, "output": 31}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "0c7762c4d19d9859"}
vcr_b54072bf15c2
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, 10, 20, 37, 46], "target": 20}, "output": 2}, {"input": {"nums": [9, 26, 31], "target": 5}, "output": 0}, {"input": {"nums": [1, 21, 24, 28, 33, 37], "target": 21}, "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": "96d3def3739450c1"}
vcr_7ea8f83a8182
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, 4, 5, 8, 11, 12, 12, 13, 13, 15]}, "output": [0, 1, 3, 4, 5, 8, 11, 12, 13, 15]}, {"input": {"nums": [2, 2, 4, 5, 9, 11, 12]}, "output": [2, 4, 5, 9, 11, 12]}, {"input": {"nums": [0, 9, 11, 12, 12]}, "output": [0, 9, 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": "f970b54da21eede7"}
vcr_208c4818e8f9
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [18, 28, 12]}, "output": 30}, {"input": {"nums": [11, 1, 14, 14, 1]}, "output": 26}, {"input": {"nums": [5, 19, 4, 20, 15, 16]}, "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": "758632c05aead457"}
vcr_5ba6067c3aab
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": [7, 22, 26, 33, 35], "target": 42}, "output": [0, 4]}, {"input": {"nums": [-17, -10, -4, 11, 18, 19, 25, 26, 31, 34], "target": 21}, "output": [1, 8]}, {"input": {"nums": [-16, -12, -9, -6, -5, 0, 3, 25, 28, 32], "target": 12}, "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": "070999fae999c4dc"}
vcr_cb6230000fb8
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": [30, 17, 18, 32, -5, -1, 7, -8, 1, -4, 5, 22, -10]}, "output": [30, 47, 65, 97, 92, 91, 98, 90, 91, 87, 92, 114, 104]}, {"input": {"nums": [-11, -6, 9, 3, -8, 16, 9, 4]}, "output": [-11, -17, -8, -5, -13, 3, 12, 16]}, {"input": {"nums": [-2, -5, 1, -6, -2]}, "output": [-2, -7, -6, -12, -14]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "ab8fcd9b53bc6d57"}
vcr_ccaa61b914b5
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": [27, 2, 19, 4, 7, -4, 29, 6, 25, 35, -10, 28, 10, 0]}, "output": [27, 29, 48, 52, 59, 55, 84, 90, 115, 150, 140, 168, 178, 178]}, {"input": {"nums": [-3, 1, 2, -10, 18]}, "output": [-3, -2, 0, -10, 8]}, {"input": {"nums": [-2, -1, 15, -12, -6]}, "output": [-2, -3, 12, 0, -6]}]
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": "8797568fc13db9da"}
vcr_394e65e1d74b
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": ["aaaa", "ccbc", "bbb", "aab", "aca", "cc"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["abb", "aab", "baa", "baa", "bbb"]}, "output": [1, 1, 3]}, {"input": {"strs": ["aaa", "aba", "bba"]}, "output": [1, 1, 1]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "7dbbb9362b2f8b6f"}
vcr_f308af3f2d79
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": ["ba", "cbbb", "bcbc", "ca", "abc", "cab", "abaa", "aa"]}, "output": [1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["bba", "baa", "baa", "abb"]}, "output": [2, 2]}, {"input": {"strs": ["bbb", "bba", "baa", "aba", "bba", "baa"]}, "output": [1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "8b43e5028e54958a"}
vcr_560c499ec887
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": [21, 71, 25, 24, 25, 2, 10, 21, 24, 39, 28, 38, 38, 39, 28, 10, 2]}, "output": 71}, {"input": {"nums": [11, 7, 9, 7, 19, 11, 20, 52, 20, 19, 24, 9, 24]}, "output": 52}, {"input": {"nums": [6, 6, 21, 21, 16, 16, 49]}, "output": 49}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "3d37665a3ec2d930"}
vcr_0ea6e3ebc523
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [1, 13, 4, -17, 19, -11], "k": 11}, "output": [13, 4, -17, 19, -11, 1]}, {"input": {"nums": [-7, 2, -4, -4], "k": 14}, "output": [-4, -4, -7, 2]}, {"input": {"nums": [-4, 5, -1, -4, -9], "k": 3}, "output": [-1, -4, -9, -4, 5]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "80345735fce46293"}
vcr_769e910ff07f
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-2, 8, -12, -5, 3], "k": 10}, "output": [-2, 8, -12, -5, 3]}, {"input": {"nums": [-10, 6, 7, -10, 2, -1], "k": 15}, "output": [-10, 2, -1, -10, 6, 7]}, {"input": {"nums": [-1, 7, -1, -10, -10, 0, 0, -7, -7], "k": 5}, "output": [-10, 0, 0, -7, -7, -1, 7, -1, -10]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "d11028e88b63c2fb"}
vcr_ff0d302b182f
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": [23, 45, 47, 0, 20, 22, 47, 43, 48, 24, 10, 13, 13, 7]}, "output": true}, {"input": {"nums": [8, 8, 18]}, "output": true}, {"input": {"nums": [14, 14, 11, 9, 24, 19, 3, 3, 23, 3, 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": "de29904fc488af5d"}
vcr_548679c14a2e
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": [76, 69, 55, -36, 6, -13, -1, 77, 75, -20, -31, 46, -5, 16], "target": 44}, "output": [8, 10]}, {"input": {"nums": [9, 36, 32, 40, -13], "target": 27}, "output": [3, 4]}, {"input": {"nums": [-5, 0, -14, 47, -30, 24, 7, 32, 25, 41, 1], "target": 31}, "output": [5, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "5c44222959ea87e3"}
vcr_825cb8b9d7fd
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": "abbabcccbcaba"}, "output": 10}, {"input": {"s": "ccbcbb"}, "output": 4}, {"input": {"s": "bccbbba"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "4ee7c4200058bfb6"}
vcr_706899601e74
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": "aabaaaacbbb"}, "output": 5}, {"input": {"s": "bcaabc"}, "output": 5}, {"input": {"s": "baca"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "ae035fb3b4b13eb3"}
vcr_ae3fc18aecf8
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [6, 6, 2, 0, 6]}, "output": [0, 0, 0, 432, 0]}, {"input": {"nums": [2, 5, 3, 2, 2]}, "output": [60, 24, 40, 60, 60]}, {"input": {"nums": [5, 5, 4, 2]}, "output": [40, 40, 50, 100]}]
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": "403800d75b905d67"}
vcr_2ebd613f127c
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": [30, 6, 29, 20]}, "output": 59}, {"input": {"nums": [14, 16, 20, 3, 1]}, "output": 35}, {"input": {"nums": [11, 20]}, "output": 20}]
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": "5253b55c06892aab"}
vcr_97444ee84421
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": [-13, 28, -11, 32, 13]}, "output": [-13, 15, 4, 36, 49]}, {"input": {"nums": [-12, 10, -10]}, "output": [-12, -2, -12]}, {"input": {"nums": [-12, -8, -12, 18, 6, 18]}, "output": [-12, -20, -32, -14, -8, 10]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "8bf3c80969167240"}
vcr_fe790aa3b18f
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": 29484}, "output": 8}, {"input": {"n": 15815}, "output": 10}, {"input": {"n": 12948}, "output": 6}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "bc179df4c3041022"}
vcr_34bb3ba2b472
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": [25, 35, 38, 35, 38, 4, 7, 14, 55, 14, 7, 25, 4]}, "output": 55}, {"input": {"nums": [15, 6, 21, 14, 9, 9, 43, 15, 2, 21, 2, 14, 6]}, "output": 43}, {"input": {"nums": [23, 23, 44, 3, 3]}, "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": "ac80fe305e0e726a"}
vcr_f5c3ed823d90
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [9, -16, 9, 21, -4, 4, -9, -7, -17, 17, 14, 21, -1]}, "output": 52}, {"input": {"nums": [-12, 11, -1, 12, -8, -1, -10, -10]}, "output": 22}, {"input": {"nums": [8, 13, -10, -10]}, "output": 21}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "a79389f612bfb968"}
vcr_9cd248792ccd
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [45, 31, 44, 32, 29, 48, 40, 49, 37, 13, 13, 20, 14]}, "output": 20}, {"input": {"prices": [28, 40, 31, 31, 17, 34, 40, 11, 35]}, "output": 24}, {"input": {"prices": [36, 33, 22, 35, 4]}, "output": 13}]
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": "72192473b324dc6a"}
vcr_bfbc90369d69
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": 147, "b": 208}, "output": 1}, {"input": {"a": 117, "b": 10}, "output": 1}, {"input": {"a": 52, "b": 160}, "output": 4}]
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": "0c0ed34f2f057a93"}
vcr_9292c2c208f9
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 2, 13, 17, 20, 26, 27, 34, 35, 46], "target": 46}, "output": 9}, {"input": {"nums": [28, 29, 33], "target": 13}, "output": 0}, {"input": {"nums": [0, 2, 9, 14, 15, 23, 29, 31, 34, 35, 37], "target": 25}, "output": 6}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "106b3f9fa5bf71c0"}
vcr_d9a22f6f562f
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": "baadc a a dc dbdb babd"}, "output": "babd dbdb dc a a baadc"}, {"input": {"s": "bb aabb a a aab"}, "output": "aab a a aabb bb"}, {"input": {"s": "aab aaab a baab b"}, "output": "b baab a aaab aab"}]
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": "cbeab0a483e2e6b8"}
vcr_f5e764bb0f1c
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-6, 15, -1, 12, 15, 10, 9]}, "output": 60}, {"input": {"nums": [-10, -3, -8, 4, -11, 3, -1, -11]}, "output": 4}, {"input": {"nums": [7, 8, -2, 13, -11]}, "output": 26}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "c728be15573c944b"}
vcr_e84b7670a94a
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [12, 26, 25, 1, 12, 35, 39, 1, 25, 15, 31, 39, 31, 51, 35, 15, 26]}, "output": 51}, {"input": {"nums": [3, 21, 6, 46, 21, 6, 3]}, "output": 46}, {"input": {"nums": [9, 59, 3, 11, 3, 9, 1, 11, 1]}, "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": "7d39d8d6e888c9c7"}
vcr_30dc26886079
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": ["bba", "bb", "cbac", "bcbc", "bcc"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["aaa", "bba", "bbb"]}, "output": [1, 1, 1]}, {"input": {"strs": ["baa", "bbb", "baa", "abb", "abb", "aba"]}, "output": [1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "1706ca41eaa8bfe6"}
vcr_0471d2fc6837
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [1, 16, 18, 11, 26, 5, 0, 21, 21, 3, 19]}, "output": 85}, {"input": {"nums": [13, 14, 2, 17, 16, 9, 12, 18, 6]}, "output": 58}, {"input": {"nums": [12, 2, 8, 14]}, "output": 26}]
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": "b6a6c4cfbf65ce48"}
vcr_c54acd8bcbd7
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": [10, 20, 67, 2, 2, 10, 20, 11, 11]}, "output": 67}, {"input": {"nums": [23, 3, 28, 28, 17, 3, 23, 44, 5, 26, 5, 26, 17]}, "output": 44}, {"input": {"nums": [18, 25, 8, 8, 25, 54, 18]}, "output": 54}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "0bb06a75a44bb769"}
vcr_6b8569122f0a
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [7, 7, 1, 2, 4, 3, 7, 2]}, "output": [2352, 2352, 16464, 8232, 4116, 5488, 2352, 8232]}, {"input": {"nums": [3, 3, 3, 2]}, "output": [18, 18, 18, 27]}, {"input": {"nums": [5, 4, 3]}, "output": [12, 15, 20]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "4d6137a31a899f64"}
vcr_2662651b2bf2
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": [-20, 7, -18, -11, 0], "k": 0}, "output": [-20, 7, -18, -11, 0]}, {"input": {"nums": [-8, 8, 1], "k": 4}, "output": [1, -8, 8]}, {"input": {"nums": [-8, -10, -3], "k": 10}, "output": [-3, -8, -10]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "2c5c26bed6472f1e"}
vcr_ad4c1bccd097
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, 14, 17, 1], "k": 0}, "output": [13, 14, 17, 1]}, {"input": {"nums": [0, -4, 5, 7, 2, -5, 2, 9, 1], "k": 0}, "output": [0, -4, 5, 7, 2, -5, 2, 9, 1]}, {"input": {"nums": [-10, 6, -1, -6, -10, -10, -3], "k": 0}, "output": [-10, 6, -1, -6, -10, -10, -3]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "d7090beb9cb539a8"}
vcr_550016dc2718
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-28, -23, -20, -17, -3, 3, 11, 13, 49], "target": 10}, "output": [4, 7]}, {"input": {"nums": [-19, -13, 13, 16, 19, 20, 23, 26, 31, 34], "target": 36}, "output": [2, 6]}, {"input": {"nums": [-12, -4, -1, 4, 9, 23, 29, 36, 38], "target": 0}, "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": "2a4311c475d32c28"}
vcr_d2054f2b08c9
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, 7, 8, 10, 11, 12]}, "output": [2, 7, 8, 10, 11, 12]}, {"input": {"nums": [2, 3, 4, 6, 9, 9, 10, 11]}, "output": [2, 3, 4, 6, 9, 10, 11]}, {"input": {"nums": [0, 1, 4, 5, 5, 5, 6, 10]}, "output": [0, 1, 4, 5, 6, 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": "b04b38454ef80b7b"}
vcr_c4528dab1f13
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, 15, -7, 10, 7, 21, -14]}, "output": 67}, {"input": {"nums": [6, 4, -4, 13]}, "output": 19}, {"input": {"nums": [9, -6, 1, -10, -9, -7, 11, 8, 4, -4, 10]}, "output": 29}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "7783f76f9d2bac24"}
vcr_9e90cec484c6
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": 9458}, "output": 7}, {"input": {"n": 16155}, "output": 10}, {"input": {"n": 9192}, "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": "2e7d22d6ce20c526"}
vcr_3a3df38ed2b5
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": [2, 8, 22, 16, -4, -17, -17, -11, -21, 34, 3]}, "output": [2, 10, 32, 48, 44, 27, 10, -1, -22, 12, 15]}, {"input": {"nums": [10, 15, -11, 11, 2, 14, 11, 11, -11, 5]}, "output": [10, 25, 14, 25, 27, 41, 52, 63, 52, 57]}, {"input": {"nums": [10, 2, -4, 0, 16, -9, 1]}, "output": [10, 12, 8, 8, 24, 15, ...
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "0ea9c2a552550abb"}
vcr_ba0e4ca2fc46
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, -22, -12, 7, 2, -18, 17, -23, 7, 33, 35]}, "output": [-24, -46, -58, -51, -49, -67, -50, -73, -66, -33, 2]}, {"input": {"nums": [18, 4, 6]}, "output": [18, 22, 28]}, {"input": {"nums": [10, 5, 5]}, "output": [10, 15, 20]}]
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": "0e27599513fb0c3f"}
vcr_d48b1dfc479d
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": "abcccaac"}, "output": 5}, {"input": {"s": "cbabbcbbccc"}, "output": 7}, {"input": {"s": "cacbcbaaacc"}, "output": 8}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "308fdb7f704223b6"}
vcr_e8de9fae8a53
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": [21, 9, 24, 18, 22]}, "output": 67}, {"input": {"nums": [8, 7, 13, 8]}, "output": 21}, {"input": {"nums": [9, 13]}, "output": 13}]
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": "48ea5289533e5273"}
vcr_18702f5eda70
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [12, 57, 30, 7, 29, 29, 7, 30, 12]}, "output": 57}, {"input": {"nums": [24, 15, 16, 10, 16, 15, 19, 10, 42, 19, 24]}, "output": 42}, {"input": {"nums": [13, 48, 13, 10, 10, 5, 21, 5, 21]}, "output": 48}]
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": "ef3818b4c05f3eb4"}
vcr_b3e5b9d3257b
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": [-10, 27, 33, 28, 15, 13, 17, -13, -6, -16, 6, 3, 23, -10]}, "output": [-10, 17, 50, 78, 93, 106, 123, 110, 104, 88, 94, 97, 120, 110]}, {"input": {"nums": [3, -12, -7, -9, 16, 18]}, "output": [3, -9, -16, -25, -9, 9]}, {"input": {"nums": [1, -3, 6, -7, -4, 10, -3, 12, 4, 17]}, "output": [1, -2, 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": "714f1af316b49dae"}
vcr_ad72b7f5a4ee
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": ["bbba", "baaa", "cbca", "caca", "acb"]}, "output": [1, 1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bba", "aab", "bab", "aaa", "aab"]}, "output": [1, 1, 2, 2]}, {"input": {"strs": ["abb", "bbb", "aaa", "bba", "aab", "bbb", "bba"]}, "output": [1, 1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "d59803d3167a2139"}
vcr_3825cc822779
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, 8, 11, 14, 15]}, "output": [1, 2, 3, 4, 8, 11, 14, 15]}, {"input": {"nums": [0, 0, 1, 3, 4, 5, 5, 8, 8, 8, 9]}, "output": [0, 1, 3, 4, 5, 8, 9]}, {"input": {"nums": [0, 2, 4, 8, 11]}, "output": [0, 2, 4, 8, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "251d715745086913"}
vcr_92f087e5775e
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": [14, 19, 21, 24, 25, 27, 28, 30, 37, 43, 44], "target": 6}, "output": 0}, {"input": {"nums": [1, 3, 7, 8, 9, 22, 25, 30, 34], "target": 7}, "output": 2}, {"input": {"nums": [0, 11, 20, 26, 35], "target": 13}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "c9e72c468b71c56c"}
vcr_e744e198aa35
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, 15, 9, 0]}, "output": 23}, {"input": {"nums": [17, 18]}, "output": 18}, {"input": {"nums": [8, 13, 13, 16, 11, 18, 3, 10, 20]}, "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": "559d01a6a5fc263d"}
vcr_ee5a8e60e3fd
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-22, -19, -6, -4, 1, 5, 25, 27, 28, 29, 35, 44], "target": 30}, "output": [4, 9]}, {"input": {"nums": [-16, -10, -7, -1, 3, 4, 7, 9, 16, 31, 33], "target": -7}, "output": [0, 7]}, {"input": {"nums": [-20, 0, 6, 13, 18, 38], "target": 51}, "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": "1f3aa478d267f1b2"}
vcr_1ef7501f3a12
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": [-32, 70, 26, 0, -35, -23, 73, -38, 40, -39, 71, 5], "target": 33}, "output": [7, 10]}, {"input": {"nums": [10, -11, 33, 41, 20], "target": 43}, "output": [0, 2]}, {"input": {"nums": [17, 26, 22, -17, 11, 4, 16, -10, 7, 30, -4], "target": 20}, "output": [5, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "2ba388523083e099"}
vcr_b7fe61b8ff0f
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": [-15, 3, -1, 30, -19, -4, -10, 15, 28, 17, 25, 14, 13, 29]}, "output": [-15, -12, -13, 17, -2, -6, -16, -1, 27, 44, 69, 83, 96, 125]}, {"input": {"nums": [-10, 10, 12]}, "output": [-10, 0, 12]}, {"input": {"nums": [8, 17, 15, -1, 13, -4, -6, -4, -9, -10]}, "output": [8, 25, 40, 39, 52, 48, 42, 38, 2...
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": "6a9386ffd22f68c8"}
vcr_a28784b59183
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": "abdbdcdbdba"}, "output": true}, {"input": {"s": "abcbbba"}, "output": false}, {"input": {"s": "cccc"}, "output": true}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "c88c43173a05feba"}
vcr_a1981a8964c9
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 3, 2, 1, 1, 0, 2, 0, 2, 0]}, "output": false}, {"input": {"nums": [2, 3, 2, 2, 0, 1, 0, 0]}, "output": false}, {"input": {"nums": [1, 2, 1, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "2706344024f84e81"}
vcr_7740625fda35
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": [4, 8, 9, 9, 10, 14]}, "output": [4, 8, 9, 10, 14]}, {"input": {"nums": [0, 0, 0, 1, 2, 4, 7, 7, 9, 9]}, "output": [0, 1, 2, 4, 7, 9]}, {"input": {"nums": [0, 0, 0, 3, 3, 3, 5, 5, 7, 9, 10, 11]}, "output": [0, 3, 5, 7, 9, 10, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "3dc99e577dafee52"}
vcr_7403aa534081
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [2, 2, 1, 4, 1, 2, 3, 3, 0]}, "output": true}, {"input": {"nums": [1, 1, 2, 3, 3]}, "output": true}, {"input": {"nums": [1, 0, 2, 0, 3, 3, 0, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "bafe20a05cd393bb"}
vcr_e72d889b557d
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, 11, 8, -17, -12, -7], "k": 19}, "output": [11, 8, -17, -12, -7, -8, -15]}, {"input": {"nums": [-4, -7, -2, -6], "k": 10}, "output": [-2, -6, -4, -7]}, {"input": {"nums": [6, -4, -8, -4, 6, 6], "k": 16}, "output": [-8, -4, 6, 6, 6, -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": "fa6eb2640edff3eb"}
vcr_61a54be30191
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [10, 14, 4, 18, 18, 6, 3, 5, 24]}, "output": 62}, {"input": {"nums": [14, 7, 3, 2, 8]}, "output": 25}, {"input": {"nums": [6, 17, 0, 3, 6, 3]}, "output": 23}]
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": "dbf60b8093e8061a"}
vcr_3c7bece9c888
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": "ddcdbadda"}, "output": false}, {"input": {"s": "ab"}, "output": false}, {"input": {"s": "abbcaca"}, "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": "57f35e352f597749"}
vcr_b3851dddea3c
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": "eab edb cdcac e bcacad bcab c"}, "output": "c bcab bcacad e cdcac edb eab"}, {"input": {"s": "b bbb"}, "output": "bbb b"}, {"input": {"s": "abab baaa aab abab bb"}, "output": "bb abab aab baaa abab"}]
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": "f22a88be43ad549e"}
vcr_28db4f3ab316
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-18, 3, -17, 22, 8, -9]}, "output": 30}, {"input": {"nums": [-3, 4, -3]}, "output": 4}, {"input": {"nums": [11, -9, 10, -7, 7, 8, 0, 11, 8]}, "output": 39}]
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": "96ebb4eceaea8115"}
vcr_ac1f3178c3fe
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, 6, 17, 17, 13, 6, 23, 70, 23, 5, 34, 34, 7, 19, 13, 19, 7]}, "output": 70}, {"input": {"nums": [8, 2, 23, 54, 8, 2, 23]}, "output": 54}, {"input": {"nums": [26, 18, 26, 11, 11, 20, 45, 20, 18]}, "output": 45}]
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": "d463a4d379e1f5de"}
vcr_3b976175ecd8
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [4, 0, 4, 7, 5]}, "output": [0, 560, 0, 0, 0]}, {"input": {"nums": [3, 2, 2]}, "output": [4, 6, 6]}, {"input": {"nums": [2, 2, 2, 2, 3]}, "output": [24, 24, 24, 24, 16]}]
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": "4c7db04f5298cbb0"}
vcr_cb6c9cf0f3ce
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-1, 3, 49, 10, 4, 48, -32], "target": -22}, "output": [3, 6]}, {"input": {"nums": [-22, 1, 36, -7, 19, -28, 8, 16], "target": -21}, "output": [0, 1]}, {"input": {"nums": [19, 43, 49, -16, -26, 12], "target": 27}, "output": [1, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "14332c610c801906"}
vcr_2c18716bc320
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [33, -25, 34, -2, 8, -8, 16, 28, -23]}, "output": [33, 8, 42, 40, 48, 40, 56, 84, 61]}, {"input": {"nums": [18, -8, 3]}, "output": [18, 10, 13]}, {"input": {"nums": [11, 5, 6, 8, 17]}, "output": [11, 16, 22, 30, 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": "23476532256bebf0"}
vcr_9fbe69216fd2
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": [52, 2, 47, 33, 20, 30, 54, 9, 10]}, "output": 52}, {"input": {"prices": [5, 1, 34, 3, 2, 12, 39, 19, 28, 17, 10]}, "output": 38}, {"input": {"prices": [29, 13, 5, 19, 12, 16]}, "output": 14}]
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": "d84a3cb81a4da7e6"}
vcr_c9d1421dfe9a
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, 3, 5, 7, 7, 13, 13, 15]}, "output": [0, 3, 5, 7, 13, 15]}, {"input": {"nums": [0, 5, 7, 9, 12]}, "output": [0, 5, 7, 9, 12]}, {"input": {"nums": [1, 2, 3, 5, 5, 5, 8, 9, 10, 10]}, "output": [1, 2, 3, 5, 8, 9, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "3503e71f4cd3e818"}
vcr_ecc7f3533155
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": [20, 10, -14, 0, 10, 14, 16, -16], "k": 9}, "output": [-16, 20, 10, -14, 0, 10, 14, 16]}, {"input": {"nums": [10, -6, -6, 7], "k": 1}, "output": [7, 10, -6, -6]}, {"input": {"nums": [10, 10, -2, 10], "k": 13}, "output": [10, 10, 10, -2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "e57fbb984c113391"}
vcr_5c516fd1f089
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [26, 25, 0, 7, 9, 23, 11, 22, 17, 24]}, "output": 102}, {"input": {"nums": [19, 18, 12, 0, 3, 3, 20, 9]}, "output": 54}, {"input": {"nums": [6, 1, 9, 16, 18, 13, 19]}, "output": 52}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "4a2749d5a6a1208f"}
vcr_cc3d0087bf52
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [12, 34, 29, 14, 14, 71, 33, 33, 29, 12, 34]}, "output": 71}, {"input": {"nums": [8, 2, 8, 14, 14, 17, 2, 17, 44]}, "output": 44}, {"input": {"nums": [19, 11, 8, 19, 1, 8, 15, 1, 57, 15, 11]}, "output": 57}]
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": "be07d25523440a8c"}
vcr_f82b4b8361f3
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, 5, 15, 22, 24, 25, 31, 32, 37, 38, 39, 41, 42, 43], "target": 6}, "output": 2}, {"input": {"nums": [5, 11, 17, 24, 27, 37], "target": 13}, "output": 2}, {"input": {"nums": [5, 6, 8, 9, 12, 15, 16, 20, 23, 28, 32, 35], "target": 3}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "903abd9d2b0716cc"}
vcr_382ffb9572b7
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "d dbece db"}, "output": "db dbece d"}, {"input": {"s": "babb a b"}, "output": "b a babb"}, {"input": {"s": "aaba a abbb a"}, "output": "a abbb a aaba"}]
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": "30cbb7d36b04b6ca"}
vcr_6084bca2efde
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, -25, 6, 37, 42], "target": 7}, "output": [0, 3]}, {"input": {"nums": [-15, 1, 5, 14, 23, 30, 33, 37], "target": 34}, "output": [1, 6]}, {"input": {"nums": [-17, -16, 19, 20], "target": 3}, "output": [0, 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": "369b0cbebe235139"}
vcr_e0343f9d852f
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [-6, 45, -13, 34, -22, -18, 38, 0], "target": 12}, "output": [3, 4]}, {"input": {"nums": [-28, -29, 1, -8, -25, -1, -27, 19], "target": -52}, "output": [4, 6]}, {"input": {"nums": [40, -24, 23, -28, -6, -1, 42], "target": 14}, "output": [3, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "b11faa43a68dd963"}
vcr_6f060bcc9d8a
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, -2, 10, 29, 25, -23, 33, 15]}, "output": [-14, -16, -6, 23, 48, 25, 58, 73]}, {"input": {"nums": [-3, 13, 0, -8, 4, 17, 16, -10, 0]}, "output": [-3, 10, 10, 2, 6, 23, 39, 29, 29]}, {"input": {"nums": [-8, 8, 11, 4]}, "output": [-8, 0, 11, 15]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "7148debf9f3b94d5"}
vcr_9cf46aa4fe2d
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": [26, 32, 26, 20, 27, 20, 55, 17, 27, 32, 17]}, "output": 55}, {"input": {"nums": [5, 29, 29, 40, 20, 12, 12, 6, 5, 20, 6]}, "output": 40}, {"input": {"nums": [7, 54, 1, 7, 1]}, "output": 54}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "78ea06d46705a47f"}
vcr_1fbc256c1113
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": "eae a edc d c bbdbed"}, "output": "bbdbed c d edc a eae"}, {"input": {"s": "b aaba"}, "output": "aaba b"}, {"input": {"s": "b aab"}, "output": "aab b"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "04a7bf46c3ea02aa"}
vcr_f33a989c3b52
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-18, -3, -16, -17, 17, 6, 12, -16, -2], "k": 24}, "output": [-17, 17, 6, 12, -16, -2, -18, -3, -16]}, {"input": {"nums": [10, 4, 9, -2, -5, -4, 6], "k": 19}, "output": [9, -2, -5, -4, 6, 10, 4]}, {"input": {"nums": [-8, -5, 2, 0], "k": 20}, "output": [-8, -5, 2, 0]}]
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": "cff261f5a58a8083"}
vcr_9e42f63b1335
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [11, 148, 113, 21, 179, 77, 4, 14, 99, 140, 82, 173, 63, 126, 106, 150, 0, 104]}, "output": false}, {"input": {"nums": [9, 5, 6, 4, 1, 14, 21, 4, 17, 1]}, "output": true}, {"input": {"nums": [17, 20, 23, 21, 8, 12, 21, 15, 21, 12, 2, 20]}, "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": "1101ad2e9834e3fe"}
vcr_29e65dede846
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, -24, -22, 1, 12, 16, 19, 21, 29, 33, 49], "target": -15}, "output": [1, 5]}, {"input": {"nums": [-17, -14, -13, 1, 10, 15, 32, 35, 36], "target": -31}, "output": [0, 1]}, {"input": {"nums": [6, 7, 16, 17, 20, 34, 35], "target": 13}, "output": [0, 1]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "32c635a9f2152d86"}
vcr_58c7040c4802
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-25, -22, -20, -19, -18, -5, 13, 22, 33], "target": -40}, "output": [1, 4]}, {"input": {"nums": [-20, -12, -7, -2, 5, 9, 17, 23, 25, 26, 36], "target": 32}, "output": [5, 7]}, {"input": {"nums": [-15, -9, -7, 1, 5, 19, 32, 33, 36], "target": 24}, "output": [1, 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": "4d4e44c47133e7b3"}
vcr_eb933c9d11dd
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": "aaaaacbacbb"}, "output": 6}, {"input": {"s": "cbbcaabaabcc"}, "output": 8}, {"input": {"s": "cbaaacaa"}, "output": 5}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "b3786f901daecc54"}
vcr_c09ca6d5e63b
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": 21, "b": 136}, "output": 1}, {"input": {"a": 88, "b": 163}, "output": 1}, {"input": {"a": 89, "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": "40a3fd4a31a42df2"}
vcr_4546c6d67da3
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": "bbbbacca"}, "output": 4}, {"input": {"s": "acbabcabbc"}, "output": 9}, {"input": {"s": "caacca"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "b43dc53f7495cd2a"}
vcr_a9e9f1c5a223
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 3, 2, 3, 0, 4, 0, 3, 0]}, "output": true}, {"input": {"nums": [0, 1, 1, 1, 0, 3, 2]}, "output": false}, {"input": {"nums": [3, 1, 2, 1, 1, 1, 0, 3, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "7a99aa4108874240"}
vcr_a4b7db2d81ca
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": 247, "b": 107}, "output": 1}, {"input": {"a": 70, "b": 92}, "output": 2}, {"input": {"a": 18, "b": 7}, "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": "77a10bb82ac5ee57"}