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_80e5f7ef14e0
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": [22, -13, 18, 18, 6, 22, 15, -6, -11, -12, -4, 21]}, "output": 88}, {"input": {"nums": [14, 9, 3, 3, 0, -8, -6, 0, -4, -5, -7]}, "output": 29}, {"input": {"nums": [-4, 13, -12, -8, 7, -2, -2, 12, 10, 2]}, "output": 27}]
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": "7525e1c5db4711f0"}
vcr_33480a5f5487
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": 77, "b": 143}, "output": 11}, {"input": {"a": 121, "b": 79}, "output": 1}, {"input": {"a": 48, "b": 48}, "output": 48}]
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": "212c99a0a869ba7f"}
vcr_c6fcb63e336a
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-14, 4, 6, -3, -7, 14, 16, 0, -3]}, "output": 30}, {"input": {"nums": [-11, 3, -5, -8, 0, 3, 13, -9, 6, 0]}, "output": 16}, {"input": {"nums": [-10, 4, 10]}, "output": 14}]
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": "992575c4a22a28e6"}
vcr_417b9bb3c23f
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": [0, -19, 18, 32, 16, 10, 33, -23, 18, 27, 24, -1, -13, -6]}, "output": [0, -19, -1, 31, 47, 57, 90, 67, 85, 112, 136, 135, 122, 116]}, {"input": {"nums": [-11, 8, 4, 10, 14, -3, -2, -11]}, "output": [-11, -3, 1, 11, 25, 22, 20, 9]}, {"input": {"nums": [-2, 17, -1, 17, 14, -1, 6, -5, 9, 6]}, "output"...
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "f0fd7b7c6674e328"}
vcr_015e584a19cb
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": [-4, 6, 11, 25, 29, 36, 49], "target": 61}, "output": [3, 5]}, {"input": {"nums": [-17, -16, -14, -11, -6, -3, 10, 14, 23, 37], "target": 0}, "output": [2, 7]}, {"input": {"nums": [-9, 5, 10, 11, 38, 39], "target": 43}, "output": [1, 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": "0746e4beeb09d6e0"}
vcr_c68edbf3c8c2
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["caa", "bcac", "aac", "bcc", "ccb"]}, "output": [1, 2, 2]}, {"input": {"strs": ["bab", "aba", "aba", "bab"]}, "output": [2, 2]}, {"input": {"strs": ["bab", "aab", "aaa", "bab"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "78ffe888a115e8b2"}
vcr_58d4cd498fe2
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, 1, 2, 10, 15, 27], "target": -28}, "output": [0, 1]}, {"input": {"nums": [-19, 6, 11, 33, 36], "target": 47}, "output": [2, 4]}, {"input": {"nums": [-20, -15, -14, -10, -8, -7, 2, 14, 31, 39], "target": 41}, "output": [6, 9]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "ed6bbb1d8640b59d"}
vcr_cd62906c4fa8
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": "bbabaabaaabab"}, "output": 9}, {"input": {"s": "bccbbcc"}, "output": 4}, {"input": {"s": "aca"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "eceec92c897a125d"}
vcr_bad1ae3325b3
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, -11, 16, 3, 7]}, "output": 26}, {"input": {"nums": [-3, 2, -4, -2]}, "output": 2}, {"input": {"nums": [3, 8, 13, 1, 11, -7]}, "output": 36}]
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": "b2e20c8ea1b2a72f"}
vcr_7962f3aadf0f
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, 27, 19, 21, 14]}, "output": 48}, {"input": {"nums": [14, 13, 9]}, "output": 23}, {"input": {"nums": [16, 20, 4, 9, 4, 14, 6, 12, 17]}, "output": 60}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "2f56329622065daa"}
vcr_01a769762efb
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": 24580}, "output": 3}, {"input": {"n": 11425}, "output": 6}, {"input": {"n": 10292}, "output": 5}]
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": "e40e7b28be0addb0"}
vcr_ed207cdc0dda
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": [30, -30, -5, -4, 46, -6, 10, 72], "target": -9}, "output": [2, 3]}, {"input": {"nums": [47, 16, -23, 22], "target": -7}, "output": [1, 2]}, {"input": {"nums": [-9, 8, 30, 5, 49, 24, 15, 39, 41, 42, 19], "target": 35}, "output": [2, 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": "7b2ddc568272e39e"}
vcr_6fe2062f7a46
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, 8, 10, 15, 18, 19, 20, 25, 26, 30, 37, 41], "target": 30}, "output": 9}, {"input": {"nums": [2, 5, 7, 10, 13, 21, 23, 29, 38], "target": 31}, "output": 8}, {"input": {"nums": [1, 3, 19, 20, 29, 30, 31], "target": 31}, "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": "2e327f32fd32a3ac"}
vcr_0036d0656121
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": "aacccccba"}, "output": 4}, {"input": {"s": "ccbacbbaac"}, "output": 7}, {"input": {"s": "cacbcacb"}, "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": "b000ff91489f9827"}
vcr_a0ed77d07748
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": 32352}, "output": 8}, {"input": {"n": 3551}, "output": 10}, {"input": {"n": 14325}, "output": 11}]
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": "64ceb0e7f8e3f6af"}
vcr_edeb7f9d233d
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [43, 37, 42, 15, 32, 18, 26, 19, 1]}, "output": 17}, {"input": {"prices": [25, 1, 18, 22, 4, 26, 11, 32]}, "output": 31}, {"input": {"prices": [12, 2, 39, 37]}, "output": 37}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "1a2c20f99f56a790"}
vcr_4c7a2947e4e8
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": [31, 13, 13, 13, -22, 30, -10, -9, 19, 27, 32]}, "output": [31, 44, 57, 70, 48, 78, 68, 59, 78, 105, 137]}, {"input": {"nums": [9, -10, -8, 17, 16, -9]}, "output": [9, -1, -9, 8, 24, 15]}, {"input": {"nums": [5, 14, 1, 2, 13]}, "output": [5, 19, 20, 22, 35]}]
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": "841ee4a4a8b2ea63"}
vcr_1ec5f329d58e
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, 4, 0, 2, 3, 3, 0]}, "output": true}, {"input": {"nums": [3, 2, 2, 1, 1]}, "output": true}, {"input": {"nums": [3, 1, 0, 0, 3, 0, 3, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "e9350ab2fbbf275f"}
vcr_e75a6af8d273
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 6, 9, 19, 21, 25, 27, 31, 35, 38, 40, 44, 49], "target": 33}, "output": 8}, {"input": {"nums": [1, 3, 12, 13, 14, 27], "target": 26}, "output": 5}, {"input": {"nums": [1, 9, 15, 18, 19, 22, 23, 24, 29, 36], "target": 34}, "output": 9}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "d32acca16479b50b"}
vcr_1b8ff0856f92
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [9, 10, 14, 24, 41, 47], "target": 17}, "output": 3}, {"input": {"nums": [0, 2, 14, 21, 23, 26, 27, 36, 37, 38, 39], "target": 28}, "output": 7}, {"input": {"nums": [8, 9, 10, 17, 34], "target": 25}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "48a8276687883fca"}
vcr_371595bb7ab3
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": ["bca", "bac", "cc", "bb"]}, "output": [1, 1, 2]}, {"input": {"strs": ["aab", "bab", "bba", "abb", "bab", "baa"]}, "output": [2, 4]}, {"input": {"strs": ["aab", "bba", "aba", "aaa", "bbb", "bbb", "aab"]}, "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": "9de1bb3502e32c78"}
vcr_3abe0773dcc4
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 4, 1, 0, 4, 2, 4, 2, 0]}, "output": true}, {"input": {"nums": [1, 0, 3, 1, 1, 2, 2]}, "output": false}, {"input": {"nums": [1, 0, 1, 0, 2, 2, 3]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "50d1deb04decfca0"}
vcr_5806ec6ef7d2
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, 6, -18, 11, 3, 20, 1, -7, -1, -14], "k": 6}, "output": [3, 20, 1, -7, -1, -14, 8, 6, -18, 11]}, {"input": {"nums": [-5, 10, 8, -7, 1, 4, 3, 3, 5], "k": 5}, "output": [1, 4, 3, 3, 5, -5, 10, 8, -7]}, {"input": {"nums": [4, -10, 6, 3], "k": 16}, "output": [4, -10, 6, 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": "6c1099e56e877271"}
vcr_8d0e117e9902
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": "bbdceb d cbdeda baedb"}, "output": "baedb cbdeda d bbdceb"}, {"input": {"s": "a baab b"}, "output": "b baab a"}, {"input": {"s": "ba aa b b abbb"}, "output": "abbb b b aa ba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "588655c41bd058df"}
vcr_a6d3edb00802
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [17, 20, 16]}, "output": 33}, {"input": {"nums": [9, 17, 11, 18, 13, 0, 1, 14, 11, 3]}, "output": 52}, {"input": {"nums": [2, 0, 19]}, "output": 21}]
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": "c0ee0d2057c276a5"}
vcr_9d43225d2719
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": [23, 74, 76, 72, 49], "target": 125}, "output": [2, 4]}, {"input": {"nums": [23, 11, -17, 40, 43, 14, -29, 7], "target": 50}, "output": [4, 7]}, {"input": {"nums": [18, 34, -2, 9, -9, 48, 15], "target": 82}, "output": [1, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "7c130e3e75326e97"}
vcr_77ded9978090
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": "bddbabcbabddb"}, "output": true}, {"input": {"s": "cb"}, "output": false}, {"input": {"s": "ccaabacbba"}, "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": "217205089b97e551"}
vcr_fa1942463b77
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, 3, 3, 0, 4, 0]}, "output": true}, {"input": {"nums": [1, 3, 3, 2, 1]}, "output": true}, {"input": {"nums": [0, 2, 3, 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": "29a19ae35a23a9aa"}
vcr_601bdf14feb4
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": [8, 9, 13, 15, 26, 42], "target": 2}, "output": 0}, {"input": {"nums": [0, 6, 7, 12, 20, 21, 23, 24, 29], "target": 4}, "output": 1}, {"input": {"nums": [2, 8, 14, 15, 27, 30, 38], "target": 19}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "baa16e4c1f4d4c0e"}
vcr_459ca1052ebc
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 2, 13, 15, 33], "target": 3}, "output": 2}, {"input": {"nums": [10, 12, 19, 21, 25, 27, 29, 30, 34, 39], "target": 28}, "output": 6}, {"input": {"nums": [7, 32, 33], "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": "d101458ebc860c41"}
vcr_c681b4c37ad6
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": [-37, 51, 0, -4, -13, 31, 41, 14, 19, 48, -2], "target": 89}, "output": [6, 9]}, {"input": {"nums": [16, 37, 3, -3, 30, -16], "target": 27}, "output": [3, 4]}, {"input": {"nums": [46, 0, 49, 15, 21, 35, 14, 6, -26, 13, -21], "target": 64}, "output": [2, 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": "05350324dc4ad37e"}
vcr_046cdffc0071
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-23, -10, -5, 7, 17, 33, 34, 40, 42], "target": 7}, "output": [1, 4]}, {"input": {"nums": [-9, -8, -3, 1, 2, 7, 15, 24, 36], "target": 43}, "output": [5, 8]}, {"input": {"nums": [-14, -9, -3, 8, 18, 27, 31, 32, 35, 39], "target": 4}, "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": "b5e0daa44e730aed"}
vcr_410a7448d40a
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": [53, 53, 43, 4, 8, 27, 13, 60, 14, 60]}, "output": 56}, {"input": {"prices": [17, 29, 20, 27, 38, 17, 15, 16, 28, 37, 37]}, "output": 22}, {"input": {"prices": [7, 31, 7, 34, 32, 23, 26, 30, 34, 10, 17, 39]}, "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": "1ba5b800ef1b09d7"}
vcr_2047b6ce55ba
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, 0, 14, 18, 14, -17, -15]}, "output": 67}, {"input": {"nums": [-1, 1, -7, -8, 10, -5]}, "output": 10}, {"input": {"nums": [11, 7, -4, 6, 3, 13, 10, -2, 14, 3]}, "output": 61}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "27aa107fa4d42de2"}
vcr_44c1ebd6ff79
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["cbb", "aac", "ba", "bb", "cb", "cbcc", "cc", "cba", "bb"]}, "output": [1, 1, 1, 1, 1, 1, 1, 2]}, {"input": {"strs": ["baa", "aaa", "abb", "aab", "aab", "baa", "baa"]}, "output": [1, 1, 5]}, {"input": {"strs": ["aaa", "abb", "abb"]}, "output": [1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "e88d2a90a97f4624"}
vcr_675a0598b620
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-23, -13, -7, 4, 6, 12, 16, 27, 30], "target": -7}, "output": [0, 6]}, {"input": {"nums": [-19, -15, -13, -12, -10, -3, 4, 8, 32, 37], "target": -5}, "output": [2, 7]}, {"input": {"nums": [-14, 9, 12, 21, 22, 26, 27], "target": 48}, "output": [3, 6]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "e35dfc0c980904f2"}
vcr_1d87b858f31e
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": ["aca", "cbcb", "cbc", "cabb"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bab", "abb", "aba", "aab", "aab", "bbb", "baa"]}, "output": [1, 2, 4]}, {"input": {"strs": ["aba", "aaa", "bbb", "aba"]}, "output": [1, 1, 2]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "2f062affb3d9bffc"}
vcr_91ef57396bbc
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, 11, 12, 23, 24, 26, 36, 38, 39, 41, 42, 44, 49], "target": 0}, "output": 0}, {"input": {"nums": [2, 6, 9, 13, 15, 16, 17, 20, 23, 38], "target": 36}, "output": 9}, {"input": {"nums": [2, 6, 9, 10, 21, 24, 27, 30, 33, 34], "target": 7}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "e4442fb9bde38ed1"}
vcr_ed75a7605764
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, 5, 9, -18, -17, -11, 10, -18, -16, 16, 15, 5]}, "output": 36}, {"input": {"nums": [12, 1, 10]}, "output": 23}, {"input": {"nums": [-6, 14, -12, 13, -7, -11, -7, 1, 9]}, "output": 15}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "1759cad05eb4cd27"}
vcr_e77ed4c3ac7d
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 1, 7, 9, 9, 11, 12, 15]}, "output": [0, 1, 7, 9, 11, 12, 15]}, {"input": {"nums": [0, 0, 2, 3, 3, 8, 8, 9, 12]}, "output": [0, 2, 3, 8, 9, 12]}, {"input": {"nums": [0, 1, 4, 5, 6, 8]}, "output": [0, 1, 4, 5, 6, 8]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "cf2ea53966541c17"}
vcr_22b2dca999bd
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["bb", "cbba", "ca", "bac", "abba", "acbc", "ca", "ac"]}, "output": [1, 1, 1, 1, 1, 3]}, {"input": {"strs": ["bbb", "baa", "aba", "aba", "aaa", "abb", "aba"]}, "output": [1, 1, 1, 4]}, {"input": {"strs": ["abb", "abb", "aba", "bbb", "bab"]}, "output": [1, 1, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "ff2000d7b634456d"}
vcr_80658180b3a3
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [4, -5, -20, 10, 19, 16, 0], "k": 14}, "output": [4, -5, -20, 10, 19, 16, 0]}, {"input": {"nums": [9, 9, -9, 9, -10], "k": 3}, "output": [-9, 9, -10, 9, 9]}, {"input": {"nums": [-9, 5, 0, -9, 6, -9], "k": 10}, "output": [0, -9, 6, -9, -9, 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": "6a2e5fb6c0f5a498"}
vcr_03fac1435e84
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 3, 1, 3, 1, 4, 4, 1, 0]}, "output": true}, {"input": {"nums": [2, 3, 2]}, "output": true}, {"input": {"nums": [2, 2, 2, 2, 1, 3, 3, 3, 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": "1168ac918afc55ef"}
vcr_b1a5b1d7e86e
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 5, 6, 4, 3, 4]}, "output": [1440, 576, 480, 720, 960, 720]}, {"input": {"nums": [1, 1, 3, 4, 1, 3, 2]}, "output": [72, 72, 24, 18, 72, 24, 36]}, {"input": {"nums": [4, 4, 5, 2, 4, 1, 3]}, "output": [480, 480, 384, 960, 480, 1920, 640]}]
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": "5d7c0d7c47ba78f5"}
vcr_6747dd174160
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": [29, 16, 19, 17, -20, 31, 29, 25, 26, 2, -16, 33, 28, -12]}, "output": [29, 45, 64, 81, 61, 92, 121, 146, 172, 174, 158, 191, 219, 207]}, {"input": {"nums": [10, 8]}, "output": [10, 18]}, {"input": {"nums": [18, 9, 7, -4, -1, 12, 4]}, "output": [18, 27, 34, 30, 29, 41, 45]}]
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": "37d72783566ae9d4"}
vcr_fbaf1c759a0d
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [58, 25, 11, 9, 32, 29, 29, 28, 50, 21, 60]}, "output": 51}, {"input": {"prices": [20, 28, 8, 13, 32, 6, 8, 13, 12, 13]}, "output": 24}, {"input": {"prices": [18, 24, 27, 9, 5, 2, 37, 10, 29]}, "output": 35}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "593455de3c326490"}
vcr_55f1b2c5b586
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [-5, -4, -15, 19, 15, 21, 13, -5, 4, -2, -1, -8, 0]}, "output": 68}, {"input": {"nums": [0, 8, 2, -11, 1, 7]}, "output": 10}, {"input": {"nums": [13, -5, -9, -11, 13, 3, 1, 10, 2, 10, -9]}, "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": "f4a3d94dc44e30af"}
vcr_5afc62d067da
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-14, -12, 2, -19, 1, -11], "k": 16}, "output": [2, -19, 1, -11, -14, -12]}, {"input": {"nums": [10, -1, 1, -6, -10, 1, 2, -1, 8], "k": 5}, "output": [-10, 1, 2, -1, 8, 10, -1, 1, -6]}, {"input": {"nums": [3, 1, 0, -8, -5, -6, -7, 3, 0, -5], "k": 10}, "output": [3, 1, 0, -8, -5, -6, -7, 3, 0, -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": "d6c1825665a31a4e"}
vcr_20c016df19b2
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [14, -20, 8, -24, 18, -18, 27, -23]}, "output": [14, -6, 2, -22, -4, -22, 5, -18]}, {"input": {"nums": [14, -7, -1]}, "output": [14, 7, 6]}, {"input": {"nums": [14, -10, 11, 16, 5, 14, -6]}, "output": [14, 4, 15, 31, 36, 50, 44]}]
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": "85575698e9847461"}
vcr_c07bf046cca9
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 1, 5, 6, 4, 1]}, "output": [120, 120, 24, 20, 30, 120]}, {"input": {"nums": [3, 4, 1, 2, 5, 1, 1]}, "output": [40, 30, 120, 60, 24, 120, 120]}, {"input": {"nums": [3, 3, 3]}, "output": [9, 9, 9]}]
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": "472893f79c26ed5f"}
vcr_4450ff80a217
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": [37, 54, 51, 37, 4, 53, 2, 56, 57, 15]}, "output": 55}, {"input": {"prices": [1, 3, 29, 18, 37, 33]}, "output": 36}, {"input": {"prices": [5, 11, 28]}, "output": 23}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "19197e7e0c4aeec1"}
vcr_43d1062c7a67
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": [14, 41, 36, 26, 51, 14, 50, 30, 21, 29, 4, 57, 27]}, "output": 53}, {"input": {"prices": [20, 16, 1, 15, 12, 18, 8, 15, 18, 28, 23, 22]}, "output": 27}, {"input": {"prices": [35, 23, 40, 29, 40, 3, 38, 19, 34, 24, 18, 21]}, "output": 35}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "130a8300cf9722f1"}
vcr_ba0f7891ec4b
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [4, 24, -18]}, "output": [4, 28, 10]}, {"input": {"nums": [10, 18, -1, -6, 7, -8, 5, 12, 4, 18]}, "output": [10, 28, 27, 21, 28, 20, 25, 37, 41, 59]}, {"input": {"nums": [5, 4, 10, 9, 1, -4]}, "output": [5, 9, 19, 28, 29, 25]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "455ebf9c764d0624"}
vcr_e4682f0654d8
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": [90, 8, 7, 135, 164, 18, 140, 56]}, "output": false}, {"input": {"nums": [18, 8, 21, 2, 14]}, "output": false}, {"input": {"nums": [18, 9, 21, 20, 23, 20, 11, 20, 12, 4, 24, 25]}, "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": "3bdd174e1b06ce53"}
vcr_b6881da42ff6
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": [7, 5, 2, 31, 48, 43, 25, 5, 47, 8, 18, 24, 48, 18, 29, 41, 43, 16]}, "output": true}, {"input": {"nums": [3, 11, 10, 18, 14, 6, 9, 23, 14, 11, 20, 6]}, "output": true}, {"input": {"nums": [3, 21, 10, 6, 15, 16, 14, 8, 24, 18, 22, 25]}, "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": "10b0b521c0f186ca"}
vcr_2bbe04d8b508
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": [3, 27, 20, -23, 8, -5]}, "output": [3, 30, 50, 27, 35, 30]}, {"input": {"nums": [-8, 4, 5, 9, -11, 18, 17, 11, 1, -1]}, "output": [-8, -4, 1, 10, -1, 17, 34, 45, 46, 45]}, {"input": {"nums": [-9, -11]}, "output": [-9, -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": "25c4675e8d58f338"}
vcr_d4928bb23b26
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 5, 5, 6, 6, 6, 7, 12, 12, 15, 15, 15]}, "output": [0, 2, 5, 6, 7, 12, 15]}, {"input": {"nums": [0, 0, 0, 1, 1, 3, 4, 5, 8, 9]}, "output": [0, 1, 3, 4, 5, 8, 9]}, {"input": {"nums": [1, 2, 9, 9, 10, 11]}, "output": [1, 2, 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": "0477be2c421c833d"}
vcr_9b93e2d2282e
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, 10, 14, 14, 11, 34, 35, 3, -5]}, "output": [-24, -14, 0, 14, 25, 59, 94, 97, 92]}, {"input": {"nums": [12, -2, 2, -6, -5, 18, 3]}, "output": [12, 10, 12, 6, 1, 19, 22]}, {"input": {"nums": [17, 8, 15, 3, 12]}, "output": [17, 25, 40, 43, 55]}]
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": "f8c5057e7a75546a"}
vcr_288d855dd898
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": [8, 9, 9, 15, 16, 34, 16, 34, 21, 38, 38, 12, 78, 15, 21, 12, 8]}, "output": 78}, {"input": {"nums": [1, 29, 29, 11, 42, 2, 21, 11, 1, 21, 2]}, "output": 42}, {"input": {"nums": [8, 9, 7, 9, 16, 5, 7, 8, 16, 5, 27, 53, 27]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "f8edd350a47e8e39"}
vcr_fdf589d5004e
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, 4, 1, 1, 4, 2, 0]}, "output": true}, {"input": {"nums": [3, 3, 1]}, "output": true}, {"input": {"nums": [3, 0, 1, 0, 2, 2, 0, 1, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "a2d56079aaaff035"}
vcr_3602c54662b8
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": [27, 17, 44, 34, 24, 41, 52, 36, 41, 4, 21, 59]}, "output": 55}, {"input": {"prices": [8, 6, 16]}, "output": 10}, {"input": {"prices": [24, 4, 5, 2, 38, 38, 18, 19, 12, 25, 16, 37]}, "output": 36}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "eec1c92fce0c4f59"}
vcr_8acd69967b9a
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -22, -21, -17, 0, 14, 21, 43], "target": 43}, "output": [4, 7]}, {"input": {"nums": [-14, -10, 15, 17, 25, 33], "target": 23}, "output": [1, 5]}, {"input": {"nums": [-11, -9, -8, -5, 4, 8, 14, 17, 30], "target": 9}, "output": [2, 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": "078b1ce4970b82b8"}
vcr_d74662e0bcbe
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [5, 4, 14, 14, 9, 20]}, "output": 39}, {"input": {"nums": [18, 16, 8, 5, 18]}, "output": 44}, {"input": {"nums": [17, 9, 6, 11, 7, 10, 0, 13, 12]}, "output": 51}]
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": "1c0f5ad199a6c622"}
vcr_1149a8eaab9b
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": [0, 0, 17, -11, -19, 5, 11, -17, 2, 11, 10], "k": 12}, "output": [10, 0, 0, 17, -11, -19, 5, 11, -17, 2, 11]}, {"input": {"nums": [-1, -8, 0, -9, 3, 8, -2, -5], "k": 7}, "output": [-8, 0, -9, 3, 8, -2, -5, -1]}, {"input": {"nums": [5, 3, 4, -4, -3, 4, -3, 10, 10, 7], "k": 4}, "output": [-3, 10, 10, ...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "2670d8d9f86b2c8a"}
vcr_36cdf9b4a9c4
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [8, 21, 43, 22, 31, 6, 15, 16, 12, 4, 33, 4, 30, 2, 45, 33]}, "output": true}, {"input": {"nums": [8, 0, 20, 22, 24, 12, 22, 25, 4, 25]}, "output": true}, {"input": {"nums": [5, 9, 16, 25, 17, 5, 15, 8, 10, 7, 22, 21]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "c92a91eb5aa87aaf"}
vcr_1979182bf4f9
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 4, 2, 4, 1, 6, 2]}, "output": [384, 192, 384, 192, 768, 128, 384]}, {"input": {"nums": [1, 4, 4, 3, 4]}, "output": [192, 48, 48, 64, 48]}, {"input": {"nums": [5, 2, 1, 2, 3, 5, 3]}, "output": [180, 450, 900, 450, 300, 180, 300]}]
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": "6c5304b47ce06479"}
vcr_bcab560c271d
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": "aac bce ca db ae bdddcd dabe"}, "output": "dabe bdddcd ae db ca bce aac"}, {"input": {"s": "bbba baa bbbb"}, "output": "bbbb baa bbba"}, {"input": {"s": "bbba aba"}, "output": "aba bbba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "9304646855a3029a"}
vcr_3c2fbe23bf4b
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, 2, 6, 20, 30, 20, 15, 13, 15, 15, 13, 34]}, "output": true}, {"input": {"nums": [11, 9, 13, 4, 8, 3]}, "output": false}, {"input": {"nums": [17, 18, 22, 23, 22, 1, 6]}, "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": "7cc1aa5f318afddb"}
vcr_3d60c4a4cdb9
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [6, 4, 7, 7, 2, 7, 2, 6]}, "output": [32928, 49392, 28224, 28224, 98784, 28224, 98784, 32928]}, {"input": {"nums": [2, 2, 3, 1, 1, 4]}, "output": [24, 24, 16, 48, 48, 12]}, {"input": {"nums": [3, 1, 1, 3, 4, 4]}, "output": [48, 144, 144, 48, 36, 36]}]
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": "470892342b33dacf"}
vcr_2f36d246c014
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, 2, 4, 2, 2]}, "output": [32, 64, 32, 64, 64]}, {"input": {"nums": [1, 2, 1, 5, 3, 3, 5]}, "output": [450, 225, 450, 90, 150, 150, 90]}, {"input": {"nums": [2, 4, 2, 2, 4, 5]}, "output": [320, 160, 320, 320, 160, 128]}]
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": "a3cb4a610cd605d2"}
vcr_618a1fb5ee34
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": "cacabbcbcab"}, "output": 10}, {"input": {"s": "aba"}, "output": 3}, {"input": {"s": "cabcbcaacbb"}, "output": 9}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "21a4b6c88cd078d0"}
vcr_f44e83aeb512
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": "cd daaabd cdd eebb"}, "output": "eebb cdd daaabd cd"}, {"input": {"s": "aa aab"}, "output": "aab aa"}, {"input": {"s": "ba aabb"}, "output": "aabb ba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "c4ef4ddaff404fe8"}
vcr_28f587a97acc
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, 19, 2, 20, 7, 5, 26, 19, 22, 25, 13]}, "output": 100}, {"input": {"nums": [19, 20, 10, 19, 2, 12, 17, 16, 13]}, "output": 69}, {"input": {"nums": [9, 11, 18, 7, 14, 4, 5, 17, 7]}, "output": 58}]
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": "7e6434634577d769"}
vcr_fbfba9c9eee7
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [0, 8, 25, 30, 32, 34, 38, 40, 44], "target": 36}, "output": 6}, {"input": {"nums": [13, 19, 28, 31, 35], "target": 27}, "output": 2}, {"input": {"nums": [0, 2, 10, 18, 27, 29], "target": 38}, "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": "d8f682eceb9119bc"}
vcr_8950df215167
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": "aad bd bba bbe"}, "output": "bbe bba bd aad"}, {"input": {"s": "bb aab"}, "output": "aab bb"}, {"input": {"s": "bbaa bb baa abb bbbb"}, "output": "bbbb abb baa bb bbaa"}]
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": "7969bae2c32ae6a2"}
vcr_28e273111653
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, 21, 23, 39, 45], "target": 44}, "output": [1, 2]}, {"input": {"nums": [-15, -14, -9, -8, -4, 3, 10, 38], "target": -12}, "output": [0, 5]}, {"input": {"nums": [-20, -8, 3, 4, 31, 33, 37], "target": 34}, "output": [2, 4]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "c3fb3dfc6332be12"}
vcr_8c2d81637582
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": "bbaacababaaa"}, "output": 8}, {"input": {"s": "babb"}, "output": 3}, {"input": {"s": "bbcacbbcbaab"}, "output": 9}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "5183476e6b327317"}
vcr_9394fac5bd70
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-9, -1, -16, 7, 16], "k": 5}, "output": [-9, -1, -16, 7, 16]}, {"input": {"nums": [-9, -3, -7, 4], "k": 17}, "output": [4, -9, -3, -7]}, {"input": {"nums": [-2, 1, -2, -1, -8, -6, 9, 0, -5], "k": 7}, "output": [-2, -1, -8, -6, 9, 0, -5, -2, 1]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "177f69d68f680560"}
vcr_da00654687bc
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": [22, 28, 12, 6, 14, 11, 34, 21, 5, 21, 23, 8, 36, 29, 16, 40, 8]}, "output": true}, {"input": {"nums": [12, 13, 15, 14, 3, 20, 19, 1, 5, 6, 6]}, "output": true}, {"input": {"nums": [17, 4, 17, 25, 22, 14, 16, 23, 25]}, "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": "424374bc35666bbd"}
vcr_9d03aeea0c2c
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": [-24, 32, -10, -39, 26, 21, 79, -9, 24], "target": 50}, "output": [4, 8]}, {"input": {"nums": [15, 3, 47, -22], "target": 62}, "output": [0, 2]}, {"input": {"nums": [3, 11, 32, -13, -1, 16, 10, -26, 28, -6, 47], "target": 50}, "output": [0, 10]}]
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": "c8ffce11fcc1c5d9"}
vcr_92f6c1b40d0f
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": "caacbbbcabcbcc"}, "output": 10}, {"input": {"s": "abababcbbbb"}, "output": 8}, {"input": {"s": "acb"}, "output": 3}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "48f6820792fabbce"}
vcr_1d80b85310ef
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": [35, 8, 10, 75, 3, 8, 10, 3, 35, 13, 13]}, "output": 75}, {"input": {"nums": [3, 58, 2, 8, 3, 8, 2]}, "output": 58}, {"input": {"nums": [20, 20, 44, 29, 22, 4, 4, 29, 22]}, "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": "0426184d488b3f68"}
vcr_0513d0c117a5
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": [-33, 65, 27, 35, -31], "target": 4}, "output": [3, 4]}, {"input": {"nums": [0, 37, 28, 14, 21, 22, 40, 35, 13, 49, -6], "target": 28}, "output": [0, 2]}, {"input": {"nums": [-30, 6, 27, 20, 23, 36, 19, -9, 47, 41, -4], "target": 33}, "output": [1, 2]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "ff7e81716042be3b"}
vcr_3fd5f7cdf5e0
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [11, -5, 10, 20, 10, -4, -3, -5, 13], "k": 23}, "output": [10, -4, -3, -5, 13, 11, -5, 10, 20]}, {"input": {"nums": [-3, 3, 1, -9, 2, -7, 4], "k": 16}, "output": [-7, 4, -3, 3, 1, -9, 2]}, {"input": {"nums": [-4, -3, 0, 1], "k": 16}, "output": [-4, -3, 0, 1]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "04e9bc83bd15e97c"}
vcr_bdb52b563cbd
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [6, 22, 30, 33, 40, 41, 42], "target": 33}, "output": 3}, {"input": {"nums": [3, 11, 24, 30], "target": 14}, "output": 2}, {"input": {"nums": [2, 4, 6, 15, 16, 21, 22, 27, 29, 34], "target": 33}, "output": 9}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "75c82bfdf4cfdf20"}
vcr_5e379a1b2122
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": "b db b bba b"}, "output": "b bba b db b"}, {"input": {"s": "ab b babb aaa babb"}, "output": "babb aaa babb b ab"}, {"input": {"s": "b aa"}, "output": "aa 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": "e35a802c99f23776"}
vcr_2311f379998e
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": [-7, 44, 65, 67, 32, -33, 9, 30, 2, -1, 6, 27], "target": 32}, "output": [2, 5]}, {"input": {"nums": [1, -22, -23, -24, 42, 16], "target": 18}, "output": [3, 4]}, {"input": {"nums": [-14, 25, 22, -12], "target": 11}, "output": [0, 1]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "3609a722a4588edc"}
vcr_6159282e51f9
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, 2, 4, 6, 6, 7, 8, 8, 10, 13, 14]}, "output": [1, 2, 4, 6, 7, 8, 10, 13, 14]}, {"input": {"nums": [1, 3, 5, 6, 8, 11]}, "output": [1, 3, 5, 6, 8, 11]}, {"input": {"nums": [0, 0, 0, 2, 4, 7, 8, 8, 8, 9]}, "output": [0, 2, 4, 7, 8, 9]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "7cb1ccd26cec2fcb"}
vcr_526cd25201af
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": [5, 27, 47, 37, 60, 7, 58, 51, 41, 18, 9]}, "output": 55}, {"input": {"prices": [30, 23, 36]}, "output": 13}, {"input": {"prices": [9, 37, 29, 29, 25, 9, 7, 34]}, "output": 28}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "e5ecaf09a7a23268"}
vcr_1af8db844c6f
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [1, 2, 0, 4, 3, 1, 4, 4, 3, 0]}, "output": true}, {"input": {"nums": [2, 1, 0]}, "output": true}, {"input": {"nums": [0, 0, 2, 3, 3]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "fcd12227d30fc662"}
vcr_5a72d61668e4
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [22, 36, 1, 3, 12, 33, 33, 12, 22, 1, 36, 18, 7, 18, 78, 3, 7]}, "output": 78}, {"input": {"nums": [51, 2, 12, 19, 12, 2, 19]}, "output": 51}, {"input": {"nums": [13, 17, 17, 13, 46]}, "output": 46}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "66743acb27af4f61"}
vcr_a4cd114e090d
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": 208, "b": 165}, "output": 1}, {"input": {"a": 63, "b": 2}, "output": 1}, {"input": {"a": 69, "b": 78}, "output": 3}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "935fa529bea3f66c"}
vcr_defdfa3ecec5
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-30, -28, -27, -13, 14, 18, 19, 21, 26, 29, 33, 37], "target": 33}, "output": [4, 6]}, {"input": {"nums": [-13, -12, -11, -10, -7, 3, 8, 10, 30], "target": -4}, "output": [1, 6]}, {"input": {"nums": [-14, 6, 15, 28], "target": 21}, "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": "57f0af70d740f1ec"}
vcr_3fa66a5d1d09
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, 17, 31, -11, 12, -19, 1, 10, -15, 33, 17, -4, 4]}, "output": [13, 30, 61, 50, 62, 43, 44, 54, 39, 72, 89, 85, 89]}, {"input": {"nums": [17, 5, 2, 2, 11, 17, -7]}, "output": [17, 22, 24, 26, 37, 54, 47]}, {"input": {"nums": [-12, -3, -9]}, "output": [-12, -15, -24]}]
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": "fb52ac1a66d61a14"}
vcr_8f9947ff638b
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": [0, 6, 3, 7, 6]}, "output": [756, 0, 0, 0, 0]}, {"input": {"nums": [3, 3, 2, 3, 3]}, "output": [54, 54, 81, 54, 54]}, {"input": {"nums": [3, 1, 4, 5, 1]}, "output": [20, 60, 15, 12, 60]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "bf8b331ac5f0f1c7"}
vcr_82a2e215a0b0
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": "babaabccabaacb"}, "output": 11}, {"input": {"s": "caccacc"}, "output": 5}, {"input": {"s": "cbac"}, "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": "3619f2937d34561e"}
vcr_98bab82dc9ae
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, -18, 5, 22, -13, 5, -9, 17, -18, -10, -5, -5, -13, 1, -17]}, "output": 27}, {"input": {"nums": [-11, 9, 3, 6]}, "output": 18}, {"input": {"nums": [2, -4, -8, 10, -3, 6, 12, -3]}, "output": 25}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "27d6e042123951d4"}
vcr_05f617ee1bd6
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": [16, 42, 24, 22, 16, 48, 29, 8, 49, 38, 22, 42, 3, 1, 10, 33]}, "output": true}, {"input": {"nums": [15, 25, 10]}, "output": false}, {"input": {"nums": [5, 18, 3, 14, 15, 24, 7]}, "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": "ffb3451da2033b4a"}
vcr_001ae1c6d525
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": [35, -9, 13, 11, 25, 30, -9]}, "output": [35, 26, 39, 50, 75, 105, 96]}, {"input": {"nums": [9, 8, -12, 7, -9, -6, -1, 15]}, "output": [9, 17, 5, 12, 3, -3, -4, 11]}, {"input": {"nums": [4, -5, -7, 8, 5, -10, 0]}, "output": [4, -1, -8, 0, 5, -5, -5]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "d2d2c543659a3ff8"}
vcr_4d09d7de605f
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": "addc cacd bad bcddcc ee b adaa"}, "output": "adaa b ee bcddcc bad cacd addc"}, {"input": {"s": "abb bba baaa babb"}, "output": "babb baaa bba abb"}, {"input": {"s": "aab bb bab"}, "output": "bab bb 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": "d332782f087d5287"}